1 package matchers_test
2
3 import (
4 . "github.com/onsi/ginkgo/v2"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/matchers"
7 )
8
9 var _ = Describe("HaveKeyWithValue", func() {
10 var (
11 stringKeys map[string]int
12 intKeys map[int]string
13 objKeys map[*myCustomType]*myCustomType
14
15 customA *myCustomType
16 customB *myCustomType
17 )
18 BeforeEach(func() {
19 stringKeys = map[string]int{"foo": 2, "bar": 3}
20 intKeys = map[int]string{2: "foo", 3: "bar"}
21
22 customA = &myCustomType{s: "a", n: 2, f: 2.3, arr: []string{"ice", "cream"}}
23 customB = &myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}
24 objKeys = map[*myCustomType]*myCustomType{customA: customA, customB: customA}
25 })
26
27 When("passed a map", func() {
28 It("should do the right thing", func() {
29 Expect(stringKeys).Should(HaveKeyWithValue("foo", 2))
30 Expect(stringKeys).ShouldNot(HaveKeyWithValue("foo", 1))
31 Expect(stringKeys).ShouldNot(HaveKeyWithValue("baz", 2))
32 Expect(stringKeys).ShouldNot(HaveKeyWithValue("baz", 1))
33
34 Expect(intKeys).Should(HaveKeyWithValue(2, "foo"))
35 Expect(intKeys).ShouldNot(HaveKeyWithValue(4, "foo"))
36 Expect(intKeys).ShouldNot(HaveKeyWithValue(2, "baz"))
37
38 Expect(objKeys).Should(HaveKeyWithValue(customA, customA))
39 Expect(objKeys).Should(HaveKeyWithValue(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}, &myCustomType{s: "a", n: 2, f: 2.3, arr: []string{"ice", "cream"}}))
40 Expect(objKeys).ShouldNot(HaveKeyWithValue(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"apple", "pie"}}, customA))
41 })
42 })
43
44 When("passed a correctly typed nil", func() {
45 It("should operate succesfully on the passed in value", func() {
46 var nilMap map[int]string
47 Expect(nilMap).ShouldNot(HaveKeyWithValue("foo", "bar"))
48 })
49 })
50
51 When("the passed in key or value is actually a matcher", func() {
52 It("should pass each element through the matcher", func() {
53 Expect(stringKeys).Should(HaveKeyWithValue(ContainSubstring("oo"), 2))
54 Expect(intKeys).Should(HaveKeyWithValue(2, ContainSubstring("oo")))
55 Expect(stringKeys).ShouldNot(HaveKeyWithValue(ContainSubstring("foobar"), 2))
56 })
57
58 It("should fail if the matcher ever fails", func() {
59 actual := map[int]string{1: "a", 3: "b", 2: "c"}
60 success, err := (&HaveKeyWithValueMatcher{Key: ContainSubstring("ar"), Value: 2}).Match(actual)
61 Expect(success).Should(BeFalse())
62 Expect(err).Should(HaveOccurred())
63
64 otherActual := map[string]int{"a": 1, "b": 2, "c": 3}
65 success, err = (&HaveKeyWithValueMatcher{Key: "a", Value: ContainSubstring("1")}).Match(otherActual)
66 Expect(success).Should(BeFalse())
67 Expect(err).Should(HaveOccurred())
68 })
69 })
70
71 When("passed something that is not a map", func() {
72 It("should error", func() {
73 success, err := (&HaveKeyWithValueMatcher{Key: "foo", Value: "bar"}).Match([]string{"foo"})
74 Expect(success).Should(BeFalse())
75 Expect(err).Should(HaveOccurred())
76
77 success, err = (&HaveKeyWithValueMatcher{Key: "foo", Value: "bar"}).Match(nil)
78 Expect(success).Should(BeFalse())
79 Expect(err).Should(HaveOccurred())
80 })
81 })
82 })
83
View as plain text