github.com/cloudflare/circl@v1.5.0/abe/cpabe/tkn20/internal/dsl/lexer_test.go (about)

     1  package dsl
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  )
     7  
     8  func TestLexerErr(t *testing.T) {
     9  	l := newLexer("sleep: @)")
    10  	err := l.scanTokens()
    11  	expectedErr := errors.New("unexpected character(s): '@'")
    12  	if err == nil {
    13  		t.Fatalf("missing expected err %v", expectedErr)
    14  	}
    15  	if expectedErr.Error() != err.Error() {
    16  		t.Fatalf("incorrect error: expected %v, received %v", expectedErr, err)
    17  	}
    18  }
    19  
    20  func TestLexer(t *testing.T) {
    21  	l := newLexer("(sleep \n: \nnice\n)")
    22  	err := l.scanTokens()
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	types := []string{LeftParen, Identifier, Colon, Identifier, RightParen, EOF}
    28  
    29  	if len(l.tokens) != len(types) {
    30  		t.Fatalf("expected %d tokens, received: %v", len(types), len(l.tokens))
    31  	}
    32  	for i, typ := range types {
    33  		if typ != l.tokens[i].Type {
    34  			t.Fatalf("expected token %s, received: %s", typ, l.tokens[i].Type)
    35  		}
    36  	}
    37  
    38  	if l.tokens[2].Line != 2 {
    39  		t.Fatalf("expected line 2, received: %d", l.tokens[2].Line)
    40  	}
    41  	if l.tokens[3].Line != 3 {
    42  		t.Fatalf("expected line 3, received: %d", l.tokens[3].Line)
    43  	}
    44  }