Skip to content

Commit 1d2432c

Browse files
committed
Add 'TestExpandFrameworkStringyValueSet'.
1 parent 85fbb38 commit 1d2432c

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

internal/framework/flex/set.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ func ExpandFrameworkStringValueSet(ctx context.Context, v basetypes.SetValuable)
6363
}
6464

6565
func ExpandFrameworkStringyValueSet[T ~string](ctx context.Context, v basetypes.SetValuable) itypes.Set[T] {
66-
return tfslices.ApplyToAll(ExpandFrameworkStringValueSet(ctx, v), func(s string) T { return T(s) })
66+
vs := ExpandFrameworkStringValueSet(ctx, v)
67+
if vs == nil {
68+
return nil
69+
}
70+
return tfslices.ApplyToAll(vs, func(s string) T { return T(s) })
6771
}
6872

6973
// FlattenFrameworkInt64Set converts a slice of int32 pointers to a framework Set value.

internal/framework/flex/set_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,59 @@ func TestExpandFrameworkStringValueSet(t *testing.T) {
309309
}
310310
}
311311

312+
func TestExpandFrameworkStringyValueSet(t *testing.T) {
313+
t.Parallel()
314+
315+
type testEnum string
316+
var testVal1 testEnum = "testVal1"
317+
var testVal2 testEnum = "testVal2"
318+
319+
type testCase struct {
320+
input types.Set
321+
expected itypes.Set[testEnum]
322+
}
323+
tests := map[string]testCase{
324+
"null": {
325+
input: types.SetNull(types.StringType),
326+
expected: nil,
327+
},
328+
"unknown": {
329+
input: types.SetUnknown(types.StringType),
330+
expected: nil,
331+
},
332+
"two elements": {
333+
input: types.SetValueMust(types.StringType, []attr.Value{
334+
types.StringValue(string(testVal1)),
335+
types.StringValue(string(testVal2)),
336+
}),
337+
expected: []testEnum{testVal1, testVal2},
338+
},
339+
"zero elements": {
340+
input: types.SetValueMust(types.StringType, []attr.Value{}),
341+
expected: []testEnum{},
342+
},
343+
"invalid element type": {
344+
input: types.SetValueMust(types.Int64Type, []attr.Value{
345+
types.Int64Value(42),
346+
}),
347+
expected: nil,
348+
},
349+
}
350+
351+
for name, test := range tests {
352+
name, test := name, test
353+
t.Run(name, func(t *testing.T) {
354+
t.Parallel()
355+
356+
got := flex.ExpandFrameworkStringyValueSet[testEnum](context.Background(), test.input)
357+
358+
if diff := cmp.Diff(got, test.expected); diff != "" {
359+
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
360+
}
361+
})
362+
}
363+
}
364+
312365
func TestFlattenFrameworkInt32Set(t *testing.T) {
313366
t.Parallel()
314367

0 commit comments

Comments
 (0)