forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes_test.go
91 lines (82 loc) · 1.96 KB
/
types_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
package statsig
import (
"encoding/json"
"reflect"
"testing"
)
func doValidation(t *testing.T, c *configBase) {
if c.Name != "test" {
t.Errorf("Failed to set name")
}
if c.RuleID != "rule_id" {
t.Errorf("Failed to set rule_id")
}
if c.GroupName != "group_name" {
t.Errorf("Failed to set group_name")
}
if c.GetString("String", "abc") != "str" {
t.Errorf("Failed to get string")
}
if c.GetString("Number", "abc") != "abc" {
t.Errorf("Failed to use fallback string")
}
if c.GetString("Object", "def") != "def" {
t.Errorf("Failed to use fallback string")
}
if c.GetNumber("String", 0.07) != 0.07 {
t.Errorf("Failed to use fallback number")
}
if c.GetNumber("Number", 0.07) != 143.7 {
t.Errorf("Failed to get number")
}
if c.GetNumber("Object", 4) != 4 {
t.Errorf("Failed to use fallback number")
}
if !c.GetBool("String", true) {
t.Errorf("Failed to use fallback boolean")
}
if !c.GetBool("Boolean", false) {
t.Errorf("Failed to get boolean")
}
if c.GetBool("Object", false) {
t.Errorf("Failed to use fallback boolean")
}
}
func TestBasic(t *testing.T) {
jsonMap := make(map[string]interface{})
_ = json.Unmarshal(
[]byte(
`{
"Boolean": true,
"Number": 143.7,
"String": "str",
"Object": {
"NestedBool": false,
"NestedNum": 37
},
"Array":[1,2,3]
}`,
),
&jsonMap,
)
c := NewConfig(
"test",
jsonMap,
"rule_id",
"group_name",
nil,
)
doValidation(t, &c.configBase)
l := NewLayer("test", jsonMap, "rule_id", "group_name", nil)
doValidation(t, &l.configBase)
fallbackValues := make([]interface{}, 0)
fallbackValues = append(fallbackValues, 4, 5, 6)
if !reflect.DeepEqual(c.GetSlice("String", fallbackValues), fallbackValues) {
t.Errorf("Failed to use fallback slice")
}
actualValues := make([]interface{}, 0)
actualValues = append(actualValues, 1.0, 2.0, 3.0)
if !reflect.DeepEqual(c.GetSlice("Array", fallbackValues), actualValues) {
t.Errorf("Failed to get number array")
}
}