-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_test.go
100 lines (96 loc) · 2.96 KB
/
result_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 greddis_test
import (
"bufio"
"bytes"
"errors"
"testing"
gomock "github.com/golang/mock/gomock"
"github.com/mikn/greddis"
"github.com/mikn/greddis/mocks/mock_greddis"
"github.com/mikn/greddis/mocks/mock_io"
//"github.com/mikn/greddis/mocks/mock_net"
"github.com/stretchr/testify/require"
)
var (
TEST_SIMPLE_STRING = "+test\r\n"
TEST_BULK_STRING_CORRECT = []byte("$11\r\ntest string\r\n")
TEST_INTEGER = ":15\r\n"
)
//go:generate mockgen -destination ./mocks/mock_io/mock_writer.go io Writer
func TestScan(t *testing.T) {
t.Run("string", func(t *testing.T) {
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanSimpleString)
res := greddis.NewResult(r)
var str string
target := &str
res.Scan(target)
require.Equal(t, "test", str)
})
t.Run("int", func(t *testing.T) {
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_INTEGER)))
r.Next(greddis.ScanInteger)
res := greddis.NewResult(r)
var i int
target := &i
res.Scan(target)
require.Equal(t, 15, i)
})
t.Run("invalid int", func(t *testing.T) {
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanInteger)
res := greddis.NewResult(r)
var i int
target := &i
err := res.Scan(target)
require.Error(t, err)
})
t.Run("[]byte", func(t *testing.T) {
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanSimpleString)
res := greddis.NewResult(r)
b := make([]byte, 4)
target := &b
res.Scan(target)
require.Equal(t, []byte("test"), b)
})
t.Run("io.Writer", func(t *testing.T) {
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanSimpleString)
res := greddis.NewResult(r)
b := &bytes.Buffer{}
res.Scan(b)
require.Equal(t, "test", b.String())
})
t.Run("io.Writer error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockWriter := mock_io.NewMockWriter(ctrl)
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanSimpleString)
res := greddis.NewResult(r)
b := mockWriter
mockWriter.EXPECT().Write(gomock.Any()).Return(0, errors.New("test"))
err := res.Scan(b)
require.Error(t, err)
})
t.Run("greddis.Scanner", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockScanner := mock_greddis.NewMockScanner(ctrl)
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanSimpleString)
res := greddis.NewResult(r)
mockScanner.EXPECT().Scan([]byte("test")).Return(nil)
res.Scan(mockScanner)
})
t.Run("invalid type", func(t *testing.T) {
r := greddis.NewReader(bufio.NewReader(bytes.NewBufferString(TEST_SIMPLE_STRING)))
r.Next(greddis.ScanSimpleString)
res := greddis.NewResult(r)
type test struct{}
target := &test{}
err := res.Scan(target)
require.Error(t, err)
})
}