-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz_test.go
64 lines (53 loc) · 1.3 KB
/
quiz_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
package main
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"testing/fstest"
"time"
)
func TestRunQuiz(t *testing.T) {
cases := []struct {
name string
pause int
answer string
expectedScore int
}{
{"CorrectAnswer", 0, "2", 1},
{"IncorrectAnswer", 0, "3", 0},
{"LateAnswer", 2, "2", 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
testRunQuiz(t, c.pause, c.answer, c.expectedScore)
})
}
}
func testRunQuiz(t *testing.T, pause int, answer string, expectedScore int) {
const testFilename = "test.csv"
var expectedOutput = fmt.Sprintf("You scored %d out of 1.", expectedScore)
fs := fstest.MapFS{
testFilename: {
Data: []byte("1 + 1,2"),
},
}
stdin, stdinWriter := io.Pipe()
var stdout bytes.Buffer
go func() {
time.Sleep(time.Second * time.Duration(pause))
_, _ = stdinWriter.Write([]byte(fmt.Sprintf("%s\n", answer)))
}()
err := runQuiz(stdin, &stdout, fs, testFilename, 1)
if err != nil {
t.Fatalf("Failed with error: %s", err)
}
result := strings.Split(stdout.String(), "\n")
finalLine := result[len(result)-2]
parts := strings.Split(finalLine, "=")
output := strings.TrimSpace(parts[len(parts)-1])
if output != expectedOutput {
t.Fatalf("Expected the output to be '%s' but got '%s'", expectedOutput, output)
}
}