-
Notifications
You must be signed in to change notification settings - Fork 2
/
ej_test.go
100 lines (71 loc) · 1.94 KB
/
ej_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
91
92
93
94
95
96
97
98
99
100
package ej
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
var errTest = errors.New("test error")
type dataTest struct {
Number int
Txt string
}
type jsonHandlerMock struct {
mock.Mock
}
func (j *jsonHandlerMock) Read() ([]byte, error) {
args := j.Called()
return args.Get(0).([]byte), args.Error(1)
}
func (j *jsonHandlerMock) Write(data []byte) error {
return j.Called(data).Error(0)
}
type ejSuite struct {
suite.Suite
}
func (e *ejSuite) TestParseToData__ReadError() {
jsonHandler := new(jsonHandlerMock)
jsonHandler.On("Read").Return([]byte{}, errTest)
e.EqualError(JSON(jsonHandler).ParseToData(nil), "read: test error")
}
func (e *ejSuite) TestParseToData__UnmarshalError() {
jsonHandler := new(jsonHandlerMock)
jsonHandler.On("Read").Return([]byte("{{{"), nil)
e.EqualError(JSON(jsonHandler).ParseToData(nil),
"json unmarshal: invalid character '{' looking for beginning of object key string")
}
func (e *ejSuite) TestParseToData__NoError() {
expectedData := dataTest{
Number: 2,
Txt: "hello",
}
data := dataTest{}
jsonHandler := new(jsonHandlerMock)
jsonHandler.On("Read").Return([]byte(`{"number": 2, "txt": "hello"}`), nil)
e.NoError(JSON(jsonHandler).ParseToData(&data))
e.Equal(expectedData, data)
}
func (e *ejSuite) TestWrite__WriteError() {
expectedData := dataTest{
Number: 15,
Txt: "__hi__",
}
data, _ := json.Marshal(expectedData)
jsonHandler := new(jsonHandlerMock)
jsonHandler.On("Write", data).Return(errTest)
e.EqualError(JSON(jsonHandler).Write(expectedData), "write: test error")
}
func (e *ejSuite) TestWrite__NoError() {
expectedData := dataTest{
Number: 15,
Txt: "__hi__",
}
data, _ := json.Marshal(expectedData)
jsonHandler := new(jsonHandlerMock)
jsonHandler.On("Write", data).Return(nil)
e.NoError(JSON(jsonHandler).Write(expectedData))
}
func TestEJ(t *testing.T) {
suite.Run(t, new(ejSuite))
}