-
Notifications
You must be signed in to change notification settings - Fork 0
/
same_test.go
87 lines (71 loc) · 1.72 KB
/
same_test.go
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
79
80
81
82
83
84
85
86
87
package matchers_test
import (
"errors"
"fmt"
"testing"
matchers "github.com/Storytel/gomock-matchers/v2"
"github.com/stretchr/testify/assert"
)
func TestSameMatcher(t *testing.T) {
str1 := "something"
str2 := "something"
err1 := errors.New("error")
err2 := errors.New("error")
tests := []struct {
lhs interface{}
rhs interface{}
same bool
}{
{"asdf", "asdf", true},
{&err1, &err1, true},
{12, 12, true},
{&str1, &str1, true},
{&str1, &str2, false},
{&err1, &err2, false},
{"bsdf", "asdf", false},
}
for i, test := range tests {
t.Run(fmt.Sprintf("Test #%d", i), func(t *testing.T) {
assert := assert.New(t)
m := matchers.Same(test.lhs)
asserter := assert.False
if test.same {
asserter = assert.True
}
asserter(m.Matches(test.rhs))
})
}
}
type StringTestType int
func (s StringTestType) String() string {
return "StringTestType#String()"
}
func TestSameMatcherString(t *testing.T) {
tests := []struct {
v interface{}
s string
}{
{"asdf", "is same as 'asdf'"},
{12, "is same as '12'"},
{StringTestType(12), "is same as 'StringTestType#String()'"},
{[]int{1, 2, 3}, "is same as '[1 2 3]'"},
}
for i, test := range tests {
t.Run(fmt.Sprintf("Test #%d", i), func(t *testing.T) {
assert := assert.New(t)
m := matchers.Same(test.v)
assert.Equal(test.s, m.String())
})
}
}
func TestSame(t *testing.T) {
assert := assert.New(t)
myString := "something"
otherString := "something"
m := matchers.Same(&myString)
assert.True(m.Matches(&myString))
assert.False(m.Matches(&otherString)) // Not the same pointer
m2 := matchers.Same(myString)
assert.True(m2.Matches(myString))
assert.True(m2.Matches(otherString)) // Same value because they're not pointers
}