test-js.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. "add-values-to-primitive.js",
  56. "automatic-semicolon-insertion.js",
  57. "comments-basic.js",
  58. "debugger-statement.js",
  59. "empty-statements.js",
  60. "exception-ReferenceError.js",
  61. "exponentiation-basic.js",
  62. "indexed-access-string-object.js",
  63. "invalid-lhs-in-assignment.js",
  64. "let-scoping.js",
  65. "new-expression.js",
  66. "numeric-literals-basic.js",
  67. "object-getter-setter-shorthand.js",
  68. "object-method-shorthand.js",
  69. "object-spread.js",
  70. "tagged-template-literals.js",
  71. "test-common-tests.js",
  72. "switch-basic.js",
  73. "update-expression-on-member-expression.js",
  74. };
  75. struct FileTest {
  76. String name;
  77. bool passed;
  78. };
  79. struct FileSuite {
  80. String name;
  81. int passed { 0 };
  82. int failed { 0 };
  83. Vector<FileTest> tests {};
  84. };
  85. struct TestError {
  86. JS::Parser::Error error;
  87. String hint;
  88. };
  89. struct FileResults {
  90. String file;
  91. Optional<TestError> error {};
  92. int passed { 0 };
  93. int failed { 0 };
  94. Vector<FileSuite> suites {};
  95. };
  96. struct Results {
  97. Vector<FileResults> file_results {};
  98. };
  99. Optional<TestError> parse_and_run_file(JS::Interpreter& interpreter, const String& path)
  100. {
  101. auto file = Core::File::construct(path);
  102. auto result = file->open(Core::IODevice::ReadOnly);
  103. ASSERT(result);
  104. auto contents = file->read_all();
  105. String test_file_string(reinterpret_cast<const char*>(contents.data()), contents.size());
  106. file->close();
  107. auto parser = JS::Parser(JS::Lexer(test_file_string));
  108. auto program = parser.parse_program();
  109. if (parser.has_errors()) {
  110. auto error = parser.errors()[0];
  111. return TestError { error, error.source_location_hint(test_file_string) };
  112. } else {
  113. interpreter.run(interpreter.global_object(), *program);
  114. }
  115. return {};
  116. }
  117. FileResults run_test(const String& path, const String& test_root)
  118. {
  119. auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
  120. if (parse_and_run_file(*interpreter, String::format("%s/test-common.js", test_root.characters())).has_value()) {
  121. dbg() << "test-common.js failed to parse";
  122. exit(1);
  123. }
  124. auto source_file_result = parse_and_run_file(*interpreter, String::format("%s/%s", test_root.characters(), path.characters()));
  125. if (source_file_result.has_value())
  126. return { path, source_file_result };
  127. // Print any output
  128. // FIXME: Should be printed to stdout in a nice format
  129. auto& arr = interpreter->get_variable("__UserOutput__", interpreter->global_object()).as_array();
  130. for (auto& entry : arr.indexed_properties()) {
  131. dbg() << "OUTPUT: " << entry.value_and_attributes(&interpreter->global_object()).value.to_string_without_side_effects();
  132. }
  133. // FIXME: This is _so_ scuffed
  134. auto result = interpreter->get_variable("__TestResults__", interpreter->global_object());
  135. auto json_object = interpreter->get_variable("JSON", interpreter->global_object());
  136. auto stringify = json_object.as_object().get("stringify");
  137. JS::MarkedValueList arguments(interpreter->heap());
  138. arguments.append(result);
  139. auto json_string = interpreter->call(stringify.as_function(), interpreter->this_value(interpreter->global_object()), move(arguments)).to_string(*interpreter);
  140. auto json_result = JsonValue::from_string(json_string);
  141. if (!json_result.has_value()) {
  142. dbg() << "BAD JSON:";
  143. dbg() << json_string;
  144. return {};
  145. }
  146. auto json = json_result.value();
  147. FileResults results { path };
  148. json.as_object().for_each_member([&](const String& property, const JsonValue& value) {
  149. FileSuite suite { property };
  150. value.as_object().for_each_member([&](const String& property1, const JsonValue& value1) {
  151. FileTest test { property1, false };
  152. if (value1.is_object()) {
  153. auto obj = value1.as_object();
  154. if (obj.has("passed")) {
  155. auto passed = obj.get("passed");
  156. test.passed = passed.is_bool() && passed.as_bool();
  157. }
  158. }
  159. if (test.passed) {
  160. suite.passed++;
  161. } else {
  162. suite.failed++;
  163. }
  164. suite.tests.append(test);
  165. });
  166. if (suite.failed) {
  167. results.failed++;
  168. } else {
  169. results.passed++;
  170. }
  171. results.suites.append(suite);
  172. });
  173. return results;
  174. }
  175. bool skip_test(char* test_name)
  176. {
  177. return !strcmp(test_name, "test-common.js") || !strcmp(test_name, "run_tests.sh");
  178. }
  179. enum Modifier {
  180. BG_RED,
  181. BG_GREEN,
  182. FG_RED,
  183. FG_GREEN,
  184. FG_GRAY,
  185. FG_BLACK,
  186. FG_BOLD,
  187. CLEAR,
  188. };
  189. void print_modifiers(Vector<Modifier> modifiers)
  190. {
  191. for (auto& modifier : modifiers) {
  192. auto code = [&]() -> String {
  193. switch (modifier) {
  194. case BG_RED:
  195. return "\033[48;2;255;0;102m";
  196. case BG_GREEN:
  197. return "\033[48;2;102;255;0m";
  198. case FG_RED:
  199. return "\033[38;2;255;0;102m";
  200. case FG_GREEN:
  201. return "\033[38;2;102;255;0m";
  202. case FG_GRAY:
  203. return "\033[38;2;135;139;148m";
  204. case FG_BLACK:
  205. return "\033[30m";
  206. case FG_BOLD:
  207. return "\033[1m";
  208. case CLEAR:
  209. return "\033[0m";
  210. }
  211. ASSERT_NOT_REACHED();
  212. };
  213. printf("%s", code().characters());
  214. }
  215. }
  216. void print_file_results(const FileResults& results)
  217. {
  218. if (results.failed || results.error.has_value()) {
  219. print_modifiers({ BG_RED, FG_BLACK, FG_BOLD });
  220. printf(" FAIL ");
  221. print_modifiers({ CLEAR });
  222. } else {
  223. print_modifiers({ BG_GREEN, FG_BLACK, FG_BOLD });
  224. printf(" PASS ");
  225. print_modifiers({ CLEAR });
  226. }
  227. printf(" %s\n", results.file.characters());
  228. if (results.error.has_value()) {
  229. auto test_error = results.error.value();
  230. print_modifiers({ FG_RED });
  231. printf(" ❌ The file failed to parse\n\n");
  232. print_modifiers({ FG_GRAY });
  233. for (auto& message : test_error.hint.split('\n', true)) {
  234. printf(" %s\n", message.characters());
  235. }
  236. print_modifiers({ FG_RED });
  237. printf(" %s\n\n", test_error.error.to_string().characters());
  238. return;
  239. }
  240. if (results.failed) {
  241. for (auto& suite : results.suites) {
  242. if (!suite.failed)
  243. continue;
  244. bool top_level = suite.name == TOP_LEVEL_TEST_NAME;
  245. if (!top_level) {
  246. print_modifiers({ FG_GRAY, FG_BOLD });
  247. printf(" ❌ Suite: ");
  248. print_modifiers({ CLEAR, FG_RED });
  249. printf("%s\n", suite.name.characters());
  250. print_modifiers({ CLEAR });
  251. }
  252. for (auto& test : suite.tests) {
  253. if (test.passed)
  254. continue;
  255. if (!top_level) {
  256. print_modifiers({ FG_GRAY, FG_BOLD });
  257. printf(" Test: ");
  258. print_modifiers({ CLEAR, FG_RED });
  259. printf("%s\n", test.name.characters());
  260. print_modifiers({ CLEAR });
  261. } else {
  262. print_modifiers({ FG_GRAY, FG_BOLD });
  263. printf(" ❌ Test: ");
  264. print_modifiers({ CLEAR, FG_RED });
  265. printf("%s\n", test.name.characters());
  266. print_modifiers({ CLEAR });
  267. }
  268. }
  269. }
  270. }
  271. }
  272. void print_results(const Results& results, double time_elapsed)
  273. {
  274. for (auto& result : results.file_results)
  275. print_file_results(result);
  276. int suites_passed = 0;
  277. int suites_failed = 0;
  278. int tests_passed = 0;
  279. int tests_failed = 0;
  280. for (auto& file_result : results.file_results) {
  281. for (auto& suite : file_result.suites) {
  282. tests_passed += suite.passed;
  283. tests_failed += suite.failed;
  284. if (suite.failed) {
  285. suites_failed++;
  286. } else {
  287. suites_passed++;
  288. }
  289. }
  290. }
  291. printf("\nTest Suites: ");
  292. if (suites_failed) {
  293. print_modifiers({ FG_RED });
  294. printf("%d failed, ", suites_failed);
  295. print_modifiers({ CLEAR });
  296. }
  297. if (suites_passed) {
  298. print_modifiers({ FG_GREEN });
  299. printf("%d passed, ", suites_passed);
  300. print_modifiers({ CLEAR });
  301. }
  302. printf("%d total\n", suites_failed + suites_passed);
  303. printf("Tests: ");
  304. if (tests_failed) {
  305. print_modifiers({ FG_RED });
  306. printf("%d failed, ", tests_failed);
  307. print_modifiers({ CLEAR });
  308. }
  309. if (tests_passed) {
  310. print_modifiers({ FG_GREEN });
  311. printf("%d passed, ", tests_passed);
  312. print_modifiers({ CLEAR });
  313. }
  314. printf("%d total\n", tests_failed + tests_passed);
  315. printf("Time: %-.3fs\n\n", time_elapsed);
  316. }
  317. double get_time()
  318. {
  319. struct timeval tv1;
  320. struct timezone tz1;
  321. auto return_code = gettimeofday(&tv1, &tz1);
  322. ASSERT(return_code >= 0);
  323. return static_cast<double>(tv1.tv_sec) + static_cast<double>(tv1.tv_usec) / 1'000'000;
  324. }
  325. int main(int, char** argv)
  326. {
  327. String test_root = argv[1];
  328. Results results;
  329. double start_time = get_time();
  330. for (auto& test : tests_to_run)
  331. results.file_results.append(run_test(test, test_root));
  332. print_results(results, get_time() - start_time);
  333. return 0;
  334. }