-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencoding_test.go
80 lines (66 loc) · 2.5 KB
/
encoding_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 main
import (
"fmt"
"testing"
)
type encTestObj struct {
Name string `json:"name"`
Value int `json:"value"`
}
// ----------------------------------------------
// JSONEncoder.Encode TESTS
// ----------------------------------------------
// Tests the "happy path" for the JSONEncoder.Encode() function.
func Test_JSONEncoder_Encode(t *testing.T) {
o := encTestObj{Name: "my name", Value: 50}
expected := fmt.Sprintf(`{"name":"%s","value":%d}`, o.Name, o.Value)
enc := JSONEncoder{}
actual := enc.Encode(o)
assert.Equal(t, actual, expected, "JSONEncoder.Encode() returned unexpected value")
}
// ----------------------------------------------
// JSONEncoder.EncodeMulti TESTS
// ----------------------------------------------
// Tests the "happy path" for the JSONEncoder.EncodeMulti() function.
func Test_JSONEncoder_EncodeMulti(t *testing.T) {
o1 := encTestObj{Name: "my name", Value: 50}
o2 := encTestObj{Name: "your name", Value: 25}
col := []interface{}{o1, o2}
str1 := fmt.Sprintf(`{"name":"%s","value":%d}`, o1.Name, o1.Value)
str2 := fmt.Sprintf(`{"name":"%s","value":%d}`, o2.Name, o2.Value)
expected := fmt.Sprintf("[%s,%s]", str1, str2)
enc := JSONEncoder{}
actual := enc.EncodeMulti(col...)
assert.Equal(t, actual, expected, "JSONEncoder.EncodeMulti() returned unexpected value")
}
func Test_JSONEncoder_EncodeMulti_Empty(t *testing.T) {
col := []interface{}{}
enc := JSONEncoder{}
actual := enc.EncodeMulti(col...)
expected := "[]"
assert.Equal(t, actual, expected, "JSONEncoder.EncodeMulti() returned unexpected value")
}
func Test_JSONEncoder_EncodeMulti_Nil(t *testing.T) {
enc := JSONEncoder{}
actual := enc.EncodeMulti(nil)
expected := "[]"
assert.Equal(t, actual, expected, "JSONEncoder.EncodeMulti() returned unexpected value")
}
func Test_JSONEncoder_EncodeMulti_NoValues(t *testing.T) {
enc := JSONEncoder{}
actual := enc.EncodeMulti()
expected := "[]"
assert.Equal(t, actual, expected, "JSONEncoder.EncodeMulti() returned unexpected value")
}
// ----------------------------------------------
// JSONEncoder.Decode TESTS
// ----------------------------------------------
// Tests the "happy path" for the JSONEncoder.Decode() function.
func Test_JSONEncoder_Decode(t *testing.T) {
expected := encTestObj{Name: "my name", Value: 50}
str := fmt.Sprintf(`{"name":"%s","value":%d}`, expected.Name, expected.Value)
actual := encTestObj{}
enc := JSONEncoder{}
_ = enc.Decode([]byte(str), &actual)
assert.Equal(t, actual, expected, "JSONEncoder.Decode() returned unexpected value")
}