-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb_test.go
120 lines (115 loc) · 2.21 KB
/
bb_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
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetFlags(0)
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}
func TestMain(t *testing.T) {
admin = "root" // for docker!
tests := []struct {
name string
action func()
expectedOutput string
}{
{
name: "happy: type q to quit",
action: func() {
var offset int64 = 0
input, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer input.Close()
_, err = io.WriteString(input, "q")
if err != nil {
t.Fatal(err)
}
_, err = input.Seek(offset, 0)
if err != nil {
t.Fatal(err)
}
initiateBB([]string{" "})
ViewBB(" ", input, 500)
},
expectedOutput: "",
},
{
name: "happy: type h for help",
action: func() {
var wg sync.WaitGroup
var offset int64 = 0
input, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer input.Close()
_, err = io.WriteString(input, "h")
if err != nil {
t.Fatal(err)
}
_, err = input.Seek(offset, 0)
if err != nil {
t.Fatal(err)
}
wg.Add(1)
go func() {
defer wg.Done()
ViewBB(" ", input, 500)
}()
offset++
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(1 * time.Duration(time.Second))
_, err = io.WriteString(input, "!")
if err != nil {
t.Fatal(err)
}
_, err = input.Seek(offset, 0)
if err != nil {
t.Fatal(err)
}
}()
offset++
go func() {
defer wg.Done()
time.Sleep(1 * time.Duration(time.Second))
_, err = io.WriteString(input, "q")
if err != nil {
t.Fatal(err)
}
_, err = input.Seek(offset, 0)
if err != nil {
t.Fatal(err)
}
}()
offset++
wg.Wait()
},
expectedOutput: "",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
actualOutput := captureOutput(tt.action)
log.Println("_______________")
time.Sleep(time.Duration(1) * time.Second)
assert.Equal(t, tt.expectedOutput, actualOutput, tt.name)
})
}
}