-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser_test.go
157 lines (123 loc) · 3.13 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
package denada
import "testing"
import "strings"
import "encoding/json"
import . "github.com/onsi/gomega"
var sample_noexprs = `
class ABC() "D1" {
Real foo;
Integer x;
}
class DEF "D2" {
String y();
Boolean x "bool";
}
`
var sample = `
printer 'ABC' {
set location = "Mike's desk";
set model = "HP 8860";
}
'printer' DEF {
set location = "Coffee machine";
set model = "HP 8860";
set networkName = "PrinterDEF";
}
computer XYZ {
set location = "Mike's desk";
set 'model' = "Mac Book Air";
}
`
var sample_exprs = `
Real x = 5.0;
Integer y = 1;
String z = "This is a \"test\"";
Object a = {"key1": 5, "\"test\"": 2, "nested": {"r": "another string"}};
Null b = null;
Boolean c = [true, false];
Array d = [{"x": 5}, "foo", "\"test\"", true];
class Foo(x=5.0, y=1, z="This is a \"test\"", a={"key1": 5}, b=null, c=[true, false],
d = [{"x": 5}, "foo"]) {
Real x = 5.0;
}
`
func Test_SimpleDeclaration(t *testing.T) {
RegisterTestingT(t)
r := strings.NewReader("set x = 5 \"Description\";")
elems, err := Parse(r)
Expect(err).To(BeNil())
Expect(len(elems)).To(Equal(1))
elem := elems[0]
Expect(elem.IsDeclaration()).To(BeTrue())
Expect(elem.IsDefinition()).To(BeFalse())
Expect(len(elem.Modifications)).To(Equal(0))
Expect(elem.Qualifiers).To(Equal([]string{"set"}))
Expect(elem.Name).To(Equal("x"))
Expect(elem.Description).To(Equal("Description"))
v, err := elem.Value.Int()
Expect(err).To(BeNil())
Expect(v).To(Equal(5))
}
func Test_Errors(t *testing.T) {
RegisterTestingT(t)
r := strings.NewReader("set x = 5")
_, err := Parse(r)
Expect(err).ToNot(BeNil())
}
func Test_SampleInput(t *testing.T) {
RegisterTestingT(t)
r := strings.NewReader(sample)
el, err := Parse(r)
Expect(err).To(BeNil())
Expect(len(el)).To(Equal(3))
Expect(el[0].IsDefinition()).To(BeTrue())
Expect(el[1].IsDefinition()).To(BeTrue())
Expect(el[2].IsDefinition()).To(BeTrue())
}
func Test_SampleNoExprInput(t *testing.T) {
RegisterTestingT(t)
r := strings.NewReader(sample_noexprs)
el, err := Parse(r)
Expect(err).To(BeNil())
Expect(len(el)).To(Equal(2))
Expect(el[0].IsDefinition()).To(BeTrue())
Expect(el[1].IsDefinition()).To(BeTrue())
}
func Test_JsonTypes(t *testing.T) {
var expr interface{}
str := `{"minItems": 1}`
err := json.Unmarshal([]byte(str), &expr)
Expect(err).To(BeNil())
asmap, ok := expr.(map[string]interface{})
Expect(ok).To(Equal(true))
v, exists := asmap["minItems"]
Expect(exists).To(Equal(true))
Expect(v).To(Equal(1.0))
}
func Test_NumbersInExpr(t *testing.T) {
elems, err := ParseString(`var x = {"minItems": 1 };`)
Expect(err).To(BeNil())
e := elems[0]
v := e.Value
asmap, err := v.Map()
Expect(err).To(BeNil())
mif, exists := asmap["minItems"]
Expect(exists).To(Equal(true))
Expect(mif).To(Equal(1.0))
mi := v.Get("minItems").MustInt()
Expect(mi).To(Equal(1))
}
func Test_SampleJSONInput(t *testing.T) {
RegisterTestingT(t)
r := strings.NewReader(sample_exprs)
el, err := Parse(r)
Expect(err).To(BeNil())
Expect(len(el)).To(Equal(8))
for i, e := range el {
if i == 7 {
Expect(e.IsDefinition()).To(BeTrue())
} else {
Expect(e.IsDefinition()).To(BeFalse())
}
}
}