mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:37:43 +00:00
js: Add a new --test-mode option, which exposes an assert() function.
Introduce a central assert implementation so that the LibJS test suite can take advantage of one implementation instead of copy/pasting the same function into every test case file.
This commit is contained in:
parent
a7d708e47d
commit
4233c8662b
1 changed files with 23 additions and 0 deletions
|
@ -331,16 +331,33 @@ void repl(JS::Interpreter& interpreter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS::Value assert_impl(JS::Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
if (!interpreter.argument_count())
|
||||||
|
return interpreter.throw_exception<JS::Error>("TypeError", "No arguments specified");
|
||||||
|
|
||||||
|
auto assertion_value = interpreter.argument(0);
|
||||||
|
if (!assertion_value.is_boolean())
|
||||||
|
return interpreter.throw_exception<JS::Error>("TypeError", "The first argument is not a boolean");
|
||||||
|
|
||||||
|
if (!assertion_value.to_boolean())
|
||||||
|
return interpreter.throw_exception<JS::Error>("AssertionError", "The assertion failed!");
|
||||||
|
|
||||||
|
return assertion_value;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
bool gc_on_every_allocation = false;
|
bool gc_on_every_allocation = false;
|
||||||
bool print_last_result = false;
|
bool print_last_result = false;
|
||||||
|
bool test_mode = false;
|
||||||
const char* script_path = nullptr;
|
const char* script_path = nullptr;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(dump_ast, "Dump the AST", "dump-ast", 'A');
|
args_parser.add_option(dump_ast, "Dump the AST", "dump-ast", 'A');
|
||||||
args_parser.add_option(print_last_result, "Print last result", "print-last-result", 'l');
|
args_parser.add_option(print_last_result, "Print last result", "print-last-result", 'l');
|
||||||
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
||||||
|
args_parser.add_option(test_mode, "Run the interpretter with added functionality for the test harness", "test-mode", 't');
|
||||||
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
@ -348,6 +365,9 @@ int main(int argc, char** argv)
|
||||||
auto interpreter = JS::Interpreter::create<ReplObject>();
|
auto interpreter = JS::Interpreter::create<ReplObject>();
|
||||||
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||||
interpreter->global_object().put("global", &interpreter->global_object());
|
interpreter->global_object().put("global", &interpreter->global_object());
|
||||||
|
if (test_mode) {
|
||||||
|
interpreter->global_object().put_native_function("assert", assert_impl);
|
||||||
|
}
|
||||||
|
|
||||||
editor = make<Line::Editor>();
|
editor = make<Line::Editor>();
|
||||||
editor->initialize();
|
editor->initialize();
|
||||||
|
@ -356,6 +376,9 @@ int main(int argc, char** argv)
|
||||||
auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
||||||
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||||
interpreter->global_object().put("global", &interpreter->global_object());
|
interpreter->global_object().put("global", &interpreter->global_object());
|
||||||
|
if (test_mode) {
|
||||||
|
interpreter->global_object().put_native_function("assert", assert_impl);
|
||||||
|
}
|
||||||
|
|
||||||
auto file = Core::File::construct(script_path);
|
auto file = Core::File::construct(script_path);
|
||||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue