forked from dfabulich/choicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicktest.html
354 lines (318 loc) · 10.6 KB
/
quicktest.html
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
<!DOCTYPE html>
<html>
<head>
<title>Quicktest</title>
<script src=web/scene.js></script>
<script src=web/navigator.js></script>
<script src=web/util.js></script>
<script src=web/mygame/mygame.js></script>
<script src=editor/embeddable-autotester.js></script>
<script>
nav.setStartingStatsClone(stats);
function doneLoading() {
var loadingDiv = document.getElementById("loading");
if (loadingDiv) loadingDiv.parentNode.removeChild(loadingDiv);
}
function changeTitle() {}
knownScenes = [];
function addFile(name) {
for (var i = 0; i < knownScenes.length; i++) {
if (knownScenes[i] == name) return;
}
knownScenes.push(name);
}
var sceneName = nav.getStartupScene();
for (var i = 0; i < nav._sceneList.length; i++) {
addFile(nav._sceneList[i] + ".txt");
}
addFile("choicescript_stats.txt");
addFile("choicescript_upgrade.txt");
Scene.prototype.verifySceneFile = function webVerifySceneFile(name) {
addFile(name+".txt");
}
Scene.prototype.verifyImage = function webVerifyImage(fileName) {
var xhr = new XMLHttpRequest();
var url = "web/mygame/" + fileName;
xhr.open("GET", url, false);
try {
xhr.send();
} catch (e) {
throw new Error(this.lineMsg() + "File does not exist: " + fileName);
}
if (xhr.status && xhr.status != 200) {
throw new Error(this.lineMsg() + "File does not exist: " + fileName);
}
}
printFooter = function() {};
printShareLinks = function() {};
printLink = function() {};
printButton = function() {};
printImage = function() {};
showPassword = function() {};
achieve = function() {};
loginForm = function() {};
isRegistered = function() {return false;};
isRegisterAllowed = function() {return false;};
isRestorePurchasesSupported = function() {return false;};
isFullScreenAdvertisingSupported = function() {return false;};
areSaveSlotsSupported = function() {return false;};
function safeCall(obj, fn) {
if (obj) return fn.call(obj);
return fn.call();
}
function initStore() { return false; }
clearScreen = function clearScreen(code) {
code.call();
};
var oldLog = console.log;
saveCookie = function(callback) { if (callback) callback.call(); };
function printBody(msg, parent) {
if (msg === null || msg === undefined || msg === "") return;
if (!parent) parent = document.body;
if (msg == " ") {
// IE7 doesn't like innerHTML that's nothing but " "
parent.appendChild(document.createTextNode(" "));
return;
}
msg = (msg+"").replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/\[b\]/g, '<b>')
.replace(/\[\/b\]/g, '</b>')
.replace(/\[i\]/g, '<i>')
.replace(/\[\/i\]/g, '</i>');
var frag = document.createDocumentFragment();
temp = document.createElement('div');
temp.innerHTML = msg;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
parent.appendChild(frag);
}
function println() {}
console.log = function() {
var msg = arguments[0];
printBody(msg);
var br = document.createElement("br");
document.body.appendChild(br);
br.scrollIntoView();
oldLog.apply(this, arguments);
}
uncoveredScenes = [];
function testForInvalidCharacters(text) {
var lines = text.split(/\r?\n/);
var firstLine = lines[0];
// strip byte order mark
if (firstLine.charCodeAt(0) == 65279) lines[0] = firstLine.substring(1);
for (i = 0; i < lines.length; i++) {
var line = lines[i];
var invalidCharacter = line.match(/^(.*)\ufffd/);
if (invalidCharacter) throw new Error("line " + (i+1) + ": invalid character. Is this text Unicode?\n" + invalidCharacter[0]);
}
}
var success = true;
var titleIncluded, authorIncluded;
var gotoSceneLabels = {};
function loadScenes(sceneNum) {
var fileName = knownScenes[sceneNum];
var sceneName = fileName.replace(/.txt$/, "");
var url = "web/mygame/scenes/" + fileName;
xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
var skip = false;
var loadFailed = false;
try {
xhr.send();
} catch (x) {
if (/^choicescript_(stats|upgrade)$/.test(sceneName)) {
skip = true;
} else {
if (window.location.protocol == "file:" && /Chrome/.test(navigator.userAgent)) {
doneLoading();
console.log("We're sorry, Google Chrome has blocked ChoiceScript from functioning. (\"file:\" URLs cannot "+
"load files in Chrome.) ChoiceScript works just fine in Chrome, but only on a published website like "+
"choiceofgames.com. For the time being, please try another browser like Mozilla Firefox.");
success = false;
return;
}
loadFailed = true;
}
}
if (xhr.status && xhr.status != 200) {
loadFailed = true;
}
if (loadFailed) {
if (/^choicescript_(stats|upgrade)$/.test(sceneName)) {
skip = true;
} else {
doneLoading();
console.log("QUICKTEST FAILED");
console.log("ERROR: couldn't open " + url);
success = false;
return;
}
}
if (!skip) {
function parseSceneList(lines, lineNum) {
var nextIndent = null;
var scenes = [];
var purchases = {};
var line;
while(typeof (line = lines[++lineNum]) != "undefined") {
if (!line.trim()) continue;
var indent = /^(\s*)/.exec(line)[1].length;
if (nextIndent === null || nextIndent === undefined) {
// initialize nextIndent with whatever indentation the line turns out to be
// ...unless it's not indented at all
if (indent === 0) throw new Error("invalid scene_list indent, expected at least one row");
this.indent = nextIndent = indent;
}
if (indent === 0) break;
if (indent != this.indent) {
// all scenes are supposed to be at the same indentation level
throw new Error("invalid scene_list indent, expected "+this.indent+", was " + indent);
}
line = line.trim();
var purchaseMatch = /^\$(\w*)\s+(.*)/.exec(line);
if (purchaseMatch) {
line = purchaseMatch[2];
var product = purchaseMatch[1].trim() || "adfree";
purchases[line] = product;
}
if (!scenes.length && "startup" != line) scenes.push("startup");
scenes.push(line);
}
return {scenes:scenes, purchases:purchases, lineNum:lineNum-1};
}
try {
var sceneLines = xhr.responseText.split("\n");
for (var j = 0; j < sceneLines.length; j++) {
var line = sceneLines[j];
var words;
var result = /^\s*\*(\w+)(.*)/.exec(line);
if (!result) continue;
var command = result[1].toLowerCase();
var data = trim(result[2]);
if (command == "goto_scene" || command == "gosub_scene") {
if (data === null) data = "";
words = data.split(/ /);
if (words[0] !== "") {
addFile(words[0]+".txt");
}
if (words.length > 1) {
if (!gotoSceneLabels[words[0]]) gotoSceneLabels[words[0]] = [];
gotoSceneLabels[words[0]].push({origin:sceneName, originLine:j, label:words[1]});
}
} else if (command == "save_game") {
if (data !== null) addFile(data+".txt");
} else if (command == "scene_list" && sceneNum === 0) {
var sceneList = parseSceneList(sceneLines, j);
j = sceneList.lineNum;
for (var k = 0; k < sceneList.scenes.length; k++) {
addFile(sceneList.scenes[k]+".txt");
}
}
}
} catch (e) {
console.log("Error parsing "+sceneName+" line "+(j+1)+ "; we'll flag the error in detail later\n");
console.log(e);
}
}
if (++sceneNum < knownScenes.length) return setTimeout(function(){loadScenes(sceneNum)}, 0);
testScene(0);
}
function testScene(sceneNum) {
var fileName = knownScenes[sceneNum];
var sceneName = fileName.replace(/.txt$/, "");
var url = "web/mygame/scenes/" + fileName;
xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
var skip = false;
var loadFailed = false;
try {
xhr.send();
} catch (x) {
if (/^choicescript_(stats|upgrade)$/.test(sceneName)) {
skip = true;
} else {
if (window.location.protocol == "file:" && /Chrome/.test(navigator.userAgent)) {
doneLoading();
console.log("We're sorry, Google Chrome has blocked ChoiceScript from functioning. (\"file:\" URLs cannot "+
"load files in Chrome.) ChoiceScript works just fine in Chrome, but only on a published website like "+
"choiceofgames.com. For the time being, please try another browser like Mozilla Firefox.");
success = false;
return;
}
loadFailed = true;
}
}
if (xhr.status && xhr.status != 200) {
loadFailed = true;
}
if (loadFailed) {
if (/^choicescript_(stats|upgrade)$/.test(sceneName)) {
skip = true;
} else {
doneLoading();
console.log("QUICKTEST FAILED");
console.log("ERROR: couldn't open " + url);
success = false;
return;
}
}
if (!skip) {
console.log(sceneName);
try {
testForInvalidCharacters(xhr.responseText);
var uncovered = autotester(xhr.responseText, nav, sceneName, gotoSceneLabels[sceneName])[1];
if (uncovered) {
uncoveredScenes.push({name:sceneName, lines:uncovered});
}
if (sceneNum === 0) {
titleIncluded = /\*title /m.test(xhr.responseText);
authorIncluded = /^\*author /m.test(xhr.responseText);
}
} catch (x) {
console.log("QUICKTEST FAILED");
console.log(x);
console.error(x.stack);
success = false;
return;
}
}
if (++sceneNum < knownScenes.length) return setTimeout(function(){testScene(sceneNum)}, 0);
if (success) {
var allLinesTested = true;
for (var i = 0; i < uncoveredScenes.length; i++) {
allLinesTested = false;
var uncoveredScene = uncoveredScenes[i];
for (var j = 0; j < uncoveredScene.lines.length; j++) {
console.log("UNTESTED " + uncoveredScene.name + " " + uncoveredScene.lines[j]);
};
}
(function() {
if (nav.achievementList && nav.achievementList.length) {
for (var i = 0; i < nav.achievementList.length; i++) {
var name = nav.achievementList[i];
if (!nav.achieved[name]) {
console.log("UNUSED achievement: " + name);
}
}
}
})();
if (!allLinesTested) console.log("SOME LINES UNTESTED");
if (!titleIncluded) console.log("MISSING *TITLE COMMAND");
if (!authorIncluded) console.log("MISSING *AUTHOR COMMAND");
console.log("QUICKTEST PASSED");
}
}
</script>
</head>
<body>
<div id="loading">Loading...</div>
<script>
loadScenes(0);
</script>
</body>
</html>