github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/legacy/helper/schema/set_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package schema 5 6 import ( 7 "reflect" 8 "testing" 9 ) 10 11 func TestSetAdd(t *testing.T) { 12 s := &Set{F: testSetInt} 13 s.Add(1) 14 s.Add(5) 15 s.Add(25) 16 17 expected := []interface{}{1, 25, 5} 18 actual := s.List() 19 if !reflect.DeepEqual(actual, expected) { 20 t.Fatalf("bad: %#v", actual) 21 } 22 } 23 24 func TestSetAdd_negative(t *testing.T) { 25 // Since we don't allow negative hashes, this should just hash to the 26 // same thing... 27 s := &Set{F: testSetInt} 28 s.Add(-1) 29 s.Add(1) 30 31 expected := []interface{}{-1} 32 actual := s.List() 33 if !reflect.DeepEqual(actual, expected) { 34 t.Fatalf("bad: %#v", actual) 35 } 36 } 37 38 func TestSetContains(t *testing.T) { 39 s := &Set{F: testSetInt} 40 s.Add(5) 41 s.Add(-5) 42 43 if s.Contains(2) { 44 t.Fatal("should not contain") 45 } 46 if !s.Contains(5) { 47 t.Fatal("should contain") 48 } 49 if !s.Contains(-5) { 50 t.Fatal("should contain") 51 } 52 } 53 54 func TestSetDifference(t *testing.T) { 55 s1 := &Set{F: testSetInt} 56 s2 := &Set{F: testSetInt} 57 58 s1.Add(1) 59 s1.Add(5) 60 61 s2.Add(5) 62 s2.Add(25) 63 64 difference := s1.Difference(s2) 65 difference.Add(2) 66 67 expected := []interface{}{1, 2} 68 actual := difference.List() 69 if !reflect.DeepEqual(actual, expected) { 70 t.Fatalf("bad: %#v", actual) 71 } 72 } 73 74 func TestSetIntersection(t *testing.T) { 75 s1 := &Set{F: testSetInt} 76 s2 := &Set{F: testSetInt} 77 78 s1.Add(1) 79 s1.Add(5) 80 81 s2.Add(5) 82 s2.Add(25) 83 84 intersection := s1.Intersection(s2) 85 intersection.Add(2) 86 87 expected := []interface{}{2, 5} 88 actual := intersection.List() 89 if !reflect.DeepEqual(actual, expected) { 90 t.Fatalf("bad: %#v", actual) 91 } 92 } 93 94 func TestSetUnion(t *testing.T) { 95 s1 := &Set{F: testSetInt} 96 s2 := &Set{F: testSetInt} 97 98 s1.Add(1) 99 s1.Add(5) 100 101 s2.Add(5) 102 s2.Add(25) 103 104 union := s1.Union(s2) 105 union.Add(2) 106 107 expected := []interface{}{1, 2, 25, 5} 108 actual := union.List() 109 if !reflect.DeepEqual(actual, expected) { 110 t.Fatalf("bad: %#v", actual) 111 } 112 } 113 114 func testSetInt(v interface{}) int { 115 return v.(int) 116 } 117 118 func TestHashResource_nil(t *testing.T) { 119 resource := &Resource{ 120 Schema: map[string]*Schema{ 121 "name": { 122 Type: TypeString, 123 Optional: true, 124 }, 125 }, 126 } 127 f := HashResource(resource) 128 129 idx := f(nil) 130 if idx != 0 { 131 t.Fatalf("Expected 0 when hashing nil, given: %d", idx) 132 } 133 } 134 135 func TestHashEqual(t *testing.T) { 136 nested := &Resource{ 137 Schema: map[string]*Schema{ 138 "foo": { 139 Type: TypeString, 140 Optional: true, 141 }, 142 }, 143 } 144 root := &Resource{ 145 Schema: map[string]*Schema{ 146 "bar": { 147 Type: TypeString, 148 Optional: true, 149 }, 150 "nested": { 151 Type: TypeSet, 152 Optional: true, 153 Elem: nested, 154 }, 155 }, 156 } 157 n1 := map[string]interface{}{"foo": "bar"} 158 n2 := map[string]interface{}{"foo": "baz"} 159 160 r1 := map[string]interface{}{ 161 "bar": "baz", 162 "nested": NewSet(HashResource(nested), []interface{}{n1}), 163 } 164 r2 := map[string]interface{}{ 165 "bar": "qux", 166 "nested": NewSet(HashResource(nested), []interface{}{n2}), 167 } 168 r3 := map[string]interface{}{ 169 "bar": "baz", 170 "nested": NewSet(HashResource(nested), []interface{}{n2}), 171 } 172 r4 := map[string]interface{}{ 173 "bar": "qux", 174 "nested": NewSet(HashResource(nested), []interface{}{n1}), 175 } 176 s1 := NewSet(HashResource(root), []interface{}{r1}) 177 s2 := NewSet(HashResource(root), []interface{}{r2}) 178 s3 := NewSet(HashResource(root), []interface{}{r3}) 179 s4 := NewSet(HashResource(root), []interface{}{r4}) 180 181 cases := []struct { 182 name string 183 set *Set 184 compare *Set 185 expected bool 186 }{ 187 { 188 name: "equal", 189 set: s1, 190 compare: s1, 191 expected: true, 192 }, 193 { 194 name: "not equal", 195 set: s1, 196 compare: s2, 197 expected: false, 198 }, 199 { 200 name: "outer equal, should still not be equal", 201 set: s1, 202 compare: s3, 203 expected: false, 204 }, 205 { 206 name: "inner equal, should still not be equal", 207 set: s1, 208 compare: s4, 209 expected: false, 210 }, 211 } 212 for _, tc := range cases { 213 t.Run(tc.name, func(t *testing.T) { 214 actual := tc.set.HashEqual(tc.compare) 215 if tc.expected != actual { 216 t.Fatalf("expected %t, got %t", tc.expected, actual) 217 } 218 }) 219 } 220 }