-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsyntax_errors_test.go
More file actions
100 lines (89 loc) · 2.9 KB
/
syntax_errors_test.go
File metadata and controls
100 lines (89 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package syntaxerrors
import (
"testing"
"github.com/antlr4-go/antlr/v4"
"github.com/stretchr/testify/assert"
"github.com/unpackdev/solgo/parser"
"github.com/unpackdev/solgo/tests"
)
func TestSyntaxErrorListener(t *testing.T) {
testCases := []struct {
name string
contract string
expected []SyntaxError
}{
{
name: "Randomly Corrupted Contract",
contract: tests.ReadContractFileForTest(t, "BuggyContract").Content,
expected: []SyntaxError{
{
Line: 9,
Column: 4,
Message: "missing ';' at '}'",
Severity: SeverityError,
Context: "SourceUnit",
},
{
Line: 17,
Column: 12,
Message: "mismatched input '(' expecting {'constant', 'error', 'from', 'global', 'immutable', 'internal', 'override', 'private', 'public', 'revert', Identifier}",
Severity: SeverityError,
Context: "SourceUnit",
},
{
Line: 17,
Column: 27,
Message: "mismatched input ')' expecting {';', '='}",
Severity: SeverityError,
Context: "SourceUnit",
},
{
Line: 18,
Column: 14,
Message: "extraneous input '=' expecting {'constant', 'error', 'from', 'global', 'immutable', 'internal', 'override', 'private', 'public', 'revert', Identifier}",
Severity: SeverityError,
Context: "SourceUnit",
},
{
Line: 24,
Column: 4,
Message: "missing ';' at '}'",
Severity: SeverityError,
Context: "SourceUnit",
},
{
Line: 25,
Column: 0,
Message: "extraneous input '}' expecting {<EOF>, 'abstract', 'address', 'bool', 'bytes', 'contract', 'enum', 'error', Fixed, FixedBytes, 'from', Function, 'global', 'import', 'interface', 'library', 'mapping', 'pragma', 'revert', SignedIntegerType, 'string', 'struct', 'type', Ufixed, UnsignedIntegerType, 'using', Identifier}",
Severity: SeverityError,
Context: "SourceUnit",
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create an ANTLR input stream from the contract string
input := antlr.NewInputStream(tc.contract)
// Create a Solidity lexer
lexer := parser.NewSolidityLexer(input)
// Create an ANTLR token stream from the lexer
tokens := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
// Create a new SyntaxErrorListener
listener := NewSyntaxErrorListener()
// Create a ContextualParser with the token stream and listener
parser := NewContextualParser(tokens, listener)
// Parse the contract
parser.SourceUnit()
// Check that the errors match the expected errors
assert.Equal(t, tc.expected, listener.Errors)
for _, err := range listener.Errors {
assert.Equal(t, err.Context, "SourceUnit")
assert.Equal(t, err.Severity.String(), SeverityError.String())
assert.Equal(t, err.Line > 0, true)
assert.Equal(t, err.Message != "", true)
assert.NotEmpty(t, err.Error())
}
})
}
}