2018-10-25 07:17:05 +03:00
|
|
|
#![feature(test)]
|
2019-11-17 07:21:53 +03:00
|
|
|
|
2018-10-25 07:17:05 +03:00
|
|
|
extern crate test;
|
2019-11-17 07:21:53 +03:00
|
|
|
|
2018-10-25 07:17:05 +03:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
fs::{read_dir, File},
|
|
|
|
io::{self, Read, Write},
|
2018-11-10 11:44:35 +03:00
|
|
|
path::Path,
|
2018-10-25 07:17:05 +03:00
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2020-07-31 12:49:07 +03:00
|
|
|
use swc_common::comments::SingleThreadedComments;
|
2021-08-03 18:52:47 +03:00
|
|
|
use swc_ecma_codegen::{self, text_writer::WriteJs, Emitter};
|
2020-07-31 12:49:07 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
|
2019-10-23 14:19:49 +03:00
|
|
|
use test::{
|
|
|
|
test_main, DynTestFn, Options, ShouldPanic::No, TestDesc, TestDescAndFn, TestName, TestType,
|
|
|
|
};
|
2018-11-10 11:44:35 +03:00
|
|
|
use testing::NormalizedOutput;
|
2018-10-25 07:17:05 +03:00
|
|
|
|
|
|
|
const IGNORED_PASS_TESTS: &[&str] = &[
|
|
|
|
// Temporalily ignored
|
|
|
|
"431ecef8c85d4d24.js",
|
|
|
|
"8386fbff927a9e0e.js",
|
2019-01-07 13:43:47 +03:00
|
|
|
"5654d4106d7025c2.js",
|
2020-05-17 12:50:52 +03:00
|
|
|
// Stack size (Stupid parens)
|
|
|
|
"6b5e7e125097d439.js",
|
|
|
|
"714be6d28082eaa7.js",
|
|
|
|
"882910de7dd1aef9.js",
|
|
|
|
"dd3c63403db5c06e.js",
|
2018-10-25 07:17:05 +03:00
|
|
|
// Wrong tests (variable name or value is different)
|
|
|
|
"0339fa95c78c11bd.js",
|
|
|
|
"0426f15dac46e92d.js",
|
|
|
|
"0b4d61559ccce0f9.js",
|
|
|
|
"0f88c334715d2489.js",
|
|
|
|
"1093d98f5fc0758d.js",
|
|
|
|
"15d9592709b947a0.js",
|
|
|
|
"2179895ec5cc6276.js",
|
|
|
|
"247a3a57e8176ebd.js",
|
|
|
|
"441a92357939904a.js",
|
|
|
|
"47f974d6fc52e3e4.js",
|
|
|
|
"4e1a0da46ca45afe.js",
|
|
|
|
"5829d742ab805866.js",
|
|
|
|
"589dc8ad3b9aa28f.js",
|
|
|
|
"598a5cedba92154d.js",
|
|
|
|
"72d79750e81ef03d.js",
|
|
|
|
"7788d3c1e1247da9.js",
|
|
|
|
"7b72d7b43bedc895.js",
|
|
|
|
"7dab6e55461806c9.js",
|
|
|
|
"82c827ccaecbe22b.js",
|
|
|
|
"87a9b0d1d80812cc.js",
|
|
|
|
"8c80f7ee04352eba.js",
|
|
|
|
"96f5d93be9a54573.js",
|
|
|
|
"988e362ed9ddcac5.js",
|
|
|
|
"9bcae7c7f00b4e3c.js",
|
|
|
|
"a8a03a88237c4e8f.js",
|
|
|
|
"ad06370e34811a6a.js",
|
|
|
|
"b0fdc038ee292aba.js",
|
|
|
|
"b62c6dd890bef675.js",
|
|
|
|
"cb211fadccb029c7.js",
|
|
|
|
"ce968fcdf3a1987c.js",
|
|
|
|
"db3c01738aaf0b92.js",
|
|
|
|
"e1387fe892984e2b.js",
|
|
|
|
"e71c1d5f0b6b833c.js",
|
|
|
|
"e8ea384458526db0.js",
|
|
|
|
// We don't implement Annex B fully.
|
|
|
|
"1c1e2a43fe5515b6.js",
|
|
|
|
"3dabeca76119d501.js",
|
|
|
|
"52aeec7b8da212a2.js",
|
|
|
|
"59ae0289778b80cd.js",
|
|
|
|
"a4d62a651f69d815.js",
|
|
|
|
"c06df922631aeabc.js",
|
|
|
|
];
|
|
|
|
|
|
|
|
fn add_test<F: FnOnce() + Send + 'static>(
|
|
|
|
tests: &mut Vec<TestDescAndFn>,
|
|
|
|
name: String,
|
|
|
|
ignore: bool,
|
|
|
|
f: F,
|
|
|
|
) {
|
|
|
|
tests.push(TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2019-10-23 14:19:49 +03:00
|
|
|
test_type: TestType::UnitTest,
|
2018-10-25 07:17:05 +03:00
|
|
|
name: TestName::DynTestName(name),
|
|
|
|
ignore,
|
|
|
|
should_panic: No,
|
|
|
|
allow_fail: false,
|
|
|
|
},
|
2020-07-23 20:18:22 +03:00
|
|
|
testfn: DynTestFn(Box::new(f)),
|
2018-10-25 07:17:05 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:52:47 +03:00
|
|
|
fn error_tests(tests: &mut Vec<TestDescAndFn>, minify: bool) -> Result<(), io::Error> {
|
2018-10-25 07:17:05 +03:00
|
|
|
let ref_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("tests")
|
2021-08-03 18:52:47 +03:00
|
|
|
.join(if minify { "test262-min" } else { "test262" });
|
2018-10-25 07:17:05 +03:00
|
|
|
let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("parser")
|
|
|
|
.join("tests")
|
|
|
|
.join("test262-parser")
|
|
|
|
.join("pass");
|
|
|
|
|
|
|
|
eprintln!("Loading tests from {}", dir.display());
|
|
|
|
|
|
|
|
for entry in read_dir(&dir).expect("failed to read directory") {
|
|
|
|
let entry = entry?;
|
|
|
|
let file_name = entry
|
|
|
|
.path()
|
|
|
|
.strip_prefix(&dir)
|
|
|
|
.expect("failed to strip prefix")
|
|
|
|
.to_str()
|
|
|
|
.expect("to_str() failed")
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let input = {
|
|
|
|
let mut buf = String::new();
|
|
|
|
File::open(entry.path())?.read_to_string(&mut buf)?;
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
|
|
|
let ignore = IGNORED_PASS_TESTS.contains(&&*file_name);
|
|
|
|
|
|
|
|
let module = file_name.contains("module");
|
|
|
|
|
|
|
|
let ref_dir = ref_dir.clone();
|
2021-08-03 18:52:47 +03:00
|
|
|
let name = format!(
|
|
|
|
"test262::golden::{}::{}",
|
|
|
|
if minify { "minify" } else { "normal" },
|
|
|
|
file_name
|
|
|
|
);
|
2018-10-25 07:17:05 +03:00
|
|
|
|
|
|
|
add_test(tests, name, ignore, move || {
|
|
|
|
let msg = format!(
|
|
|
|
"\n\n========== Running codegen test {}\nSource:\n{}\n",
|
|
|
|
file_name, input
|
|
|
|
);
|
|
|
|
let mut wr = Buf(Arc::new(RwLock::new(vec![])));
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
::testing::run_test(false, |cm, handler| {
|
2018-10-25 07:17:05 +03:00
|
|
|
let src = cm.load_file(&entry.path()).expect("failed to load file");
|
|
|
|
eprintln!(
|
|
|
|
"{}\nPos: {:?} ~ {:?} (L{})",
|
|
|
|
msg,
|
|
|
|
src.start_pos,
|
|
|
|
src.end_pos,
|
|
|
|
src.count_lines()
|
|
|
|
);
|
|
|
|
|
2020-07-31 12:49:07 +03:00
|
|
|
let comments = SingleThreadedComments::default();
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2019-01-08 10:34:35 +03:00
|
|
|
(&*src).into(),
|
2019-02-26 07:56:58 +03:00
|
|
|
Some(&comments),
|
2019-01-08 10:34:35 +03:00
|
|
|
);
|
2020-07-31 12:49:07 +03:00
|
|
|
let mut parser: Parser<Lexer<StringInput>> = Parser::new_from(lexer);
|
2018-10-25 07:17:05 +03:00
|
|
|
|
|
|
|
{
|
2021-08-03 18:52:47 +03:00
|
|
|
let mut wr = Box::new(swc_ecma_codegen::text_writer::JsWriter::new(
|
|
|
|
cm.clone(),
|
|
|
|
"\n",
|
|
|
|
&mut wr,
|
|
|
|
None,
|
|
|
|
)) as Box<dyn WriteJs>;
|
|
|
|
|
|
|
|
if minify {
|
|
|
|
wr = Box::new(swc_ecma_codegen::text_writer::omit_trailing_semi(wr));
|
|
|
|
}
|
|
|
|
|
2018-10-25 07:17:05 +03:00
|
|
|
let mut emitter = Emitter {
|
2021-08-03 18:52:47 +03:00
|
|
|
cfg: swc_ecma_codegen::Config { minify },
|
2018-11-10 11:44:35 +03:00
|
|
|
cm: cm.clone(),
|
2021-08-03 18:52:47 +03:00
|
|
|
wr,
|
|
|
|
comments: if minify { None } else { Some(&comments) },
|
2018-10-25 07:17:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Parse source
|
|
|
|
if module {
|
2019-01-07 13:43:47 +03:00
|
|
|
emitter
|
2020-07-25 14:26:04 +03:00
|
|
|
.emit_module(
|
|
|
|
&parser
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|e| e.into_diagnostic(handler).emit())?,
|
|
|
|
)
|
2019-01-07 13:43:47 +03:00
|
|
|
.unwrap();
|
2018-10-25 07:17:05 +03:00
|
|
|
} else {
|
2019-01-07 13:43:47 +03:00
|
|
|
emitter
|
2020-07-25 14:26:04 +03:00
|
|
|
.emit_script(
|
|
|
|
&parser
|
|
|
|
.parse_script()
|
|
|
|
.map_err(|e| e.into_diagnostic(handler).emit())?,
|
|
|
|
)
|
2019-01-07 13:43:47 +03:00
|
|
|
.unwrap();
|
2018-10-25 07:17:05 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-10 11:44:35 +03:00
|
|
|
let ref_file = format!("{}", ref_dir.join(&file_name).display());
|
|
|
|
|
|
|
|
let code_output = wr.0.read().unwrap();
|
|
|
|
let with_srcmap =
|
|
|
|
NormalizedOutput::from(String::from_utf8_lossy(&code_output).into_owned());
|
|
|
|
with_srcmap.compare_to_file(ref_file).unwrap();
|
2018-11-16 14:09:17 +03:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("failed to run test");
|
2018-10-25 07:17:05 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn identity() {
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
let mut tests = Vec::new();
|
2021-08-04 06:31:17 +03:00
|
|
|
if !cfg!(target_os = "windows") {
|
|
|
|
error_tests(&mut tests, true).expect("failed to load testss");
|
|
|
|
}
|
2021-08-03 18:52:47 +03:00
|
|
|
error_tests(&mut tests, false).expect("failed to load testss");
|
2019-10-02 07:53:56 +03:00
|
|
|
test_main(&args, tests, Some(Options::new()));
|
2018-10-25 07:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Buf(Arc<RwLock<Vec<u8>>>);
|
|
|
|
impl Write for Buf {
|
|
|
|
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.write().unwrap().write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.0.write().unwrap().flush()
|
|
|
|
}
|
|
|
|
}
|