added support for string type
[govpp.git] / vendor / github.com / onsi / gomega / matchers / have_key_with_value_matcher_test.go
1 package matchers_test
2
3 import (
4         . "github.com/onsi/ginkgo"
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         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))
33
34                         Ω(intKeys).Should(HaveKeyWithValue(2, "foo"))
35                         Ω(intKeys).ShouldNot(HaveKeyWithValue(4, "foo"))
36                         Ω(intKeys).ShouldNot(HaveKeyWithValue(2, "baz"))
37
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))
41                 })
42         })
43
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"))
48                 })
49         })
50
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))
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                         Ω(success).Should(BeFalse())
62                         Ω(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                         Ω(success).Should(BeFalse())
67                         Ω(err).Should(HaveOccurred())
68                 })
69         })
70
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())
76
77                         success, err = (&HaveKeyWithValueMatcher{Key: "foo", Value: "bar"}).Match(nil)
78                         Ω(success).Should(BeFalse())
79                         Ω(err).Should(HaveOccurred())
80                 })
81         })
82 })