github.com/onsi/gomega@v1.32.0/matchers/contain_elements_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 ) 7 8 var _ = Describe("ContainElements", func() { 9 Context("with a slice", func() { 10 It("should do the right thing", func() { 11 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", "bar", "baz")) 12 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("bar")) 13 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements()) 14 Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("baz", "bar", "foo", "foo")) 15 }) 16 }) 17 18 Context("with an array", func() { 19 It("should do the right thing", func() { 20 Expect([3]string{"foo", "bar", "baz"}).Should(ContainElements("foo", "bar", "baz")) 21 Expect([3]string{"foo", "bar", "baz"}).Should(ContainElements("bar")) 22 Expect([3]string{"foo", "bar", "baz"}).Should(ContainElements()) 23 Expect([3]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("baz", "bar", "foo", "foo")) 24 }) 25 }) 26 27 Context("with a map", func() { 28 It("should apply to the values", func() { 29 Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ContainElements("foo", "bar", "baz")) 30 Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ContainElements("bar")) 31 Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ContainElements()) 32 Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).ShouldNot(ContainElements("baz", "bar", "foo", "foo")) 33 }) 34 35 }) 36 37 Context("with anything else", func() { 38 It("should error", func() { 39 failures := InterceptGomegaFailures(func() { 40 Expect("foo").Should(ContainElements("f", "o", "o")) 41 }) 42 43 Expect(failures).Should(HaveLen(1)) 44 }) 45 }) 46 47 Context("when passed matchers", func() { 48 It("should pass if the matchers pass", func() { 49 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", MatchRegexp("^ba"), "baz")) 50 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", MatchRegexp("^ba"))) 51 Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("foo", MatchRegexp("^ba"), MatchRegexp("foo"))) 52 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", MatchRegexp("^ba"), MatchRegexp("^ba"))) 53 Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("foo", MatchRegexp("^ba"), MatchRegexp("turducken"))) 54 }) 55 56 It("should not depend on the order of the matchers", func() { 57 Expect([][]int{{1, 2}, {2}}).Should(ContainElements(ContainElement(1), ContainElement(2))) 58 Expect([][]int{{1, 2}, {2}}).Should(ContainElements(ContainElement(2), ContainElement(1))) 59 }) 60 61 Context("when a matcher errors", func() { 62 It("should soldier on", func() { 63 Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements(BeFalse(), "foo", "bar")) 64 Expect([]interface{}{"foo", "bar", false}).Should(ContainElements(BeFalse(), ContainSubstring("foo"), "bar")) 65 }) 66 }) 67 }) 68 69 Context("when passed exactly one argument, and that argument is a slice", func() { 70 It("should match against the elements of that argument", func() { 71 Expect([]string{"foo", "bar", "baz"}).Should(ContainElements([]string{"foo", "baz"})) 72 Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements([]string{"foo", "nope"})) 73 }) 74 }) 75 76 Describe("FailureMessage", func() { 77 It("prints missing elements", func() { 78 failures := InterceptGomegaFailures(func() { 79 Expect([]int{2}).Should(ContainElements(1, 2, 3)) 80 }) 81 82 expected := "Expected\n.*\\[2\\]\nto contain elements\n.*\\[1, 2, 3\\]\nthe missing elements were\n.*\\[1, 3\\]" 83 Expect(failures).To(ContainElements(MatchRegexp(expected))) 84 }) 85 86 When("expected was specified as an array", func() { 87 It("flattens the array in the expectation message", func() { 88 failures := InterceptGomegaFailures(func() { 89 Expect([]string{"A", "B", "C"}).To(ContainElements([]string{"A", "D"})) 90 }) 91 92 expected := `Expected\n.*\["A", "B", "C"\]\nto contain elements\n.*: \["A", "D"\]\nthe missing elements were\n.*\["D"\]` 93 Expect(failures).To(ConsistOf(MatchRegexp(expected))) 94 }) 95 96 It("flattens the array in the negated expectation message", func() { 97 failures := InterceptGomegaFailures(func() { 98 Expect([]string{"A", "B"}).NotTo(ContainElements([]string{"A", "B"})) 99 }) 100 101 expected := `Expected\n.*\["A", "B"\]\nnot to contain elements\n.*: \["A", "B"\]` 102 Expect(failures).To(ConsistOf(MatchRegexp(expected))) 103 }) 104 }) 105 106 When("the expected values are the same type", func() { 107 It("uses that type for the expectation slice", func() { 108 failures := InterceptGomegaFailures(func() { 109 Expect([]string{"A", "B"}).To(ContainElements("A", "B", "C")) 110 }) 111 112 expected := `to contain elements 113 \s*<\[\]string \| len:3, cap:3>: \["A", "B", "C"\] 114 the missing elements were 115 \s*<\[\]string \| len:1, cap:1>: \["C"\]` 116 Expect(failures).To(ConsistOf(MatchRegexp(expected))) 117 }) 118 119 It("uses that type for the negated expectation slice", func() { 120 failures := InterceptGomegaFailures(func() { 121 Expect([]uint64{1, 2}).NotTo(ContainElements(uint64(1), uint64(2))) 122 }) 123 124 expected := `not to contain elements\n\s*<\[\]uint64 \| len:2, cap:2>: \[1, 2\]` 125 Expect(failures).To(ConsistOf(MatchRegexp(expected))) 126 }) 127 }) 128 129 When("the expected values are different types", func() { 130 It("uses interface{} for the expectation slice", func() { 131 failures := InterceptGomegaFailures(func() { 132 Expect([]interface{}{1, true}).To(ContainElements(1, "C")) 133 }) 134 135 expected := `to contain elements 136 \s*<\[\]interface {} \| len:2, cap:2>: \[<int>1, <string>"C"\] 137 the missing elements were 138 \s*<\[\]string \| len:1, cap:1>: \["C"\]` 139 Expect(failures).To(ConsistOf(MatchRegexp(expected))) 140 }) 141 142 It("uses interface{} for the negated expectation slice", func() { 143 failures := InterceptGomegaFailures(func() { 144 Expect([]interface{}{1, "B"}).NotTo(ContainElements(1, "B")) 145 }) 146 147 expected := `not to contain elements\n\s*<\[\]interface {} \| len:2, cap:2>: \[<int>1, <string>"B"\]` 148 Expect(failures).To(ConsistOf(MatchRegexp(expected))) 149 }) 150 }) 151 }) 152 })