-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson_test.go
More file actions
100 lines (90 loc) · 2.72 KB
/
json_test.go
File metadata and controls
100 lines (90 loc) · 2.72 KB
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
package gonfig_test
import (
. "github.com/Nomon/gonfig"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("JsonConfig", func() {
var (
err error
cfg WritableConfig
)
BeforeEach(func() {
cfg = NewJsonConfig("./config_valid.json")
err = cfg.Load()
})
Context("When the JSON config marshals properly", func() {
It("Should have a string variable in config", func() {
Expect(cfg.Get("test")).To(Equal("123"))
})
It("Should have an int variable in config", func() {
Expect(cfg.Get("test_number")).To(Equal("1"))
})
It("Should have a bool variable in config", func() {
Expect(cfg.Get("test_bool")).To(Equal("true"))
})
It("Should have a float variable in config", func() {
Expect(cfg.Get("test_float")).To(Equal("12.34"))
})
It("Should not error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("Should have the string value from a nested map", func() {
Expect(cfg.Get("test_object:nested_string")).To(Equal("abcd"))
})
It("Should have the string value from a deeply nested map", func() {
Expect(cfg.Get("double_nested:nested_object:test_inner")).To(Equal("foo"))
})
It("Should have the int value from a nested map", func() {
Expect(cfg.Get("test_object:nested_int")).To(Equal("987"))
})
It("Should have the values from an input array", func() {
Expect(cfg.Get("test_array")).To(Equal("1,2,3"))
})
})
Context("When config fails to marshal", func() {
BeforeEach(func() {
cfg = NewJsonConfig("./config_invalid.json")
err = cfg.Load()
})
It("should return a functional config", func() {
Expect(cfg).ToNot(BeZero())
cfg.Set("QQ", "123")
Expect(cfg.Get("QQ")).To(Equal("123"))
})
It("should error", func() {
Expect(err).To(HaveOccurred())
})
})
Context("When the JSON config does not exist", func() {
BeforeEach(func() {
cfg = NewJsonConfig("./config_nonexisting.json")
err = cfg.Load()
})
It("should return a functional config", func() {
Expect(cfg).ToNot(BeZero())
cfg.Set("QQ", "123")
Expect(cfg.Get("QQ")).To(Equal("123"))
})
It("should error", func() {
Expect(err).To(HaveOccurred())
})
})
Describe("Config conversion", func() {
It("Should be possible ro construct new JSON config from a gonfig hierarchy", func() {
cfg := NewConfig(nil)
cfg.Use("config_a", NewMemoryConfig())
cfg.Use("config_b", NewMemoryConfig())
cfg.Use("config_a").Set("config_a_var_a", "conf_a")
cfg.Use("config_b").Set("config_b_var_a", "conf_b")
jsonConf := &JsonConfig{cfg, "./config.json"}
err := jsonConf.Save()
Expect(err).ToNot(HaveOccurred())
jsonConf2 := NewJsonConfig("./config.json", cfg)
err = jsonConf2.Save()
Expect(err).ToNot(HaveOccurred())
})
})
Describe("Namespacing", func() {
})
})