forked from cassiobotaro/60-days-of-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfizzbuzz_test.go
95 lines (90 loc) · 2.15 KB
/
fizzbuzz_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
package main
import "testing"
// Example of how to make tests using table design.
func TestFizz(t *testing.T) {
// test numbers that are divible by three only.
// table with input and the expected results.
table := []struct {
Input int
Expected string
}{
{3, "Fizz"},
{6, "Fizz"},
{9, "Fizz"},
}
// Make channel const
count := make(chan int)
message := make(chan string)
// iterate over the table and test if obtained is equals to the expected value
go FizzBuzz(count, message)
for _, data := range table {
count <- data.Input
if actual := <-message; actual != data.Expected {
// error will handled if something goes wrong.
t.Errorf("expected %q but %q was obtained", data.Expected, actual)
}
}
}
func TestBuzz(t *testing.T) {
// test numbers that are divible by five only.
table := []struct {
Input int
Expected string
}{
{5, "Buzz"},
{10, "Buzz"},
{20, "Buzz"},
}
// Make channel const
count := make(chan int)
message := make(chan string)
go FizzBuzz(count, message)
for _, data := range table {
count <- data.Input
if actual := <-message; actual != data.Expected {
t.Errorf("expected %q but %q was obtained", data.Expected, actual)
}
}
}
func TestFizzBuzz(t *testing.T) {
// test numbers that are divible by three and five.
table := []struct {
Input int
Expected string
}{
{15, "FizzBuzz"},
{30, "FizzBuzz"},
{60, "FizzBuzz"},
}
// Make channel const
count := make(chan int)
message := make(chan string)
go FizzBuzz(count, message)
for _, data := range table {
count <- data.Input
if actual := <-message; actual != data.Expected {
t.Errorf("expected %q but %q was obtained", data.Expected, actual)
}
}
}
func TestNumbers(t *testing.T) {
// If is not divisible by three or five, return the number as string
table := []struct {
Input int
Expected string
}{
{1, "1"},
{2, "2"},
{4, "4"},
}
// Make channel const
count := make(chan int)
message := make(chan string)
go FizzBuzz(count, message)
for _, data := range table {
count <- data.Input
if actual := <-message; actual != data.Expected {
t.Errorf("expected %q but %q was obtained", data.Expected, actual)
}
}
}