-
Notifications
You must be signed in to change notification settings - Fork 13
/
marshal_test.go
57 lines (44 loc) · 1.13 KB
/
marshal_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
package aini
import (
_ "embed"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
const minMarshalInventory = `[Animals]
ET
[Animals:children]
Cats
[Cats]
Lion
`
//go:embed marshal_test_inventory.json
var minMarshalJSON string
func TestMarshalJSON(t *testing.T) {
v, err := ParseString(minMarshalInventory)
assert.Nil(t, err)
j, err := json.MarshalIndent(v, "", " ")
assert.Nil(t, err)
assert.Equal(t, minMarshalJSON, string(j))
t.Run("unmarshal", func(t *testing.T) {
var v2 InventoryData
assert.Nil(t, json.Unmarshal(j, &v2))
assert.Equal(t, v.Hosts["Lion"], v2.Hosts["Lion"])
assert.Equal(t, v.Groups["Cats"], v2.Groups["Cats"])
})
}
func TestMarshalWithVars(t *testing.T) {
v, err := ParseFile("test_data/inventory")
assert.Nil(t, err)
v.HostsToLower()
v.GroupsToLower()
v.AddVarsLowerCased("test_data")
j, err := json.MarshalIndent(v, "", " ")
assert.Nil(t, err)
t.Run("unmarshal", func(t *testing.T) {
var v2 InventoryData
assert.Nil(t, json.Unmarshal(j, &v2))
assert.Equal(t, v.Hosts["host1"], v2.Hosts["host1"])
assert.Equal(t, v.Groups["tomcat"], v2.Groups["tomcat"])
})
}