...

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

Documentation: github.com/onsi/gomega/matchers

     1  package matchers_test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/matchers"
    10  )
    11  
    12  var _ = Describe("Equal", func() {
    13  	When("asserting that nil equals nil", func() {
    14  		It("should error", func() {
    15  			success, err := (&EqualMatcher{Expected: nil}).Match(nil)
    16  
    17  			Expect(success).Should(BeFalse())
    18  			Expect(err).Should(HaveOccurred())
    19  		})
    20  	})
    21  
    22  	Context("When asserting equality between objects", func() {
    23  		It("should do the right thing", func() {
    24  			Expect(5).Should(Equal(5))
    25  			Expect(5.0).Should(Equal(5.0))
    26  
    27  			Expect(5).ShouldNot(Equal("5"))
    28  			Expect(5).ShouldNot(Equal(5.0))
    29  			Expect(5).ShouldNot(Equal(3))
    30  
    31  			Expect("5").Should(Equal("5"))
    32  			Expect([]int{1, 2}).Should(Equal([]int{1, 2}))
    33  			Expect([]int{1, 2}).ShouldNot(Equal([]int{2, 1}))
    34  			Expect([]byte{'f', 'o', 'o'}).Should(Equal([]byte{'f', 'o', 'o'}))
    35  			Expect([]byte{'f', 'o', 'o'}).ShouldNot(Equal([]byte{'b', 'a', 'r'}))
    36  			Expect(map[string]string{"a": "b", "c": "d"}).Should(Equal(map[string]string{"a": "b", "c": "d"}))
    37  			Expect(map[string]string{"a": "b", "c": "d"}).ShouldNot(Equal(map[string]string{"a": "b", "c": "e"}))
    38  			Expect(errors.New("foo")).Should(Equal(errors.New("foo")))
    39  			Expect(errors.New("foo")).ShouldNot(Equal(errors.New("bar")))
    40  
    41  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(Equal(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}))
    42  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "bar", n: 3, f: 2.0, arr: []string{"a", "b"}}))
    43  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "foo", n: 2, f: 2.0, arr: []string{"a", "b"}}))
    44  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "foo", n: 3, f: 3.0, arr: []string{"a", "b"}}))
    45  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b", "c"}}))
    46  		})
    47  	})
    48  
    49  	Describe("failure messages", func() {
    50  		It("shows the two strings simply when they are short", func() {
    51  			subject := EqualMatcher{Expected: "eric"}
    52  
    53  			failureMessage := subject.FailureMessage("tim")
    54  			Expect(failureMessage).To(BeEquivalentTo(expectedShortStringFailureMessage))
    55  		})
    56  
    57  		It("shows the exact point where two long strings differ", func() {
    58  			stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    59  			stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    60  
    61  			subject := EqualMatcher{Expected: stringWithZ}
    62  
    63  			failureMessage := subject.FailureMessage(stringWithB)
    64  			Expect(failureMessage).To(BeEquivalentTo(expectedLongStringFailureMessage))
    65  		})
    66  	})
    67  })
    68  
    69  var expectedShortStringFailureMessage = strings.TrimSpace(`
    70  Expected
    71      <string>: tim
    72  to equal
    73      <string>: eric
    74  `)
    75  var expectedLongStringFailureMessage = strings.TrimSpace(`
    76  Expected
    77      <string>: "...aaaaabaaaaa..."
    78  to equal               |
    79      <string>: "...aaaaazaaaaa..."
    80  `)
    81  

View as plain text