-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
172 lines (143 loc) · 5.07 KB
/
parser_test.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package solgo
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/unpackdev/solgo/syntaxerrors"
"github.com/unpackdev/solgo/tests"
)
func TestNew(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
assert.Equal(t, input, solgo.GetInput(), "input reader is not set correctly")
assert.NotNil(t, solgo.GetLexer(), "lexer is not initialized")
assert.NotNil(t, solgo.GetParser(), "parser is not initialized")
}
func TestParse(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
errs := solgo.Parse()
assert.Empty(t, errs, "syntax errors encountered during parsing")
}
func TestParseWithError(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
errs := solgo.Parse()
assert.NotEmpty(t, errs, "expected syntax errors, but none were encountered")
}
func TestGetTree(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
tree := solgo.GetTree()
assert.NotNil(t, tree, "parse tree is not generated")
}
func TestGetTokenStream(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
tokenStream := solgo.GetTokenStream()
assert.NotNil(t, tokenStream, "token stream is not generated")
}
func TestGetInputStream(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
inputStream := solgo.GetInputStream()
assert.NotNil(t, inputStream, "input stream is not generated")
}
func TestGetLexer(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
lexer := solgo.GetLexer()
assert.NotNil(t, lexer, "lexer is not generated")
}
func TestGetInput(t *testing.T) {
ctx := context.Background()
input := strings.NewReader("contract Test {}")
solgo, err := NewParser(ctx, input)
assert.NoError(t, err, "error creating SolGo instance")
assert.Equal(t, input, solgo.GetInput(), "input reader is not returned correctly")
}
func TestNew_SyntaxErrors(t *testing.T) {
testCases := []struct {
name string
contract string
expected []syntaxerrors.SyntaxError
}{
{
name: "Randomly Corrupted Contract",
contract: tests.ReadContractFileForTestFromRootPath(t, "BuggyContract").Content,
expected: []syntaxerrors.SyntaxError{
{
Line: 9,
Column: 4,
Message: "missing ';' at '}'",
Severity: syntaxerrors.SeverityError,
Context: "SourceUnit",
},
{
Line: 17,
Column: 12,
Message: "mismatched input '(' expecting {'constant', 'error', 'from', 'global', 'immutable', 'internal', 'override', 'private', 'public', 'revert', Identifier}",
Severity: syntaxerrors.SeverityError,
Context: "SourceUnit",
},
{
Line: 17,
Column: 27,
Message: "mismatched input ')' expecting {';', '='}",
Severity: syntaxerrors.SeverityError,
Context: "SourceUnit",
},
{
Line: 18,
Column: 14,
Message: "extraneous input '=' expecting {'constant', 'error', 'from', 'global', 'immutable', 'internal', 'override', 'private', 'public', 'revert', Identifier}",
Severity: syntaxerrors.SeverityError,
Context: "SourceUnit",
},
{
Line: 24,
Column: 4,
Message: "missing ';' at '}'",
Severity: syntaxerrors.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: syntaxerrors.SeverityError,
Context: "SourceUnit",
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a new SolGo instance
parser, err := NewParser(context.Background(), strings.NewReader(tc.contract))
assert.NoError(t, err)
assert.NotNil(t, parser)
syntaxErrors := parser.Parse()
// Check that the syntax errors match the expected syntax errors
assert.Equal(t, tc.expected, syntaxErrors)
assert.IsType(t, []syntaxerrors.SyntaxError{}, syntaxErrors)
assert.IsType(t, &Sources{}, parser.GetSources())
assert.IsType(t, &syntaxerrors.ContextualParser{}, parser.GetContextualParser())
})
}
}