4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/matchers"
9 var _ = Describe("HaveKey", func() {
11 stringKeys map[string]int
12 intKeys map[int]string
13 objKeys map[*myCustomType]string
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]string{customA: "aardvark", customB: "kangaroo"}
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"))
32 Ω(intKeys).Should(HaveKey(2))
33 Ω(intKeys).ShouldNot(HaveKey(4))
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"}}))
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"))
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")))
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())
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())
68 success, err = (&HaveKeyMatcher{Key: "foo"}).Match(nil)
69 Ω(success).Should(BeFalse())
70 Ω(err).Should(HaveOccurred())