comparison mupdf-source/thirdparty/mujs/tools/test262-harness.js @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * Runs one test file from the ES5 test suite test-262
3 * Usage: mujs <this-file> [-s ] [-f] [-l file1.js -l ...] suit-root test-file
4 * -s: print test source on failure
5 * -f: print full paths/stacktraces if possible
6 * -l: load a js file after the harness and before the test (to override things)
7 *
8 * If there are errors, print them and exits with code 1, else exit code is 0.
9 *
10 * The test suite is at: https://github.com/tc39/test262.git
11 * The ES5 suite is at branch "es5-tests"
12 *
13 * - The test suite throws on any error, possibly with info at ex.message .
14 * - Some tests make irreversible changes to global attrubutes, therefore it's
15 * required to run each test file in a new mujs instance.
16 */
17
18 (function(global) {
19 "use strict";
20
21 // clean the global environment
22 var mujs = {};
23
24 ["gc", "load", "compile", "print", "write", "read", "readline", "quit", "scriptArgs"]
25 .forEach(function(a) {
26 mujs[a] = global[a];
27 delete global[a];
28 });
29
30 // restore the original Error.toString behavior - it's being tested too
31 Error.prototype.toString = function() {
32 return this.name + ': ' + this.message;
33 }
34
35 function die_usage(str) {
36 if (str)
37 mujs.print(str);
38 mujs.print("Usage: mujs <this-file> [-f] [-l file1.js -l ...] suit-root test-file");
39 mujs.quit(1);
40 }
41
42 // our file loader
43 function load(str, as_filename) {
44 try {
45 var runtime_err = false;
46 var compiled = mujs.compile(str, as_filename);
47 runtime_err = true;
48 compiled();
49 return false;
50 } catch (e) {
51 return {err: e, runtime: runtime_err};
52 }
53 }
54
55 var args = mujs.scriptArgs;
56 var full_mode = false;
57 var print_src = false;
58 var overrides = [];
59 while ((""+args[0])[0] == "-") {
60 switch (args[0]) {
61 case "-f": full_mode = true;
62 break;
63 case "-s": print_src = true;
64 break;
65 case "-l": args.shift();
66 overrides.push(args[0]);
67 break;
68 default: die_usage("Unknown option " + args[0]);
69 }
70 args.shift();
71 }
72 if (args.length != 2)
73 die_usage("Exactly 2 paths are expected");
74 var root_path = args[0];
75 var test_path = args[1];
76
77 // load suite utils
78 ["sta.js", "testBuiltInObject.js", "testIntl.js"]
79 .forEach(function(u) {
80 var path = root_path + "/test/harness/" + u;
81 var as_file = full_mode ? path : "test/harness/" + u;
82 var err = load(mujs.read(path), as_file);
83 if (err) throw (err.err);
84 });
85
86 // load user overrides (e.g. reduced getPrecision), with a global mujs
87 if (overrides.length) {
88 global.mujs = mujs
89 overrides.forEach(function(f) {
90 var err = load(mujs.read(f), f);
91 if (err) throw (err.err);
92 });
93 delete global.mujs;
94 }
95
96 // the actual test
97 var source = mujs.read(test_path);
98 var negative = !!source.match(/@negative/);
99 if (negative)
100 var neg_str = (source.match(/@negative (.*)/) || [])[1];
101 var as_file = test_path;
102 if (!full_mode) {
103 as_file = test_path.replace(/\\/g, "/");
104 var sub = as_file.indexOf("/suite/");
105 if (sub >= 0)
106 as_file = "test" + as_file.substring(sub);
107 }
108
109 var result = load(mujs.read(test_path), as_file);
110 if (!!result == negative) {
111 // The docs don't really help about matching str, but this covers all cases
112 if (neg_str)
113 var err_for_match = /NotEarlyError/.test(neg_str) ? result.err.message : result.err.name;
114 if (!negative || !neg_str || RegExp(neg_str).exec(err_for_match))
115 mujs.quit(0);
116 }
117
118 // failed
119 // FIXME: @description can span lines. E.g. test/suite/bestPractice/Sbp_A3_T2.js
120 var desc = source.match(/@description (.*)/);
121 var info = "[File] " + as_file +
122 (desc ? "\n[Desc] " + desc[1] : "") +
123 "\n";
124
125 if (result) {
126 var err = result.err;
127 var msg = !neg_str ? err : "[Mismatch @negative " + neg_str + "]" + "\n " + err;
128
129 info += (result.runtime ? "[run] " : "[load] ") + msg;
130 if (err && err.stackTrace && (result.runtime || full_mode)) {
131 if (full_mode) {
132 info += err.stackTrace;
133 } else {
134 // trim the internal loader from the trace
135 var internal = err.stackTrace.indexOf("\n" + load("mujs_blahblah()").err.stackTrace.trim().split("\n")[1]);
136 if (internal >= 0)
137 info += err.stackTrace.substring(0, internal);
138 else
139 info += err.stackTrace;
140 }
141 }
142 } else {
143 info += "[run] [Error expected but none thrown]";
144 }
145
146 if (print_src)
147 info += "\n[Source]\n" + source;
148
149 mujs.print(info);
150 mujs.quit(1);
151
152 })(this)