-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
104 lines (82 loc) · 1.89 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
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"os"
"strings"
"testing"
)
var mazeJsonStr = `
{
"cells": [
[{
"northRoute": false,
"westRoute": false
}, {
"northRoute": true,
"westRoute": false
}],
[{
"northRoute": false,
"westRoute": false
}, {
"northRoute": true,
"westRoute": true
}]
]
}
`
func TestMainLoadCreatesNonZeroOutput(t *testing.T) {
var buf bytes.Buffer
out := bufio.NewWriter(&buf)
in := strings.NewReader(mazeJsonStr)
run(map[string]string{}, out, in, true)
out.Flush()
if len(buf.Bytes()) <= 0 {
t.Errorf("output size error: %d bytes", len(buf.Bytes()))
}
}
func TestMainLoadWithBadJSONDoesNotPanic(t *testing.T) {
var buf bytes.Buffer
out := bufio.NewWriter(&buf)
in := strings.NewReader("NOT-JSON")
run(map[string]string{}, out, in, true)
}
func TestMainGenerateCreatesNonZeroOutput(t *testing.T) {
var buf bytes.Buffer
out := bufio.NewWriter(&buf)
in := bufio.NewReader(os.Stdin)
run(map[string]string{}, out, in, false)
out.Flush()
if len(buf.Bytes()) <= 0 {
t.Errorf("output size error: %d bytes", len(buf.Bytes()))
}
}
func TestMainWithWidthCreatesNonZeroOutput(t *testing.T) {
var buf bytes.Buffer
out := bufio.NewWriter(&buf)
in := bufio.NewReader(os.Stdin)
args := map[string]string{"width": "10", "height": "10"}
run(args, out, in, false)
out.Flush()
if len(buf.Bytes()) <= 0 {
t.Errorf("output size error: %d bytes", len(buf.Bytes()))
}
}
func TestMainWithJSONContainsCellsField(t *testing.T) {
var buf bytes.Buffer
out := bufio.NewWriter(&buf)
in := bufio.NewReader(os.Stdin)
args := map[string]string{"out": "json"}
run(args, out, in, false)
out.Flush()
var outputMap map[string]json.RawMessage
err := json.Unmarshal(buf.Bytes(), &outputMap)
if err != nil {
t.Errorf(err.Error())
}
if _, ok := outputMap["cells"]; !ok {
t.Errorf("maze output json does not contain 'cells' field")
}
}