github.com/acm1/terraform@v0.6.2-0.20150729164239-1f314444f45c/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  	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, 5, 25}
   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  }