diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2023-05-07 20:14:06 +0200 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-05-14 15:39:38 -0600 |
commit | f890b70eae1f0723913aa799a2b25ad3f1d4bf87 (patch) | |
tree | 89c27cf70ff5499f335d1fe4997d76ee9a3994f4 /Tests/LibJS/test-bytecode-js.cpp | |
parent | 87a7299078fe563b3e49f15994ee697b89d380a2 (diff) | |
download | serenity-f890b70eae1f0723913aa799a2b25ad3f1d4bf87.zip |
Tests: Prefer TRY_OR_FAIL() and MUST() over EXPECT(!.is_error())
Note that in some cases (in particular SQL::Result and PDFErrorOr),
there is no Formatter defined for the error type, hence TRY_OR_FAIL
cannot work as-is. Furthermore, this commit leaves untouched the places
where MUST could be replaced by TRY_OR_FAIL.
Inspired by:
https://github.com/SerenityOS/serenity/pull/18710#discussion_r1186892445
Diffstat (limited to 'Tests/LibJS/test-bytecode-js.cpp')
-rw-r--r-- | Tests/LibJS/test-bytecode-js.cpp | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/Tests/LibJS/test-bytecode-js.cpp b/Tests/LibJS/test-bytecode-js.cpp index 57eade2d0d..ed2641a1ed 100644 --- a/Tests/LibJS/test-bytecode-js.cpp +++ b/Tests/LibJS/test-bytecode-js.cpp @@ -12,33 +12,32 @@ #include <LibJS/Script.h> #include <LibTest/TestCase.h> -#define SETUP_AND_PARSE(source) \ - auto vm = MUST(JS::VM::create()); \ - auto ast_interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm); \ - \ - auto script_or_error = JS::Script::parse(source##sv, ast_interpreter->realm()); \ - EXPECT(!script_or_error.is_error()); \ - \ - auto script = script_or_error.release_value(); \ - auto const& program = script->parse_node(); \ +#define SETUP_AND_PARSE(source) \ + auto vm = MUST(JS::VM::create()); \ + auto ast_interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm); \ + \ + auto script = MUST(JS::Script::parse(source##sv, ast_interpreter->realm())); \ + auto const& program = script->parse_node(); \ JS::Bytecode::Interpreter bytecode_interpreter(ast_interpreter->realm()); -#define EXPECT_NO_EXCEPTION(executable) \ - auto executable = MUST(JS::Bytecode::Generator::generate(program)); \ - auto result = bytecode_interpreter.run(*executable); \ - EXPECT(!result.is_error()); \ - if (result.is_error()) \ - dbgln("Error: {}", MUST(result.throw_completion().value()->to_deprecated_string(vm))); - -#define EXPECT_NO_EXCEPTION_WITH_OPTIMIZATIONS(executable) \ - auto& passes = JS::Bytecode::Interpreter::optimization_pipeline(); \ - passes.perform(*executable); \ - \ - auto result_with_optimizations = bytecode_interpreter.run(*executable); \ - \ - EXPECT(!result_with_optimizations.is_error()); \ - if (result_with_optimizations.is_error()) \ - dbgln("Error: {}", MUST(result_with_optimizations.throw_completion().value()->to_deprecated_string(vm))); +#define EXPECT_NO_EXCEPTION(executable) \ + auto executable = MUST(JS::Bytecode::Generator::generate(program)); \ + auto result = bytecode_interpreter.run(*executable); \ + if (result.is_error()) { \ + FAIL("unexpected exception"); \ + dbgln("Error: {}", MUST(result.throw_completion().value()->to_deprecated_string(vm))); \ + } + +#define EXPECT_NO_EXCEPTION_WITH_OPTIMIZATIONS(executable) \ + auto& passes = JS::Bytecode::Interpreter::optimization_pipeline(); \ + passes.perform(*executable); \ + \ + auto result_with_optimizations = bytecode_interpreter.run(*executable); \ + \ + if (result_with_optimizations.is_error()) { \ + FAIL("unexpected exception"); \ + dbgln("Error: {}", MUST(result_with_optimizations.throw_completion().value()->to_deprecated_string(vm))); \ + } #define EXPECT_NO_EXCEPTION_ALL(source) \ SETUP_AND_PARSE("(() => {\n" source "\n})()") \ @@ -111,15 +110,14 @@ TEST_CASE(loading_multiple_files) } { - auto test_file_script_or_error = JS::Script::parse("if (f() !== 'hello') throw new Exception('failed'); "sv, ast_interpreter->realm()); - EXPECT(!test_file_script_or_error.is_error()); + auto test_file_script = MUST(JS::Script::parse( + "if (f() !== 'hello') throw new Exception('failed'); "sv, ast_interpreter->realm())); - auto test_file_script = test_file_script_or_error.release_value(); auto const& test_file_program = test_file_script->parse_node(); auto executable = MUST(JS::Bytecode::Generator::generate(test_file_program)); - auto result = bytecode_interpreter.run(*executable); - EXPECT(!result.is_error()); + // TODO: This could be TRY_OR_FAIL(), if someone implements Formatter<JS::Completion>. + MUST(bytecode_interpreter.run(*executable)); } } |