-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter_test.go
198 lines (170 loc) · 4.6 KB
/
formatter_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package profaneword
import (
"fmt"
"math/big"
"testing"
)
func TestShuffleFormatter_Format(t *testing.T) {
tests := []string{
"asd",
"hello",
"world",
"a",
"",
}
s := ShuffleFormatter{CryptoRand{}}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
if got := s.Format(tt); len(got) != len(tt) {
t.Errorf("length mismatch: %v, %v", got, tt)
}
})
}
}
type countRandomDevice struct {
counter int
}
func (n *countRandomDevice) Rand() *big.Rat {
return big.NewRat(1, 1)
}
func (n *countRandomDevice) RandMax(max int) int {
if n.counter == max {
n.counter = 0
return max
}
n.counter++
return n.counter - 1
}
var _ RandomDevice = &countRandomDevice{}
func TestHorseFormatter_Format(t *testing.T) {
words := make([]string, len(horsewords))
h := HorseFormatter{&countRandomDevice{}}
for i := 0; i < len(horsewords); i++ {
got := h.Format("")
words[i] = got
}
for i, w := range words {
if horsewords[i] != w {
t.Errorf("Elements should be the same, expected %s, got %s", horsewords[i], w)
}
}
}
var _ RandomDevice = &countRandomDevice{}
type maxRandomDevice struct{}
func (maxRandomDevice) Rand() *big.Rat {
panic("implement me")
}
func (maxRandomDevice) RandMax(max int) int {
return max - 1
}
var _ RandomDevice = maxRandomDevice{}
func TestShuffleFormatter_Format_NonRandom(t *testing.T) {
in := "SOMETHING"
h := ShuffleFormatter{&countRandomDevice{}}
got := h.Format(in)
if in != got {
t.Errorf("non-random shuffle should be No-op, expected: %s, got %s", in, got)
}
sh := ShuffleFormatter{maxRandomDevice{}}
got = sh.Format(in)
if got != "GNIHTEMOS" { // S gets picked first before a s
t.Errorf("non-random max-shuffle should return GNIHTEMOS (reverse), got %s", got)
}
}
func TestStudderFormatter_Format(t *testing.T) {
in := "zero one two three four zero"
s := PerWordFormattingFormatter{StudderFormatter{&countRandomDevice{}}}
got := s.Format(in)
expected := "zero o-one t-t-two t-t-t-three f-f-f-f-four zero"
if got != expected {
t.Errorf("non random studder should be non-random: expected %s, got %s", expected, got)
}
ss := StudderFormatter{&countRandomDevice{}}
got = ss.Format(in)
if got != in {
t.Errorf("non random studder should be non-random: expected %s, got %s", expected, got)
}
}
func TestSwearFormatter_Format(t *testing.T) {
sf := swearFormatter{UnitFormatter{}}
in := "!asd "
if in != sf.Format(in) {
t.Errorf("Expected No-op when the first letter is not a unicode letter")
}
in = "asdd"
got := sf.Format(in)
if got != "asdd!" {
t.Errorf("Expected swearFormatter to add exclamation")
}
in = "asd-asd"
got = sf.Format(in)
if got != "asd!-asd" {
t.Errorf("Expected swearFormatter to add exclamation correctly")
}
}
func TestNewSwearFormatter(t *testing.T) {
input := "ASDASD"
sf := NewSwearFormatter()
got := sf.Format(input)
if len(got) != len(input)+1 {
t.Errorf("expected exactly one exclamation to be added, input: %s, got: %s", input, got)
}
}
func TestReversingFormatter_Format(t *testing.T) {
r := ReversingFormatter{}
if r.Format("dsa") != "asd" {
t.Errorf("expected ReversingFormatter to reverse the string")
}
}
func TestTitleFormatter_Format(t *testing.T) {
tf := TitleFormatter{}
if tf.Format("asd") != "Asd" {
t.Errorf("incorrect titling")
}
}
type cachingCharFormatter struct {
text []rune
}
func (c *cachingCharFormatter) FormatRune(r rune) []rune {
c.text = append(c.text, r)
return []rune{r}
}
var _ CharFormatter = &cachingCharFormatter{}
func TestCharFormatterDelegatingFormatter_Format(t *testing.T) {
del := CharFormatterDelegatingFormatter{}
del.SetCharFormatter(&cachingCharFormatter{})
del.Format("asd")
got := string(del.GetCharFormatter().(*cachingCharFormatter).text)
if got != "asd" {
t.Errorf("CharFormatterDelegatingFormatter did not pass text correctly")
}
}
type countingFormatter struct {
int
}
func (c *countingFormatter) Format(_ string) string {
defer func() { c.int++ }()
return fmt.Sprintf(`%d`, c.int)
}
func TestPerWordFormattingFormatter_Format(t *testing.T) {
pw := PerWordFormattingFormatter{}
pw.SetFormatter(&countingFormatter{})
got := pw.Format("any three words")
if got != "0 1 2" {
t.Errorf("unexpected return from PerWordFormattingFormatter")
}
}
type appendingFormatter string
func (a appendingFormatter) Format(word string) string {
return word + string(a)
}
func TestMultiFormatter_Format(t *testing.T) {
one, two, three := appendingFormatter("one"), appendingFormatter("two"), appendingFormatter("three")
mf := MultiFormatter{}
mf.With(one)
mf.With(two)
mf.With(three)
if mf.Format("") != "onetwothree" {
t.Errorf("unexpected formatting of MultiFormatter")
}
}