-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmfmt_test.go
104 lines (88 loc) · 2.56 KB
/
bmfmt_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
package bmfmt
import (
"testing"
"github.com/kami-zh/go-capturer"
"github.com/stretchr/testify/assert"
)
// TestBeautifyCauseAnErrorOnNonMap test the "Beautify" function to return an error on given argument is not a map
func TestBeautifyCauseAnErrorOnNonMap(t *testing.T) {
candidate := "not a map, just a string"
err := Beautify(candidate)
if !assert.Error(t, err, "error is expected") {
t.FailNow()
}
assert.Equal(t, "given argument is NOT a map", err.Error())
}
// TestBeautifyCauseAnErrorOnUnknownMapStructure test the "Beautify" function with an unknown map structure
func TestBeautifyCauseAnErrorOnUnknownMapStructure(t *testing.T) {
candidate := map[string]struct{}{}
err := Beautify(candidate)
if !assert.Error(t, err, "error is expected") {
t.FailNow()
}
assert.Equal(t, "(currently) unknown map structure: map[string]struct", err.Error())
}
// TestBeautifyOutputSimpleMapWithStringKeysAndStringValues test a simple map with string keys and string values
func TestBeautifyOutputSimpleMapWithStringKeysAndStringValues(t *testing.T) {
tests := map[string]struct {
in map[string]string
out string
}{
"empty key/value": {
in: map[string]string{
"a key": "",
},
out: `[ "a key" string( 5) ]: "" string( 0)
`,
},
"simple key/value": {
in: map[string]string{
"a key": "a short value",
},
out: `[ "a key" string( 5) ]: "a short value" string( 13)
`,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var err error
output := capturer.CaptureStdout(func() {
err = Beautify(tc.in)
})
assert.NoError(t, err, "nil is expected")
assert.Equal(t, tc.out, output, "beautified map output expected")
})
}
}
// TestBeautifyOutputSimpleMapWithStringKeysAndSliceOfStringValues test a valid map[string][]string output
func TestBeautifyOutputSimpleMapWithStringKeysAndSliceOfStringValues(t *testing.T) {
tests := map[string]struct {
in map[string][]string
out string
}{
"single value": {
in: map[string][]string{
"a key": {"a single value"},
},
out: `[ "a key" string( 5) ]: "a single value" string( 14)
`,
},
"multiple values": {
in: map[string][]string{
"next key": {"value one", "value two"},
},
out: `[ "next key" string( 8) ]: "value one", "value two" string( 22)
`,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var err error
output := capturer.CaptureStdout(func() {
err = Beautify(tc.in)
})
assert.NoError(t, err, "nil is expected")
assert.Equal(t, tc.out, output, "beautified map output expected")
})
}
}