github.com/onsi/gomega@v1.32.0/matchers/be_key_of_matcher_test.go (about) 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("BeKeyOf", func() { 10 When("passed a map", func() { 11 It("should do the right thing", func() { 12 Expect(2).Should(BeKeyOf(map[int]bool{1: true, 2: false})) 13 Expect(3).ShouldNot(BeKeyOf(map[int]bool{1: true, 2: false})) 14 15 var mone map[int]bool 16 Expect(42).ShouldNot(BeKeyOf(mone)) 17 18 two := 2 19 Expect(&two).Should(BeKeyOf(map[*int]bool{&two: true, nil: false})) 20 }) 21 }) 22 23 When("passed a correctly typed nil", func() { 24 It("should operate successfully on the passed in value", func() { 25 two := 2 26 Expect((*int)(nil)).Should(BeKeyOf(map[*int]bool{&two: true, nil: false})) 27 28 one := 1 29 Expect((*int)(nil)).ShouldNot(BeKeyOf(map[*int]bool{&two: true, &one: false})) 30 }) 31 }) 32 33 When("passed an unsupported type", func() { 34 It("should error", func() { 35 success, err := (&BeKeyOfMatcher{Map: []interface{}{0}}).Match(nil) 36 Expect(success).Should(BeFalse()) 37 Expect(err).Should(HaveOccurred()) 38 39 success, err = (&BeKeyOfMatcher{Map: nil}).Match(nil) 40 Expect(success).Should(BeFalse()) 41 Expect(err).Should(HaveOccurred()) 42 }) 43 }) 44 45 It("builds failure message", func() { 46 actual := BeKeyOf(map[int]bool{1: true, 2: false}).FailureMessage(42) 47 Expect(actual).To(MatchRegexp("Expected\n <int>: 42\nto be a key of\n <\\[\\]bool | len:2, cap:2>: \\[(true, false)|(false, true)\\]")) 48 }) 49 50 It("builds negated failure message", func() { 51 actual := BeKeyOf(map[int]bool{1: true, 2: false}).NegatedFailureMessage(42) 52 Expect(actual).To(MatchRegexp("Expected\n <int>: 42\nnot to be a key of\n <\\[\\]bool | len:2, cap:2>: \\[(true, false)|(false, true)\\]")) 53 }) 54 55 })