-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
139 lines (114 loc) · 2.76 KB
/
main_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
package main
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/yanndr/topstories/provider"
)
type fakeStory struct {
}
func (fakeStory) Title() string {
return "title"
}
func (fakeStory) URL() string {
return "url"
}
type fakeProvider struct {
}
func (fakeProvider) GetStories(limit int) (<-chan provider.Response, error) {
ch := make(chan provider.Response)
go func() {
for i := 0; i < limit; i++ {
ch <- provider.Response{Story: &fakeStory{}}
}
close(ch)
}()
return ch, nil
}
type errorProvider struct {
}
func (errorProvider) GetStories(limit int) (<-chan provider.Response, error) {
return nil, fmt.Errorf("error")
}
type fakeStoryErrorProvider struct {
}
func (fakeStoryErrorProvider) GetStories(limit int) (<-chan provider.Response, error) {
ch := make(chan provider.Response)
go func() {
for i := 0; i < limit; i++ {
ch <- provider.Response{Error: fmt.Errorf("error")}
}
close(ch)
}()
return ch, nil
}
type fakeStoryWriter struct {
w io.Writer
}
func (w fakeStoryWriter) Write(s provider.Story) error {
_, err := w.w.Write([]byte(fmt.Sprintf("|%-s|%s|\n", s.Title(), s.URL())))
return err
}
func (fakeStoryWriter) Flush() error { return nil }
type fakeErrorStoryWriter struct {
}
func (w fakeErrorStoryWriter) Write(s provider.Story) error {
return fmt.Errorf("error")
}
func (fakeErrorStoryWriter) Flush() error { return nil }
func TestRun(t *testing.T) {
b := &bytes.Buffer{}
tt := []struct {
name string
n int
provider provider.StoryProvider
writter provider.StoryWriter
err bool
}{
{"normal", 5, &fakeProvider{}, &fakeStoryWriter{w: b}, false},
{"provider error", 5, &errorProvider{}, &fakeStoryWriter{w: b}, true},
{"story error", 5, &fakeStoryErrorProvider{}, &fakeStoryWriter{w: b}, true},
{"story error", 5, &fakeProvider{}, &fakeErrorStoryWriter{}, true},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
err := run(tc.provider, tc.writter, tc.n)
if err != nil && !tc.err {
t.Fatalf("expect no error, got %v", err)
} else if err == nil && tc.err {
t.Fatal("expect error, got no error")
}
if !tc.err && len(b.Bytes()) == 0 {
t.Fatal("output should not be empty")
}
})
}
}
func TestGetProviderByName(t *testing.T) {
tt := []struct {
name string
err bool
}{
{"HackerNews", false},
{"hackernews", false},
{"reddit", false},
{"reDDit", false},
{"false", true},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
p, err := getProviderByName(tc.name, 1)
if err != nil && !tc.err {
t.Fatalf("do not expect an error, got % v", err)
} else if err == nil && tc.err {
t.Fatal("expect an error, got no error")
}
if !tc.err {
if p == nil {
t.Fatalf("Expect a return value, got nil ")
}
}
})
}
}