File: elements_test.go

package info (click to toggle)
golang-gomega 1.36.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,984 kB
  • sloc: xml: 277; javascript: 59; makefile: 3
file content (176 lines) | stat: -rw-r--r-- 6,746 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gstruct_test

import (
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gstruct"
)

var _ = Describe("Slice", func() {
	allElements := []string{"a", "b"}
	missingElements := []string{"a"}
	extraElements := []string{"a", "b", "c"}
	duplicateElements := []string{"a", "a", "b"}
	empty := []string{}
	var nils []string

	It("should strictly match all elements", func() {
		m := MatchAllElements(id, Elements{
			"b": Equal("b"),
			"a": Equal("a"),
		})
		Expect(allElements).Should(m, "should match all elements")
		Expect(missingElements).ShouldNot(m, "should fail with missing elements")
		Expect(extraElements).ShouldNot(m, "should fail with extra elements")
		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
		Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")

		m = MatchAllElements(id, Elements{
			"a": Equal("a"),
			"b": Equal("fail"),
		})
		Expect(allElements).ShouldNot(m, "should run nested matchers")

		m = MatchAllElements(id, Elements{})
		Expect(empty).Should(m, "should handle empty slices")
		Expect(allElements).ShouldNot(m, "should handle only empty slices")
		Expect(nils).Should(m, "should handle nil slices")
	})

	It("should ignore extra elements", func() {
		m := MatchElements(id, IgnoreExtras, Elements{
			"b": Equal("b"),
			"a": Equal("a"),
		})
		Expect(allElements).Should(m, "should match all elements")
		Expect(missingElements).ShouldNot(m, "should fail with missing elements")
		Expect(extraElements).Should(m, "should ignore extra elements")
		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
		Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")
	})

	It("should ignore missing elements", func() {
		m := MatchElements(id, IgnoreMissing, Elements{
			"a": Equal("a"),
			"b": Equal("b"),
		})
		Expect(allElements).Should(m, "should match all elements")
		Expect(missingElements).Should(m, "should ignore missing elements")
		Expect(extraElements).ShouldNot(m, "should fail with extra elements")
		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
		Expect(nils).Should(m, "should ignore an uninitialized slice")
	})

	It("should ignore missing and extra elements", func() {
		m := MatchElements(id, IgnoreMissing|IgnoreExtras, Elements{
			"a": Equal("a"),
			"b": Equal("b"),
		})
		Expect(allElements).Should(m, "should match all elements")
		Expect(missingElements).Should(m, "should ignore missing elements")
		Expect(extraElements).Should(m, "should ignore extra elements")
		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
		Expect(nils).Should(m, "should ignore an uninitialized slice")

		m = MatchElements(id, IgnoreExtras|IgnoreMissing, Elements{
			"a": Equal("a"),
			"b": Equal("fail"),
		})
		Expect(allElements).ShouldNot(m, "should run nested matchers")
	})

	Context("with elements that share a key", func() {
		nonUniqueID := func(element interface{}) string {
			return element.(string)[0:1]
		}

		allElements := []string{"a123", "a213", "b321"}
		includingBadElements := []string{"a123", "b123", "b5555"}
		extraElements := []string{"a123", "b1234", "c345"}
		missingElements := []string{"b123", "b1234", "b1345"}

		It("should strictly allow multiple matches", func() {
			m := MatchElements(nonUniqueID, AllowDuplicates, Elements{
				"a": ContainSubstring("1"),
				"b": ContainSubstring("1"),
			})
			Expect(allElements).Should(m, "should match all elements")
			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
			Expect(extraElements).ShouldNot(m, "should reject with extra keys")
			Expect(missingElements).ShouldNot(m, "should reject with missing keys")
			Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")
		})

		It("should ignore missing", func() {
			m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreMissing, Elements{
				"a": ContainSubstring("1"),
				"b": ContainSubstring("1"),
			})
			Expect(allElements).Should(m, "should match all elements")
			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
			Expect(extraElements).ShouldNot(m, "should reject with extra keys")
			Expect(missingElements).Should(m, "should allow missing keys")
			Expect(nils).Should(m, "should allow an uninitialized slice")
		})

		It("should ignore extras", func() {
			m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras, Elements{
				"a": ContainSubstring("1"),
				"b": ContainSubstring("1"),
			})
			Expect(allElements).Should(m, "should match all elements")
			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
			Expect(extraElements).Should(m, "should allow extra keys")
			Expect(missingElements).ShouldNot(m, "should reject missing keys")
			Expect(nils).ShouldNot(m, "should reject an uninitialized slice")
		})

		It("should ignore missing and extras", func() {
			m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras|IgnoreMissing, Elements{
				"a": ContainSubstring("1"),
				"b": ContainSubstring("1"),
			})
			Expect(allElements).Should(m, "should match all elements")
			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
			Expect(extraElements).Should(m, "should allow extra keys")
			Expect(missingElements).Should(m, "should allow missing keys")
			Expect(nils).Should(m, "should allow an uninitialized slice")
		})
	})

	Context("with index identifier", func() {
		allElements := []string{"a", "b"}
		missingElements := []string{"a"}
		extraElements := []string{"a", "b", "c"}
		duplicateElements := []string{"a", "a", "b"}
		empty := []string{}
		var nils []string

		It("should use index", func() {
			m := MatchAllElementsWithIndex(IndexIdentity, Elements{
				"0": Equal("a"),
				"1": Equal("b"),
			})
			Expect(allElements).Should(m, "should match all elements")
			Expect(missingElements).ShouldNot(m, "should fail with missing elements")
			Expect(extraElements).ShouldNot(m, "should fail with extra elements")
			Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
			Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")

			m = MatchAllElementsWithIndex(IndexIdentity, Elements{
				"0": Equal("a"),
				"1": Equal("fail"),
			})
			Expect(allElements).ShouldNot(m, "should run nested matchers")

			m = MatchAllElementsWithIndex(IndexIdentity, Elements{})
			Expect(empty).Should(m, "should handle empty slices")
			Expect(allElements).ShouldNot(m, "should handle only empty slices")
			Expect(nils).Should(m, "should handle nil slices")
		})
	})
})

func id(element interface{}) string {
	return element.(string)
}