-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
163 lines (121 loc) · 2.72 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
158
159
160
161
162
163
package yamlink_test
import (
"strings"
"testing"
"github.com/NishantJoshi00/yamlink"
)
func TestDepth1Parse(t *testing.T) {
data := `
key1: value1
key2: value2
`
m, err := yamlink.ParseYaml(strings.NewReader(data))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
var nm map[string]interface{} = m.(map[string]interface{})
if len(nm) != 2 {
t.Errorf("Expected 2 keys, got %d", len(nm))
}
if nm["key1"] != "value1" {
t.Errorf("Expected value1, got %v", nm["key1"])
}
}
func TestPathDepth1(t *testing.T) {
table := `
key1: example.com
`
m, err := yamlink.ParseYaml(strings.NewReader(table))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
val, err := yamlink.PathLookup("key1", m)
if err != nil {
t.Errorf("Error looking up path: %v", err)
}
if val != "example.com" {
t.Errorf("Expected example.com, got %v", val)
}
}
func TestPathDepth2Map(t *testing.T) {
table := `
key1:
key2: example.com
`
m, err := yamlink.ParseYaml(strings.NewReader(table))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
val, err := yamlink.PathLookup("key1/key2", m)
if err != nil {
t.Errorf("Error looking up path: %v", err)
}
if val != "example.com" {
t.Errorf("Expected example.com, got %v", val)
}
}
func TestPathDepth2Array(t *testing.T) {
table := `
key1:
- example.com
`
m, err := yamlink.ParseYaml(strings.NewReader(table))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
val, err := yamlink.PathLookup("key1/0", m)
if err != nil {
t.Errorf("Error looking up path: %v", err)
}
if val != "example.com" {
t.Errorf("Expected example.com, got %v", val)
}
}
func TestPathDepth3ArrayMap(t *testing.T) {
table := `
key1:
- magic.com
- key2: example.com
`
m, err := yamlink.ParseYaml(strings.NewReader(table))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
val, err := yamlink.PathLookup("key1/1/key2", m)
if err != nil {
t.Errorf("Error looking up path: %v", err)
}
if val != "example.com" {
t.Errorf("Expected example.com, got %v", val)
}
}
func TestUnknownPathDepth1(t *testing.T) {
table := `
key1:
- magic.com
- key2: example.com
`
m, err := yamlink.ParseYaml(strings.NewReader(table))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
_, err = yamlink.PathLookup("key1/1/key3", m)
if err == nil {
t.Errorf("Expected error, got nil")
}
}
func TestUnknownPathDepth2(t *testing.T) {
table := `
key1:
- magic.com
- key2: example.com
`
m, err := yamlink.ParseYaml(strings.NewReader(table))
if err != nil {
t.Errorf("Error parsing yaml: %v", err)
}
_, err = yamlink.PathLookup("key1/2/key2", m)
if err == nil {
t.Errorf("Expected error, got nil")
}
}