...

Source file src/github.com/onsi/gomega/matchers/have_key_matcher_test.go

Documentation: github.com/onsi/gomega/matchers

     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("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  	When("passed a map", func() {
    28  		It("should do the right thing", func() {
    29  			Expect(stringKeys).Should(HaveKey("foo"))
    30  			Expect(stringKeys).ShouldNot(HaveKey("baz"))
    31  
    32  			Expect(intKeys).Should(HaveKey(2))
    33  			Expect(intKeys).ShouldNot(HaveKey(4))
    34  
    35  			Expect(objKeys).Should(HaveKey(customA))
    36  			Expect(objKeys).Should(HaveKey(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}))
    37  			Expect(objKeys).ShouldNot(HaveKey(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"apple", "pie"}}))
    38  		})
    39  	})
    40  
    41  	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  			Expect(nilMap).ShouldNot(HaveKey("foo"))
    45  		})
    46  	})
    47  
    48  	When("the passed in key is actually a matcher", func() {
    49  		It("should pass each element through the matcher", func() {
    50  			Expect(stringKeys).Should(HaveKey(ContainSubstring("oo")))
    51  			Expect(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  			Expect(success).Should(BeFalse())
    58  			Expect(err).Should(HaveOccurred())
    59  		})
    60  	})
    61  
    62  	When("passed something that is not a map", func() {
    63  		It("should error", func() {
    64  			success, err := (&HaveKeyMatcher{Key: "foo"}).Match([]string{"foo"})
    65  			Expect(success).Should(BeFalse())
    66  			Expect(err).Should(HaveOccurred())
    67  
    68  			success, err = (&HaveKeyMatcher{Key: "foo"}).Match(nil)
    69  			Expect(success).Should(BeFalse())
    70  			Expect(err).Should(HaveOccurred())
    71  		})
    72  	})
    73  })
    74  

View as plain text