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 It("should work with arbitrary types, including nil", func() {
18 Expect([]any{"foo", nil, "bar", 17, true, []string{"hi", "there"}}).Should(HaveExactElements("foo", nil, "bar", 17, true, []string{"hi", "there"}))
19 })
20 })
21 Context("with an array", func() {
22 It("should do the right thing", func() {
23 Expect([2]string{"foo", "bar"}).Should(HaveExactElements("foo", "bar"))
24 Expect([2]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo"))
25 Expect([2]string{"foo", "bar"}).ShouldNot(HaveExactElements("foo", "bar", "baz"))
26 Expect([2]string{"foo", "bar"}).ShouldNot(HaveExactElements("bar", "foo"))
27 })
28 })
29 Context("with map", func() {
30 It("should error", func() {
31 failures := InterceptGomegaFailures(func() {
32 Expect(map[int]string{1: "foo"}).Should(HaveExactElements("foo"))
33 })
34
35 Expect(failures).Should(HaveLen(1))
36 })
37 })
38 Context("with anything else", func() {
39 It("should error", func() {
40 failures := InterceptGomegaFailures(func() {
41 Expect("foo").Should(HaveExactElements("f", "o", "o"))
42 })
43
44 Expect(failures).Should(HaveLen(1))
45 })
46 })
47
48 When("passed matchers", func() {
49 It("should pass if matcher pass", func() {
50 Expect([]string{"foo", "bar", "baz"}).Should(HaveExactElements("foo", MatchRegexp("^ba"), MatchRegexp("az$")))
51 Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements("foo", MatchRegexp("az$"), MatchRegexp("^ba")))
52 Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements("foo", MatchRegexp("az$")))
53 Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements("foo", MatchRegexp("az$"), "baz", "bac"))
54 })
55
56 When("a matcher errors", func() {
57 It("should soldier on", func() {
58 Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements(BeFalse(), "bar", "baz"))
59 Expect([]interface{}{"foo", "bar", false}).Should(HaveExactElements(ContainSubstring("foo"), "bar", BeFalse()))
60 })
61
62 It("should include the error message, not the failure message", func() {
63 failures := InterceptGomegaFailures(func() {
64 Expect([]string{"foo", "bar", "baz"}).Should(HaveExactElements("foo", BeFalse(), "bar"))
65 })
66 Ω(failures[0]).ShouldNot(ContainSubstring("to be false"))
67 Ω(failures[0]).Should(ContainSubstring("1: Expected a boolean. Got:\n <string>: bar"))
68 })
69 })
70 })
71
72 When("passed exactly one argument, and that argument is a slice", func() {
73 It("should match against the elements of that arguments", func() {
74 Expect([]string{"foo", "bar", "baz"}).Should(HaveExactElements([]string{"foo", "bar", "baz"}))
75 Expect([]string{"foo", "bar", "baz"}).ShouldNot(HaveExactElements([]string{"foo", "bar"}))
76 })
77 })
78
79 Describe("Failure Message", func() {
80 When("actual contains extra elements", func() {
81 It("should print the starting index of the extra elements", func() {
82 failures := InterceptGomegaFailures(func() {
83 Expect([]int{1, 2}).Should(HaveExactElements(1))
84 })
85
86 expected := "Expected\n.*\\[1, 2\\]\nto have exact elements with\n.*\\[1\\]\nthe extra elements start from index 1"
87 Expect(failures).To(ConsistOf(MatchRegexp(expected)))
88 })
89 })
90
91 When("actual misses an element", func() {
92 It("should print the starting index of missing element", func() {
93 failures := InterceptGomegaFailures(func() {
94 Expect([]int{1}).Should(HaveExactElements(1, 2))
95 })
96
97 expected := "Expected\n.*\\[1\\]\nto have exact elements with\n.*\\[1, 2\\]\nthe missing elements start from index 1"
98 Expect(failures).To(ConsistOf(MatchRegexp(expected)))
99 })
100 })
101
102 When("actual have mismatched elements", func() {
103 It("should print the index, expected element, and actual element", func() {
104 failures := InterceptGomegaFailures(func() {
105 Expect([]int{1, 2}).Should(HaveExactElements(2, 1))
106 })
107
108 expected := `Expected
109 .*\[1, 2\]
110 to have exact elements with
111 .*\[2, 1\]
112 the mismatch indexes were:
113 0: Expected
114 <int>: 1
115 to equal
116 <int>: 2
117 1: Expected
118 <int>: 2
119 to equal
120 <int>: 1`
121 Expect(failures[0]).To(MatchRegexp(expected))
122 })
123 })
124 })
125
126 When("matcher instance is reused", func() {
127
128
129 It("should work properly", func() {
130 matchSingleFalse := HaveExactElements(Equal(false))
131 Expect([]bool{true}).ShouldNot(matchSingleFalse)
132 Expect([]bool{false}).Should(matchSingleFalse)
133 })
134 })
135 })
136
View as plain text