File: keys_test.go

package info (click to toggle)
golang-gomega 1.36.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,984 kB
  • sloc: xml: 277; javascript: 59; makefile: 3
file content (101 lines) | stat: -rw-r--r-- 3,131 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
package gstruct_test

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

var _ = Describe("Map", func() {
	allKeys := map[string]string{"A": "a", "B": "b"}
	missingKeys := map[string]string{"A": "a"}
	extraKeys := map[string]string{"A": "a", "B": "b", "C": "c"}
	emptyKeys := map[string]string{}

	It("should strictly match all keys", func() {
		m := MatchAllKeys(Keys{
			"B": Equal("b"),
			"A": Equal("a"),
		})
		Expect(allKeys).Should(m, "should match all keys")
		Expect(missingKeys).ShouldNot(m, "should fail with missing keys")
		Expect(extraKeys).ShouldNot(m, "should fail with extra keys")
		Expect(emptyKeys).ShouldNot(m, "should fail with empty keys")

		m = MatchAllKeys(Keys{
			"A": Equal("a"),
			"B": Equal("fail"),
		})
		Expect(allKeys).ShouldNot(m, "should run nested matchers")
	})

	It("should handle empty maps", func() {
		m := MatchAllKeys(Keys{})
		Expect(map[string]string{}).Should(m, "should handle empty maps")
		Expect(allKeys).ShouldNot(m, "should fail with extra keys")
	})

	It("should ignore missing keys", func() {
		m := MatchKeys(IgnoreMissing, Keys{
			"B": Equal("b"),
			"A": Equal("a"),
		})
		Expect(allKeys).Should(m, "should match all keys")
		Expect(missingKeys).Should(m, "should ignore missing keys")
		Expect(extraKeys).ShouldNot(m, "should fail with extra keys")
		Expect(emptyKeys).Should(m, "should match empty keys")
	})

	It("should ignore extra keys", func() {
		m := MatchKeys(IgnoreExtras, Keys{
			"B": Equal("b"),
			"A": Equal("a"),
		})
		Expect(allKeys).Should(m, "should match all keys")
		Expect(missingKeys).ShouldNot(m, "should fail with missing keys")
		Expect(extraKeys).Should(m, "should ignore extra keys")
		Expect(emptyKeys).ShouldNot(m, "should fail with empty keys")
	})

	It("should ignore missing and extra keys", func() {
		m := MatchKeys(IgnoreMissing|IgnoreExtras, Keys{
			"B": Equal("b"),
			"A": Equal("a"),
		})
		Expect(allKeys).Should(m, "should match all keys")
		Expect(missingKeys).Should(m, "should ignore missing keys")
		Expect(extraKeys).Should(m, "should ignore extra keys")
		Expect(emptyKeys).Should(m, "should match empty keys")

		m = MatchKeys(IgnoreMissing|IgnoreExtras, Keys{
			"A": Equal("a"),
			"B": Equal("fail"),
		})
		Expect(allKeys).ShouldNot(m, "should run nested matchers")
	})

	It("should produce sensible error messages", func() {
		m := MatchAllKeys(Keys{
			"B": Equal("b"),
			"A": Equal("a"),
		})

		actual := map[string]string{"A": "b", "C": "c"}

		//Because the order of the constituent errors can't be guaranteed,
		//we do a number of checks to make sure everything's included
		m.Match(actual)
		Expect(m.FailureMessage(actual)).Should(HavePrefix(
			"Expected\n    <string>: \nto match keys: {\n",
		))
		Expect(m.FailureMessage(actual)).Should(ContainSubstring(
			".\"A\":\n	Expected\n	    <string>: b\n	to equal\n	    <string>: a\n",
		))
		Expect(m.FailureMessage(actual)).Should(ContainSubstring(
			"missing expected key B\n",
		))
		Expect(m.FailureMessage(actual)).Should(ContainSubstring(
			".\"C\":\n	unexpected key C: map[",
		))
	})
})