File: marshal_test.go

package info (click to toggle)
golang-github-relex-aini 1.6.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 204 kB
  • sloc: makefile: 15
file content (57 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download
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"])
	})
}