-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_test.go
73 lines (64 loc) · 2.84 KB
/
loader_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
package gofigure
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v3"
)
var _ = Describe("Loader", func() {
It("should Load", func() {
loader := New()
Expect(loader.Load("config/app.yaml", []byte(`env: dev
name: John`))).To(BeNil())
Expect(loader.root.kind).To(Equal(yaml.MappingNode))
Expect(loader.root.mappingNodes["config"].mappingNodes["app"].mappingNodes["env"].value).To(Equal("dev"))
Expect(loader.root.mappingNodes["config"].mappingNodes["app"].mappingNodes["name"].value).To(Equal("John"))
Expect(loader.Load("config/app.yaml", []byte(`env: test`))).To(BeNil())
Expect(loader.root.mappingNodes["config"].mappingNodes["app"].mappingNodes["env"].value).To(Equal("test"))
Expect(loader.root.mappingNodes["config"].mappingNodes["app"].mappingNodes["name"].value).To(Equal("John"))
Expect(loader.Load("another.yaml", []byte(`value: another`))).To(BeNil())
Expect(loader.root.kind).To(Equal(yaml.MappingNode))
Expect(loader.root.mappingNodes["another"].mappingNodes["value"].value).To(Equal("another"))
})
It("should Get", func() {
loader := New()
Expect(loader.Load("config/app.yaml", []byte(`array:
- 213
- 456`))).To(BeNil())
var value int
Expect(loader.Get(context.Background(), "config.app.array[0]", &value)).To(BeNil())
Expect(value).To(Equal(213))
Expect(loader.Get(context.Background(), "config.app.array[1]", &value)).To(BeNil())
Expect(value).To(Equal(456))
value = 789
Expect(loader.Get(context.Background(), "config.app.array[2]", &value)).To(BeNil())
Expect(value).To(Equal(789))
})
It("should preserve original value of tagged node", func() {
loader := New().WithFeatures(FeatureFunc("!test", func(ctx context.Context, loader *Loader, node *Node) (*Node, error) {
return NewScalarNode("hello world"), nil
}))
Expect(loader.Load("app.yaml", []byte(`value: !test origin`))).To(BeNil())
var value string
Expect(loader.Get(context.Background(), "app.value", &value)).To(BeNil())
Expect(value).To(Equal("hello world"))
Expect(loader.root.mappingNodes["app"].mappingNodes["value"].value).To(Equal("origin"))
})
It("should override", func() {
loader := New()
Expect(loader.Load("", []byte(`env: dev
name:
first: John
last: Doe`))).To(BeNil())
Expect(loader.root.kind).To(Equal(yaml.MappingNode))
Expect(loader.root.mappingNodes["env"].value).To(Equal("dev"))
Expect(loader.root.mappingNodes["name"].mappingNodes["first"].value).To(Equal("John"))
Expect(loader.root.mappingNodes["name"].mappingNodes["last"].value).To(Equal("Doe"))
Expect(loader.Load("", []byte(`env: test
name:
first: Jane`))).To(BeNil())
Expect(loader.root.mappingNodes["env"].value).To(Equal("test"))
Expect(loader.root.mappingNodes["name"].mappingNodes["first"].value).To(Equal("Jane"))
Expect(loader.root.mappingNodes["name"].mappingNodes["last"].value).To(Equal("Doe"))
})
})