-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_slice_test.go
115 lines (97 loc) · 3.05 KB
/
byte_slice_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package nulls
import (
"encoding/base64"
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)
// TestNewByteSlice tests NewByteSlice.
func TestNewByteSlice(t *testing.T) {
raw := []byte("Hello World!")
b := NewByteSlice(raw)
assert.True(t, b.Valid, "should be valid")
assert.Equal(t, raw, b.ByteSlice, "should contain correct value")
}
// ByteSliceMarshalJSONSuite tests ByteSlice.MarshalJSON.
type ByteSliceMarshalJSONSuite struct {
suite.Suite
}
func (suite *ByteSliceMarshalJSONSuite) TestNotValid() {
b := ByteSlice{ByteSlice: []byte("meow")}
raw, err := json.Marshal(b)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "should return correct value")
}
func (suite *ByteSliceMarshalJSONSuite) TestOK() {
v := []byte("Hello World!")
b := NewByteSlice(v)
raw, err := json.Marshal(b)
suite.Require().NoError(err, "should not fail")
suite.Equal(marshalMust(v), raw, "should return correct value")
}
func TestByteSlice_MarshalJSON(t *testing.T) {
suite.Run(t, new(ByteSliceMarshalJSONSuite))
}
// ByteSliceUnmarshalJSONSuite tests ByteSlice.UnmarshalJSON.
type ByteSliceUnmarshalJSONSuite struct {
suite.Suite
}
func (suite *ByteSliceUnmarshalJSONSuite) TestNull() {
var b ByteSlice
err := json.Unmarshal(jsonNull, &b)
suite.Require().NoError(err, "should not fail")
suite.False(b.Valid, "should not be valid")
}
func (suite *ByteSliceUnmarshalJSONSuite) TestOK() {
v := []byte("Hello World!")
var b ByteSlice
err := json.Unmarshal(marshalMust(v), &b)
suite.Require().NoError(err, "should not fail")
suite.True(b.Valid, "should be valid")
suite.Equal(v, b.ByteSlice, "should unmarshal correct value")
}
func TestByteSlice_UnmarshalJSON(t *testing.T) {
suite.Run(t, new(ByteSliceUnmarshalJSONSuite))
}
// ByteSliceScanSuite tests ByteSlice.Scan.
type ByteSliceScanSuite struct {
suite.Suite
}
func (suite *ByteSliceScanSuite) TestNull() {
var b ByteSlice
err := b.Scan(nil)
suite.Require().NoError(err, "should not fail")
suite.False(b.Valid, "should not be valid")
}
func (suite *ByteSliceScanSuite) TestOK() {
v := []byte("Hello World!")
var b ByteSlice
err := b.Scan(base64.StdEncoding.EncodeToString(v))
suite.Require().NoError(err, "should not fail")
suite.True(b.Valid, "should be valid")
suite.Equal(v, b.ByteSlice, "should scan correct value")
}
func TestByteSlice_Scan(t *testing.T) {
suite.Run(t, new(ByteSliceScanSuite))
}
// ByteSliceValueSuite tests ByteSlice.Value.
type ByteSliceValueSuite struct {
suite.Suite
}
func (suite *ByteSliceValueSuite) TestNull() {
b := ByteSlice{ByteSlice: []byte("Hello World!")}
raw, err := b.Value()
suite.Require().NoError(err, "should not fail")
suite.Nil(raw, "should return correct value")
}
func (suite *ByteSliceValueSuite) TestOK() {
v := []byte("Hello World")
b := NewByteSlice(v)
raw, err := b.Value()
suite.Require().NoError(err, "should not fail")
suite.Equal(base64.StdEncoding.EncodeToString(v), raw, "should return correct value")
}
func TestByteSlice_Value(t *testing.T) {
suite.Run(t, new(ByteSliceValueSuite))
}