66 lines
1.7 KiB
Go
66 lines
1.7 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(`
|
|
// User holds profile information about a single user.
|
|
M0001 User {
|
|
0000 Name String,
|
|
// dog water comment
|
|
|
|
// Users is asdkjsagkj why
|
|
//
|
|
// wow
|
|
0001 Users []User,
|
|
0002 Followers U32,
|
|
}`))
|
|
if err != nil { test.Fatal(parse.Format(err)) }
|
|
|
|
correctTokens := []parse.Token {
|
|
tok(TokenComment, "// User holds profile information about a single user."),
|
|
tok(TokenMethod, "0001"),
|
|
tok(TokenIdent, "User"),
|
|
tok(TokenLBrace, "{"),
|
|
tok(TokenKey, "0000"),
|
|
tok(TokenIdent, "Name"),
|
|
tok(TokenIdent, "String"),
|
|
tok(TokenComma, ","),
|
|
tok(TokenComment, "// dog water comment"),
|
|
tok(TokenComment, "// Users is asdkjsagkj why"),
|
|
tok(TokenComment, "// "),
|
|
tok(TokenComment, "// wow"),
|
|
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,
|
|
}
|
|
}
|