jsapi.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. const [testfile, ...options] = WScript.Arguments;
  6. if (!testfile) {
  7. throw new Error("Missing testfile");
  8. }
  9. const verbose = options.indexOf("-verbose") !== -1;
  10. const self = this;
  11. const setTimeout = WScript.SetTimeout;
  12. const clearTimeout = WScript.ClearTimeout;
  13. WScript.Flag(`-wasmMaxTableSize:${(Math.pow(2,32)-1)|0}`);
  14. self.addEventListener = function() {};
  15. if (!WebAssembly.Global) {
  16. // Shim WebAssembly.Global so the tests fails, but don't crash
  17. WebAssembly.Global = class {}
  18. }
  19. class ConsoleTestEnvironment {
  20. constructor() {
  21. this.all_loaded = false;
  22. this.on_loaded = null;
  23. }
  24. on_tests_ready() {
  25. runTests();
  26. this.all_loaded = true;
  27. this.on_loaded();
  28. }
  29. // Invoked after setup() has been called to notify the test environment
  30. // of changes to the test harness properties.
  31. on_new_harness_properties(properties) {}
  32. // Should return a new unique default test name.
  33. next_default_test_name() {
  34. return this.nextName = (this.nextName | 0) + 1;
  35. }
  36. // Should return the test harness timeout duration in milliseconds.
  37. test_timeout() {
  38. return null;
  39. }
  40. add_on_loaded_callback(fn) {
  41. this.on_loaded = fn;
  42. }
  43. // Should return the global scope object.
  44. global_scope() {
  45. return self;
  46. }
  47. }
  48. var environment;
  49. function chakra_create_test_environment() {
  50. if (!environment) {
  51. environment = new ConsoleTestEnvironment();
  52. }
  53. return environment;
  54. }
  55. WScript.LoadScriptFile("testsuite/harness/sync_index.js");
  56. const script = read(testfile);
  57. const re = /\/\/ META: script=(\/wasm\/jsapi\/)?(.+\.js)/gi;
  58. const mapping = {
  59. "wasm-constants.js": "harness/wasm-constants.js",
  60. "wasm-module-builder.js": "harness/wasm-module-builder.js",
  61. };
  62. let m;
  63. do {
  64. m = re.exec(script);
  65. if (m) {
  66. const isLib = !!m[1];
  67. const scriptToImport = m[2];
  68. if (isLib) {
  69. if (scriptToImport in mapping) {
  70. WScript.LoadScriptFile(`testsuite/${mapping[scriptToImport]}`);
  71. } else {
  72. WScript.LoadScriptFile(`testsuite/js-api/${scriptToImport}`);
  73. }
  74. } else {
  75. WScript.LoadScriptFile(
  76. testfile
  77. .split("/")
  78. .slice(0, -1)
  79. .concat(scriptToImport)
  80. .join("/"));
  81. }
  82. }
  83. } while (m);
  84. WScript.LoadScript(read("testsuite/harness/testharness.js")
  85. // Use our text environment
  86. .replace(" = create_test_environment", " = chakra_create_test_environment"));
  87. function reportResult(tests, harness_status) {
  88. const status_text_harness = {
  89. [harness_status.OK]: "OK",
  90. [harness_status.ERROR]: "Error",
  91. [harness_status.TIMEOUT]: "Timeout",
  92. };
  93. const firstTest = tests[0] || {};
  94. const status_text = {
  95. [firstTest.PASS]: "Pass",
  96. [firstTest.FAIL]: "Fail",
  97. [firstTest.TIMEOUT]: "Timeout",
  98. [firstTest.NOTRUN]: "Not Run",
  99. };
  100. const status_number = tests.reduce((all, test) => {
  101. var status = status_text[test.status];
  102. all[status] = (all[status]|0) + 1;
  103. return all;
  104. }, {});
  105. function get_assertion(test) {
  106. if (test.properties.hasOwnProperty("assert")) {
  107. if (Array.isArray(test.properties.assert)) {
  108. return test.properties.assert.join(" ");
  109. }
  110. return test.properties.assert;
  111. }
  112. return "";
  113. }
  114. const testsReport = tests.map(test => {
  115. const stack = verbose ? test.stack : "";
  116. return `${status_text[test.status]} ${test.name} ${get_assertion(test)} ${test.message || ""}${stack ? "\n" + stack : stack}`;
  117. });
  118. console.log(`Harness Status: ${status_text_harness[harness_status.status]}
  119. Found ${tests.length} tests: ${Object.keys(status_number).map(key => `${key} = ${status_number[key]}`).join(" ")}
  120. ${testsReport.join("\n")}`);
  121. }
  122. function runTests() {
  123. add_completion_callback((...args) => {
  124. try {
  125. reportResult(...args);
  126. } catch (e) {
  127. console.log(e);
  128. }
  129. });
  130. WScript.LoadScriptFile(testfile);
  131. }