Debian Codegraph @

example/errormsg/errormsg_test.go

raw symbols SCIP
1 package errormsgscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/"
5 "testingscip-go gomod github.com/golang/go/src go1.24 testing/"
6
7 "github.com/goccmack/gocc/example/errormsg/errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/"
8 "github.com/goccmack/gocc/example/errormsg/lexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/lexer`/"
9 "github.com/goccmack/gocc/example/errormsg/parserscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/parser`/"
10 "github.com/goccmack/gocc/example/errormsg/tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/"
11 )
12
13 // assertEqual is a crude implementation of testify's assert.Equal.
14 func assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#, lhs interface{}, rhs interface{}) {
15 t.Helperscip-go gomod github.com/golang/go/src go1.24 testing/common#Helper().()
16
17 if lhs != rhs {
18 t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#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 parsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/parse().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#, code string, expectPass bool) {
24 t.Helperscip-go gomod github.com/golang/go/src go1.24 testing/common#Helper().()
25
26 l := lexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/lexer`/.NewLexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/lexer`/NewLexer().([]byte(code))
27 p := parserscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/parser`/.NewParserscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/parser`/NewParser().()
28 _, err := p.Parsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/parser`/Parser#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.Fatalscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatal().("test should have failed, got nil error instead")
34 }
35 } else {
36 if expectPass && err != nil {
37 t.Fatalfscip-go gomod github.com/golang/go/src go1.24 testing/common#Fatalf().("test should have passed, got error instead: %s", err.Error())
38 }
39 t.Logscip-go gomod github.com/golang/go/src go1.24 testing/common#Log().(err.Error())
40 }
41 }
42
43 func TestParsedErrors_passesscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestParsedErrors_passes().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 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.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().(candidate, func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
54 parsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/parse().(t, candidate, true)
55 })
56 }
57 }
58
59 func TestParsedError_errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestParsedError_errors().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 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.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().(candidate, func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
72 parsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/parse().(t, candidate, false)
73 })
74 }
75 }
76
77 func TestParsedErrors_ExtraTokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestParsedErrors_ExtraTokens().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
78 // Input is supposed to end at ';', so errors.Errors will have an
79 // empty expected tokens list.
80 parsescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/parse().(t, "var end = 1; oops", false)
81 }
82
83 func mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(tokenType tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Type#, lit string, line, col int) *tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Token# {
84 return &tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.Tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Token#{Typescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Token#Type.: tokenType, Litscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Token#Lit.: []byte(lit), Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Token#Pos.: tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.Posscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Pos#{Linescip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Pos#Line.: line, Columnscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Pos#Column.: col}}
85 }
86
87 func TestErrors_DescribeExpectedscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestErrors_DescribeExpected().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
88 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "unexpected additional tokens", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeExpectedscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeExpected().([]string{}))
89 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "expected TREE", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeExpectedscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeExpected().([]string{"TREE"}))
90 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "expected either TREE or ENT", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeExpectedscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeExpected().([]string{"TREE", "ENT"}))
91 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "expected one of TREE, ENT or i am groot", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeExpectedscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeExpected().([]string{"TREE", "ENT", "i am groot"}))
92 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "expected one of a, b, c, d, e, f, or g", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeExpectedscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeExpected().([]string{"a", "b", "c", "d", "e", "f", "g"}))
93 }
94
95 func TestErrors_DescribeTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestErrors_DescribeToken().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
96 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("eof", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
97 tok := mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.EOFscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/EOF., "-not-eof-", 1, 1)
98 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "end-of-file", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeToken().(tok))
99 })
100
101 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("eof", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
102 tok := mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.INVALIDscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/INVALID., "-not-eof-", 1, 1)
103 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "unknown/invalid token \"-not-eof-\"", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeToken().(tok))
104 })
105
106 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("eof", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
107 tok := mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(9001, "-not-eof-", 1, 1)
108 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "\"-not-eof-\"", errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.DescribeTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/DescribeToken().(tok))
109 })
110 }
111
112 func TestErrors_Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestErrors_Error().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
113 // Direct testing by manually constructing Error objects.
114 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("custom error", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
115 err := &errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#{ErrorTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ErrorToken.: mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(999, "", 6, 7), Errscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#Err.: fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("source on fire")}
116 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "6:7: error: source on fire", err.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#Error().())
117 })
118
119 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("no tokens", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
120 err := &errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#{ErrorTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ErrorToken.: mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(888, "biscuit", 10, 12), ExpectedTokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ExpectedTokens.: []string{}}
121 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "10:12: error: unexpected additional tokens; got: \"biscuit\"", err.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#Error().())
122 })
123
124 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("unexpected EOF", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
125 err := &errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#{ErrorTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ErrorToken.: mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(tokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/.EOFscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/EOF., "", 7, 11), ExpectedTokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ExpectedTokens.: []string{"something-else"}}
126 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "7:11: error: expected something-else; got: end-of-file", err.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#Error().())
127 })
128
129 t.Runscip-go gomod github.com/golang/go/src go1.24 testing/T#Run().("nominal error", func(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
130 err := &errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#{ErrorTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ErrorToken.: mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(100, "42", 7, 6), ExpectedTokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ExpectedTokens.: []string{"var", "let", "struct"}}
131 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, "7:6: error: expected one of var, let or struct; got: \"42\"", err.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#Error().())
132 })
133 }
134
135 func TestErrors_ErrorConextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/TestErrors_ErrorConext().(t *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Tscip-go gomod github.com/golang/go/src go1.24 testing/T#) {
136 // "Before": error without a way to know the source file.
137 err := &errorsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#{ErrorTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ErrorToken.: mockTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/mockToken().(111, "moyles", 16, 5), ExpectedTokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ExpectedTokens.: []string{"ant", "dec"}}
138 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, `16:5: error: expected either ant or dec; got: "moyles"`, err.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#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.ErrorTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#ErrorToken..Contextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/token`/Pos#Context. = &lexerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/lexer`/.SourceContextscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/lexer`/SourceContext#{Filepathscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/lexer`/SourceContext#Filepath.: "/addicted/to/plaice.lyrics"}
143 assertEqualscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg`/assertEqual().(t, `/addicted/to/plaice.lyrics:16:5: error: expected either ant or dec; got: "moyles"`, err.Errorscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/example/errormsg/errors`/Error#Error().())
144 }