-
-
Notifications
You must be signed in to change notification settings - Fork 872
/
dialogue_runner_test.dart
443 lines (405 loc) · 13.7 KB
/
dialogue_runner_test.dart
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import 'dart:async';
import 'package:jenny/jenny.dart';
import 'package:test/test.dart';
import 'test_scenario.dart';
import 'utils.dart';
void main() {
group('DialogueRunner', () {
test('plain dialogue', () async {
final yarn = YarnProject()
..strictCharacterNames = false
..parse(
'-------------\n'
'title: Hamlet\n'
'-------------\n'
"Bernardo: Who's there?\n"
'Francisco: Nay, answer me. Stand and unfold yourself.\n'
'Bernardo: Long live the King!\n'
'Francisco: Bernardo?\n'
'Bernardo: He\n'
'Francisco: You come most carefully upon your hour.\n'
"Bernardo: 'Tis now struck twelve. Get thee to bed, Francisco.\n"
"Francisco: For this relief much thanks. 'Tis bitter cold, "
'And I am sick at heart.\n'
'===\n',
);
final view = _RecordingDialogueView();
final dialogue = DialogueRunner(yarnProject: yarn, dialogueViews: [view]);
await dialogue.startDialogue('Hamlet');
expect(
view.events,
[
// ignore_for_file: prefer_adjacent_string_concatenation
'[*] onDialogueStart()',
'[*] onNodeStart(Node(Hamlet))',
"[*] onLineStart(DialogueLine(Bernardo: Who's there?))",
"[*] onLineFinish(DialogueLine(Bernardo: Who's there?))",
'[*] onLineStart(DialogueLine(Francisco: Nay, answer me. ' +
'Stand and unfold yourself.))',
'[*] onLineFinish(DialogueLine(Francisco: Nay, answer me. Stand ' +
'and unfold yourself.))',
'[*] onLineStart(DialogueLine(Bernardo: Long live the King!))',
'[*] onLineFinish(DialogueLine(Bernardo: Long live the King!))',
'[*] onLineStart(DialogueLine(Francisco: Bernardo?))',
'[*] onLineFinish(DialogueLine(Francisco: Bernardo?))',
'[*] onLineStart(DialogueLine(Bernardo: He))',
'[*] onLineFinish(DialogueLine(Bernardo: He))',
'[*] onLineStart(DialogueLine(Francisco: You come most carefully ' +
'upon your hour.))',
'[*] onLineFinish(DialogueLine(Francisco: You come most carefully ' +
'upon your hour.))',
"[*] onLineStart(DialogueLine(Bernardo: 'Tis now struck twelve. " +
'Get thee to bed, Francisco.))',
"[*] onLineFinish(DialogueLine(Bernardo: 'Tis now struck twelve. " +
'Get thee to bed, Francisco.))',
'[*] onLineStart(DialogueLine(Francisco: For this relief much ' +
"thanks. 'Tis bitter cold, And I am sick at heart.))",
'[*] onLineFinish(DialogueLine(Francisco: For this relief much ' +
"thanks. 'Tis bitter cold, And I am sick at heart.))",
'[*] onDialogueFinish()',
],
);
});
test('DialogueViews with delays', () async {
final yarn = YarnProject()
..strictCharacterNames = false
..parse('title: The Robot and the Mattress\n'
'---\n'
'Zem: Hello, robot\n'
'Marvin: Blah\n'
'===\n');
final events = <String>[];
final view1 = _DelayedDialogueView(
target: events,
name: 'A',
dialogueStartDelay: 0.1,
lineStartDelay: 0.1,
lineFinishDelay: 0.02,
);
final view2 = _DelayedDialogueView(
target: events,
name: 'B',
lineFinishDelay: 0.05,
);
final view3 = _DelayedDialogueView(
target: events,
name: 'C',
dialogueStartDelay: 0.25,
lineStartDelay: 0.15,
);
final dialogue = DialogueRunner(
yarnProject: yarn,
dialogueViews: [view1, view2, view3],
);
await dialogue.startDialogue('The Robot and the Mattress');
expect(events, [
'[A] onDialogueStart()',
'[B] onDialogueStart()',
'[C] onDialogueStart()',
'[A] onNodeStart(Node(The Robot and the Mattress))',
'[B] onNodeStart(Node(The Robot and the Mattress))',
'[C] onNodeStart(Node(The Robot and the Mattress))',
'[A] onLineStart(DialogueLine(Zem: Hello, robot))',
'[B] onLineStart(DialogueLine(Zem: Hello, robot))',
'[C] onLineStart(DialogueLine(Zem: Hello, robot))',
'[A] onLineFinish(DialogueLine(Zem: Hello, robot))',
'[B] onLineFinish(DialogueLine(Zem: Hello, robot))',
'[C] onLineFinish(DialogueLine(Zem: Hello, robot))',
'[A] onLineStart(DialogueLine(Marvin: Blah))',
'[B] onLineStart(DialogueLine(Marvin: Blah))',
'[C] onLineStart(DialogueLine(Marvin: Blah))',
'[A] onLineFinish(DialogueLine(Marvin: Blah))',
'[B] onLineFinish(DialogueLine(Marvin: Blah))',
'[C] onLineFinish(DialogueLine(Marvin: Blah))',
'[A] onDialogueFinish()',
'[B] onDialogueFinish()',
'[C] onDialogueFinish()',
]);
});
test('dialogue with choices', () async {
final yarn = YarnProject()
..parse('title: X\n---\n'
'-> Hi there\n'
'-> Howdy\n'
' Greetings to you too\n'
'-> Yo! <<if false>>\n'
'Kk-thx-bye\n'
'===\n');
final view = _RecordingDialogueView(choices: [1]);
final dialogue = DialogueRunner(yarnProject: yarn, dialogueViews: [view]);
await dialogue.startDialogue('X');
expect(
view.events,
[
'[*] onDialogueStart()',
'[*] onNodeStart(Node(X))',
'[*] onChoiceStart(DialogueChoice([Option(Hi there), ' +
'Option(Howdy), Option(Yo! #disabled)])) -> 1',
'[*] onChoiceFinish(Option(Howdy))',
'[*] onLineStart(DialogueLine(Greetings to you too))',
'[*] onLineFinish(DialogueLine(Greetings to you too))',
'[*] onLineStart(DialogueLine(Kk-thx-bye))',
'[*] onLineFinish(DialogueLine(Kk-thx-bye))',
'[*] onDialogueFinish()',
],
);
view.events.clear();
view.choices.add(0);
await dialogue.startDialogue('X');
expect(
view.events,
[
'[*] onDialogueStart()',
'[*] onNodeStart(Node(X))',
'[*] onChoiceStart(DialogueChoice([Option(Hi there), ' +
'Option(Howdy), Option(Yo! #disabled)])) -> 0',
'[*] onChoiceFinish(Option(Hi there))',
'[*] onLineStart(DialogueLine(Kk-thx-bye))',
'[*] onLineFinish(DialogueLine(Kk-thx-bye))',
'[*] onDialogueFinish()',
],
);
});
test('invalid dialogue choices', () async {
final yarn = YarnProject()
..parse('title:A\n---\n'
'-> Only one\n'
'-> Only two <<if false>>\n'
'===\n');
final view = _RecordingDialogueView(choices: [2, 1]);
final dialogue = DialogueRunner(yarnProject: yarn, dialogueViews: [view]);
await expectLater(
() => dialogue.startDialogue('A'),
hasDialogueError('Invalid option index chosen in a dialogue: 2'),
);
await expectLater(
() => dialogue.startDialogue('A'),
hasDialogueError(
'A dialogue view selected a disabled option: '
'Option(Only two #disabled)',
),
);
});
test('no decision-making views', () {
final yarn = YarnProject()..parse('title:A\n---\n-> One\n===\n');
final dialogue = DialogueRunner(
yarnProject: yarn,
dialogueViews: [_SimpleDialogueView(), _SimpleDialogueView()],
);
expect(
() => dialogue.startDialogue('A'),
hasDialogueError('No option selected in a DialogueChoice'),
);
});
test('dialogue view that makes immediate choice selection', () async {
final yarn = YarnProject()..parse('title:A\n---\n-> One\n===\n');
final view = _ImmediateChoiceDialogueView();
final dialogue = DialogueRunner(yarnProject: yarn, dialogueViews: [view]);
await dialogue.startDialogue('A');
expect(view.finished, true);
});
testScenario(
testName: 'Example.plan',
input: '''
title: Start
tags:
colorID: 0
position: 592,181
---
A: Hey, I'm a character in a script!
B: And I am too! You are talking to me!
-> What's going on
A: Why this is a demo of the script system!
B: And you're in it!
-> Um ok
A: How delightful!
B: What would you prefer to do next?
-> Leave
<<jump Leave>>
-> Learn more
<<jump LearnMore>>
===
title: Leave
tags:
colorID: 0
position: 387,487
---
A: Oh, goodbye!
B: You'll be back soon!
===
title: LearnMore
tags: rawText
colorID: 0
position: 763,472
---
A: HA HA HA
===''',
testPlan: '''
line: A: Hey, I'm a character in a script!
line: B: And I am too! You are talking to me!
option: What's going on
option: Um ok
select: 1
line: A: Why this is a demo of the script system!
line: B: And you're in it!
line: A: How delightful!
line: B: What would you prefer to do next?
option: Leave
option: Learn more
select: 1
line: A: Oh, goodbye!
line: B: You'll be back soon!
''',
);
testScenario(
testName: 'Compiler.plan',
input: r'''
<<declare $foo as Number>>
title: Start
---
// Compiler tests
This is a line!
<<if false>>
What what this is also a line!
<<endif>>
<<this is a custom command>>
<<set $foo to 1+2>>
<<if $foo is 3>>
Foo is 3!
<<elseif $foo is 4>>
Foo is 4!
<<else>>
Foo is something TOTALLY DIFFERENT.
<<endif>>
-> This is a shortcut option that you'll never see <<if false>>
Nice.
-> This is a different shortcut option
Sweet, but what about this?
-> It's ok
Cool.
-> Huh?
-> This is a shortcut option with no consequential text.
All done with the shortcut options!
===
''',
testPlan: '''
line: This is a line!
command: this is a custom command
line: Foo is 3!
option: This is a shortcut option that you'll never see [disabled]
option: This is a different shortcut option
option: This is a shortcut option with no consequential text.
select: 2
line: Sweet, but what about this?
option: It's ok
option: Huh?
select: 1
line: Cool.
line: All done with the shortcut options!
''',
commands: ['this'],
);
test('Dialogue runs node before finishing the previous one', () async {
final yarn = YarnProject()
..parse(
dedent('''
title: Start
---
First line
Second line
===
title: Other
---
Third line
===
'''),
);
final view = _RecordingDialogueView();
final dialogue = DialogueRunner(yarnProject: yarn, dialogueViews: [view]);
unawaited(dialogue.startDialogue('Start'));
expect(
() => dialogue.startDialogue('Other'),
hasDialogueError(
'Cannot run node "Other" because another node is currently running: '
'"Start"',
),
);
});
});
}
class _SimpleDialogueView extends DialogueView {}
class _RecordingDialogueView extends DialogueView {
_RecordingDialogueView({
List<String>? target,
String? name,
List<int>? choices,
}) : events = target ?? [],
name = name ?? '*',
choices = choices ?? [];
final List<String> events;
final String name;
final List<int> choices;
@override
void onDialogueStart() => _record('onDialogueStart()');
@override
void onDialogueFinish() => _record('onDialogueFinish()');
@override
void onNodeStart(Node node) => _record('onNodeStart($node)');
@override
FutureOr<bool> onLineStart(DialogueLine line) =>
_record('onLineStart($line)');
@override
void onLineFinish(DialogueLine line) => _record('onLineFinish($line)');
@override
Future<int> onChoiceStart(DialogueChoice choice) {
final selection = choices.isEmpty ? 0 : choices.removeAt(0);
_record('onChoiceStart($choice) -> $selection');
return Future<int>.value(selection);
}
@override
void onChoiceFinish(DialogueOption option) =>
_record('onChoiceFinish($option)');
bool _record(String event) {
events.add('[$name] $event');
return true;
}
}
class _DelayedDialogueView extends _RecordingDialogueView {
_DelayedDialogueView({
super.target,
super.name,
this.dialogueStartDelay = 0,
this.lineStartDelay = 0,
this.lineFinishDelay = 0,
});
final double dialogueStartDelay;
final double lineStartDelay;
final double lineFinishDelay;
@override
FutureOr<void> onDialogueStart() {
super.onDialogueStart();
if (dialogueStartDelay > 0) {
final delay = Duration(milliseconds: (dialogueStartDelay * 1000).toInt());
return Future.delayed(delay, () => null);
}
}
@override
Future<bool> onLineStart(DialogueLine line) {
super.onLineStart(line);
final delay = Duration(milliseconds: (lineStartDelay * 1000).toInt());
return Future<bool>.delayed(delay, () => true);
}
@override
Future<void> onLineFinish(DialogueLine line) {
super.onLineFinish(line);
final delay = Duration(milliseconds: (lineFinishDelay * 1000).toInt());
return Future.delayed(delay);
}
}
class _ImmediateChoiceDialogueView extends DialogueView {
bool finished = false;
@override
int onChoiceStart(DialogueChoice choice) => 0;
@override
void onDialogueFinish() => finished = true;
}