-
Notifications
You must be signed in to change notification settings - Fork 19
/
description_test.go
86 lines (77 loc) · 1.89 KB
/
description_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
package postman
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDescriptionMarshalJSON(t *testing.T) {
cases := []struct {
scenario string
description Description
expectedOutput string
}{
{
"Successfully marshalling a Description as an object",
Description{
Content: "My awesome collection",
Type: "text/plain",
Version: "v1",
},
`{"content":"My awesome collection","type":"text/plain","version":"v1"}`,
},
{
"Successfully marshalling a Description as a string",
Description{
Content: "My awesome collection",
},
`"My awesome collection"`,
},
}
for _, tc := range cases {
bytes, _ := tc.description.MarshalJSON()
assert.Equal(t, tc.expectedOutput, string(bytes), tc.scenario)
}
}
func TestDescriptionUnmarshalJSON(t *testing.T) {
cases := []struct {
scenario string
bytes []byte
expectedDescription Description
expectedError error
}{
{
"Successfully unmarshalling a Description from a string",
[]byte(`"My awesome collection"`),
Description{Content: "My awesome collection"},
nil,
},
{
"Successfully unmarshalling a Description from an empty slice of bytes",
make([]byte, 0),
Description{},
nil,
},
{
"Successfully unmarshalling a Description",
[]byte(`{"content":"My awesome collection","type":"text/plain","version":"v1"}`),
Description{
Content: "My awesome collection",
Type: "text/plain",
Version: "v1",
},
nil,
},
{
"Failed to unmarshal a Description because of an unsupported type",
[]byte(`not-a-valid-description`),
Description{},
errors.New("unsupported type for description"),
},
}
for _, tc := range cases {
var d Description
err := d.UnmarshalJSON(tc.bytes)
assert.Equal(t, tc.expectedDescription, d, tc.scenario)
assert.Equal(t, tc.expectedError, err, tc.scenario)
}
}