-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
74 lines (68 loc) · 1.83 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
package main
import (
"context"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
//nolint:gochecknoglobals // for testing this is fine
var uciOkMsg = `id name PetechesChessBot 0.0
id author Pete 'Peteches' McCabe
uciok
`
func TestUciInit(t *testing.T) {
Convey("Given a handleUci function", t, func() {
Convey("It should accept a context", func() {
So(func() {
handleUci()
}, ShouldNotPanic)
})
Convey("It will return a readonly channel ", func() {
outChan := handleUci()
So(outChan, ShouldHaveSameTypeAs, make(<-chan string))
})
Convey("It will write UCI info to the returned channel", func() {
outChan := handleUci()
out := ""
for x := range outChan {
out += x
}
So(out, ShouldEqual, uciOkMsg)
})
})
}
func TestScanForCommands(t *testing.T) {
ctx := context.Background()
Convey("Given a scanForCommands function", t, func() {
Convey("It should accept a context an io.Reader and return a readonly string channel", func() {
ctx, ctxCancel := context.WithCancel(ctx)
in := strings.NewReader("hello\n")
So(func() { scanForCommands(ctx, in) }, ShouldNotPanic)
ctxCancel()
ctx, ctxCancel = context.WithCancel(ctx)
chn := scanForCommands(ctx, in)
So(chn, ShouldHaveSameTypeAs, make(<-chan string))
ctxCancel()
})
Convey("It should read from the io.Reader", func() {
Convey("And quit when the quit command is passed in", func() {
in := strings.NewReader("quit\nnotread\n")
chn := scanForCommands(ctx, in)
msg := ""
for i := range chn {
msg += i
}
So(msg, ShouldEqual, "")
})
Convey("And send any other messages to the returned channel", func() {
in := strings.NewReader("bob\nquit")
chn := scanForCommands(ctx, in)
msg := ""
for i := range chn {
msg += i
}
So(msg, ShouldEqual, "bob")
})
})
})
}