| 1 | package scanner |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/goccmack/gocc/internal/frontend/token" |
| 8 | ) |
| 9 | |
| 10 | type testRecord struct { |
| 11 | src string |
| 12 | typ token.Type |
| 13 | tokLit string |
| 14 | } |
| 15 | |
| 16 | var testData = []testRecord{ |
| 17 | {"tokId", token.FRONTENDTokens.Type("tokId"), "tokId"}, |
| 18 | {"!whitespace", token.FRONTENDTokens.Type("ignoredTokId"), "!whitespace"}, |
| 19 | {":", token.FRONTENDTokens.Type(":"), ":"}, |
| 20 | {";", token.FRONTENDTokens.Type(";"), ";"}, |
| 21 | {"_regDefId", token.FRONTENDTokens.Type("regDefId"), "_regDefId"}, |
| 22 | {"|", token.FRONTENDTokens.Type("|"), "|"}, |
| 23 | {`'\u0011'`, token.FRONTENDTokens.Type("char_lit"), `'\u0011'`}, |
| 24 | {"-", token.FRONTENDTokens.Type("-"), "-"}, |
| 25 | {"(", token.FRONTENDTokens.Type("("), "("}, |
| 26 | {")", token.FRONTENDTokens.Type(")"), ")"}, |
| 27 | {"[", token.FRONTENDTokens.Type("["), "["}, |
| 28 | {"]", token.FRONTENDTokens.Type("]"), "]"}, |
| 29 | {"{", token.FRONTENDTokens.Type("{"), "{"}, |
| 30 | {"}", token.FRONTENDTokens.Type("}"), "}"}, |
| 31 | {"<< sdt lit >>", token.FRONTENDTokens.Type("g_sdt_lit"), "<< sdt lit >>"}, |
| 32 | {"ProdId", token.FRONTENDTokens.Type("prodId"), "ProdId"}, |
| 33 | {`"string lit"`, token.FRONTENDTokens.Type("string_lit"), `"string lit"`}, |
| 34 | } |
| 35 | |
| 36 | func Test1(tst *testing.T) { |
| 37 | s := &Scanner{} |
| 38 | for _, t := range testData { |
| 39 | s.Init([]byte(t.src), token.FRONTENDTokens) |
| 40 | tok, _ := s.Scan() |
| 41 | if tok.Type != t.typ { |
| 42 | tst.Errorf("src: %s, type: %d -- got type: %d\n", t.src, t.typ, tok.Type) |
| 43 | } |
| 44 | if string(tok.Lit) != t.tokLit { |
| 45 | tst.Errorf("src: %s, expected lit: %s, got: %s\n", t.src, t.tokLit, string(tok.Lit)) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func Test2(t *testing.T) { |
| 51 | s := &Scanner{} |
| 52 | lit := "The SDT Lit" |
| 53 | s.Init([]byte(fmt.Sprintf("<< %s >>", lit)), token.FRONTENDTokens) |
| 54 | tok, _ := s.Scan() |
| 55 | if tok.Type != token.FRONTENDTokens.Type("g_sdt_lit") { |
| 56 | t.Errorf("Expected tok type: g_sdt_lit, got: %s", token.FRONTENDTokens.TokenString(tok.Type)) |
| 57 | } |
| 58 | if tok.SDTVal() != lit { |
| 59 | t.Errorf("Expected SDTVal: %s, got: %s\n", lit, tok.SDTVal()) |
| 60 | } |
| 61 | } |