test-js.cpp 11 KB

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