github.com/alexissmirnov/terraform@v0.4.3-0.20150423153700-1ef9731a2f14/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, 5, 25}
    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  
    39  	if s.Contains(2) {
    40  		t.Fatal("should not contain")
    41  	}
    42  	if !s.Contains(5) {
    43  		t.Fatal("should contain")
    44  	}
    45  }
    46  
    47  func TestSetDifference(t *testing.T) {
    48  	s1 := &Set{F: testSetInt}
    49  	s2 := &Set{F: testSetInt}
    50  
    51  	s1.Add(1)
    52  	s1.Add(5)
    53  
    54  	s2.Add(5)
    55  	s2.Add(25)
    56  
    57  	difference := s1.Difference(s2)
    58  	difference.Add(2)
    59  
    60  	expected := []interface{}{1, 2}
    61  	actual := difference.List()
    62  	if !reflect.DeepEqual(actual, expected) {
    63  		t.Fatalf("bad: %#v", actual)
    64  	}
    65  }
    66  
    67  func TestSetIntersection(t *testing.T) {
    68  	s1 := &Set{F: testSetInt}
    69  	s2 := &Set{F: testSetInt}
    70  
    71  	s1.Add(1)
    72  	s1.Add(5)
    73  
    74  	s2.Add(5)
    75  	s2.Add(25)
    76  
    77  	intersection := s1.Intersection(s2)
    78  	intersection.Add(2)
    79  
    80  	expected := []interface{}{2, 5}
    81  	actual := intersection.List()
    82  	if !reflect.DeepEqual(actual, expected) {
    83  		t.Fatalf("bad: %#v", actual)
    84  	}
    85  }
    86  
    87  func TestSetUnion(t *testing.T) {
    88  	s1 := &Set{F: testSetInt}
    89  	s2 := &Set{F: testSetInt}
    90  
    91  	s1.Add(1)
    92  	s1.Add(5)
    93  
    94  	s2.Add(5)
    95  	s2.Add(25)
    96  
    97  	union := s1.Union(s2)
    98  	union.Add(2)
    99  
   100  	expected := []interface{}{1, 2, 5, 25}
   101  	actual := union.List()
   102  	if !reflect.DeepEqual(actual, expected) {
   103  		t.Fatalf("bad: %#v", actual)
   104  	}
   105  }
   106  
   107  func testSetInt(v interface{}) int {
   108  	return v.(int)
   109  }