|
| 1 | +package tfsdk |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + testtypes "github.com/hashicorp/terraform-plugin-framework/internal/testing/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | +) |
| 13 | + |
| 14 | +func TestConvert(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + type testCase struct { |
| 18 | + val attr.Value |
| 19 | + typ attr.Type |
| 20 | + expected attr.Value |
| 21 | + expectedDiags diag.Diagnostics |
| 22 | + } |
| 23 | + |
| 24 | + tests := map[string]testCase{ |
| 25 | + "string-to-testtype-string": { |
| 26 | + val: types.String{Value: "hello"}, |
| 27 | + typ: testtypes.StringType{}, |
| 28 | + expected: testtypes.String{ |
| 29 | + String: types.String{Value: "hello"}, |
| 30 | + CreatedBy: testtypes.StringType{}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + "testtype-string-to-string": { |
| 34 | + val: testtypes.String{ |
| 35 | + String: types.String{Value: "hello"}, |
| 36 | + CreatedBy: testtypes.StringType{}, |
| 37 | + }, |
| 38 | + typ: types.StringType, |
| 39 | + expected: types.String{Value: "hello"}, |
| 40 | + }, |
| 41 | + "string-to-number": { |
| 42 | + val: types.String{Value: "hello"}, |
| 43 | + typ: types.NumberType, |
| 44 | + expectedDiags: diag.Diagnostics{diag.NewErrorDiagnostic( |
| 45 | + "Error converting value", |
| 46 | + "An unexpected error was encountered converting a types.String to a types.NumberType. This is always a problem with the provider. Please tell the provider developers that types.String is not compatible with types.NumberType.", |
| 47 | + )}, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for name, tc := range tests { |
| 52 | + name, tc := name, tc |
| 53 | + t.Run(name, func(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + got, diags := ConvertValue(context.Background(), tc.val, tc.typ) |
| 57 | + |
| 58 | + if diff := cmp.Diff(diags, tc.expectedDiags); diff != "" { |
| 59 | + t.Fatalf("Unexpected diff in diags (-wanted, +got): %s", diff) |
| 60 | + } |
| 61 | + |
| 62 | + if diags.HasError() { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if diff := cmp.Diff(got, tc.expected); diff != "" { |
| 67 | + t.Fatalf("Unexpected diff in result (-wanted, +got): %s", diff) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments