-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
142 lines (116 loc) · 3.15 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Integration tests :)
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/skx/cpmulator/consoleout"
"github.com/skx/cpmulator/cpm"
)
// TestDriveChange ensures the drive-letter changes after
// changing drives.
func TestDriveChange(t *testing.T) {
obj, err := cpm.New(cpm.WithOutputDriver("logger"))
if err != nil {
t.Fatalf("Create CP/M failed")
}
// Load the CCP binary - resetting RAM in the process.
err = obj.LoadCCP()
if err != nil {
t.Fatalf("load CCP failed")
}
obj.SetDrives(false)
obj.StuffText("C:\r\nEXIT\r\n")
// Run it
err = obj.Execute([]string{})
if err != nil && err != cpm.ErrHalt {
t.Fatalf("failed to run: %s", err)
}
// Get our output handle
helper := obj.GetOutputDriver()
l, ok := helper.(*consoleout.OutputLoggingDriver)
if !ok {
t.Fatalf("failed to cast output driver")
}
// Get output written to the screen, and remove newlines
out := l.GetOutput()
out = strings.ReplaceAll(out, "\n", "")
out = strings.ReplaceAll(out, "\r", "")
if out != `A>C>C>` {
t.Fatalf("unexpected output '%v'", out)
}
// Reset the text - confirm it is now empty
l.Reset()
if l.GetOutput() != "" {
t.Fatalf("resetting our history didn't work")
}
}
// TestReadWriteRand invokes our help-samples to read/write
// records - via the external API.
func TestReadWriteRand(t *testing.T) {
obj, err := cpm.New()
if err != nil {
t.Fatalf("Create CP/M failed")
}
// Load the CCP binary - resetting RAM in the process.
err = obj.LoadCCP()
if err != nil {
t.Fatalf("load CCP failed")
}
obj.SetDrives(false)
obj.SetDrivePath("A", "samples/")
obj.StuffText("WRITE foo\nREAD foo\nEXIT\n")
// Run it
err = obj.Execute([]string{})
if err != nil && err != cpm.ErrHalt {
t.Fatalf("failed to run: %s", err)
}
// Remove the generated file
os.Remove(filepath.Join("samples", "FOO"))
}
// TestCompleteLighthouse plays our Lighthouse game, to completion.
//
// It uses the fast/hacky solution rather than the slow/normal/real one
// just to cut down on the scripting.
//
// However it is a great test to see that things work as expected.
func TestCompleteLighthouse(t *testing.T) {
obj, err := cpm.New(cpm.WithOutputDriver("logger"))
if err != nil {
t.Fatalf("Create CP/M failed")
}
// Load the CCP binary - resetting RAM in the process.
err = obj.LoadCCP()
if err != nil {
t.Fatalf("load CCP failed")
}
obj.SetDrives(false)
obj.SetDrivePath("A", "dist/")
obj.StuffText("LIHOUSE\nAAAA\ndown\nEXAMINE DESK\nTAKE METEOR\nUP\n\nn\nQUIT\n")
// Run it
err = obj.Execute([]string{})
if err != nil && err != cpm.ErrHalt {
t.Fatalf("failed to run: %s", err)
}
// Get our output handle
helper := obj.GetOutputDriver()
l, ok := helper.(*consoleout.OutputLoggingDriver)
if !ok {
t.Fatalf("failed to cast output driver")
}
// Get the text written to the screen
out := l.GetOutput()
// Ensure the game was completed - easy path.
if !strings.Contains(out, "Congratulations") {
t.Fatalf("failed to win")
}
if !strings.Contains(out, "You won") {
t.Fatalf("failed to win")
}
// Reset the text - confirm it is now empty
l.Reset()
if l.GetOutput() != "" {
t.Fatalf("resetting our history didn't work")
}
}