-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl_test.go
187 lines (158 loc) · 3.64 KB
/
acl_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package archercl
import (
"testing"
)
func init() {
ColoredLoggingToConsole()
}
func Test_Comment(t *testing.T) {
src := `
// This is a comment
a = 1
/* And this is also a comment
that is multi line*/
b = 2
/* Yeah? * */ c = 3 /**/
`
node := NewAclNode()
err := node.ParseString(src, nil)
if err != nil {
t.Fatal(err)
}
if node.Child("a").AsInt() != 1 {
t.Fatalf("a didn't parse properly")
}
if node.Child("b").AsInt() != 2 {
t.Fatalf("b didn't parse properly")
}
if node.Child("c").AsInt() != 3 {
t.Fatalf("v didn't parse properly")
}
}
func Test_ValuesTypes(t *testing.T) {
src := `
// Value Types
unquoted = def
squoted = 's\'in"gle'
dquoted = "dou'ble"
integer1 = 1
integer2 = 123
integer3 = +123
integer4 = -123
float1 = 0.1
float2 = +0.1
float3 = -0.1
// Arrays
array unquoted = one two three
array quoted = "the first" "the second"
`
node := NewAclNode()
err := node.ParseString(src, nil)
if err != nil {
t.Fatal(err)
}
if node.Child("unquoted").AsString() != "def" {
t.Fatal("Wrong value for unquoted")
}
if node.Child("squoted").AsString() != "s'in\"gle" {
t.Fatal("Wrong value for squoted")
}
if node.Child("dquoted").AsString() != "dou'ble" {
t.Fatal("Wrong value for unquoted")
}
if node.Child("integer1").AsInt() != 1 {
t.Fatal("Wrong value for integer1")
}
if node.Child("integer2").AsInt() != 123 {
t.Fatal("Wrong value for integer2")
}
if node.Child("integer3").AsInt() != 123 {
t.Fatal("Wrong value for integer3")
}
if node.Child("integer4").AsInt() != -123 {
t.Fatal("Wrong value for integer4")
}
if node.Child("float1").AsFloat() != 0.1 {
t.Fatal("Wrong value for float1")
}
if node.Child("float2").AsFloat() != 0.1 {
t.Fatal("Wrong value for float2")
}
if node.Child("float3").AsFloat() != -0.1 {
t.Fatal("Wrong value for float3")
}
ac := node.Child("array")
if ac == nil {
t.Fatal("No child 'array'")
}
acn := ac.Child("unquoted")
if acn == nil {
t.Fatal("No child 'array unquoted'")
}
if acn.Len() != 3 {
t.Fatal("Wrong length for array unquoted")
}
if acn.AsStringN(2) != "three" {
t.Fatal("Wrong value for array unquoted 3")
}
acq := ac.Child("quoted")
if acq == nil {
t.Fatal("No child 'array quoted'")
}
if acq.Len() != 2 {
t.Fatal("Wrong length for array quoted")
}
if acq.AsStringN(1) != "the second" {
t.Fatal("Wrong value for array quoted 2")
}
}
func Test_ValuesObject(t *testing.T) {
src := `
sub {
one: 1
second {
two: 2
}
}
`
node := NewAclNode()
err := node.ParseString(src, nil)
if err != nil {
t.Fatal(err)
}
// alog.Infof("Node is %s", node.String())
obj := node.Child("sub")
if obj == nil {
t.Fatal("No sub object")
}
if obj.Child("one").AsInt() != 1 {
t.Fatal("Wrong value for sub one")
}
second := obj.Child("second")
if second == nil {
t.Fatal("No second object")
}
if second.Child("two").AsInt() != 2 {
t.Fatal("Wrong value for sub second two")
}
}
func Test_StringOne(t *testing.T) {
root := NewAclNode()
n := NewAclNode()
n.Values = append(n.Values, 1)
n.Values = append(n.Values, 2.3)
n.Values = append(n.Values, true)
n.Values = append(n.Values, "abc")
root.Children["one"] = n
root.OrderedChildNames = append(root.OrderedChildNames, "one")
n = NewAclNode()
n.Values = append(n.Values, "world")
root.Children["hello"] = n
root.OrderedChildNames = append(root.OrderedChildNames, "hello")
str := root.String()
// fmt.Printf("%s\n", strconv.Quote(str))
should := "{ \"one\": [ 1, 2.3, true, \"abc\" ], \"hello\": \"world\" }"
if str != should {
t.Fatal("Expected: \n" + should + "But got\n" + str)
}
}