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