-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
61 lines (51 loc) · 1.49 KB
/
main_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
package main
import (
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
//go:generate go build .
//go:generate ./goautomock -template=testify io.Writer
//go:generate ./goautomock -template=testify -pkg=io ByteScanner
//go:generate ./goautomock -template=testify net/http.CookieJar
func TestMockedIOWriter(t *testing.T) {
m := NewWriterMock()
expected := []byte("hello")
m.On("Write", expected).Return(5, nil)
n, err := m.Write(expected)
assert.Equal(t, 5, n)
assert.Equal(t, nil, err)
}
func TestMockedCookieJar(t *testing.T) {
jar := NewCookieJarMock()
cookie := http.Cookie{Name: "hello", Value: "World"}
jar.On("Cookies", mock.AnythingOfType("*url.URL")).Return([]*http.Cookie{&cookie}).Once()
c := http.Client{Jar: jar}
c.Get("http://localhost")
jar.On("Cookies", mock.AnythingOfType("*url.URL")).Return(nil).Once()
c.Get("http://localhost")
}
func TestMockByteScanner(t *testing.T) {
var s io.ByteScanner
m := NewByteScannerMock()
s = m
m.On("ReadByte").Return(byte('_'), nil)
b, err := s.ReadByte()
assert.Equal(t, byte('_'), b)
assert.Equal(t, nil, err)
}
type unexported interface {
io.Reader
}
//go:generate ./goautomock unexported
//go:generate ./goautomock -mock-name unexported2 -pkg github.com/ernesto-jimenez/goautomock/automock unexported
func TestUnexported(t *testing.T) {
m := NewUnexportedMock()
m.ReadFunc = func(b []byte) (int, error) {
return 0, nil
}
m.Read([]byte{})
assert.Equal(t, 1, m.ReadTotalCalls())
}