-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
79 lines (74 loc) · 1.26 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
package main
import (
"bytes"
"io/ioutil"
"testing"
)
func TestParser(t *testing.T) {
src, err := ioutil.ReadFile("modem.conf")
if err != nil {
t.Fatal(err)
}
p, err := NewParser(bytes.NewReader(src))
if err != nil {
t.Fatal(err)
}
ass, err := p.Parse()
if err != nil {
t.Fatal(err)
}
mainSample := []struct {
key, value string
}{
{"interval", "15"},
{"group", "0"},
{"language", "en"},
}
main, err := ass.Section("main")
if err != nil {
t.Fatal(err)
}
for _, v := range mainSample {
value, err := main.Get(v.key)
if err != nil {
t.Fatal(err)
}
if value != v.value {
t.Errorf("expected %s got %s", v.value, value)
}
}
// test the encoding to json
dst := &bytes.Buffer{}
err = ass.ToJSON(dst)
if err != nil {
t.Error(err)
}
nAst := &Ast{}
err = nAst.LoadJSON(dst.Bytes())
if err != nil {
t.Error(err)
}
main, err = nAst.Section("main")
if err != nil {
t.Fatal(err)
}
for _, v := range mainSample {
value, err := main.Get(v.key)
if err != nil {
t.Fatal(err)
}
if value != v.value {
t.Errorf("expected %s got %s", v.value, value)
}
}
nBuf := &bytes.Buffer{}
PrintAst(nBuf, nAst)
np, err := NewParser(nBuf)
if err != nil {
t.Fatal(err)
}
_, err = np.Parse()
if err != nil {
t.Error(err)
}
}