4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/matchers"
9 var _ = Describe("HaveKeyWithValue", func() {
11 stringKeys map[string]int
12 intKeys map[int]string
13 objKeys map[*myCustomType]*myCustomType
19 stringKeys = map[string]int{"foo": 2, "bar": 3}
20 intKeys = map[int]string{2: "foo", 3: "bar"}
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}
27 Context("when passed a map", func() {
28 It("should do the right thing", func() {
29 Ω(stringKeys).Should(HaveKeyWithValue("foo", 2))
30 Ω(stringKeys).ShouldNot(HaveKeyWithValue("foo", 1))
31 Ω(stringKeys).ShouldNot(HaveKeyWithValue("baz", 2))
32 Ω(stringKeys).ShouldNot(HaveKeyWithValue("baz", 1))
34 Ω(intKeys).Should(HaveKeyWithValue(2, "foo"))
35 Ω(intKeys).ShouldNot(HaveKeyWithValue(4, "foo"))
36 Ω(intKeys).ShouldNot(HaveKeyWithValue(2, "baz"))
38 Ω(objKeys).Should(HaveKeyWithValue(customA, customA))
39 Ω(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 Ω(objKeys).ShouldNot(HaveKeyWithValue(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"apple", "pie"}}, customA))
44 Context("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 Ω(nilMap).ShouldNot(HaveKeyWithValue("foo", "bar"))
51 Context("when the passed in key or value is actually a matcher", func() {
52 It("should pass each element through the matcher", func() {
53 Ω(stringKeys).Should(HaveKeyWithValue(ContainSubstring("oo"), 2))
54 Ω(intKeys).Should(HaveKeyWithValue(2, ContainSubstring("oo")))
55 Ω(stringKeys).ShouldNot(HaveKeyWithValue(ContainSubstring("foobar"), 2))
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 Ω(success).Should(BeFalse())
62 Ω(err).Should(HaveOccurred())
64 otherActual := map[string]int{"a": 1, "b": 2, "c": 3}
65 success, err = (&HaveKeyWithValueMatcher{Key: "a", Value: ContainSubstring("1")}).Match(otherActual)
66 Ω(success).Should(BeFalse())
67 Ω(err).Should(HaveOccurred())
71 Context("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 Ω(success).Should(BeFalse())
75 Ω(err).Should(HaveOccurred())
77 success, err = (&HaveKeyWithValueMatcher{Key: "foo", Value: "bar"}).Match(nil)
78 Ω(success).Should(BeFalse())
79 Ω(err).Should(HaveOccurred())