-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings_test.go
115 lines (107 loc) · 2.57 KB
/
settings_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
// Copyright 2013 Fredrik Ehnbom
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package text
import (
"fmt"
"testing"
)
func TestSettings(t *testing.T) {
var (
s1, s2 HasSettings
called bool
)
s1.Settings().SetParent(&s2)
if v, ok := s1.Settings().Get("test", true).(bool); !ok || !v {
t.Error(ok, v)
}
s2.Settings().Set("test", false)
if v, ok := s1.Settings().Get("test", true).(bool); !ok || v {
t.Error(ok, v)
}
s1.Settings().AddOnChange("something", func(name string) {
called = true
})
s2.Settings().Set("test", true)
if !called {
t.Error("Should have been called..")
}
called = false
s1.Settings().ClearOnChange("something")
s2.Settings().Set("test", true)
if called {
t.Error("Should not have been called..")
}
}
func TestCallbacksOnUnmarshal(t *testing.T) {
tests := []struct {
before string
after string
cbs []string
exp map[string]bool
}{
{
`{"font_size": 14}`,
`{"font_size": 12}`,
[]string{"font_size"},
map[string]bool{"font_size": true},
},
{
`{"font_size": 14}`,
`{"font_size": 14}`,
[]string{"font_size"},
map[string]bool{},
},
{
`{"a": "t1", "b": 1, "c": true}`,
`{"a": "t2", "b": 1, "c": false}`,
[]string{"a", "c"},
map[string]bool{"a": true, "c": true},
},
{
`{"font_size": 14}`,
`{"font_size": 12}`,
[]string{},
map[string]bool{},
},
{
`{"a": "t1", "b": 1}`,
`{"a": "t1", "c": false}`,
[]string{"b", "c"},
map[string]bool{"b": true, "c": true},
},
{
`{"ignored_packages": ["Vintageous"]}`,
`{"ignored_packages": ["Six"]}`,
[]string{"ignored_packages"},
map[string]bool{"ignored_packages": true},
},
}
cnt := make(map[string]bool)
cb := func(key string) {
cnt[key] = true
}
for i, test := range tests {
cnt = make(map[string]bool)
val := NewSettings()
set := &val
if err := set.UnmarshalJSON([]byte(test.before)); err != nil {
t.Errorf("Test %d: Error on unmarshaling before data %s", i, err)
}
for _, k := range test.cbs {
set.AddOnChange(fmt.Sprintf("Test.%d.%s", i, k), cb)
}
if err := set.UnmarshalJSON([]byte(test.after)); err != nil {
t.Errorf("Test %d: Error on unmarshaling after data %s", i, err)
}
if lcnt, lexp := len(cnt), len(test.exp); lexp != lcnt {
t.Errorf("Test %d: map and counted map length difference expected %d, but got %d\nexp: %v\ncnt: %v", i, lexp, lcnt, test.exp, cnt)
continue
}
for k, _ := range test.exp {
if !cnt[k] {
t.Errorf("Test %d: Expected %s key get called, but it didn't", i, k)
}
}
}
}