package scanner import ( "bytes" "fmt" "testing" "strings" "github.com/hashicorp/hcl/hcl/token" ) var f100 = strings.Repeat("f", 100) type tokenPair struct { tok token.Type text string } var tokenLists = map[string][]tokenPair{ "comment": []tokenPair{ {token.COMMENT, "//"}, {token.COMMENT, "////"}, {token.COMMENT, "// comment"}, {token.COMMENT, "// /* comment */"}, {token.COMMENT, "// // comment //"}, {token.COMMENT, "//" + f100}, {token.COMMENT, "#"}, {token.COMMENT, "##"}, {token.COMMENT, "# comment"}, {token.COMMENT, "# /* comment */"}, {token.COMMENT, "# # comment #"}, {token.COMMENT, "#" + f100}, {token.COMMENT, "/**/"}, {token.COMMENT, "/***/"}, {token.COMMENT, "/* comment */"}, {token.COMMENT, "/* // comment */"}, {token.COMMENT, "/* /* comment */"}, {token.COMMENT, "/*\n comment\n*/"}, {token.COMMENT, "/*" + f100 + "*/"}, }, "operator": []tokenPair{ {token.LBRACK, "["}, {token.LBRACE, "{"}, {token.COMMA, ","}, {token.PERIOD, "."}, {token.RBRACK, "]"}, {token.RBRACE, "}"}, {token.ASSIGN, "="}, {token.ADD, "+"}, {token.SUB, "-"}, }, "bool": []tokenPair{ {token.BOOL, "true"}, {token.BOOL, "false"}, }, "ident": []tokenPair{ {token.IDENT, "a"}, {token.IDENT, "a0"}, {token.IDENT, "foobar"}, {token.IDENT, "foo-bar"}, {token.IDENT, "abc123"}, {token.IDENT, "LGTM"}, {token.IDENT, "_"}, {token.IDENT, "_abc123"}, {token.IDENT, "abc123_"}, {token.IDENT, "_abc_123_"}, {token.IDENT, "_äöü"}, {token.IDENT, "_本"}, {token.IDENT, "äöü"}, {token.IDENT, "本"}, {token.IDENT, "a۰۱۸"}, {token.IDENT, "foo६४"}, {token.IDENT, "bar9876"}, }, "heredoc": []tokenPair{ {token.HEREDOC, "< 0 for %q", s.ErrorCount, src) } } func testTokenList(t *testing.T, tokenList []tokenPair) { // create artifical source code buf := new(bytes.Buffer) for _, ident := range tokenList { fmt.Fprintf(buf, "%s\n", ident.text) } s := New(buf.Bytes()) for _, ident := range tokenList { tok := s.Scan() if tok.Type != ident.tok { t.Errorf("tok = %q want %q for %q\n", tok, ident.tok, ident.text) } if tok.Text != ident.text { t.Errorf("text = %q want %q", tok.String(), ident.text) } } } func countNewlines(s string) int { n := 0 for _, ch := range s { if ch == '\n' { n++ } } return n }