|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfprotov5_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 12 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 13 | +) |
| 14 | + |
| 15 | +func TestDynamicValueIsNull(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + testCases := map[string]struct { |
| 19 | + dynamicValue tfprotov5.DynamicValue |
| 20 | + expected bool |
| 21 | + expectedError error |
| 22 | + }{ |
| 23 | + "empty-dynamic-value": { |
| 24 | + dynamicValue: tfprotov5.DynamicValue{}, |
| 25 | + expected: false, |
| 26 | + expectedError: fmt.Errorf("unable to read DynamicValue: DynamicValue had no JSON or msgpack data set"), |
| 27 | + }, |
| 28 | + "null": { |
| 29 | + dynamicValue: testNewDynamicValueMust(t, |
| 30 | + tftypes.Object{ |
| 31 | + AttributeTypes: map[string]tftypes.Type{ |
| 32 | + "test_string_attribute": tftypes.String, |
| 33 | + }, |
| 34 | + }, |
| 35 | + tftypes.NewValue( |
| 36 | + tftypes.Object{ |
| 37 | + AttributeTypes: map[string]tftypes.Type{ |
| 38 | + "test_string_attribute": tftypes.String, |
| 39 | + }, |
| 40 | + }, |
| 41 | + nil, |
| 42 | + ), |
| 43 | + ), |
| 44 | + expected: true, |
| 45 | + }, |
| 46 | + "known": { |
| 47 | + dynamicValue: testNewDynamicValueMust(t, |
| 48 | + tftypes.Object{ |
| 49 | + AttributeTypes: map[string]tftypes.Type{ |
| 50 | + "test_string_attribute": tftypes.String, |
| 51 | + }, |
| 52 | + }, |
| 53 | + tftypes.NewValue( |
| 54 | + tftypes.Object{ |
| 55 | + AttributeTypes: map[string]tftypes.Type{ |
| 56 | + "test_string_attribute": tftypes.String, |
| 57 | + }, |
| 58 | + }, |
| 59 | + map[string]tftypes.Value{ |
| 60 | + "test_string_attribute": tftypes.NewValue(tftypes.String, "test-value"), |
| 61 | + }, |
| 62 | + ), |
| 63 | + ), |
| 64 | + expected: false, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for name, testCase := range testCases { |
| 69 | + name, testCase := name, testCase |
| 70 | + |
| 71 | + t.Run(name, func(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + got, err := testCase.dynamicValue.IsNull() |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + if testCase.expectedError == nil { |
| 78 | + t.Fatalf("wanted no error, got error: %s", err) |
| 79 | + } |
| 80 | + |
| 81 | + if !strings.Contains(err.Error(), testCase.expectedError.Error()) { |
| 82 | + t.Fatalf("wanted error %q, got error: %s", testCase.expectedError.Error(), err.Error()) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if err == nil && testCase.expectedError != nil { |
| 87 | + t.Fatalf("got no error, wanted err: %s", testCase.expectedError) |
| 88 | + } |
| 89 | + |
| 90 | + if got != testCase.expected { |
| 91 | + t.Errorf("expected %t, got %t", testCase.expected, got) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func testNewDynamicValueMust(t *testing.T, typ tftypes.Type, value tftypes.Value) tfprotov5.DynamicValue { |
| 98 | + t.Helper() |
| 99 | + |
| 100 | + dynamicValue, err := tfprotov5.NewDynamicValue(typ, value) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("unable to create DynamicValue: %s", err) |
| 104 | + } |
| 105 | + |
| 106 | + return dynamicValue |
| 107 | +} |
0 commit comments