-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_test.go
131 lines (102 loc) · 3.14 KB
/
console_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
package main
import (
"bytes"
"sync"
"testing"
"github.com/mauricioklein/text-search-engine/ranking"
"github.com/mauricioklein/text-search-engine/reader"
"github.com/mauricioklein/text-search-engine/report"
"github.com/stretchr/testify/assert"
)
func TestConsoleInputStreamSuccess(t *testing.T) {
c, r, _, _ := NewTestConsole("./test-utils/3-files/")
r.WriteString("Foobar\n")
actual, _ := c.Read()
expected := "Foobar"
assert.Equal(t, expected, actual)
}
func TestConsoleInputStreamError(t *testing.T) {
c, r, _, _ := NewTestConsole("./test-utils/3-files/")
r.WriteString("Foobar")
_, err := c.Read()
assert.Error(t, err)
}
func TestConsoleOutputStream(t *testing.T) {
c, _, w, _ := NewTestConsole("./test-utils/3-files/")
c.Write("Foobar")
actual, _ := w.ReadString('\x00')
expected := "Foobar"
assert.Equal(t, expected, actual)
}
func TestConsoleErrorStream(t *testing.T) {
c, _, _, e := NewTestConsole("./test-utils/3-files/")
c.Error("a generic error")
actual, _ := e.ReadString('\x00')
expected := "a generic error"
assert.Equal(t, expected, actual)
}
func TestConsoleRun3Files(t *testing.T) {
c, r, w, _ := NewTestConsole("./test-utils/3-files/")
// write "user input" data to the read stream
r.Write([]byte("Lorem\n")) // actual search sentence
r.Write([]byte(QuitSentence + "\n")) // quit command
// Wait for the run command to finish (due the quit command above)
var wg sync.WaitGroup
wg.Add(1)
go dispatchConsole(c, &wg)
wg.Wait()
// Read response from the write stream
actual, _ := w.ReadString('\x00')
expected := `search> file1.txt: 100.00% match
file3.txt: 100.00% match
file2.txt: 0.00% match
search> `
assert.Equal(t, expected, actual)
}
func TestConsoleRun11Files(t *testing.T) {
c, r, w, _ := NewTestConsole("./test-utils/11-files/")
// write "user input" data to the read stream
r.Write([]byte("Lorem ipsum dolor sit\n")) // actual search sentence
r.Write([]byte(QuitSentence + "\n")) // quit command
// Wait for the run command to finish (due the quit command above)
var wg sync.WaitGroup
wg.Add(1)
go dispatchConsole(c, &wg)
wg.Wait()
// Read response from the write stream
actual, _ := w.ReadString('\x00')
// file11.txt should not be present, since the result
// should display only the top 10
expected := `search> file1.txt: 100.00% match
file10.txt: 100.00% match
file11.txt: 100.00% match
file3.txt: 100.00% match
file6.txt: 100.00% match
file7.txt: 100.00% match
file8.txt: 100.00% match
file9.txt: 100.00% match
file2.txt: 75.00% match
file4.txt: 75.00% match
search> `
// should filter the result to the top 10
assert.Equal(t, expected, actual)
}
func NewTestConsole(dirPath string) (Console, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
files, _ := reader.Disk{}.Read(dirPath)
nWorkers := 3
processor := ranking.NewProcessor(files, nWorkers, ranking.LevenshteinRanking{})
inBuf := bytes.NewBuffer([]byte{})
outBuf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
return NewConsole(
processor,
report.SimpleReporter{},
inBuf,
outBuf,
errBuf,
), inBuf, outBuf, errBuf
}
func dispatchConsole(c Console, wg *sync.WaitGroup) {
defer wg.Done()
c.Run()
}