This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
trailer_type_test.go
80 lines (64 loc) · 1.71 KB
/
trailer_type_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
package nontransparent
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
type trailerWrapper struct {
Trailer TrailerType `json:"trailer"`
}
func TestUnmarshalTOML(t *testing.T) {
var t1 TrailerType
t1.UnmarshalTOML([]byte(`"LF"`))
assert.Equal(t, LF, t1)
var t2 TrailerType
t2.UnmarshalTOML([]byte(`LF`))
assert.Equal(t, LF, t2)
var t3 TrailerType
t3.UnmarshalTOML([]byte(`'LF'`))
assert.Equal(t, LF, t3)
var t4 TrailerType
t4.UnmarshalTOML([]byte(`"NUL"`))
assert.Equal(t, NUL, t4)
var t5 TrailerType
t5.UnmarshalTOML([]byte(`NUL`))
assert.Equal(t, NUL, t5)
var t6 TrailerType
t6.UnmarshalTOML([]byte(`'NUL'`))
assert.Equal(t, NUL, t6)
var t7 TrailerType
err := t7.UnmarshalTOML([]byte(`wrong`))
assert.Equal(t, TrailerType(-1), t7)
assert.Error(t, err)
}
func TestUnmarshalLowercase(t *testing.T) {
x := &trailerWrapper{}
in := []byte(`{"trailer": "lf"}`)
err := json.Unmarshal(in, x)
assert.Nil(t, err)
assert.Equal(t, &trailerWrapper{Trailer: LF}, x)
}
func TestUnmarshalUnknown(t *testing.T) {
x := &trailerWrapper{}
in := []byte(`{"trailer": "UNK"}`)
err := json.Unmarshal(in, x)
assert.Error(t, err)
assert.Equal(t, &trailerWrapper{Trailer: -1}, x)
}
func TestUnmarshal(t *testing.T) {
x := &trailerWrapper{}
in := []byte(`{"trailer": "NUL"}`)
err := json.Unmarshal(in, x)
assert.Nil(t, err)
assert.Equal(t, &trailerWrapper{Trailer: NUL}, x)
}
func TestMarshalUnknown(t *testing.T) {
res, err := json.Marshal(&trailerWrapper{Trailer: TrailerType(-2)})
assert.Error(t, err)
assert.Empty(t, res)
}
func TestMarshal(t *testing.T) {
res, err := json.Marshal(&trailerWrapper{Trailer: NUL})
assert.Nil(t, err)
assert.Equal(t, `{"trailer":"NUL"}`, string(res))
}