|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package flex |
| 5 | + |
| 6 | +// Tests AutoFlex's Expand/Flatten of args to validate top-level argument shape (nil/typed-nil, |
| 7 | +// pointer-ness, struct↔non-struct). They intentionally do not assert logging; only diagnostic codes. |
| 8 | + |
| 9 | +import ( |
| 10 | +"testing" |
| 11 | +) |
| 12 | + |
| 13 | +type emptyStruct struct{} |
| 14 | + |
| 15 | +func TestExpandArgs_nilAndPointers(t *testing.T) { |
| 16 | +t.Parallel() |
| 17 | + |
| 18 | +var ( |
| 19 | +typedNilSource *emptyStruct |
| 20 | +typedNilTarget *emptyStruct |
| 21 | +) |
| 22 | + |
| 23 | +testCases := autoFlexTestCases{ |
| 24 | +"nil Source": { |
| 25 | +Target: &emptyStruct{}, |
| 26 | +ExpectedDiags: diagAFNil(diagExpandingSourceIsNil), |
| 27 | +}, |
| 28 | +"typed nil Source": { |
| 29 | +Source: typedNilSource, |
| 30 | +Target: &emptyStruct{}, |
| 31 | +ExpectedDiags: diagAFNil(diagExpandingSourceIsNil), // FIXME: Should give the actual type |
| 32 | +}, |
| 33 | +"nil Target": { |
| 34 | +Source: emptyStruct{}, |
| 35 | +ExpectedDiags: diagAFNil(diagConvertingTargetIsNil), |
| 36 | +}, |
| 37 | +"typed nil Target": { |
| 38 | +Source: emptyStruct{}, |
| 39 | +Target: typedNilTarget, |
| 40 | +ExpectedDiags: diagAF[*emptyStruct](diagConvertingTargetIsNil), |
| 41 | +}, |
| 42 | +"non-pointer Target": { |
| 43 | +Source: emptyStruct{}, |
| 44 | +Target: 0, |
| 45 | +ExpectedDiags: diagAF[int](diagConvertingTargetIsNotPointer), |
| 46 | +}, |
| 47 | +} |
| 48 | + |
| 49 | +runAutoExpandTestCases(t, testCases, runChecks{CompareDiags: true, CompareTarget: true}) |
| 50 | +} |
| 51 | + |
| 52 | +func TestExpandArgs_shapeCompatibility(t *testing.T) { |
| 53 | +t.Parallel() |
| 54 | + |
| 55 | +testString := "test" |
| 56 | + |
| 57 | +testCases := autoFlexTestCases{ |
| 58 | +"non-struct Source struct Target": { |
| 59 | +Source: testString, |
| 60 | +Target: &emptyStruct{}, |
| 61 | +ExpectedDiags: diagAF[string](diagExpandingSourceDoesNotImplementAttrValue), |
| 62 | +}, |
| 63 | +"struct Source non-struct Target": { |
| 64 | +Source: emptyStruct{}, |
| 65 | +Target: &testString, |
| 66 | +ExpectedDiags: diagAF[emptyStruct](diagExpandingSourceDoesNotImplementAttrValue), |
| 67 | +}, |
| 68 | +"empty struct Source and Target": { |
| 69 | +Source: emptyStruct{}, |
| 70 | +Target: &emptyStruct{}, |
| 71 | +WantTarget: &emptyStruct{}, |
| 72 | +}, |
| 73 | +"empty struct pointer Source and Target": { |
| 74 | +Source: &emptyStruct{}, |
| 75 | +Target: &emptyStruct{}, |
| 76 | +WantTarget: &emptyStruct{}, |
| 77 | +}, |
| 78 | +} |
| 79 | + |
| 80 | +runAutoExpandTestCases(t, testCases, runChecks{CompareDiags: true, CompareTarget: true}) |
| 81 | +} |
| 82 | + |
| 83 | +func TestFlattenArgs_nilAndPointers(t *testing.T) { |
| 84 | +t.Parallel() |
| 85 | + |
| 86 | +var ( |
| 87 | +typedNilSource *emptyStruct |
| 88 | +typedNilTarget *emptyStruct |
| 89 | +) |
| 90 | + |
| 91 | +testCases := autoFlexTestCases{ |
| 92 | +"nil Source": { |
| 93 | +Target: &emptyStruct{}, |
| 94 | +ExpectedDiags: diagAFNil(diagFlatteningSourceIsNil), |
| 95 | +}, |
| 96 | +"typed nil Source": { |
| 97 | +Source: typedNilSource, |
| 98 | +Target: &emptyStruct{}, |
| 99 | +ExpectedDiags: diagAF[*emptyStruct](diagFlatteningSourceIsNil), |
| 100 | +}, |
| 101 | +"nil Target": { |
| 102 | +Source: emptyStruct{}, |
| 103 | +ExpectedDiags: diagAFNil(diagConvertingTargetIsNil), |
| 104 | +}, |
| 105 | +"typed nil Target": { |
| 106 | +Source: emptyStruct{}, |
| 107 | +Target: typedNilTarget, |
| 108 | +ExpectedDiags: diagAF[*emptyStruct](diagConvertingTargetIsNil), |
| 109 | +}, |
| 110 | +"non-pointer Target": { |
| 111 | +Source: emptyStruct{}, |
| 112 | +Target: 0, |
| 113 | +ExpectedDiags: diagAF[int](diagConvertingTargetIsNotPointer), |
| 114 | +}, |
| 115 | +} |
| 116 | + |
| 117 | +runAutoFlattenTestCases(t, testCases, runChecks{CompareDiags: true, CompareTarget: true}) |
| 118 | +} |
| 119 | + |
| 120 | +func TestFlattenArgs_shapeCompatibility(t *testing.T) { |
| 121 | +t.Parallel() |
| 122 | + |
| 123 | +testString := "test" |
| 124 | + |
| 125 | +testCases := autoFlexTestCases{ |
| 126 | +"non-struct Source struct Target": { |
| 127 | +Source: testString, |
| 128 | +Target: &emptyStruct{}, |
| 129 | +ExpectedDiags: diagAF[emptyStruct](diagFlatteningTargetDoesNotImplementAttrValue), |
| 130 | +}, |
| 131 | +"struct Source non-struct Target": { |
| 132 | +Source: emptyStruct{}, |
| 133 | +Target: &testString, |
| 134 | +ExpectedDiags: diagAF[string](diagFlatteningTargetDoesNotImplementAttrValue), |
| 135 | +}, |
| 136 | +"empty struct Source and Target": { |
| 137 | +Source: emptyStruct{}, |
| 138 | +Target: &emptyStruct{}, |
| 139 | +WantTarget: &emptyStruct{}, |
| 140 | +}, |
| 141 | +"empty struct pointer Source and Target": { |
| 142 | +Source: &emptyStruct{}, |
| 143 | +Target: &emptyStruct{}, |
| 144 | +WantTarget: &emptyStruct{}, |
| 145 | +}, |
| 146 | +} |
| 147 | + |
| 148 | +runAutoFlattenTestCases(t, testCases, runChecks{CompareDiags: true, CompareTarget: true}) |
| 149 | +} |
0 commit comments