/
common_test.go
120 lines (104 loc) · 2.16 KB
/
common_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
116
117
118
119
120
// Copyright 2014 Travis Keep. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or
// at http://opensource.org/licenses/BSD-3-Clause.
package common_test
import (
"fmt"
"github.com/keep94/common"
"strings"
"testing"
)
var (
kNone1 noneType
kNone2 noneType
kNone3 noneType
kSingle1 singleType = "one"
kSingle2 singleType = "two"
kSingle3 singleType = "three"
kMulti456 = multiType{
singleType("four"),
singleType("five"),
singleType("six"),
}
kMulti78 = multiType{
singleType("seven"),
singleType("eight"),
}
)
func TestControl(t *testing.T) {
arr := multiType{
kNone2,
kMulti78,
kSingle1,
}
expected := "[NONE [seven eight] one]"
assertStrEqual(t, expected, arr.ToString())
}
func TestNone(t *testing.T) {
assertStrEqual(t, "NONE", join(nil))
assertStrEqual(t, "NONE", join([]fooType{kNone1, kNone2, kNone3}))
}
func TestSingle(t *testing.T) {
assertStrEqual(t, "one", join([]fooType{kSingle1}))
assertStrEqual(t, "one", join([]fooType{kNone1, kSingle1, kNone2}))
}
func TestMulti(t *testing.T) {
assertStrEqual(
t,
"[one three four five six two seven eight]",
join([]fooType{
kNone1,
kSingle1,
kSingle3,
kNone2,
kMulti456,
kSingle2,
kMulti78,
kNone3,
}))
assertStrEqual(
t,
"[four five six]",
join([]fooType{
kNone1,
kMulti456,
kNone3,
}))
assertStrEqual(
t,
"[four five six]",
join([]fooType{
kMulti456,
}))
}
func join(arr []fooType) string {
var agg multiType
var none noneType
return common.Join(arr, agg, none).(fooType).ToString()
}
func assertStrEqual(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("Expected %s, got %s", expected, actual)
}
}
type fooType interface {
ToString() string
}
type noneType struct {
}
func (n noneType) ToString() string {
return "NONE"
}
type singleType string
func (s singleType) ToString() string {
return string(s)
}
type multiType []fooType
func (m multiType) ToString() string {
strs := make([]string, len(m))
for i := range m {
strs[i] = m[i].ToString()
}
return fmt.Sprintf("[%s]", strings.Join(strs, " "))
}