github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/test_helpers/matchers/contain_exactly_matcher_test.go (about)

     1  package matchers_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"github.com/cloudfoundry-incubator/ltc/test_helpers/matchers"
    10  )
    11  
    12  type woohoo struct {
    13  	Flag bool
    14  }
    15  
    16  type woomap map[woohoo]string
    17  
    18  var _ = Describe("ContainExactlyMatcher", func() {
    19  	It("matches if the array contains exactly the elements in the expected array, but is order independent.", func() {
    20  		Expect([]string{"hi there", "ho there", "hallo"}).To(matchers.ContainExactly([]string{"hi there", "ho there", "hallo"}))
    21  		Expect([]string{"hi there", "ho there", "hallo"}).To(matchers.ContainExactly([]string{"ho there", "hallo", "hi there"}))
    22  		Expect([]woohoo{woohoo{Flag: true}}).To(matchers.ContainExactly([]woohoo{woohoo{Flag: true}}))
    23  		Expect([]woohoo{woohoo{Flag: true}, woohoo{Flag: false}}).To(matchers.ContainExactly([]woohoo{woohoo{Flag: true}, woohoo{Flag: false}}))
    24  
    25  		Expect([]string{"hi there", "ho there", "hallo"}).ToNot(matchers.ContainExactly([]string{"hi there", "bye bye"}))
    26  		Expect([]string{"hi there", "ho there", "hallo"}).ToNot(matchers.ContainExactly([]string{"ho there", "hi there"}))
    27  		Expect([]string{"ho there", "hallo"}).ToNot(matchers.ContainExactly([]string{"ho there", "hi there", "hallo"}))
    28  		Expect([]string{"hi there", "ho there", "hallo"}).ToNot(matchers.ContainExactly([]string{"buhbye"}))
    29  		Expect([]string{"hi there", "ho there", "hallo"}).ToNot(matchers.ContainExactly([]string{}))
    30  
    31  		Expect([]woohoo{woohoo{Flag: false}}).ToNot(matchers.ContainExactly([]woohoo{woohoo{Flag: true}}))
    32  		Expect([]woohoo{woohoo{Flag: false}, woohoo{Flag: false}}).ToNot(matchers.ContainExactly([]woohoo{woohoo{Flag: true}, woohoo{Flag: false}}))
    33  	})
    34  
    35  	It("handles map types", func() {
    36  		Expect(woomap{woohoo{true}: "fun", woohoo{false}: "not fun"}).To(matchers.ContainExactly(woomap{woohoo{false}: "not fun", woohoo{true}: "fun"}))
    37  	})
    38  
    39  	It("handles duplicate elements", func() {
    40  		Expect([]int{-7, -7, 9, 4}).To(matchers.ContainExactly([]int{4, 9, -7, -7}))
    41  		Expect([]int{-7, -7, 9, 4}).ToNot(matchers.ContainExactly([]int{4, 9, -7, 44}))
    42  		Expect([]int{4, -7, 9, 44}).ToNot(matchers.ContainExactly([]int{4, 9, -7, -7}))
    43  	})
    44  
    45  	It("fails for non-array or slices", func() {
    46  		failures := InterceptGomegaFailures(func() {
    47  			Expect([]string{"hi there", "ho there", "hallo"}).ToNot(matchers.ContainExactly(46))
    48  			Expect(23).ToNot(matchers.ContainExactly([]string{"hi there", "ho there", "hallo"}))
    49  			Expect("woo").ToNot(matchers.ContainExactly([]woohoo{woohoo{Flag: true}, woohoo{Flag: false}}))
    50  			Expect([]string{"hi there", "ho there", "hallo"}).ToNot(matchers.ContainExactly(nil))
    51  		})
    52  		Expect(failures[0]).To(Equal("Matcher can only take an array, slice or map"))
    53  		Expect(failures[1]).To(Equal("Matcher can only take an array, slice or map"))
    54  		Expect(failures[2]).To(Equal("Matcher can only take an array, slice or map"))
    55  		Expect(failures[3]).To(Equal("Matcher can only take an array, slice or map"))
    56  	})
    57  
    58  	Context("when the matcher assertion fails", func() {
    59  		var sliceA, sliceB []string
    60  
    61  		BeforeEach(func() {
    62  			sliceA = []string{"hi there", "ho there", "hallo"}
    63  			sliceB = []string{"goodbye"}
    64  		})
    65  
    66  		It("prints a failure message for the matcher", func() {
    67  			failures := InterceptGomegaFailures(func() {
    68  				Expect(sliceA).To(matchers.ContainExactly(sliceB))
    69  			})
    70  			Expect(failures[0]).To(Equal(fmt.Sprintf("Expected %#v\n to contain exactly: %#v\n but it did not.", sliceA, sliceB)))
    71  		})
    72  
    73  		It("prints a negated failure meessage for the matcher", func() {
    74  			failures := InterceptGomegaFailures(func() {
    75  				Expect(sliceA).ToNot(matchers.ContainExactly(sliceA))
    76  			})
    77  			Expect(failures[0]).To(Equal(fmt.Sprintf("Expected %#v\n not to contain exactly: %#v\n but it did!", sliceA, sliceA)))
    78  		})
    79  	})
    80  
    81  })