-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontains_test.go
90 lines (75 loc) · 2.94 KB
/
contains_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
88
89
90
package hamcrest_test
import (
"bytes"
. "github.com/pepinns/go-hamcrest"
"io"
"testing"
)
func TestStringContainsMatchesStringInsideString(t *testing.T) {
Assert(t).That("some long string", Contains("long"))
}
func TestStringNotContainsMatchesStringInsideString(t *testing.T) {
Assert(t).That("some long string", Not(Contains("extra longlong")))
}
type TestReader struct {
inner io.Reader
}
func (me *TestReader) Read(b []byte) (int, error) {
return me.inner.Read(b)
}
func TestIoReaderContainsMatchString(t *testing.T) {
b := bytes.NewBufferString("Some data to read as a buffer")
Assert(t).That(io.Reader(&TestReader{b}), Contains("read as"))
}
func TestIoReaderErrorMessageIsClear(t *testing.T) {
b := bytes.NewBufferString("Some data to read as a buffer")
AssertFailureMessage(t, io.Reader(&TestReader{b}), Contains("read as"), Equals(`"Some data to read as a buffer" contains string "read as"`))
}
func TestIoReaderErrorMessageIsClearForNegative(t *testing.T) {
b := bytes.NewBufferString("Some data to read as a buffer")
AssertFailureMessage(t, io.Reader(&TestReader{b}), Contains("NOT MATCH read as"), Equals(`"Some data to read as a buffer" does not contain string "NOT MATCH read as"`))
}
func TestContainsCanTakeMultipleArgsAndTurnIntoAllOf(t *testing.T) {
Assert(t).That([]string{"somestring", "otherstring"}, Contains("otherstring", "somestring"))
}
func TestStringSliceCanContainString(t *testing.T) {
Assert(t).That([]string{"somestring", "otherstring"}, Contains("otherstring"))
}
func TestStringSliceMessageIsClear(t *testing.T) {
AssertFailureMessage(t, []string{"item1", "item2"}, Contains("item2"), Equals(`matched items [
[1] "item2" is equal to "item2"
]`))
AssertFailureMessage(t, []string{"item1", "item2"}, Contains("item3"), Equals(`failed to match [
[0] "item1" is not equal to "item3"
[1] "item2" is not equal to "item3"
]`))
}
func TestStringSliceCanNotContainString(t *testing.T) {
Assert(t).That([]string{"somestring", "otherstring"}, Not(Contains("somestring otherstring")))
}
func TestStringSliceCanNotContainInt(t *testing.T) {
Assert(t).That([]string{"somestring", "otherstring"}, Not(Contains(38)))
}
func TestIntegerSliceCanContainInt(t *testing.T) {
Assert(t).That([]int{34, 38}, Contains(38))
}
func TestInt32SliceCanContainInt(t *testing.T) {
Assert(t).That([]int32{int32(34), int32(38)}, Contains(38))
}
func TestInt32SliceCanContainInt16(t *testing.T) {
Assert(t).That([]int32{int32(34), int32(38)}, Contains(int16(38)))
}
func TestIntegerSliceCanNotContainInt(t *testing.T) {
Assert(t).That([]int{34, 38}, Not(Contains(438)))
}
type Foo struct {
Name string
}
func TestContainsMixedWithHasFieldCanFindFieldInPtrObjByNameAndValue(t *testing.T) {
slice := []*Foo{{"Name1"}}
Assert(t).That(slice, Contains(HasFieldThat("Name", Equals("Name1"))))
}
func TestContainsMixedWithHasFieldCanFindFieldByNameAndValue(t *testing.T) {
slice := []Foo{{"Name1"}}
Assert(t).That(slice, Contains(HasFieldThat("Name", Equals("Name1"))))
}