initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / have_key_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("HaveKey", func() {
10         var (
11                 stringKeys map[string]int
12                 intKeys    map[int]string
13                 objKeys    map[*myCustomType]string
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]string{customA: "aardvark", customB: "kangaroo"}
25         })
26
27         Context("when passed a map", func() {
28                 It("should do the right thing", func() {
29                         Ω(stringKeys).Should(HaveKey("foo"))
30                         Ω(stringKeys).ShouldNot(HaveKey("baz"))
31
32                         Ω(intKeys).Should(HaveKey(2))
33                         Ω(intKeys).ShouldNot(HaveKey(4))
34
35                         Ω(objKeys).Should(HaveKey(customA))
36                         Ω(objKeys).Should(HaveKey(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}))
37                         Ω(objKeys).ShouldNot(HaveKey(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"apple", "pie"}}))
38                 })
39         })
40
41         Context("when passed a correctly typed nil", func() {
42                 It("should operate succesfully on the passed in value", func() {
43                         var nilMap map[int]string
44                         Ω(nilMap).ShouldNot(HaveKey("foo"))
45                 })
46         })
47
48         Context("when the passed in key is actually a matcher", func() {
49                 It("should pass each element through the matcher", func() {
50                         Ω(stringKeys).Should(HaveKey(ContainSubstring("oo")))
51                         Ω(stringKeys).ShouldNot(HaveKey(ContainSubstring("foobar")))
52                 })
53
54                 It("should fail if the matcher ever fails", func() {
55                         actual := map[int]string{1: "a", 3: "b", 2: "c"}
56                         success, err := (&HaveKeyMatcher{Key: ContainSubstring("ar")}).Match(actual)
57                         Ω(success).Should(BeFalse())
58                         Ω(err).Should(HaveOccurred())
59                 })
60         })
61
62         Context("when passed something that is not a map", func() {
63                 It("should error", func() {
64                         success, err := (&HaveKeyMatcher{Key: "foo"}).Match([]string{"foo"})
65                         Ω(success).Should(BeFalse())
66                         Ω(err).Should(HaveOccurred())
67
68                         success, err = (&HaveKeyMatcher{Key: "foo"}).Match(nil)
69                         Ω(success).Should(BeFalse())
70                         Ω(err).Should(HaveOccurred())
71                 })
72         })
73 })