55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package generate
|
|
|
|
import "strings"
|
|
import "testing"
|
|
import "git.tebibyte.media/sashakoshka/goparse"
|
|
|
|
func TestLex(test *testing.T) {
|
|
lexer, err := Lex("test.pdl", strings.NewReader(`
|
|
M0001 User {
|
|
0000 Name String,
|
|
0001 Users []User,
|
|
0002 Followers U32,
|
|
}`))
|
|
if err != nil { test.Fatal(parse.Format(err)) }
|
|
|
|
correctTokens := []parse.Token {
|
|
tok(TokenMethod, "0001"),
|
|
tok(TokenIdent, "User"),
|
|
tok(TokenLBrace, "{"),
|
|
tok(TokenKey, "0000"),
|
|
tok(TokenIdent, "Name"),
|
|
tok(TokenIdent, "String"),
|
|
tok(TokenComma, ","),
|
|
tok(TokenKey, "0001"),
|
|
tok(TokenIdent, "Users"),
|
|
tok(TokenLBracket, "["),
|
|
tok(TokenRBracket, "]"),
|
|
tok(TokenIdent, "User"),
|
|
tok(TokenComma, ","),
|
|
tok(TokenKey, "0002"),
|
|
tok(TokenIdent, "Followers"),
|
|
tok(TokenIdent, "U32"),
|
|
tok(TokenComma, ","),
|
|
tok(TokenRBrace, "}"),
|
|
tok(parse.EOF, ""),
|
|
}
|
|
|
|
for index, correct := range correctTokens {
|
|
got, err := lexer.Next()
|
|
if err != nil { test.Fatal(parse.Format(err)) }
|
|
if got.Kind != correct.Kind || got.Value != correct.Value {
|
|
test.Logf("token %d mismatch", index)
|
|
test.Log("GOT:", tokenNames[got.Kind], got.Value)
|
|
test.Fatal("CORRECT:", tokenNames[correct.Kind], correct.Value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func tok(kind parse.TokenKind, value string) parse.Token {
|
|
return parse.Token {
|
|
Kind: kind,
|
|
Value: value,
|
|
}
|
|
}
|