-
Notifications
You must be signed in to change notification settings - Fork 2
/
testplan.go
185 lines (171 loc) · 4.91 KB
/
testplan.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2021 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package yarn
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// TestPlan implements test plans. A test plan is a dialogue handler that
// expects specific lines and options from the dialogue system.
type TestPlan struct {
StringTable *StringTable
Steps []TestStep
Step int
dialogueCompleted bool
FakeDialogueHandler // implements remaining methods
}
// LoadTestPlanFile is a convenient function for loading a test plan given a
// file path.
func LoadTestPlanFile(testPlanPath string) (*TestPlan, error) {
tpf, err := os.Open(testPlanPath)
if err != nil {
return nil, fmt.Errorf("opening testplan file: %w", err)
}
defer tpf.Close()
tp, err := ReadTestPlan(tpf)
if err != nil {
return nil, fmt.Errorf("reading testplan file: %w", err)
}
return tp, nil
}
// ReadTestPlan reads a testplan from an io.Reader into a TestPlan.
func ReadTestPlan(r io.Reader) (*TestPlan, error) {
var tp TestPlan
sc := bufio.NewScanner(r)
for sc.Scan() {
txt := strings.TrimSpace(sc.Text())
if txt == "" || strings.HasPrefix(txt, "#") {
// Skip blanks and comments
continue
}
if strings.HasPrefix(txt, "stop") {
// Superfluous stop at end of file
break
}
tok := strings.SplitN(txt, ":", 2)
if len(tok) < 2 {
return nil, fmt.Errorf("malformed step %q", txt)
}
tp.Steps = append(tp.Steps, TestStep{
Type: strings.TrimSpace(tok[0]),
Contents: strings.TrimSpace(tok[1]),
})
}
if err := sc.Err(); err != nil {
return nil, err
}
return &tp, nil
}
// TestStep is a step in a test plan.
type TestStep struct {
Type string
Contents string
}
func (s TestStep) String() string { return s.Type + ": " + s.Contents }
// Complete checks if the test plan was completed.
func (p *TestPlan) Complete() error {
if p.Step != len(p.Steps) {
return fmt.Errorf("on step %d %v", p.Step, p.Steps[p.Step])
}
if !p.dialogueCompleted {
return errors.New("did not receive DialogueComplete")
}
return nil
}
// Line checks that the line matches the one expected by the plan.
func (p *TestPlan) Line(line Line) error {
if p.Step >= len(p.Steps) {
return errors.New("next step after end")
}
step := p.Steps[p.Step]
if step.Type != "line" {
return fmt.Errorf("testplan got line, want %q", step.Type)
}
p.Step++
text, err := p.StringTable.Render(line)
if err != nil {
return err
}
if text.String() != step.Contents {
return fmt.Errorf("testplan got line %q, want %q", text, step.Contents)
}
return nil
}
// Options checks that the options match those expected by the plan, then
// selects the option specified in the plan.
func (p *TestPlan) Options(opts []Option) (int, error) {
for _, opt := range opts {
if p.Step >= len(p.Steps) {
return 0, errors.New("next testplan step after end")
}
step := p.Steps[p.Step]
if step.Type != "option" {
return 0, fmt.Errorf("testplan got option, want %q", step.Type)
}
p.Step++
text, err := p.StringTable.Render(opt.Line)
if err != nil {
return 0, err
}
disabled := strings.HasSuffix(step.Contents, " [disabled]")
if disabled {
step.Contents = strings.TrimSuffix(step.Contents, " [disabled]")
}
if text.String() != step.Contents {
return 0, fmt.Errorf("testplan got option line %q, want %q", text, step.Contents)
}
if got, want := opt.IsAvailable, !disabled; got != want {
return 0, fmt.Errorf("testplan got option %q IsAvailable %t, want %t", text, got, want)
}
}
// Next step should be a select
if p.Step >= len(p.Steps) {
return 0, errors.New("next testplan step after end")
}
step := p.Steps[p.Step]
if step.Type != "select" {
return 0, fmt.Errorf("testplan got select, want %q", step.Type)
}
p.Step++
n, err := strconv.Atoi(step.Contents)
if err != nil {
return 0, fmt.Errorf("converting testplan step to int: %w", err)
}
return n - 1, nil
}
// Command handles the command... somehow.
func (p *TestPlan) Command(command string) error {
if p.Step >= len(p.Steps) {
return errors.New("next testplan step after end")
}
step := p.Steps[p.Step]
if step.Type != "command" {
return fmt.Errorf("testplan got command, want %q", step.Type)
}
p.Step++
if command != step.Contents {
return fmt.Errorf("testplan got command %q, want %q", command, step.Contents)
}
return nil
}
// DialogueComplete records the event in p.DialogueCompleted.
func (p *TestPlan) DialogueComplete() error {
p.dialogueCompleted = true
return nil
}