This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstack_test.go
More file actions
118 lines (100 loc) · 2.09 KB
/
Copy pathstack_test.go
File metadata and controls
118 lines (100 loc) · 2.09 KB
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
116
117
118
package stack_test
import (
"encoding/json"
"reflect"
"testing"
"koding/kites/kloud/utils/object"
"koding/klientctl/endpoint/stack/stackfixture"
"github.com/hashicorp/hcl"
)
func TestFixHCL(t *testing.T) {
var vHCL interface{}
if err := hcl.Unmarshal(stackfixture.StackHCL, &vHCL); err != nil {
t.Fatalf("hcl.Unmarshal()=%s", err)
}
if testing.Verbose() {
t.Logf("original (vHCL):\n%s", mustJSON(vHCL))
}
if reflect.DeepEqual(vHCL, stackfixture.Stack) {
t.Fatal("expected HCL-encoded stack to not unmarshal cleanly")
}
object.FixHCL(vHCL)
if !reflect.DeepEqual(vHCL, stackfixture.Stack) {
t.Fatalf("got %+v, want %+v", vHCL, stackfixture.Stack)
}
var vJSON interface{}
if err := json.Unmarshal(stackfixture.StackJSON, &vJSON); err != nil {
t.Fatalf("json.Unmarshal()=%s", err)
}
if testing.Verbose() {
t.Logf("fixed (vJSON):\n%s", mustJSON(vJSON))
}
if !reflect.DeepEqual(vJSON, vHCL) {
t.Fatalf("got %+v, want %+v", vJSON, vHCL)
}
}
func TestFixYAML(t *testing.T) {
cases := map[string]struct {
yaml interface{}
want interface{}
}{
"fix map": {
map[interface{}]interface{}{
"abc": 1,
},
map[string]interface{}{
"abc": 1,
},
},
"fix slice of maps": {
[]interface{}{
"abc",
map[interface{}]interface{}{
"abc": 1,
},
1,
},
[]interface{}{
"abc",
map[string]interface{}{
"abc": 1,
},
1,
},
},
"fix nested maps": {
map[interface{}]interface{}{
"abc": map[interface{}]interface{}{
"abc": map[interface{}]interface{}{
"abc": 1,
},
},
},
map[string]interface{}{
"abc": map[string]interface{}{
"abc": map[string]interface{}{
"abc": 1,
},
},
},
},
}
for name, cas := range cases {
// capture range variable here
cas := cas
t.Run(name, func(t *testing.T) {
t.Parallel()
got := object.FixYAML(cas.yaml)
if !reflect.DeepEqual(got, cas.want) {
t.Fatalf("got %#v, want %#v", got, cas.want)
}
})
}
}
func mustJSON(v interface{}) []byte {
p, err := json.MarshalIndent(v, "", "\t")
if err != nil {
panic(err)
}
return p
}