-
Notifications
You must be signed in to change notification settings - Fork 10
/
verify.js
145 lines (124 loc) · 3.38 KB
/
verify.js
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
'use strict';
var xorshift = require('xorshift');
var Engine = require('./engine');
var Console = require('./console');
var Scanner = require('./scanner');
var OutlineLexer = require('./outline-lexer');
var InlineLexer = require('./inline-lexer');
var Parser = require('./parser');
var Story = require('./story');
var Path = require('./path');
var grammar = require('./grammar');
var link = require('./link');
module.exports = verify;
function verify(kni, trans, handler, kniscript) {
var lines = trans.split('\n');
// filter the transcript for given answers
var answers = [];
for (const line of lines) {
if (line.lastIndexOf('>', 0) === 0) {
answers.push(line.slice(1).trim());
}
}
var path = Path.start();
var base = [];
// build a story from the kni
var story = new Story();
var p = new Parser(grammar.start(story, path, base));
var il = new InlineLexer(p);
var ol = new OutlineLexer(il);
var s = new Scanner(ol, kniscript);
s.next(kni);
s.return();
link(story);
if (story.errors.length) {
var errors = '';
for (const err of story.errors) {
errors += err + '\n';
}
if (errors === trans) {
return {
pass: true,
expected: 'errors',
actual: 'errors',
};
}
for (const err of story.errors) {
console.error(err);
}
return {
pass: false,
expected: trans,
actual: errors,
};
}
var states = story.states;
// TODO support alternate seeds
var seed = 0;
// I rolled 4d64k this morning, for kni.js
var randomer = new xorshift.constructor([
37615 ^ seed,
54552 ^ seed,
59156 ^ seed,
24695 ^ seed
]);
var writer = new StringWriter();
var render = new Console(writer);
var readline = new FakeReadline(writer, answers);
var engine = new Engine({
story: states,
start: 'start',
handler: handler,
render: render,
dialog: readline,
randomer: randomer
});
readline.engine = engine;
engine.reset();
var expected = trans.trim();
var actual = writer.string.trim();
return {
pass: expected === actual,
expected: expected,
actual: actual
};
}
class FakeReadline {
constructor(writer, answers) {
this.writer = writer;
this.answers = answers;
this.engine = null;
this.history = [];
Object.seal(this);
}
ask(_question) {
var answer = this.answers.shift();
if (answer == null) {
return;
}
this.writer.write(('> ' + answer).trim() + '\n');
if (answer === 'quit') {
// noop
} else if (answer === 'replay') {
this.writer.write('\n');
this.engine.resume(this.engine.waypoint);
} else if (answer === 'back') {
this.writer.write('\n');
this.engine.waypoint = this.history.pop();
this.engine.resume(this.engine.waypoint);
} else {
this.history.push(this.engine.waypoint);
this.engine.answer(answer);
}
}
close() {
}
}
class StringWriter {
constructor() {
this.string = '';
}
write(string) {
this.string += string;
}
}