Skip to content

Commit

Permalink
extracted tc39-test262 crate
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelJastrzebski committed Mar 6, 2024
1 parent e3a9952 commit 154f7a1
Show file tree
Hide file tree
Showing 14 changed files with 584 additions and 500 deletions.
22 changes: 17 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions tests/tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ boa_runtime.workspace = true
boa_gc.workspace = true
clap = { workspace = true, features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
serde_yaml = "0.9.32"
tc39-test262 = { path = "../../tools/tc39-test262"}
serde_json.workspace = true
bitflags.workspace = true
regex.workspace = true
once_cell.workspace = true
colored.workspace = true
rustc-hash = { workspace = true, features = ["std"] }
rayon = "1.8.1"
toml = "0.8.10"
color-eyre = "0.6.2"
phf = { workspace = true, features = ["macros"] }
comfy-table = "7.1.0"
serde_repr = "0.1.18"
bus = "2.4.1"
time.workspace = true

Expand Down
167 changes: 98 additions & 69 deletions tests/tester/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

mod js262;

use crate::{
test252_parser::{ErrorType, Harness, Outcome, Phase, SpecEdition, Test, TestSuite}, Statistics, SuiteResult, TestFlags, TestOutcomeResult, TestResult, VersionedStats
};
use crate::{Statistics, SuiteResult, TestFlags, TestOutcomeResult, TestResult, VersionedStats};
use boa_engine::{
builtins::promise::PromiseState,
js_string,
Expand All @@ -20,12 +18,41 @@ use colored::Colorize;
use rayon::prelude::*;
use rustc_hash::FxHashSet;
use std::{cell::RefCell, eprintln, rc::Rc};
use tc39_test262::{ErrorType, Harness, Outcome, Phase, SpecEdition, Test, TestSuite};

use self::js262::WorkerHandles;

impl TestSuite {
pub(crate) trait RunTest<TO, TR> {
/// Runs the test.
fn run(&self, harness: &Harness, verbose: u8, optimizer_options: TO, console: bool) -> TR;

/// Runs the test once, in strict or non-strict mode
fn run_once(
&self,
harness: &Harness,
strict: bool,
verbose: u8,
optimizer_options: OptimizerOptions,
console: bool,
) -> TestResult;
}

pub(crate) trait RunTestSuite<TO, TR> {
/// Runs the test suite.
pub(crate) fn run(
fn run(
&self,
harness: &Harness,
verbose: u8,
parallel: bool,
max_edition: SpecEdition,
optimizer_options: OptimizerOptions,
console: bool,
) -> SuiteResult;
}

impl RunTestSuite<OptimizerOptions, SuiteResult> for TestSuite {
/// Runs the test suite.
fn run(
&self,
harness: &Harness,
verbose: u8,
Expand Down Expand Up @@ -159,9 +186,9 @@ impl TestSuite {
}
}

impl Test {
impl RunTest<OptimizerOptions, TestResult> for Test {
/// Runs the test.
pub(crate) fn run(
fn run(
&self,
harness: &Harness,
verbose: u8,
Expand Down Expand Up @@ -262,7 +289,8 @@ impl Test {

let mut handles = WorkerHandles::new();

if let Err(e) = self.set_up_env(
if let Err(e) = set_up_env(
&self,
harness,
context,
async_result.clone(),
Expand Down Expand Up @@ -452,7 +480,8 @@ impl Test {

let mut handles = WorkerHandles::new();

if let Err(e) = self.set_up_env(
if let Err(e) = set_up_env(
&self,
harness,
context,
AsyncResult::default(),
Expand Down Expand Up @@ -588,73 +617,73 @@ impl Test {
result_text: result_text.into_boxed_str(),
}
}
}

/// Sets the environment up to run the test.
fn set_up_env(
&self,
harness: &Harness,
context: &mut Context,
async_result: AsyncResult,
handles: WorkerHandles,
console: bool,
) -> Result<(), String> {
// Register the print() function.
register_print_fn(context, async_result);

// add the $262 object.
let _js262 = js262::register_js262(handles, context);

if console {
let console = boa_runtime::Console::init(context);
context
.register_global_property(
js_string!(boa_runtime::Console::NAME),
console,
Attribute::all(),
)
.expect("the console builtin shouldn't exist");
}

if self.flags.contains(TestFlags::RAW) {
return Ok(());
}
/// Sets the environment up to run the test.
fn set_up_env(
test: &Test,
harness: &Harness,
context: &mut Context,
async_result: AsyncResult,
handles: WorkerHandles,
console: bool,
) -> Result<(), String> {
// Register the print() function.
register_print_fn(context, async_result);

// add the $262 object.
let _js262 = js262::register_js262(handles, context);

if console {
let console = boa_runtime::Console::init(context);
context
.register_global_property(
js_string!(boa_runtime::Console::NAME),
console,
Attribute::all(),
)
.expect("the console builtin shouldn't exist");
}

let assert = Source::from_reader(
harness.assert.content.as_bytes(),
Some(&harness.assert.path),
);
let sta = Source::from_reader(harness.sta.content.as_bytes(), Some(&harness.sta.path));
if test.flags.contains(TestFlags::RAW) {
return Ok(());
}

context
.eval(assert)
.map_err(|e| format!("could not run assert.js:\n{e}"))?;
context
.eval(sta)
.map_err(|e| format!("could not run sta.js:\n{e}"))?;
let assert = Source::from_reader(
harness.assert.content.as_bytes(),
Some(&harness.assert.path),
);
let sta = Source::from_reader(harness.sta.content.as_bytes(), Some(&harness.sta.path));

if self.flags.contains(TestFlags::ASYNC) {
let dph = Source::from_reader(
harness.doneprint_handle.content.as_bytes(),
Some(&harness.doneprint_handle.path),
);
context
.eval(dph)
.map_err(|e| format!("could not run doneprintHandle.js:\n{e}"))?;
}
context
.eval(assert)
.map_err(|e| format!("could not run assert.js:\n{e}"))?;
context
.eval(sta)
.map_err(|e| format!("could not run sta.js:\n{e}"))?;

for include_name in &self.includes {
let include = harness
.includes
.get(include_name)
.ok_or_else(|| format!("could not find the {include_name} include file."))?;
let source = Source::from_reader(include.content.as_bytes(), Some(&include.path));
context.eval(source).map_err(|e| {
format!("could not run the harness `{include_name}`:\nUncaught {e}",)
})?;
}
if test.flags.contains(TestFlags::ASYNC) {
let dph = Source::from_reader(
harness.doneprint_handle.content.as_bytes(),
Some(&harness.doneprint_handle.path),
);
context
.eval(dph)
.map_err(|e| format!("could not run doneprintHandle.js:\n{e}"))?;
}

Ok(())
for include_name in &test.includes {
let include = harness
.includes
.get(include_name)
.ok_or_else(|| format!("could not find the {include_name} include file."))?;
let source = Source::from_reader(include.content.as_bytes(), Some(&include.path));
context
.eval(source)
.map_err(|e| format!("could not run the harness `{include_name}`:\nUncaught {e}",))?;
}

Ok(())
}

/// Returns `true` if `error` is a `target_type` error.
Expand Down
46 changes: 10 additions & 36 deletions tests/tester/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
)]

mod exec;
mod git;
mod test252_parser;
mod results;

use test252_parser::{TestFlags, SpecEdition, Ignored, read_harness, read_suite, read_test};
use exec::{RunTestSuite, RunTest};
use tc39_test262::{read, Ignored, SpecEdition, TestFlags};

use self::results::{compare_results, write_json};

use boa_engine::optimizer::OptimizerOptions;
Expand All @@ -26,9 +26,7 @@ use color_eyre::{
use colored::Colorize;
use once_cell::sync::Lazy;
use rustc_hash::FxHashSet;
use serde::{
Deserialize, Deserializer, Serialize,
};
use serde::{Deserialize, Deserializer, Serialize};
use std::{
ops::{Add, AddAssign},
path::{Path, PathBuf},
Expand Down Expand Up @@ -128,8 +126,6 @@ enum Cli {
},
}

const DEFAULT_TEST262_DIRECTORY: &str = "test262";

/// Program entry point.
fn main() -> Result<()> {
// Safety: This is needed because we run tests in multiple threads.
Expand Down Expand Up @@ -168,9 +164,8 @@ fn main() -> Result<()> {
let test262_path = if let Some(path) = test262_path.as_deref() {
path
} else {
clone_test262(test262_commit, verbose)?;

Path::new(DEFAULT_TEST262_DIRECTORY)
tc39_test262::clone_test262(test262_commit, verbose)?;
Path::new(tc39_test262::TEST262_DIRECTORY)
};

run_test_suite(
Expand Down Expand Up @@ -198,17 +193,6 @@ fn main() -> Result<()> {
}
}

fn clone_test262(commit: Option<&str>, verbose: u8) -> Result<()> {
const TEST262_REPOSITORY: &str = "https://github.com/tc39/test262";
git::clone(
DEFAULT_TEST262_DIRECTORY,
TEST262_REPOSITORY,
&"origin/main",
commit,
verbose,
)
}

/// Runs the full test suite.
#[allow(clippy::too_many_arguments)]
fn run_test_suite(
Expand Down Expand Up @@ -236,10 +220,10 @@ fn run_test_suite(
if verbose != 0 {
println!("Loading the test suite...");
}
let harness = read_harness(test262_path).wrap_err("could not read harness")?;
let harness = read::read_harness(test262_path).wrap_err("could not read harness")?;

if suite.to_string_lossy().ends_with(".js") {
let test = read_test(&test262_path.join(suite)).wrap_err_with(|| {
let test = read::read_test(&test262_path.join(suite)).wrap_err_with(|| {
let suite = suite.display();
format!("could not read the test {suite}")
})?;
Expand All @@ -257,8 +241,8 @@ fn run_test_suite(

println!();
} else {
let suite =
read_suite(&test262_path.join(suite), config.ignored(), false).wrap_err_with(|| {
let suite = read::read_suite(&test262_path.join(suite), config.ignored(), false)
.wrap_err_with(|| {
let suite = suite.display();
format!("could not read the suite {suite}")
})?;
Expand Down Expand Up @@ -573,13 +557,3 @@ enum TestOutcomeResult {
#[serde(rename = "P")]
Panic,
}

#[cfg(test)]
mod tests {

#[test]
#[ignore = "manual testing"]
fn should_clone_test262() {
super::clone_test262(None, 0).unwrap();
}
}
Loading

0 comments on commit 154f7a1

Please sign in to comment.