| 1 | package errormsg |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/goccmack/gocc/example/errormsg/errors" |
| 8 | "github.com/goccmack/gocc/example/errormsg/lexer" |
| 9 | "github.com/goccmack/gocc/example/errormsg/parser" |
| 10 | "github.com/goccmack/gocc/example/errormsg/token" |
| 11 | ) |
| 12 | |
| 13 | // assertEqual is a crude implementation of testify's assert.Equal. |
| 14 | func assertEqual(t *testing.T, lhs interface{}, rhs interface{}) { |
| 15 | t.Helper() |
| 16 | |
| 17 | if lhs != rhs { |
| 18 | t.Fatalf("mismatch: expected %v, got %v", lhs, rhs) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // parse boolean-checks the parseability of a given string against 'expectPass'. |
| 23 | func parse(t *testing.T, code string, expectPass bool) { |
| 24 | t.Helper() |
| 25 | |
| 26 | l := lexer.NewLexer([]byte(code)) |
| 27 | p := parser.NewParser() |
| 28 | _, err := p.Parse(l) |
| 29 | |
| 30 | // Match err to expectPass (nil = passed, !nil = failed), and log if not nill |
| 31 | if err == nil { |
| 32 | if !expectPass { |
| 33 | t.Fatal("test should have failed, got nil error instead") |
| 34 | } |
| 35 | } else { |
| 36 | if expectPass && err != nil { |
| 37 | t.Fatalf("test should have passed, got error instead: %s", err.Error()) |
| 38 | } |
| 39 | t.Log(err.Error()) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestParsedErrors_passes(t *testing.T) { |
| 44 | // Validate basic assumptions, that supposed-good strings parse ok. |
| 45 | candidates := []string{ |
| 46 | "var abcd = 123;", |
| 47 | " var _ := abcd ;", |
| 48 | "var a = 1.23 ;", |
| 49 | "var x;", |
| 50 | } |
| 51 | for _, candidate := range candidates { |
| 52 | candidate := candidate |
| 53 | t.Run(candidate, func(t *testing.T) { |
| 54 | parse(t, candidate, true) |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestParsedError_errors(t *testing.T) { |
| 60 | // Verify that supposed-bad strings generate errors. |
| 61 | candidates := []string{ |
| 62 | "var a = 1", // missing ';'. |
| 63 | "var a = 1\n", // '\n' not listsed in grammar. |
| 64 | "let a = 1;", // first option is var and only var. |
| 65 | "var = 1;", // second field must be identifier or '_'. |
| 66 | "var _ = ;", // third field must be one of '=', ':=' or ';'. |
| 67 | "var xyz = {}", // fourth, 'Default' has four candidates. |
| 68 | } |
| 69 | for _, candidate := range candidates { |
| 70 | candidate := candidate |
| 71 | t.Run(candidate, func(t *testing.T) { |
| 72 | parse(t, candidate, false) |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestParsedErrors_ExtraTokens(t *testing.T) { |
| 78 | // Input is supposed to end at ';', so errors.Errors will have an |
| 79 | // empty expected tokens list. |
| 80 | parse(t, "var end = 1; oops", false) |
| 81 | } |
| 82 | |
| 83 | func mockToken(tokenType token.Type, lit string, line, col int) *token.Token { |
| 84 | return &token.Token{Type: tokenType, Lit: []byte(lit), Pos: token.Pos{Line: line, Column: col}} |
| 85 | } |
| 86 | |
| 87 | func TestErrors_DescribeExpected(t *testing.T) { |
| 88 | assertEqual(t, "unexpected additional tokens", errors.DescribeExpected([]string{})) |
| 89 | assertEqual(t, "expected TREE", errors.DescribeExpected([]string{"TREE"})) |
| 90 | assertEqual(t, "expected either TREE or ENT", errors.DescribeExpected([]string{"TREE", "ENT"})) |
| 91 | assertEqual(t, "expected one of TREE, ENT or i am groot", errors.DescribeExpected([]string{"TREE", "ENT", "i am groot"})) |
| 92 | assertEqual(t, "expected one of a, b, c, d, e, f, or g", errors.DescribeExpected([]string{"a", "b", "c", "d", "e", "f", "g"})) |
| 93 | } |
| 94 | |
| 95 | func TestErrors_DescribeToken(t *testing.T) { |
| 96 | t.Run("eof", func(t *testing.T) { |
| 97 | tok := mockToken(token.EOF, "-not-eof-", 1, 1) |
| 98 | assertEqual(t, "end-of-file", errors.DescribeToken(tok)) |
| 99 | }) |
| 100 | |
| 101 | t.Run("eof", func(t *testing.T) { |
| 102 | tok := mockToken(token.INVALID, "-not-eof-", 1, 1) |
| 103 | assertEqual(t, "unknown/invalid token \"-not-eof-\"", errors.DescribeToken(tok)) |
| 104 | }) |
| 105 | |
| 106 | t.Run("eof", func(t *testing.T) { |
| 107 | tok := mockToken(9001, "-not-eof-", 1, 1) |
| 108 | assertEqual(t, "\"-not-eof-\"", errors.DescribeToken(tok)) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | func TestErrors_Error(t *testing.T) { |
| 113 | // Direct testing by manually constructing Error objects. |
| 114 | t.Run("custom error", func(t *testing.T) { |
| 115 | err := &errors.Error{ErrorToken: mockToken(999, "", 6, 7), Err: fmt.Errorf("source on fire")} |
| 116 | assertEqual(t, "6:7: error: source on fire", err.Error()) |
| 117 | }) |
| 118 | |
| 119 | t.Run("no tokens", func(t *testing.T) { |
| 120 | err := &errors.Error{ErrorToken: mockToken(888, "biscuit", 10, 12), ExpectedTokens: []string{}} |
| 121 | assertEqual(t, "10:12: error: unexpected additional tokens; got: \"biscuit\"", err.Error()) |
| 122 | }) |
| 123 | |
| 124 | t.Run("unexpected EOF", func(t *testing.T) { |
| 125 | err := &errors.Error{ErrorToken: mockToken(token.EOF, "", 7, 11), ExpectedTokens: []string{"something-else"}} |
| 126 | assertEqual(t, "7:11: error: expected something-else; got: end-of-file", err.Error()) |
| 127 | }) |
| 128 | |
| 129 | t.Run("nominal error", func(t *testing.T) { |
| 130 | err := &errors.Error{ErrorToken: mockToken(100, "42", 7, 6), ExpectedTokens: []string{"var", "let", "struct"}} |
| 131 | assertEqual(t, "7:6: error: expected one of var, let or struct; got: \"42\"", err.Error()) |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | func TestErrors_ErrorConext(t *testing.T) { |
| 136 | // "Before": error without a way to know the source file. |
| 137 | err := &errors.Error{ErrorToken: mockToken(111, "moyles", 16, 5), ExpectedTokens: []string{"ant", "dec"}} |
| 138 | assertEqual(t, `16:5: error: expected either ant or dec; got: "moyles"`, err.Error()) |
| 139 | |
| 140 | // Now attach a Context that implements `Source() string` and verify we |
| 141 | // get a proper File-Line-Column error using the same err. |
| 142 | err.ErrorToken.Context = &lexer.SourceContext{Filepath: "/addicted/to/plaice.lyrics"} |
| 143 | assertEqual(t, `/addicted/to/plaice.lyrics:16:5: error: expected either ant or dec; got: "moyles"`, err.Error()) |
| 144 | } |