package parser import ( "bytes" "fmt" "testing" ) var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" type token struct { tok Token text string } func TestIdent(t *testing.T) { var identList = []token{ {IDENT, "a"}, {IDENT, "a0"}, {IDENT, "foobar"}, {IDENT, "abc123"}, {IDENT, "LGTM"}, {IDENT, "_"}, {IDENT, "_abc123"}, {IDENT, "abc123_"}, {IDENT, "_abc_123_"}, {IDENT, "_äöü"}, {IDENT, "_本"}, {IDENT, "äöü"}, {IDENT, "本"}, {IDENT, "a۰۱۸"}, {IDENT, "foo६४"}, {IDENT, "bar9876"}, } // create artifical source code buf := new(bytes.Buffer) for _, ident := range identList { fmt.Fprintf(buf, " \t%s\n", ident.text) } l, err := NewLexer(buf) if err != nil { t.Fatal(err) } for _, ident := range identList { tok, lit := l.Scan() if tok != ident.tok { t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text) } if lit != ident.text { t.Errorf("text = %s want %s", lit, ident.text) } } } func TestString(t *testing.T) { var identList = []token{ {STRING, `" "`}, {STRING, `"a"`}, {STRING, `"本"`}, // {STRING, `"\a"`}, // {STRING, `"\b"`}, // {STRING, `"\f"`}, // {STRING, `"\n"`}, // {STRING, `"\r"`}, // {STRING, `"\t"`}, // {STRING, `"\v"`}, // {STRING, `"\""`}, // {STRING, `"\000"`}, // {STRING, `"\777"`}, // {STRING, `"\x00"`}, // {STRING, `"\xff"`}, // {STRING, `"\u0000"`}, // {STRING, `"\ufA16"`}, // {STRING, `"\U00000000"`}, // {STRING, `"\U0000ffAB"`}, // {STRING, `"` + f100 + `"`}, } // create artifical source code buf := new(bytes.Buffer) for _, ident := range identList { fmt.Fprintf(buf, " \t%s\n", ident.text) } l, err := NewLexer(buf) if err != nil { t.Fatal(err) } for _, ident := range identList { tok, lit := l.Scan() if tok != ident.tok { t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text) } if lit != ident.text { t.Errorf("text = %s want %s", lit, ident.text) } } }