test-js.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonValue.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/LogStream.h>
  29. #include <LibCore/File.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Lexer.h>
  32. #include <LibJS/Parser.h>
  33. #include <LibJS/Runtime/Array.h>
  34. #include <LibJS/Runtime/GlobalObject.h>
  35. #include <LibJS/Runtime/MarkedValueList.h>
  36. #include <sys/time.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #define TOP_LEVEL_TEST_NAME "__$$TOP_LEVEL$$__"
  40. // FIXME: Will eventually not be necessary when all tests are converted
  41. Vector<String> tests_to_run = {
  42. "builtins/Proxy/Proxy.js",
  43. "builtins/Proxy/Proxy.handler-apply.js",
  44. "builtins/Proxy/Proxy.handler-construct.js",
  45. "builtins/Proxy/Proxy.handler-defineProperty.js",
  46. "builtins/Proxy/Proxy.handler-deleteProperty.js",
  47. "builtins/Proxy/Proxy.handler-get.js",
  48. "builtins/Proxy/Proxy.handler-getOwnPropertyDescriptor.js",
  49. "builtins/Proxy/Proxy.handler-getPrototypeOf.js",
  50. "builtins/Proxy/Proxy.handler-has.js",
  51. "builtins/Proxy/Proxy.handler-isExtensible.js",
  52. "builtins/Proxy/Proxy.handler-preventExtensions.js",
  53. "builtins/Proxy/Proxy.handler-set.js",
  54. "builtins/Proxy/Proxy.handler-setPrototypeOf.js",
  55. "builtins/Reflect/Reflect.apply.js",
  56. "builtins/Reflect/Reflect.construct.js",
  57. "builtins/Reflect/Reflect.defineProperty.js",
  58. "builtins/Reflect/Reflect.deleteProperty.js",
  59. "builtins/Reflect/Reflect.get.js",
  60. "builtins/Reflect/Reflect.getOwnPropertyDescriptor.js",
  61. "builtins/Reflect/Reflect.getPrototypeOf.js",
  62. "builtins/Reflect/Reflect.has.js",
  63. "builtins/Reflect/Reflect.isExtensible.js",
  64. "builtins/Reflect/Reflect.ownKeys.js",
  65. "builtins/Reflect/Reflect.preventExtensions.js",
  66. "builtins/Reflect/Reflect.set.js",
  67. "builtins/Reflect/Reflect.setPrototypeOf.js",
  68. "add-values-to-primitive.js",
  69. "automatic-semicolon-insertion.js",
  70. "comments-basic.js",
  71. "debugger-statement.js",
  72. "empty-statements.js",
  73. "exception-ReferenceError.js",
  74. "exponentiation-basic.js",
  75. "indexed-access-string-object.js",
  76. "invalid-lhs-in-assignment.js",
  77. "let-scoping.js",
  78. "new-expression.js",
  79. "numeric-literals-basic.js",
  80. "object-getter-setter-shorthand.js",
  81. "object-method-shorthand.js",
  82. "object-spread.js",
  83. "tagged-template-literals.js",
  84. "test-common-tests.js",
  85. "switch-basic.js",
  86. "update-expression-on-member-expression.js",
  87. };
  88. struct FileTest {
  89. String name;
  90. bool passed;
  91. };
  92. struct FileSuite {
  93. String name;
  94. int passed { 0 };
  95. int failed { 0 };
  96. Vector<FileTest> tests {};
  97. };
  98. struct TestError {
  99. JS::Parser::Error error;
  100. String hint;
  101. };
  102. struct FileResults {
  103. String file;
  104. Optional<TestError> error {};
  105. int passed { 0 };
  106. int failed { 0 };
  107. Vector<FileSuite> suites {};
  108. };
  109. struct Results {
  110. Vector<FileResults> file_results {};
  111. };
  112. Optional<TestError> parse_and_run_file(JS::Interpreter& interpreter, const String& path)
  113. {
  114. auto file = Core::File::construct(path);
  115. auto result = file->open(Core::IODevice::ReadOnly);
  116. ASSERT(result);
  117. auto contents = file->read_all();
  118. String test_file_string(reinterpret_cast<const char*>(contents.data()), contents.size());
  119. file->close();
  120. auto parser = JS::Parser(JS::Lexer(test_file_string));
  121. auto program = parser.parse_program();
  122. if (parser.has_errors()) {
  123. auto error = parser.errors()[0];
  124. return TestError { error, error.source_location_hint(test_file_string) };
  125. } else {
  126. interpreter.run(interpreter.global_object(), *program);
  127. }
  128. return {};
  129. }
  130. FileResults run_test(const String& path, const String& test_root)
  131. {
  132. auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
  133. if (parse_and_run_file(*interpreter, String::format("%s/test-common.js", test_root.characters())).has_value()) {
  134. dbg() << "test-common.js failed to parse";
  135. exit(1);
  136. }
  137. auto source_file_result = parse_and_run_file(*interpreter, String::format("%s/%s", test_root.characters(), path.characters()));
  138. if (source_file_result.has_value())
  139. return { path, source_file_result };
  140. // Print any output
  141. // FIXME: Should be printed to stdout in a nice format
  142. auto& arr = interpreter->get_variable("__UserOutput__", interpreter->global_object()).as_array();
  143. for (auto& entry : arr.indexed_properties()) {
  144. dbg() << "OUTPUT: " << entry.value_and_attributes(&interpreter->global_object()).value.to_string_without_side_effects();
  145. }
  146. // FIXME: This is _so_ scuffed
  147. auto result = interpreter->get_variable("__TestResults__", interpreter->global_object());
  148. auto json_object = interpreter->get_variable("JSON", interpreter->global_object());
  149. auto stringify = json_object.as_object().get("stringify");
  150. JS::MarkedValueList arguments(interpreter->heap());
  151. arguments.append(result);
  152. auto json_string = interpreter->call(stringify.as_function(), interpreter->this_value(interpreter->global_object()), move(arguments)).to_string(*interpreter);
  153. auto json_result = JsonValue::from_string(json_string);
  154. if (!json_result.has_value()) {
  155. dbg() << "BAD JSON:";
  156. dbg() << json_string;
  157. return {};
  158. }
  159. auto json = json_result.value();
  160. FileResults results { path };
  161. json.as_object().for_each_member([&](const String& property, const JsonValue& value) {
  162. FileSuite suite { property };
  163. value.as_object().for_each_member([&](const String& property1, const JsonValue& value1) {
  164. FileTest test { property1, false };
  165. if (value1.is_object()) {
  166. auto obj = value1.as_object();
  167. if (obj.has("passed")) {
  168. auto passed = obj.get("passed");
  169. test.passed = passed.is_bool() && passed.as_bool();
  170. }
  171. }
  172. if (test.passed) {
  173. suite.passed++;
  174. } else {
  175. suite.failed++;
  176. }
  177. suite.tests.append(test);
  178. });
  179. if (suite.failed) {
  180. results.failed++;
  181. } else {
  182. results.passed++;
  183. }
  184. results.suites.append(suite);
  185. });
  186. return results;
  187. }
  188. bool skip_test(char* test_name)
  189. {
  190. return !strcmp(test_name, "test-common.js") || !strcmp(test_name, "run_tests.sh");
  191. }
  192. enum Modifier {
  193. BG_RED,
  194. BG_GREEN,
  195. FG_RED,
  196. FG_GREEN,
  197. FG_GRAY,
  198. FG_BLACK,
  199. FG_BOLD,
  200. CLEAR,
  201. };
  202. void print_modifiers(Vector<Modifier> modifiers)
  203. {
  204. for (auto& modifier : modifiers) {
  205. auto code = [&]() -> String {
  206. switch (modifier) {
  207. case BG_RED:
  208. return "\033[48;2;255;0;102m";
  209. case BG_GREEN:
  210. return "\033[48;2;102;255;0m";
  211. case FG_RED:
  212. return "\033[38;2;255;0;102m";
  213. case FG_GREEN:
  214. return "\033[38;2;102;255;0m";
  215. case FG_GRAY:
  216. return "\033[38;2;135;139;148m";
  217. case FG_BLACK:
  218. return "\033[30m";
  219. case FG_BOLD:
  220. return "\033[1m";
  221. case CLEAR:
  222. return "\033[0m";
  223. }
  224. ASSERT_NOT_REACHED();
  225. };
  226. printf("%s", code().characters());
  227. }
  228. }
  229. void print_file_results(const FileResults& results)
  230. {
  231. if (results.failed || results.error.has_value()) {
  232. print_modifiers({ BG_RED, FG_BLACK, FG_BOLD });
  233. printf(" FAIL ");
  234. print_modifiers({ CLEAR });
  235. } else {
  236. print_modifiers({ BG_GREEN, FG_BLACK, FG_BOLD });
  237. printf(" PASS ");
  238. print_modifiers({ CLEAR });
  239. }
  240. printf(" %s\n", results.file.characters());
  241. if (results.error.has_value()) {
  242. auto test_error = results.error.value();
  243. print_modifiers({ FG_RED });
  244. printf(" ❌ The file failed to parse\n\n");
  245. print_modifiers({ FG_GRAY });
  246. for (auto& message : test_error.hint.split('\n', true)) {
  247. printf(" %s\n", message.characters());
  248. }
  249. print_modifiers({ FG_RED });
  250. printf(" %s\n\n", test_error.error.to_string().characters());
  251. return;
  252. }
  253. if (results.failed) {
  254. for (auto& suite : results.suites) {
  255. if (!suite.failed)
  256. continue;
  257. bool top_level = suite.name == TOP_LEVEL_TEST_NAME;
  258. if (!top_level) {
  259. print_modifiers({ FG_GRAY, FG_BOLD });
  260. printf(" ❌ Suite: ");
  261. print_modifiers({ CLEAR, FG_RED });
  262. printf("%s\n", suite.name.characters());
  263. print_modifiers({ CLEAR });
  264. }
  265. for (auto& test : suite.tests) {
  266. if (test.passed)
  267. continue;
  268. if (!top_level) {
  269. print_modifiers({ FG_GRAY, FG_BOLD });
  270. printf(" Test: ");
  271. print_modifiers({ CLEAR, FG_RED });
  272. printf("%s\n", test.name.characters());
  273. print_modifiers({ CLEAR });
  274. } else {
  275. print_modifiers({ FG_GRAY, FG_BOLD });
  276. printf(" ❌ Test: ");
  277. print_modifiers({ CLEAR, FG_RED });
  278. printf("%s\n", test.name.characters());
  279. print_modifiers({ CLEAR });
  280. }
  281. }
  282. }
  283. }
  284. }
  285. void print_results(const Results& results, double time_elapsed)
  286. {
  287. for (auto& result : results.file_results)
  288. print_file_results(result);
  289. int suites_passed = 0;
  290. int suites_failed = 0;
  291. int tests_passed = 0;
  292. int tests_failed = 0;
  293. for (auto& file_result : results.file_results) {
  294. for (auto& suite : file_result.suites) {
  295. tests_passed += suite.passed;
  296. tests_failed += suite.failed;
  297. if (suite.failed) {
  298. suites_failed++;
  299. } else {
  300. suites_passed++;
  301. }
  302. }
  303. }
  304. printf("\nTest Suites: ");
  305. if (suites_failed) {
  306. print_modifiers({ FG_RED });
  307. printf("%d failed, ", suites_failed);
  308. print_modifiers({ CLEAR });
  309. }
  310. if (suites_passed) {
  311. print_modifiers({ FG_GREEN });
  312. printf("%d passed, ", suites_passed);
  313. print_modifiers({ CLEAR });
  314. }
  315. printf("%d total\n", suites_failed + suites_passed);
  316. printf("Tests: ");
  317. if (tests_failed) {
  318. print_modifiers({ FG_RED });
  319. printf("%d failed, ", tests_failed);
  320. print_modifiers({ CLEAR });
  321. }
  322. if (tests_passed) {
  323. print_modifiers({ FG_GREEN });
  324. printf("%d passed, ", tests_passed);
  325. print_modifiers({ CLEAR });
  326. }
  327. printf("%d total\n", tests_failed + tests_passed);
  328. printf("Time: %-.3fs\n\n", time_elapsed);
  329. }
  330. double get_time()
  331. {
  332. struct timeval tv1;
  333. struct timezone tz1;
  334. auto return_code = gettimeofday(&tv1, &tz1);
  335. ASSERT(return_code >= 0);
  336. return static_cast<double>(tv1.tv_sec) + static_cast<double>(tv1.tv_usec) / 1'000'000;
  337. }
  338. int main(int, char** argv)
  339. {
  340. String test_root = argv[1];
  341. Results results;
  342. double start_time = get_time();
  343. for (auto& test : tests_to_run)
  344. results.file_results.append(run_test(test, test_root));
  345. print_results(results, get_time() - start_time);
  346. return 0;
  347. }