2014-08-21 03:30:28 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetAdd(t *testing.T) {
|
|
|
|
s := &Set{F: testSetInt}
|
|
|
|
s.Add(1)
|
|
|
|
s.Add(5)
|
|
|
|
s.Add(25)
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
expected := []interface{}{1, 25, 5}
|
2014-08-21 03:30:28 +02:00
|
|
|
actual := s.List()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:57:26 +02:00
|
|
|
func TestSetAdd_negative(t *testing.T) {
|
|
|
|
// Since we don't allow negative hashes, this should just hash to the
|
|
|
|
// same thing...
|
|
|
|
s := &Set{F: testSetInt}
|
|
|
|
s.Add(-1)
|
|
|
|
s.Add(1)
|
|
|
|
|
|
|
|
expected := []interface{}{-1}
|
|
|
|
actual := s.List()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:02:42 +02:00
|
|
|
func TestSetContains(t *testing.T) {
|
|
|
|
s := &Set{F: testSetInt}
|
|
|
|
s.Add(5)
|
2015-04-23 18:23:33 +02:00
|
|
|
s.Add(-5)
|
2014-08-21 06:02:42 +02:00
|
|
|
|
|
|
|
if s.Contains(2) {
|
|
|
|
t.Fatal("should not contain")
|
|
|
|
}
|
|
|
|
if !s.Contains(5) {
|
|
|
|
t.Fatal("should contain")
|
|
|
|
}
|
2015-04-23 18:23:33 +02:00
|
|
|
if !s.Contains(-5) {
|
|
|
|
t.Fatal("should contain")
|
|
|
|
}
|
2014-08-21 06:02:42 +02:00
|
|
|
}
|
|
|
|
|
2014-08-21 03:30:28 +02:00
|
|
|
func TestSetDifference(t *testing.T) {
|
|
|
|
s1 := &Set{F: testSetInt}
|
2014-08-21 07:24:35 +02:00
|
|
|
s2 := &Set{F: testSetInt}
|
2014-08-21 03:30:28 +02:00
|
|
|
|
|
|
|
s1.Add(1)
|
|
|
|
s1.Add(5)
|
|
|
|
|
|
|
|
s2.Add(5)
|
|
|
|
s2.Add(25)
|
|
|
|
|
2015-01-15 15:33:52 +01:00
|
|
|
difference := s1.Difference(s2)
|
|
|
|
difference.Add(2)
|
|
|
|
|
|
|
|
expected := []interface{}{1, 2}
|
|
|
|
actual := difference.List()
|
2014-08-21 03:30:28 +02:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetIntersection(t *testing.T) {
|
|
|
|
s1 := &Set{F: testSetInt}
|
2014-08-21 07:24:35 +02:00
|
|
|
s2 := &Set{F: testSetInt}
|
2014-08-21 03:30:28 +02:00
|
|
|
|
|
|
|
s1.Add(1)
|
|
|
|
s1.Add(5)
|
|
|
|
|
|
|
|
s2.Add(5)
|
|
|
|
s2.Add(25)
|
|
|
|
|
2015-01-15 15:33:52 +01:00
|
|
|
intersection := s1.Intersection(s2)
|
|
|
|
intersection.Add(2)
|
|
|
|
|
|
|
|
expected := []interface{}{2, 5}
|
|
|
|
actual := intersection.List()
|
2014-08-21 03:30:28 +02:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetUnion(t *testing.T) {
|
|
|
|
s1 := &Set{F: testSetInt}
|
2014-08-21 07:24:35 +02:00
|
|
|
s2 := &Set{F: testSetInt}
|
2014-08-21 03:30:28 +02:00
|
|
|
|
|
|
|
s1.Add(1)
|
|
|
|
s1.Add(5)
|
|
|
|
|
|
|
|
s2.Add(5)
|
|
|
|
s2.Add(25)
|
|
|
|
|
2015-01-15 15:33:52 +01:00
|
|
|
union := s1.Union(s2)
|
|
|
|
union.Add(2)
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
expected := []interface{}{1, 2, 25, 5}
|
2015-01-15 15:33:52 +01:00
|
|
|
actual := union.List()
|
2014-08-21 03:30:28 +02:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetInt(v interface{}) int {
|
|
|
|
return v.(int)
|
|
|
|
}
|
2017-05-09 20:45:53 +02:00
|
|
|
|
|
|
|
func TestHashResource_nil(t *testing.T) {
|
|
|
|
resource := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"name": {
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f := HashResource(resource)
|
|
|
|
|
|
|
|
idx := f(nil)
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("Expected 0 when hashing nil, given: %d", idx)
|
|
|
|
}
|
|
|
|
}
|
2017-08-16 06:45:39 +02:00
|
|
|
|
|
|
|
func TestHashEqual(t *testing.T) {
|
|
|
|
nested := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": {
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
root := &Resource{
|
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"bar": {
|
|
|
|
Type: TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"nested": {
|
|
|
|
Type: TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: nested,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
n1 := map[string]interface{}{"foo": "bar"}
|
|
|
|
n2 := map[string]interface{}{"foo": "baz"}
|
|
|
|
|
|
|
|
r1 := map[string]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
"nested": NewSet(HashResource(nested), []interface{}{n1}),
|
|
|
|
}
|
|
|
|
r2 := map[string]interface{}{
|
|
|
|
"bar": "qux",
|
|
|
|
"nested": NewSet(HashResource(nested), []interface{}{n2}),
|
|
|
|
}
|
2017-08-16 06:56:01 +02:00
|
|
|
r3 := map[string]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
"nested": NewSet(HashResource(nested), []interface{}{n2}),
|
|
|
|
}
|
|
|
|
r4 := map[string]interface{}{
|
|
|
|
"bar": "qux",
|
|
|
|
"nested": NewSet(HashResource(nested), []interface{}{n1}),
|
|
|
|
}
|
2017-08-16 06:45:39 +02:00
|
|
|
s1 := NewSet(HashResource(root), []interface{}{r1})
|
|
|
|
s2 := NewSet(HashResource(root), []interface{}{r2})
|
2017-08-16 06:56:01 +02:00
|
|
|
s3 := NewSet(HashResource(root), []interface{}{r3})
|
|
|
|
s4 := NewSet(HashResource(root), []interface{}{r4})
|
2017-08-16 06:45:39 +02:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
set *Set
|
|
|
|
compare *Set
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "equal",
|
|
|
|
set: s1,
|
|
|
|
compare: s1,
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not equal",
|
|
|
|
set: s1,
|
|
|
|
compare: s2,
|
|
|
|
expected: false,
|
|
|
|
},
|
2017-08-16 06:56:01 +02:00
|
|
|
{
|
|
|
|
name: "outer equal, should still not be equal",
|
|
|
|
set: s1,
|
|
|
|
compare: s3,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "inner equal, should still not be equal",
|
|
|
|
set: s1,
|
|
|
|
compare: s4,
|
|
|
|
expected: false,
|
|
|
|
},
|
2017-08-16 06:45:39 +02:00
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
actual := tc.set.HashEqual(tc.compare)
|
|
|
|
if tc.expected != actual {
|
|
|
|
t.Fatalf("expected %t, got %t", tc.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|