|
| 1 | +package matchers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo/v2" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | +) |
| 7 | + |
| 8 | +var _ = Describe("HaveExactElements", func() { |
| 9 | + Context("with a slice", func() { |
| 10 | + It("should do the right thing", func() { |
| 11 | + Expect([]string{"foo", "bar"}).Should(HaveExactElements("foo", "bar")) |
| 12 | + Expect([]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo")) |
| 13 | + Expect([]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo", "bar", "baz")) |
| 14 | + Expect([]string{"foo", "bar"}).ShouldNot(HaveExactElements("bar", "foo")) |
| 15 | + }) |
| 16 | + }) |
| 17 | + Context("with an array", func() { |
| 18 | + It("should do the right thing", func() { |
| 19 | + Expect([2]string{"foo", "bar"}).Should(HaveExactElements("foo", "bar")) |
| 20 | + Expect([2]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo")) |
| 21 | + Expect([2]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo", "bar", "baz")) |
| 22 | + Expect([2]string{"foo", "bar"}).ShouldNot(HaveExactElements("bar", "foo")) |
| 23 | + }) |
| 24 | + }) |
| 25 | + Context("with map", func() { |
| 26 | + It("should error", func() { |
| 27 | + failures := InterceptGomegaFailures(func() { |
| 28 | + Expect(map[int]string{1: "foo"}).Should(HaveExactElements("foo")) |
| 29 | + }) |
| 30 | + |
| 31 | + Expect(failures).Should(HaveLen(1)) |
| 32 | + }) |
| 33 | + }) |
| 34 | + Context("with anything else", func() { |
| 35 | + It("should error", func() { |
| 36 | + failures := InterceptGomegaFailures(func() { |
| 37 | + Expect("foo").Should(HaveExactElements("f", "o", "o")) |
| 38 | + }) |
| 39 | + |
| 40 | + Expect(failures).Should(HaveLen(1)) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + When("passed matchers", func() { |
| 45 | + It("should pass if matcher pass", func() { |
| 46 | + Expect([]string{"foo", "bar", "baz"}).Should(HaveExactElements("foo", MatchRegexp("^ba"), MatchRegexp("az$"))) |
| 47 | + Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements("foo", MatchRegexp("az$"), MatchRegexp("^ba"))) |
| 48 | + Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements("foo", MatchRegexp("az$"))) |
| 49 | + Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements("foo", MatchRegexp("az$"), "baz", "bac")) |
| 50 | + }) |
| 51 | + |
| 52 | + When("a matcher errors", func() { |
| 53 | + It("should soldier on", func() { |
| 54 | + Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements(BeFalse(), "bar", "baz")) |
| 55 | + Expect([]interface{}{"foo", "bar", false}).Should(HaveExactElements(ContainSubstring("foo"), "bar", BeFalse())) |
| 56 | + }) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + When("passed exactly one argument, and that argument is a slice", func() { |
| 61 | + It("should match against the elements of that arguments", func() { |
| 62 | + Expect([]string{"foo", "bar", "baz"}).Should(HaveExactElements([]string{"foo", "bar", "baz"})) |
| 63 | + Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements([]string{"foo", "bar"})) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + Describe("Failure Message", func() { |
| 68 | + When("actual contains extra elements", func() { |
| 69 | + It("should print the starting index of the extra elements", func() { |
| 70 | + failures := InterceptGomegaFailures(func() { |
| 71 | + Expect([]int{1, 2}).Should(HaveExactElements(1)) |
| 72 | + }) |
| 73 | + |
| 74 | + expected := "Expected\n.*\\[1, 2\\]\nto have exact elements with\n.*\\[1\\]\nthe extra elements start from index 1" |
| 75 | + Expect(failures).To(ConsistOf(MatchRegexp(expected))) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + When("actual misses an element", func() { |
| 80 | + It("should print the starting index of missing element", func() { |
| 81 | + failures := InterceptGomegaFailures(func() { |
| 82 | + Expect([]int{1}).Should(HaveExactElements(1, 2)) |
| 83 | + }) |
| 84 | + |
| 85 | + expected := "Expected\n.*\\[1\\]\nto have exact elements with\n.*\\[1, 2\\]\nthe missing elements start from index 1" |
| 86 | + Expect(failures).To(ConsistOf(MatchRegexp(expected))) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + When("actual have mismatched elements", func() { |
| 91 | + It("should print the index, expected element, and actual element", func() { |
| 92 | + failures := InterceptGomegaFailures(func() { |
| 93 | + Expect([]int{1, 2}).Should(HaveExactElements(2, 1)) |
| 94 | + }) |
| 95 | + |
| 96 | + expected := `Expected |
| 97 | +.*\[1, 2\] |
| 98 | +to have exact elements with |
| 99 | +.*\[2, 1\] |
| 100 | +the mismatch indexes were: |
| 101 | +0: Expected |
| 102 | + <int>: 1 |
| 103 | +to equal |
| 104 | + <int>: 2 |
| 105 | +1: Expected |
| 106 | + <int>: 2 |
| 107 | +to equal |
| 108 | + <int>: 1` |
| 109 | + Expect(failures[0]).To(MatchRegexp(expected)) |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments