test-js.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibTest/JavaScriptTestRunner.h>
  8. TEST_ROOT("Userland/Libraries/LibJS/Tests");
  9. TESTJS_PROGRAM_FLAG(test262_parser_tests, "Run test262 parser tests", "test262-parser-tests", 0);
  10. TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
  11. {
  12. return JS::Value(vm.in_strict_mode());
  13. }
  14. TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
  15. {
  16. auto source = vm.argument(0).to_string(global_object);
  17. if (vm.exception())
  18. return {};
  19. auto parser = JS::Parser(JS::Lexer(source));
  20. parser.parse_program();
  21. return JS::Value(!parser.has_errors());
  22. }
  23. TESTJS_GLOBAL_FUNCTION(run_queued_promise_jobs, runQueuedPromiseJobs)
  24. {
  25. vm.run_queued_promise_jobs();
  26. return JS::js_undefined();
  27. }
  28. TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
  29. {
  30. auto* object = vm.argument(0).to_object(global_object);
  31. if (!object)
  32. return {};
  33. if (!is<JS::WeakSet>(object)) {
  34. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WeakSet");
  35. return {};
  36. }
  37. auto* weak_set = static_cast<JS::WeakSet*>(object);
  38. return JS::Value(weak_set->values().size());
  39. }
  40. TESTJS_RUN_FILE_FUNCTION(const String& test_file, JS::Interpreter&)
  41. {
  42. if (!test262_parser_tests)
  43. return Test::JS::RunFileHookResult::RunAsNormal;
  44. auto start_time = Test::JS::get_time_in_ms();
  45. LexicalPath path(test_file);
  46. auto& dirname = path.dirname();
  47. enum {
  48. Early,
  49. Fail,
  50. Pass,
  51. ExplicitPass,
  52. } expectation { Pass };
  53. if (dirname.ends_with("early"))
  54. expectation = Early;
  55. else if (dirname.ends_with("fail"))
  56. expectation = Fail;
  57. else if (dirname.ends_with("pass-explicit"))
  58. expectation = ExplicitPass;
  59. else if (dirname.ends_with("pass"))
  60. expectation = Pass;
  61. else
  62. return Test::JS::RunFileHookResult::SkipFile;
  63. auto parse_result = Test::JS::parse_file(test_file);
  64. bool test_passed = true;
  65. String message;
  66. String expectation_string;
  67. switch (expectation) {
  68. case Early:
  69. case Fail:
  70. expectation_string = "File should not parse";
  71. test_passed = parse_result.is_error();
  72. if (!test_passed)
  73. message = "Expected the file to fail parsing, but it did not";
  74. break;
  75. case Pass:
  76. case ExplicitPass:
  77. expectation_string = "File should parse";
  78. test_passed = !parse_result.is_error();
  79. if (!test_passed)
  80. message = "Expected the file to parse, but it did not";
  81. break;
  82. }
  83. auto test_result = test_passed ? Test::Result::Pass : Test::Result::Fail;
  84. return Test::JS::JSFileResult {
  85. LexicalPath::relative_path(test_file, Test::JS::g_test_root),
  86. {},
  87. Test::JS::get_time_in_ms() - start_time,
  88. test_result,
  89. { Test::Suite { "Parse file", test_result, { { expectation_string, test_result, message } } } }
  90. };
  91. }