forked from freeconf/yang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_test.go
219 lines (199 loc) · 4.32 KB
/
meta_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package meta
import (
"testing"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/val"
)
func TestMetaLeafList(t *testing.T) {
b := &Builder{}
m := b.Module("m", nil)
l1 := b.Leaf(m, "x")
dt1 := b.Type(l1, "string")
l2 := b.LeafList(m, "y")
dt2 := b.Type(l2, "string")
if err := Compile(m); err != nil {
t.Error(err)
}
fc.AssertEqual(t, val.FmtString, dt1.Format())
fc.AssertEqual(t, val.FmtStringList, dt2.Format())
}
func TestMetaIsConfig(t *testing.T) {
b := &Builder{}
m := b.Module("m", nil)
c := b.Container(m, "c")
l := b.List(c, "l")
if err := Compile(m); err != nil {
t.Error(err)
}
if !l.Config() {
t.Error("Should be config")
}
}
// https://github.com/freeconf/yang/issues/85
func TestChoiceIsConfig(t *testing.T) {
b := &Builder{}
m := b.Module("m", nil)
c := b.Choice(m, "c")
cc := b.Case(c, "l")
b.Config(cc, false)
l := b.Leaf(cc, "l")
b.Type(l, "string")
fc.RequireEqual(t, nil, Compile(m))
fc.AssertEqual(t, false, l.Config())
}
func TestMetaUses(t *testing.T) {
b := &Builder{}
fc.DebugLog(true)
m := b.Module("m", nil)
g := b.Grouping(m, "g")
b.List(g, "l")
b.Uses(m, "g")
if err := Compile(m); err != nil {
t.Error(err)
}
fc.AssertEqual(t, "l", m.DataDefinitions()[0].Ident())
}
func TestChoice(t *testing.T) {
b := &Builder{}
m := b.Module("m", nil)
c := b.Choice(m, "c")
cc1 := b.Case(c, "cc1")
b.Type(b.Leaf(cc1, "l1"), val.FmtString.String())
cc2 := b.Case(c, "cc2")
b.Type(b.Leaf(cc2, "l2"), val.FmtString.String())
if err := Compile(m); err != nil {
t.Error(err)
}
t.Logf("%v", m.DataDefinitions())
actual := c.Cases()["cc2"]
if actual.Ident() != "cc2" {
t.Error("GetCase failed")
}
}
func TestRefine(t *testing.T) {
b := &Builder{}
m := b.Module("m", nil)
g := b.Grouping(m, "x")
u := b.Uses(m, "x")
b.Type(b.Leaf(g, "l"), val.FmtString.String())
r := b.Refine(u, "l")
b.Config(r, false)
if err := Compile(m); err != nil {
t.Error(err)
}
ddef := m.DataDefinitions()[0]
if ddef.(HasDetails).Config() {
t.Fail()
}
}
func TestIfFeature(t *testing.T) {
features := map[string]*Feature{
"foo": {ident: "foo"},
"bar": {ident: "bar"},
}
tests := []struct {
expr string
expected bool
err bool
}{
{
expr: "foo",
expected: true,
},
{
expr: "not foo",
expected: false,
},
{
expr: "not ( foo )",
expected: false,
},
{
expr: "goo",
expected: false,
},
{
expr: "foo and goo",
expected: false,
},
{
expr: "foo or goo",
expected: true,
},
{
expr: "not foo or goo",
expected: false,
},
{
expr: "not (foo and goo)",
expected: true,
},
{
expr: "not foo or bar and baz",
expected: false,
},
{
expr: "(not foo) or (bar and baz)",
expected: false,
},
{
expr: "not not foo",
expected: true,
},
{
expr: "foo bar",
err: true,
},
{
expr: "and foo",
err: true,
},
}
for _, test := range tests {
t.Log(test.expr)
iff := &IfFeature{expr: test.expr}
actual, err := iff.Evaluate(features)
if err != nil {
if !test.err {
t.Error(err)
}
} else {
fc.AssertEqual(t, test.expected, actual)
}
}
}
func TestRefineSplit(t *testing.T) {
r := &Refine{ident: "a/b/c"}
ident, path := r.splitIdent()
fc.AssertEqual(t, "a", ident)
fc.AssertEqual(t, path, "b/c")
r = &Refine{ident: "a"}
ident, path = r.splitIdent()
fc.AssertEqual(t, "a", ident)
fc.AssertEqual(t, path, "")
}
func TestAugment(t *testing.T) {
b := &Builder{}
m := b.Module("m", nil)
x := b.Container(m, "x")
b.Type(b.Leaf(x, "a"), val.FmtInt32.String())
b.Type(b.Leaf(x, "b"), val.FmtInt32.String())
x2 := b.Container(x, "x2")
b.Type(b.Leaf(x2, "d"), val.FmtInt32.String())
y := b.Augment(m, "x/x2")
b.Type(b.Leaf(y, "c"), val.FmtString.String())
b.Type(b.Leaf(y, "f"), val.FmtString.String())
if err := Compile(m); err != nil {
t.Error(err)
}
xActual := m.DataDefinitions() // x
fc.AssertEqual(t, "x", xActual[0].Ident())
fc.AssertEqual(t, 1, len(xActual))
xDefs := xActual[0].(HasDataDefinitions).DataDefinitions() // a, b, x2
fc.AssertEqual(t, 3, len(xDefs))
fc.AssertEqual(t, "x2", xDefs[2].Ident())
x2ActualDefs := xDefs[2].(HasDataDefinitions).DataDefinitions()
fc.AssertEqual(t, "d", x2ActualDefs[0].Ident())
fc.AssertEqual(t, "c", x2ActualDefs[1].Ident())
fc.AssertEqual(t, "f", x2ActualDefs[2].Ident())
}