aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/onsi/gomega/matchers/equal_matcher_test.go
blob: 2add0b7499ee27c0ca55aced06ba03ce7f71a844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package matchers_test

import (
	"errors"
	"strings"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/matchers"
)

var _ = Describe("Equal", func() {
	Context("when asserting that nil equals nil", func() {
		It("should error", func() {
			success, err := (&EqualMatcher{Expected: nil}).Match(nil)

			Ω(success).Should(BeFalse())
			Ω(err).Should(HaveOccurred())
		})
	})

	Context("When asserting equality between objects", func() {
		It("should do the right thing", func() {
			Ω(5).Should(Equal(5))
			Ω(5.0).Should(Equal(5.0))

			Ω(5).ShouldNot(Equal("5"))
			Ω(5).ShouldNot(Equal(5.0))
			Ω(5).ShouldNot(Equal(3))

			Ω("5").Should(Equal("5"))
			Ω([]int{1, 2}).Should(Equal([]int{1, 2}))
			Ω([]int{1, 2}).ShouldNot(Equal([]int{2, 1}))
			Ω(map[string]string{"a": "b", "c": "d"}).Should(Equal(map[string]string{"a": "b", "c": "d"}))
			Ω(map[string]string{"a": "b", "c": "d"}).ShouldNot(Equal(map[string]string{"a": "b", "c": "e"}))
			Ω(errors.New("foo")).Should(Equal(errors.New("foo")))
			Ω(errors.New("foo")).ShouldNot(Equal(errors.New("bar")))

			Ω(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"}}))
			Ω(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"}}))
			Ω(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"}}))
			Ω(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"}}))
			Ω(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"}}))
		})
	})

	Describe("failure messages", func() {
		It("shows the two strings simply when they are short", func() {
			subject := EqualMatcher{Expected: "eric"}

			failureMessage := subject.FailureMessage("tim")
			Ω(failureMessage).To(BeEquivalentTo(expectedShortStringFailureMessage))
		})

		It("shows the exact point where two long strings differ", func() {
			stringWithB := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
			stringWithZ := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

			subject := EqualMatcher{Expected: stringWithZ}

			failureMessage := subject.FailureMessage(stringWithB)
			Ω(failureMessage).To(BeEquivalentTo(expectedLongStringFailureMessage))
		})
	})
})

var expectedShortStringFailureMessage = strings.TrimSpace(`
Expected
    <string>: tim
to equal
    <string>: eric
`)
var expectedLongStringFailureMessage = strings.TrimSpace(`
Expected
    <string>: "...aaaaabaaaaa..."
to equal               |
    <string>: "...aaaaazaaaaa..."
`)