test-js.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/ArrayBuffer.h>
  8. #include <LibTest/JavaScriptTestRunner.h>
  9. TEST_ROOT("Userland/Libraries/LibJS/Tests");
  10. TESTJS_PROGRAM_FLAG(test262_parser_tests, "Run test262 parser tests", "test262-parser-tests", 0);
  11. TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
  12. {
  13. return JS::Value(vm.in_strict_mode());
  14. }
  15. TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
  16. {
  17. auto source = TRY(vm.argument(0).to_deprecated_string(vm));
  18. auto parser = JS::Parser(JS::Lexer(source));
  19. (void)parser.parse_program();
  20. return JS::Value(!parser.has_errors());
  21. }
  22. TESTJS_GLOBAL_FUNCTION(run_queued_promise_jobs, runQueuedPromiseJobs)
  23. {
  24. vm.run_queued_promise_jobs();
  25. return JS::js_undefined();
  26. }
  27. TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
  28. {
  29. auto object = TRY(vm.argument(0).to_object(vm));
  30. if (!is<JS::WeakSet>(*object))
  31. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakSet");
  32. auto& weak_set = static_cast<JS::WeakSet&>(*object);
  33. return JS::Value(weak_set.values().size());
  34. }
  35. TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
  36. {
  37. auto object = TRY(vm.argument(0).to_object(vm));
  38. if (!is<JS::WeakMap>(*object))
  39. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakMap");
  40. auto& weak_map = static_cast<JS::WeakMap&>(*object);
  41. return JS::Value(weak_map.values().size());
  42. }
  43. TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer)
  44. {
  45. auto array_buffer = vm.argument(0);
  46. if (!array_buffer.is_object() || !is<JS::ArrayBuffer>(array_buffer.as_object()))
  47. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
  48. auto& array_buffer_object = static_cast<JS::ArrayBuffer&>(array_buffer.as_object());
  49. TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
  50. return JS::js_null();
  51. }
  52. TESTJS_RUN_FILE_FUNCTION(DeprecatedString const& test_file, JS::Interpreter& interpreter, JS::ExecutionContext&)
  53. {
  54. if (!test262_parser_tests)
  55. return Test::JS::RunFileHookResult::RunAsNormal;
  56. auto start_time = Test::get_time_in_ms();
  57. LexicalPath path(test_file);
  58. auto dirname = path.dirname();
  59. enum {
  60. Early,
  61. Fail,
  62. Pass,
  63. ExplicitPass,
  64. } expectation { Pass };
  65. if (dirname.ends_with("early"sv))
  66. expectation = Early;
  67. else if (dirname.ends_with("fail"sv))
  68. expectation = Fail;
  69. else if (dirname.ends_with("pass-explicit"sv))
  70. expectation = ExplicitPass;
  71. else if (dirname.ends_with("pass"sv))
  72. expectation = Pass;
  73. else
  74. return Test::JS::RunFileHookResult::SkipFile;
  75. auto program_type = path.basename().ends_with(".module.js"sv) ? JS::Program::Type::Module : JS::Program::Type::Script;
  76. bool parse_succeeded = false;
  77. if (program_type == JS::Program::Type::Module)
  78. parse_succeeded = !Test::JS::parse_module(test_file, interpreter.realm()).is_error();
  79. else
  80. parse_succeeded = !Test::JS::parse_script(test_file, interpreter.realm()).is_error();
  81. bool test_passed = true;
  82. DeprecatedString message;
  83. DeprecatedString expectation_string;
  84. switch (expectation) {
  85. case Early:
  86. case Fail:
  87. expectation_string = "File should not parse";
  88. test_passed = !parse_succeeded;
  89. if (!test_passed)
  90. message = "Expected the file to fail parsing, but it did not";
  91. break;
  92. case Pass:
  93. case ExplicitPass:
  94. expectation_string = "File should parse";
  95. test_passed = parse_succeeded;
  96. if (!test_passed)
  97. message = "Expected the file to parse, but it did not";
  98. break;
  99. }
  100. auto test_result = test_passed ? Test::Result::Pass : Test::Result::Fail;
  101. auto test_path = LexicalPath::relative_path(test_file, Test::JS::g_test_root);
  102. auto duration_ms = Test::get_time_in_ms() - start_time;
  103. return Test::JS::JSFileResult {
  104. test_path,
  105. {},
  106. duration_ms,
  107. test_result,
  108. { Test::Suite { test_path, "Parse file", test_result, { { expectation_string, test_result, message, static_cast<u64>(duration_ms) * 1000u } } } }
  109. };
  110. }