| 1 | //Copyright 2012 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package example |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/goccmack/gocc/example/usercontext/ast" |
| 22 | "github.com/goccmack/gocc/example/usercontext/lexer" |
| 23 | "github.com/goccmack/gocc/example/usercontext/parser" |
| 24 | ) |
| 25 | |
| 26 | func tryParse(t *testing.T, code string, lexContext, parseContext interface{}) (interface{}, error) { |
| 27 | t.Helper() |
| 28 | |
| 29 | l := lexer.NewLexer([]byte(code)) |
| 30 | l.Context = lexContext |
| 31 | |
| 32 | p := parser.NewParser() |
| 33 | p.Context = parseContext |
| 34 | |
| 35 | return p.Parse(l) |
| 36 | } |
| 37 | |
| 38 | func TestTokenContext(t *testing.T) { |
| 39 | t.Run("nil context", func(t *testing.T) { |
| 40 | t.Run("successful parse", func(t *testing.T) { |
| 41 | if _, err := tryParse(t, "Life Universe Everything ...42...", nil, nil); err != nil { |
| 42 | t.Fatalf("not expecting an error, got: %q", err.Error()) |
| 43 | } |
| 44 | }) |
| 45 | |
| 46 | t.Run("syntax error", func(t *testing.T) { |
| 47 | // missing identifiers. |
| 48 | _, err := tryParse(t, "...42...", nil, nil) |
| 49 | if err == nil { |
| 50 | t.Fatalf("expecting an error") |
| 51 | } |
| 52 | const expected = `1:1: error: expected either capitalized or lowercase; got: "...42..."` |
| 53 | if err.Error() != expected { |
| 54 | t.Fatalf("incorrect error:\nexpected: %q\nactual : %q", expected, err.Error()) |
| 55 | } |
| 56 | t.Log(err.Error()) |
| 57 | }) |
| 58 | |
| 59 | t.Run("context type mismatch", func(t *testing.T) { |
| 60 | // if we use an identifier, it requires context, and the ast code will |
| 61 | // reject the context for being nil. |
| 62 | _, err := tryParse(t, "sixbyseven ...42...", nil, nil) |
| 63 | if err == nil { |
| 64 | t.Fatalf("expecting an error") |
| 65 | } |
| 66 | // not sure why err.ErrorToken has 12 in its pos.Column field, but currently |
| 67 | // this is correct. |
| 68 | const expected = `1:12: error: sixbyseven: NewIdentifier expected ParserContext, got <nil>` |
| 69 | if err.Error() != expected { |
| 70 | t.Fatalf("incorrect error:\nexpected: %q\nactual : %q", expected, err.Error()) |
| 71 | } |
| 72 | t.Log(err.Error()) |
| 73 | }) |
| 74 | }) |
| 75 | |
| 76 | t.Run("contexts", func(t *testing.T) { |
| 77 | lc := &ast.LexicalContext{SourceFile: "bender.txt", ForbiddenWords: []string{"daisy"}} |
| 78 | pc := &ast.ParserContext{Visitors: make([]string, 0)} // not using any other fields |
| 79 | |
| 80 | sym, err := tryParse(t, "Bite My shiny metal ...42...", lc, pc) |
| 81 | if err != nil { |
| 82 | t.Fatalf("not expecting an error, got: %s", err.Error()) |
| 83 | } |
| 84 | t.Logf("returned symbol was %#+v\n", sym) |
| 85 | |
| 86 | // Because of the grammar, the ast should be just the last Identifier created by |
| 87 | // "Words", which should correspond to "metal". |
| 88 | switch ident := sym.(type) { |
| 89 | case *ast.Identifier: |
| 90 | if string(ident.Name.Lit) != "metal" { |
| 91 | t.Fatalf("expected 'metal' symbol, got: %s: %v", string(ident.Name.Lit), sym) |
| 92 | } |
| 93 | if ident.ParserContext != pc { |
| 94 | t.Fatalf("expected ident.ParserContext to be &ParserContext, got: %v", ident.ParserContext) |
| 95 | } |
| 96 | if ident.Name.Pos.Context != lc { |
| 97 | t.Fatalf("expected ident.Name.Pos.Context to be &LexicalContext, got: %v", ident.Name.Pos.Context) |
| 98 | } |
| 99 | |
| 100 | default: |
| 101 | t.Fatalf("first argument of parse is not the identifier we expected: got %#+v", sym) |
| 102 | } |
| 103 | |
| 104 | // Verify that ParserContext.Visitors was populated. |
| 105 | if len(pc.Visitors) != 2 { |
| 106 | t.Fatalf("wrong count for pc.Visitors, expected: %d, got: %d", 2, len(pc.Visitors)) |
| 107 | } |
| 108 | expected := "shiny metal" |
| 109 | actual := strings.Join(pc.Visitors, " ") |
| 110 | t.Logf("visitors: %s", actual) |
| 111 | if actual != expected { |
| 112 | t.Fatalf("wrong Visitor list, expected: %q, got: %q", expected, actual) |
| 113 | } |
| 114 | }) |
| 115 | } |