-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NewValue checks to be properly recursive (#79)
Fix the checks that NewValue runs to ensure that a value can be used with a Type to properly handle DynamicPseudoTypes nested in complex types. Builds on and supersedes #76. Should we make this part of the behavior for `.Is`? My gut says "no", because `Is` is supposed to be about checking whether a type is semantically similar, not that it can be used as another type. Maybe we want a `Fulfills` method, similar to Is, that does this? I don't know that this behavior needs to be exported, though... Fixes a bug in #74 that would panic for complex types containing DynamicPseudoTypes. Co-authored-by: Paul Tyng <[email protected]>
- Loading branch information
1 parent
e0e351e
commit 012c7f0
Showing
12 changed files
with
660 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package tftypes | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func Test_newValue_list(t *testing.T) { | ||
t.Parallel() | ||
type testCase struct { | ||
typ Type | ||
val interface{} | ||
err *regexp.Regexp | ||
expected Value | ||
} | ||
tests := map[string]testCase{ | ||
"normal": { | ||
typ: List{ElementType: String}, | ||
val: []Value{ | ||
NewValue(String, "hello"), | ||
NewValue(String, "world"), | ||
}, | ||
expected: Value{ | ||
typ: List{ElementType: String}, | ||
value: []Value{ | ||
{ | ||
typ: String, | ||
value: "hello", | ||
}, | ||
{ | ||
typ: String, | ||
value: "world", | ||
}, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
"dynamic": { | ||
typ: List{ElementType: DynamicPseudoType}, | ||
val: []Value{ | ||
NewValue(String, "hello"), | ||
NewValue(String, "world"), | ||
}, | ||
expected: Value{ | ||
typ: List{ElementType: DynamicPseudoType}, | ||
value: []Value{ | ||
{ | ||
typ: String, | ||
value: "hello", | ||
}, | ||
{ | ||
typ: String, | ||
value: "world", | ||
}, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
"dynamic-different-types": { | ||
typ: List{ElementType: DynamicPseudoType}, | ||
val: []Value{ | ||
NewValue(String, "hello"), | ||
NewValue(Number, 123), | ||
}, | ||
err: regexp.MustCompile(`lists must only contain one type of element, saw tftypes.String and tftypes.Number`), | ||
}, | ||
"wrong-type": { | ||
typ: List{ElementType: String}, | ||
val: []Value{ | ||
NewValue(String, "foo"), | ||
NewValue(Number, 123), | ||
}, | ||
err: regexp.MustCompile(`ElementKeyInt\(1\): can't use tftypes.Number as tftypes.String`), | ||
}, | ||
} | ||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
res, err := newValue(test.typ, test.val) | ||
if err == nil && test.err != nil { | ||
t.Errorf("Expected error to match %q, got nil", test.err) | ||
} else if err != nil && test.err == nil { | ||
t.Errorf("Expected error to be nil, got %q", err) | ||
} else if err != nil && test.err != nil && !test.err.MatchString(err.Error()) { | ||
t.Errorf("Expected error to match %q, got %q", test.err, err.Error()) | ||
} | ||
if !res.Equal(test.expected) { | ||
t.Errorf("Expected value to be %s, got %s", test.expected, res) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package tftypes | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func Test_newValue_map(t *testing.T) { | ||
t.Parallel() | ||
type testCase struct { | ||
typ Type | ||
val interface{} | ||
err *regexp.Regexp | ||
expected Value | ||
} | ||
tests := map[string]testCase{ | ||
"normal": { | ||
typ: Map{AttributeType: String}, | ||
val: map[string]Value{ | ||
"a": NewValue(String, "hello"), | ||
"b": NewValue(String, "world"), | ||
}, | ||
expected: Value{ | ||
typ: Map{AttributeType: String}, | ||
value: map[string]Value{ | ||
"a": { | ||
typ: String, | ||
value: "hello", | ||
}, | ||
"b": { | ||
typ: String, | ||
value: "world", | ||
}, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
"dynamic": { | ||
typ: Map{AttributeType: DynamicPseudoType}, | ||
val: map[string]Value{ | ||
"a": NewValue(String, "hello"), | ||
"b": NewValue(String, "world"), | ||
}, | ||
expected: Value{ | ||
typ: Map{AttributeType: DynamicPseudoType}, | ||
value: map[string]Value{ | ||
"a": { | ||
typ: String, | ||
value: "hello", | ||
}, | ||
"b": { | ||
typ: String, | ||
value: "world", | ||
}, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
"dynamic-different-types": { | ||
typ: Map{AttributeType: DynamicPseudoType}, | ||
val: map[string]Value{ | ||
"a": NewValue(String, "hello"), | ||
"b": NewValue(Number, 123), | ||
}, | ||
err: regexp.MustCompile(`maps must only contain one type of element, saw tftypes.String and tftypes.Number`), | ||
}, | ||
"wrong-type": { | ||
typ: Map{AttributeType: String}, | ||
val: map[string]Value{ | ||
"a": NewValue(String, "foo"), | ||
"b": NewValue(Number, 123), | ||
}, | ||
err: regexp.MustCompile(`ElementKeyString\("b"\): can't use tftypes.Number as tftypes.String`), | ||
}, | ||
} | ||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
res, err := newValue(test.typ, test.val) | ||
if err == nil && test.err != nil { | ||
t.Errorf("Expected error to match %q, got nil", test.err) | ||
} else if err != nil && test.err == nil { | ||
t.Errorf("Expected error to be nil, got %q", err) | ||
} else if err != nil && test.err != nil && !test.err.MatchString(err.Error()) { | ||
t.Errorf("Expected error to match %q, got %q", test.err, err.Error()) | ||
} | ||
if !res.Equal(test.expected) { | ||
t.Errorf("Expected value to be %s, got %s", test.expected, res) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.