This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsessionInfoToVarMap_test.go
68 lines (59 loc) · 1.56 KB
/
sessionInfoToVarMap_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
package webwire
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestSessionInfoToVarMap tests the SessionInfoToVarMap function
// using the generic session info implementation
func TestSessionInfoToVarMap(t *testing.T) {
check := func(varMap map[string]interface{}) {
expectedStruct := struct {
Name string
Weight float64
}{
Name: "samplename",
Weight: 20.5,
}
require.Len(t, varMap, 4)
require.Equal(t, "value1", varMap["field1"])
require.Equal(t, int(42), varMap["field2"])
require.Equal(t, expectedStruct, varMap["field3"])
require.IsType(t, []string{}, varMap["field4"])
require.ElementsMatch(t, []string{"item1", "item2"}, varMap["field4"])
}
info := &GenericSessionInfo{
data: map[string]interface{}{
"field1": "value1",
"field2": int(42),
"field3": struct {
Name string
Weight float64
}{
Name: "samplename",
Weight: 20.5,
},
"field4": []string{"item1", "item2"},
},
}
varMap := SessionInfoToVarMap(SessionInfo(info))
check(varMap)
// Test immutability, ensure fields won't mutate
// even if the original session info object was changed
info.data["field1"] = "mutated"
info.data["field2"] = int(84)
info.data["field3"] = struct {
Name string
Weight float64
}{
Name: "another name",
Weight: 0.75,
}
info.data["field4"] = []string{"item3"}
check(varMap)
}
// TestSessionInfoToVarMapNil tests the SessionInfoToVarMap function
// with a nil session info
func TestSessionInfoToVarMapNil(t *testing.T) {
varMap := SessionInfoToVarMap(nil)
require.Nil(t, varMap)
}