-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
175 lines (166 loc) · 3.46 KB
/
parse_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
package nestext
import (
"fmt"
"log"
"strings"
"testing"
)
func TestParserUsageError(t *testing.T) {
_, err := Parse(strings.NewReader(""), TopLevel("dict.config"))
if err != nil {
t.Logf("got error = %v", err)
t.Error("expected top-level 'dict.config' to be ok; produced error")
}
_, err = Parse(nil, TopLevel("dict-config"))
if err == nil {
t.Error("expected top-level 'dict-config' to produce an error; didn't")
} else {
t.Logf("got expected error = %v", err)
}
}
func TestParserStack(t *testing.T) {
p := newParser()
p.pushNonterm(false)
if ok := p.stack.pushKV(nil, "one"); !ok {
t.Fatal("pushing a value onto the stack failed")
}
two := "2"
if ok := p.stack.pushKV(&two, "two"); ok {
t.Fatal("pushing a key onto the stack should fail, didn't")
}
}
func TestSimpleDict(t *testing.T) {
input := `
# Example for a dict
a: Hello
b: World
`
result, err := Parse(strings.NewReader(input))
if err != nil {
log.Fatal("parsing failed")
}
fmt.Printf("result = %#v\n", result)
}
func TestTableParse(t *testing.T) {
p := newParser()
t.Logf("============================================================")
inputs := []struct {
text string
correct bool
}{
{`# strange key with :
-:: x
`, true},
{`# string
> Hello
> World
`, true},
{`# string with error
> Hello
> World!
: key
`, false}, // extra ':' line
{`# inline dict with inline list
{a: [x]}
`, true},
{`# inline dict in list
-
{a: 0}
`, true},
{`# inline dict with comma
{a: x, }
`, false},
{`# empty dict entry
:
>
`, true},
{`# multi-line list item
- Hello
-
> World
> !
`, true},
{`# dict
a: Hello
b: World
`, true},
{`# multi-line dict
a:
> Hello World!
b: How are you?
`, true},
{`# multi-line dict
: A
: a
> Hello World!
b: How are you?
`, true},
{`# dict indent error
- Hello
- World!
`, false},
}
for i, input := range inputs {
buf := strings.NewReader(input.text)
result, err := p.Parse(buf)
if err == nil && !input.correct {
t.Errorf("[%2d] expected error to occur, didn't", i)
} else if err == nil {
t.Logf("[%2d] ( %v ) of type %T\n", i, result, result)
} else if err != nil && input.correct {
t.Errorf("[%2d] %s\n", i, err.Error())
} else {
t.Logf("[%2d] got expected error: %s", i, err.Error())
}
t.Logf("------------------------------------------------------------")
}
}
func TestParseForExample(t *testing.T) {
address := `
name: Katheryn McDaniel
address:
> 138 Almond Street
> Topeka, Kansas 20697
phone:
cell: 1-210-555-5297
home: 1-210-555-8470
# Katheryn prefers that we always call her on her cell phone.
email: [email protected]
additional roles:
- board member
`
result, err := Parse(strings.NewReader(address))
if err != nil {
t.Error(err)
}
dump(" ", result.(map[string]interface{}))
}
// ----------------------------------------------------------------------
func dump(space string, v interface{}) {
fmt.Print(space)
_dump(space, v)
}
func _dump(space string, v interface{}) {
if m, ok := v.(map[string]interface{}); ok {
fmt.Printf("{\n")
for k, v := range m {
fmt.Printf(space+" "+"\"%v\": ", k)
if s, ok := v.(string); ok {
fmt.Printf("\"%v\":\n", s)
} else {
_dump(space+" ", v)
}
}
fmt.Printf(space + "}\n")
} else if s, ok := v.(string); ok {
fmt.Printf("%s\"%v\"\n", space, s)
} else if l, ok := v.([]interface{}); ok {
fmt.Printf("[\n")
for _, lv := range l {
_dump(space+" ", lv)
}
fmt.Printf(space + "]\n")
} else {
fmt.Printf("%v%v\n", space, v)
}
}