From 258b8826b302e09e0630f03a066c6f8740c80f90 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Fri, 5 Apr 2019 12:42:51 -0400 Subject: [PATCH] decoder_test check for decodedFields and unusedKeys --- decoder_test.go | 34 +++++++++++++++++++++ test-fixtures/structure_map_extra_keys.hcl | 4 +++ test-fixtures/structure_map_extra_keys.json | 6 ++++ 3 files changed, 44 insertions(+) create mode 100644 test-fixtures/structure_map_extra_keys.hcl create mode 100644 test-fixtures/structure_map_extra_keys.json diff --git a/decoder_test.go b/decoder_test.go index 3ffdcfd..adc4ee6 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -786,6 +786,40 @@ func TestDecode_structureMapInvalid(t *testing.T) { } } +func TestDecode_structureMapExtraKeys(t *testing.T) { + type hclVariable struct { + A int + B int + Found []string `hcl:",decodedFields"` + Extra []string `hcl:",unusedKeys"` + } + + q := hclVariable{ + A: 1, + B: 2, + Found: []string{"A", "B"}, + Extra: []string{"extra1", "extra2"}, + } + + var p hclVariable + ast, _ := Parse(testReadFile(t, "structure_map_extra_keys.hcl")) + DecodeObject(&p, ast) + if !(p.A == q.A && p.B == q.B && + reflect.DeepEqual(p.Found, q.Found) && + reflect.DeepEqual(p.Extra, q.Extra)) { + t.Fatal("not equal") + } + + var j hclVariable + ast, _ = Parse(testReadFile(t, "structure_map_extra_keys.json")) + DecodeObject(&j, ast) + if !(j.A == q.A && j.B == q.B && + reflect.DeepEqual(j.Found, q.Found) && + reflect.DeepEqual(j.Extra, q.Extra)) { + t.Fatal("not equal") + } +} + func TestDecode_interfaceNonPointer(t *testing.T) { var value interface{} err := Decode(value, testReadFile(t, "basic_int_string.hcl")) diff --git a/test-fixtures/structure_map_extra_keys.hcl b/test-fixtures/structure_map_extra_keys.hcl new file mode 100644 index 0000000..b888c76 --- /dev/null +++ b/test-fixtures/structure_map_extra_keys.hcl @@ -0,0 +1,4 @@ +a = 1 +b = 2 +extra1 = 3 +extra2 = 4 diff --git a/test-fixtures/structure_map_extra_keys.json b/test-fixtures/structure_map_extra_keys.json new file mode 100644 index 0000000..2c04972 --- /dev/null +++ b/test-fixtures/structure_map_extra_keys.json @@ -0,0 +1,6 @@ +{ + "a": 1, + "b": 2, + "extra1": 3, + "extra2": 4 +}