mod api;
mod array;
mod async_await;
mod async_iter;
mod basics;
mod boolean;
mod bytecode;
mod class;
mod console;
mod control_flow;
mod cycle_leak;
mod date;
mod decorator;
mod enum_test;
mod error;
mod eval;
mod function;
mod gc;
mod generator;
mod global;
mod json;
mod map;
mod math;
mod modules;
mod namespace;
mod number;
mod object;
mod orders;
mod promise;
mod proxy;
mod regexp;
mod set;
mod step;
mod strict;
mod string;
mod symbol;
mod typescript;
use tsrun::{Interpreter, JsError, JsValue, RuntimeValue, StepResult};
pub fn create_test_runtime() -> Interpreter {
let interp = Interpreter::new();
let gc_threshold = std::env::var("GC_THRESHOLD")
.ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1);
interp.set_gc_threshold(gc_threshold);
interp
}
pub fn run_to_completion(interp: &mut Interpreter) -> Result<StepResult, JsError> {
loop {
match interp.step()? {
StepResult::Continue => continue,
result => return Ok(result),
}
}
}
pub fn run(
interp: &mut Interpreter,
source: &str,
path: Option<&str>,
) -> Result<StepResult, JsError> {
interp.prepare(source, path.map(tsrun::ModulePath::new))?;
run_to_completion(interp)
}
#[allow(clippy::expect_used)]
pub fn eval(source: &str) -> RuntimeValue {
eval_result(source).expect("eval failed")
}
pub fn eval_result(source: &str) -> Result<RuntimeValue, JsError> {
let mut interp = create_test_runtime();
interp.prepare(source, None)?;
match run_to_completion(&mut interp)? {
StepResult::Complete(rv) => Ok(rv),
StepResult::NeedImports(specifiers) => Err(JsError::type_error(format!(
"Missing imports in test: {:?}",
specifiers
))),
StepResult::Suspended { pending, .. } => {
Err(JsError::type_error(format!(
"Test suspended waiting for {} orders",
pending.len()
)))
}
StepResult::Continue => Err(JsError::internal_error(
"Unexpected Continue from run_to_completion",
)),
StepResult::Done => Ok(RuntimeValue::unguarded(JsValue::Undefined)),
}
}
pub fn throws_error(source: &str, error_contains: &str) -> bool {
match eval_result(source) {
Err(e) => format!("{:?}", e).contains(error_contains),
Ok(_) => false,
}
}