-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
59 lines (47 loc) · 1.32 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
package main
import (
"bytes"
"os"
"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 TestMain(t *testing.T) {
os.Args = append(os.Args, "-directory", "./test-utils/3-files")
// IO buffers
inBuf := bytes.NewBuffer([]byte{})
outBuf := bytes.NewBuffer([]byte{})
// overwrite the standard streams from main
// to use our mocked one
inStream = inBuf
outStream = outBuf
// user input
inBuf.WriteString("Lorem ipsum\n")
inBuf.WriteString(QuitSentence + "\n")
// dispatch main method in a separated routine
var wg sync.WaitGroup
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
main()
}(&wg)
wg.Wait()
actual, _ := outBuf.ReadString('\x00')
expected := `search> file1.txt: 100.00% match
file3.txt: 100.00% match
file2.txt: 50.00% match
search> `
assert.Equal(t, expected, actual)
}
func TestInstantiateReader(t *testing.T) {
assert.IsType(t, reader.Disk{}, instantiateReader(""))
}
func TestInstantiateReporter(t *testing.T) {
assert.IsType(t, report.SimpleReporter{}, instantiateReporter(""))
}
func TestInstantiateRankingAlgorithm(t *testing.T) {
assert.IsType(t, ranking.LevenshteinRanking{}, instantiateRankingAlgorithm(""))
}