-
Notifications
You must be signed in to change notification settings - Fork 0
/
astBuilder_test.go
131 lines (106 loc) · 3.59 KB
/
astBuilder_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
package gojacego
import (
"reflect"
"testing"
)
func getConstantRegistry() *constantRegistry {
return &constantRegistry{
caseSensitive: false,
}
}
func getFunctionRegistry() *functionRegistry {
return &functionRegistry{
caseSensitive: false,
functions: map[string]functionInfo{},
}
}
func TestBuildFormula1(test *testing.T) {
astBuilder := newAstBuilder(false, getFunctionRegistry(), getConstantRegistry(), nil)
params := []token{
{Value: '(', Type: tt_LEFT_BRACKET},
{Value: 42, Type: tt_INTEGER},
{Value: '+', Type: tt_OPERATION},
{Value: 8, Type: tt_INTEGER},
{Value: ')', Type: tt_RIGHT_BRACKET},
{Value: '*', Type: tt_OPERATION},
{Value: 2, Type: tt_INTEGER},
}
op, _ := astBuilder.build(params)
if reflect.TypeOf(op).String() != "*gojacego.multiplicationOperation" {
test.Errorf("expected: multiplicationOperation, got: %s", reflect.TypeOf(op).String())
}
multiplication := op.(*multiplicationOperation)
addition := multiplication.OperationOne.(*addOperation)
add_one := addition.OperationOne.(*constantOperation).Value
if add_one != 42 {
test.Errorf("exptected: 42, got: %d", add_one)
}
add_two := addition.OperationTwo.(*constantOperation).Value
if add_two != 8 {
test.Errorf("exptected: 8, got: %d", add_one)
}
multi_two := multiplication.OperationTwo.(*constantOperation).Value
if multi_two != 2 {
test.Errorf("exptected: 2, got: %d", add_one)
}
}
func TestBuildFormula2(test *testing.T) {
astBuilder := newAstBuilder(false, getFunctionRegistry(), getConstantRegistry(), nil)
params := []token{
{Value: 2, Type: tt_INTEGER},
{Value: '+', Type: tt_OPERATION},
{Value: 8, Type: tt_INTEGER},
{Value: '*', Type: tt_OPERATION},
{Value: 3, Type: tt_INTEGER},
}
op, _ := astBuilder.build(params)
if reflect.TypeOf(op).String() != "*gojacego.addOperation" {
test.Errorf("expected: AddOperation, got: %s", reflect.TypeOf(op).String())
}
addition := op.(*addOperation)
multiplication := addition.OperationTwo.(*multiplicationOperation)
add_one := addition.OperationOne.(*constantOperation).Value
if add_one != 2 {
test.Errorf("exptected: 2, got: %d", add_one)
}
multi_one := multiplication.OperationOne.(*constantOperation).Value
if multi_one != 8 {
test.Errorf("exptected: 8, got: %d", multi_one)
}
multi_two := multiplication.OperationTwo.(*constantOperation).Value
if multi_two != 3 {
test.Errorf("exptected: 3, got: %d", add_one)
}
}
func TestUnaryMinus(test *testing.T) {
astBuilder := newAstBuilder(false, getFunctionRegistry(), getConstantRegistry(), nil)
params := []token{
{Value: 5.3, Type: tt_FLOATING_POINT},
{Value: '*', Type: tt_OPERATION},
{Value: '_', Type: tt_OPERATION},
{Value: '(', Type: tt_LEFT_BRACKET},
{Value: 5, Type: tt_INTEGER},
{Value: '+', Type: tt_OPERATION},
{Value: 42, Type: tt_INTEGER},
{Value: ')', Type: tt_RIGHT_BRACKET},
}
op, _ := astBuilder.build(params)
if reflect.TypeOf(op).String() != "*gojacego.multiplicationOperation" {
test.Errorf("expected: multiplicationOperation, got: %s", reflect.TypeOf(op).String())
}
multiplication := op.(*multiplicationOperation)
multi_one := multiplication.OperationOne.(*constantOperation).Value
if multi_one != 5.3 {
test.Errorf("exptected: 5.3, got: %d", multi_one)
}
unaryMinus := multiplication.OperationTwo.(*unaryMinusOperation)
addition := unaryMinus.Operation.(*addOperation)
add_one := addition.OperationOne.(*constantOperation).Value
if add_one != 5 {
test.Errorf("exptected: 5, got: %d", add_one)
}
add_two := addition.OperationTwo.(*constantOperation).Value
if add_two != 42 {
test.Errorf("exptected: 42, got: %d", add_one)
}
}