github.com/cmalfait/terraform@v0.11.12-beta1/dag/set_test.go (about)

     1  package dag
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestSetDifference(t *testing.T) {
     9  	cases := []struct {
    10  		Name     string
    11  		A, B     []interface{}
    12  		Expected []interface{}
    13  	}{
    14  		{
    15  			"same",
    16  			[]interface{}{1, 2, 3},
    17  			[]interface{}{3, 1, 2},
    18  			[]interface{}{},
    19  		},
    20  
    21  		{
    22  			"A has extra elements",
    23  			[]interface{}{1, 2, 3},
    24  			[]interface{}{3, 2},
    25  			[]interface{}{1},
    26  		},
    27  
    28  		{
    29  			"B has extra elements",
    30  			[]interface{}{1, 2, 3},
    31  			[]interface{}{3, 2, 1, 4},
    32  			[]interface{}{},
    33  		},
    34  	}
    35  
    36  	for i, tc := range cases {
    37  		t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
    38  			var one, two, expected Set
    39  			for _, v := range tc.A {
    40  				one.Add(v)
    41  			}
    42  			for _, v := range tc.B {
    43  				two.Add(v)
    44  			}
    45  			for _, v := range tc.Expected {
    46  				expected.Add(v)
    47  			}
    48  
    49  			actual := one.Difference(&two)
    50  			match := actual.Intersection(&expected)
    51  			if match.Len() != expected.Len() {
    52  				t.Fatalf("bad: %#v", actual.List())
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestSetFilter(t *testing.T) {
    59  	cases := []struct {
    60  		Input    []interface{}
    61  		Expected []interface{}
    62  	}{
    63  		{
    64  			[]interface{}{1, 2, 3},
    65  			[]interface{}{1, 2, 3},
    66  		},
    67  
    68  		{
    69  			[]interface{}{4, 5, 6},
    70  			[]interface{}{4},
    71  		},
    72  
    73  		{
    74  			[]interface{}{7, 8, 9},
    75  			[]interface{}{},
    76  		},
    77  	}
    78  
    79  	for i, tc := range cases {
    80  		t.Run(fmt.Sprintf("%d-%#v", i, tc.Input), func(t *testing.T) {
    81  			var input, expected Set
    82  			for _, v := range tc.Input {
    83  				input.Add(v)
    84  			}
    85  			for _, v := range tc.Expected {
    86  				expected.Add(v)
    87  			}
    88  
    89  			actual := input.Filter(func(v interface{}) bool {
    90  				return v.(int) < 5
    91  			})
    92  			match := actual.Intersection(&expected)
    93  			if match.Len() != expected.Len() {
    94  				t.Fatalf("bad: %#v", actual.List())
    95  			}
    96  		})
    97  	}
    98  }