hcl/decoder_test.go

103 lines
1.6 KiB
Go
Raw Normal View History

2014-08-02 22:44:45 +00:00
package hcl
import (
"io/ioutil"
"path/filepath"
"reflect"
"testing"
)
func TestDecode(t *testing.T) {
cases := []struct {
File string
Err bool
Out interface{}
}{
{
"basic.hcl",
false,
map[string]interface{}{
"foo": "bar",
},
},
2014-08-02 23:07:54 +00:00
{
"structure.hcl",
false,
map[string]interface{}{
2014-08-03 03:40:41 +00:00
"foo": []interface{}{
map[string]interface{}{
"baz": []interface{}{
map[string]interface{}{
"key": 7,
"foo": "bar",
},
},
},
},
2014-08-02 23:07:54 +00:00
},
},
2014-08-02 22:44:45 +00:00
}
for _, tc := range cases {
d, err := ioutil.ReadFile(filepath.Join(fixtureDir, tc.File))
if err != nil {
t.Fatalf("err: %s", err)
}
var out map[string]interface{}
err = Decode(&out, string(d))
if (err != nil) != tc.Err {
t.Fatalf("Input: %s\n\nError: %s", tc.File, err)
}
if !reflect.DeepEqual(out, tc.Out) {
t.Fatalf("Input: %s\n\n%#v", tc.File, out)
}
}
}
2014-08-03 03:48:36 +00:00
func TestDecode_equal(t *testing.T) {
cases := []struct {
One, Two string
}{
{
"basic.hcl",
"basic.json",
},
{
"structure.hcl",
"structure.json",
},
}
for _, tc := range cases {
p1 := filepath.Join(fixtureDir, tc.One)
p2 := filepath.Join(fixtureDir, tc.Two)
d1, err := ioutil.ReadFile(p1)
if err != nil {
t.Fatalf("err: %s", err)
}
d2, err := ioutil.ReadFile(p2)
if err != nil {
t.Fatalf("err: %s", err)
}
var i1, i2 interface{}
err = Decode(&i1, string(d1))
if err != nil {
t.Fatalf("err: %s", err)
}
err = Decode(&i2, string(d2))
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(i1, i2) {
t.Fatalf("%#v\n\n%#v", i1, i2)
}
}
}