-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_room_test.go
199 lines (189 loc) · 4.73 KB
/
sorting_room_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
199
package sorting
import "testing"
func TestDescribeNumber(t *testing.T) {
tests := []struct {
description string
input float64
want string
}{
{
description: "Describe 4.1",
input: 4.1,
want: "This is the number 4.1",
},
{
description: "Describe -3.2",
input: -3.2,
want: "This is the number -3.2",
},
{
description: "Pads to single decimal place",
input: 4.0,
want: "This is the number 4.0",
},
{
description: "Truncates to single decimal place",
input: 7.11,
want: "This is the number 7.1",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if got := DescribeNumber(test.input); got != test.want {
t.Errorf("DescribeNumber(%v) = %v; want %v", test.input, got, test.want)
}
})
}
}
type testNumberBox struct {
n int
}
func (nb testNumberBox) Number() int {
return nb.n
}
func TestDescribeNumberBox(t *testing.T) {
tests := []struct {
description string
input NumberBox
want string
}{
{
description: "Describe NumberBox with 4",
input: testNumberBox{4},
want: "This is a box containing the number 4.0",
},
{
description: "Describe NumberBox with -3",
input: testNumberBox{-3},
want: "This is a box containing the number -3.0",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if got := DescribeNumberBox(test.input); got != test.want {
t.Errorf("DescribeNumberBox(%v) = %v; want %v", test.input, got, test.want)
}
})
}
}
type differentFancyNumber struct {
num string
}
func (i differentFancyNumber) Value() string {
return i.num
}
func TestExtractFancyNumber(t *testing.T) {
tests := []struct {
description string
input FancyNumberBox
want int
}{
{
description: "Extract fancy number 11",
input: FancyNumber{"11"},
want: 11,
},
{
description: "Extract fancy number 0",
input: FancyNumber{"0"},
want: 0,
},
{
description: "Extract a differentFancyNumber returns 0",
input: differentFancyNumber{"4"},
want: 0,
},
{
description: "Extract an invalid fancy number returns 0",
input: FancyNumber{"two"},
want: 0,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if got := ExtractFancyNumber(test.input); got != test.want {
t.Errorf("ExtractFancyNumber(%v) = %v; want %v", test.input, got, test.want)
}
})
}
}
func TestDescribeFancyNumberBox(t *testing.T) {
tests := []struct {
description string
input FancyNumberBox
want string
}{
{
description: "Describe fancy number 12",
input: FancyNumber{"12"},
want: "This is a fancy box containing the number 12.0",
},
{
description: "Describe fancy number 0",
input: FancyNumber{"0"},
want: "This is a fancy box containing the number 0.0",
},
{
description: "Describe a different fancy number",
input: differentFancyNumber{"three"},
want: "This is a fancy box containing the number 0.0",
},
{
description: "Describe a valid different fancy number",
input: differentFancyNumber{"4"},
want: "This is a fancy box containing the number 0.0",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if got := DescribeFancyNumberBox(test.input); got != test.want {
t.Errorf("DescribeFancyNumberBox(%v) = %v; want %v", test.input, got, test.want)
}
})
}
}
func TestDescribeAnything(t *testing.T) {
tests := []struct {
description string
input interface{}
want string
}{
{
description: "Describe 7.2",
input: 7.2,
want: "This is the number 7.2",
},
{
description: "Describe 42",
input: 42,
want: "This is the number 42.0",
},
{
description: "Describe NumberBox with 16",
input: testNumberBox{16},
want: "This is a box containing the number 16.0",
},
{
description: "Describe FancyNumber with 16",
input: FancyNumber{"16"},
want: "This is a fancy box containing the number 16.0",
},
{
description: "Describe a different FancyNumberBox",
input: differentFancyNumber{"ten"},
want: "This is a fancy box containing the number 0.0",
},
{
description: "Something unknown is labelled return to sender",
input: "something we did not anticipate",
want: "Return to sender",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if got := DescribeAnything(test.input); got != test.want {
t.Errorf("DescribeAnything(%v) = %v; want %v", test.input, got, test.want)
}
})
}
}