-
Notifications
You must be signed in to change notification settings - Fork 2
/
fake.go
102 lines (84 loc) · 3.69 KB
/
fake.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
// 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 "errors"
// FakeDialogueHandler implements DialogueHandler with minimal, do-nothing
// methods. This is useful both for testing, and for satisfying the
// DialogueHandler via embedding, e.g.:
//
// type MyHandler struct {
// FakeDialogueHandler
// }
// // MyHandler is only interested in Line and Options.
// func (m MyHandler) Line(line Line) error { ... }
// func (m MyHandler) Options(options []Option) (int, error) { ... }
// // All the other DialogueHandler methods provided by FakeDialogueHandler.
type FakeDialogueHandler struct{}
// NodeStart returns nil.
func (FakeDialogueHandler) NodeStart(string) error { return nil }
// PrepareForLines returns nil.
func (FakeDialogueHandler) PrepareForLines([]string) error { return nil }
// Line returns nil.
func (FakeDialogueHandler) Line(Line) error { return nil }
// Options returns the first option ID, or an error if there are no options.
func (FakeDialogueHandler) Options(options []Option) (int, error) {
if len(options) == 0 {
return 0, errors.New("no options delivered")
}
return options[0].ID, nil
}
// Command returns nil.
func (FakeDialogueHandler) Command(string) error { return nil }
// NodeComplete returns nil.
func (FakeDialogueHandler) NodeComplete(string) error { return nil }
// DialogueComplete returns nil.
func (FakeDialogueHandler) DialogueComplete() error { return nil }
// FakeAsyncDialogueHandler implements AsyncDialogueHandler with minimal
// methods that immediately continue the VM. This is useful both for testing,
// and for satisfying the AsyncDialogueHandler interface via embedding, e.g.:
//
// type MyHandler struct {
// FakeAsyncDialogueHandler
// }
// // MyHandler is only interested in Line and Options.
// func (m MyHandler) Line(line Line) { ... }
// func (m MyHandler) Options(options []Option) { ... }
// // All the other AsyncDialogueHandler methods provided by
// // FakeAsyncDialogueHandler.
//
// Note that FakeAsyncDialogueHandler needs a reference to the AsyncAdapter (in
// order to call Go or GoWithChoice).
type FakeAsyncDialogueHandler struct {
AsyncAdapter *AsyncAdapter
}
// NodeStart calls AsyncAdapter.Go.
func (f FakeAsyncDialogueHandler) NodeStart(string) { f.AsyncAdapter.Go() }
// PrepareForLines calls AsyncAdapter.Go.
func (f FakeAsyncDialogueHandler) PrepareForLines([]string) { f.AsyncAdapter.Go() }
// Line calls AsyncAdapter.Go.
func (f FakeAsyncDialogueHandler) Line(Line) { f.AsyncAdapter.Go() }
// Options calls AsyncAdapter.GoWithChoice with the ID of the first option,
// or AsyncAdapter.Abort if there are no options.
func (f FakeAsyncDialogueHandler) Options(options []Option) {
if len(options) == 0 {
f.AsyncAdapter.Abort(errors.New("no options delivered"))
}
f.AsyncAdapter.GoWithChoice(options[0].ID)
}
// Command calls AsyncAdapter.Go.
func (f FakeAsyncDialogueHandler) Command(string) { f.AsyncAdapter.Go() }
// NodeComplete calls AsyncAdapter.Go.
func (f FakeAsyncDialogueHandler) NodeComplete(string) { f.AsyncAdapter.Go() }
// DialogueComplete calls AsyncAdapter.Go.
func (f FakeAsyncDialogueHandler) DialogueComplete() { f.AsyncAdapter.Go() }