github.com/mgoltzsche/ctnr@v0.7.1-alpha/pkg/sliceutils/sliceutils_test.go (about) 1 package sliceutils 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestAddToSet(t *testing.T) { 9 for _, c := range []struct { 10 value []string 11 addv string 12 expected string 13 added bool 14 }{ 15 {nil, "a", "[a]", true}, 16 {[]string{"a"}, "b", "[a b]", true}, 17 {[]string{"a"}, "", "[a ]", true}, 18 {[]string{"a"}, "a", "[a]", false}, 19 } { 20 added := AddToSet(&c.value, c.addv) 21 a := fmt.Sprintf("%+v", c.value) 22 e := fmt.Sprintf("%+v", c.expected) 23 if a != e { 24 t.Errorf("AddToSet(%+v, %q) => %s but expected %s", c.value, c.addv, a, e) 25 } 26 if added != c.added { 27 t.Errorf("AddToSet(%+v, %q) returned %v but expected %v", c.value, c.addv, added, c.added) 28 } 29 } 30 } 31 32 func TestRemoveFromSet(t *testing.T) { 33 for _, c := range []struct { 34 value []string 35 remv string 36 expected string 37 removed bool 38 }{ 39 {nil, "a", "[]", false}, 40 {[]string{"a", "b"}, "c", "[a b]", false}, 41 {[]string{"a", "b"}, "b", "[a]", true}, 42 {[]string{"a"}, "a", "[]", true}, 43 {[]string{"a", ""}, "", "[a]", true}, 44 } { 45 removed := RemoveFromSet(&c.value, c.remv) 46 a := fmt.Sprintf("%+v", c.value) 47 e := fmt.Sprintf("%+v", c.expected) 48 if a != e { 49 t.Errorf("RemoveFromSet(%+v, %q) => %s but expected %s", c.value, c.remv, a, e) 50 } 51 if removed != c.removed { 52 t.Errorf("RemoveFromSet(%+v, %q) returned %v but expected %v", c.value, c.remv, removed, c.removed) 53 } 54 } 55 } 56 57 func TestContains(t *testing.T) { 58 for _, c := range []struct { 59 l []string 60 v string 61 contained bool 62 }{ 63 {nil, "a", false}, 64 {[]string{"a"}, "b", false}, 65 {[]string{"a"}, "", false}, 66 {[]string{"a"}, "a", true}, 67 {[]string{"a", "b"}, "b", true}, 68 {[]string{"a", ""}, "", true}, 69 } { 70 a := Contains(c.l, c.v) 71 if a != c.contained { 72 t.Errorf("Contains(%+v, %q) must return true", c.l, c.v) 73 } 74 } 75 }