forked from asmr-hex/dockerlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
96 lines (83 loc) · 2.12 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
package dockerlang
import (
"github.com/stretchr/testify/suite"
)
type ParserSuite struct {
suite.Suite
}
func (s *ParserSuite) AfterTest(suiteName, testName string) {
ShutdownExecutionEngine()
}
func (s *ParserSuite) TestParser() {
compt := &Compterpreter{Config: &Config{SrcFileName: "src.doc"}}
compt.Tokens = []Token{
{Type: PUNCTUATION, Value: "("},
{Type: OPERATOR, Value: "‡"},
{Type: PUNCTUATION, Value: "("},
{Type: OPERATOR, Value: "+"},
// is whint? ;)
{Type: INT, Value: "2"},
{Type: INT, Value: "3"},
{Type: PUNCTUATION, Value: ")"},
{Type: INT, Value: "1"},
{Type: PUNCTUATION, Value: ")"},
}
err := compt.Parse()
s.NoError(err)
expectedTree := &Expr{
Operands: []AST{
&Expr{
Op: DIVISION_OPERATOR,
Arity: OP_TO_ARITY[DIVISION_OPERATOR],
Operands: []AST{
&Expr{
Op: ADDITION_OPERATOR,
Arity: OP_TO_ARITY[ADDITION_OPERATOR],
Operands: []AST{
&Literal{Type: INT, Value: "2"},
&Literal{Type: INT, Value: "3"},
},
},
&Literal{Type: INT, Value: "1"},
},
},
},
}
s.EqualValues(expectedTree, compt.StackTree)
}
func (s *ParserSuite) TestParser_SyntaxError() {
compt := &Compterpreter{Config: &Config{SrcFileName: "src.doc"}}
compt.Tokens = []Token{
{Type: PUNCTUATION, Value: "("},
{Type: OPERATOR, Value: "+"},
{Type: OPERATOR, Value: "+"},
{Type: INT, Value: "3"},
{Type: PUNCTUATION, Value: ")"},
}
err := compt.Parse()
s.IsType(DockerlangSyntaxError{}, err)
compt.Tokens = []Token{
{Type: PUNCTUATION, Value: "("},
{Type: INT, Value: "3"},
{Type: OPERATOR, Value: "+"},
{Type: PUNCTUATION, Value: ")"},
}
err = compt.Parse()
s.IsType(DockerlangSyntaxError{}, err)
compt.Tokens = []Token{
{Type: PUNCTUATION, Value: "("},
{Type: OPERATOR, Value: "+"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: INT, Value: "3"},
{Type: PUNCTUATION, Value: ")"},
}
err = compt.Parse()
s.IsType(DockerlangSyntaxError{}, err)
}