2018-01-12 10:53:06 +03:00
|
|
|
#![feature(box_syntax)]
|
2019-02-24 08:12:04 +03:00
|
|
|
#![feature(box_patterns)]
|
2018-01-12 10:53:06 +03:00
|
|
|
#![feature(specialization)]
|
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
extern crate swc_common;
|
2018-11-17 06:30:49 +03:00
|
|
|
extern crate swc_ecma_ast;
|
2018-01-12 14:22:45 +03:00
|
|
|
extern crate swc_ecma_parser;
|
2018-01-12 10:53:06 +03:00
|
|
|
extern crate test;
|
|
|
|
extern crate testing;
|
2018-09-16 14:25:41 +03:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
fs::{read_dir, File},
|
|
|
|
io::{self, Read},
|
|
|
|
path::Path,
|
|
|
|
};
|
2018-11-25 05:31:48 +03:00
|
|
|
use swc_common::{Fold, FoldWith, Span};
|
2018-11-17 06:30:49 +03:00
|
|
|
use swc_ecma_ast::*;
|
2019-11-06 07:14:44 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, PResult, Parser, Session, SourceFileInput, Syntax};
|
2019-10-23 14:19:49 +03:00
|
|
|
use test::{
|
|
|
|
test_main, DynTestFn, Options, ShouldPanic::No, TestDesc, TestDescAndFn, TestName, TestType,
|
|
|
|
};
|
2018-11-16 14:09:17 +03:00
|
|
|
use testing::{NormalizedOutput, StdErr};
|
2018-01-12 10:53:06 +03:00
|
|
|
|
|
|
|
const IGNORED_PASS_TESTS: &[&str] = &[
|
2018-01-13 14:42:52 +03:00
|
|
|
// Temporalily ignored
|
|
|
|
"431ecef8c85d4d24.js",
|
|
|
|
"8386fbff927a9e0e.js",
|
2019-01-07 13:43:47 +03:00
|
|
|
"5654d4106d7025c2.js",
|
2018-01-12 10:53:06 +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-01-12 10:53:06 +03:00
|
|
|
name: TestName::DynTestName(name),
|
2018-06-02 12:01:00 +03:00
|
|
|
ignore,
|
2018-01-12 10:53:06 +03:00
|
|
|
should_panic: No,
|
|
|
|
allow_fail: false,
|
|
|
|
},
|
2019-03-23 07:18:55 +03:00
|
|
|
testfn: DynTestFn(box f),
|
2018-01-12 10:53:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:53:30 +03:00
|
|
|
fn error_tests(tests: &mut Vec<TestDescAndFn>) -> Result<(), io::Error> {
|
|
|
|
const IGNORED_ERROR_TESTS: &[&str] = &[
|
2018-11-17 06:30:49 +03:00
|
|
|
// Old (wrong) tests
|
|
|
|
"569a2c1bad3beeb2.js",
|
2018-01-26 15:53:30 +03:00
|
|
|
// Wrong tests
|
|
|
|
"0d5e450f1da8a92a.js",
|
2018-11-03 10:56:43 +03:00
|
|
|
"346316bef54d805a.js",
|
|
|
|
"976b6247ca78ab51.js",
|
|
|
|
"ae0a7ac275bc9f5c.js",
|
2018-01-26 15:53:30 +03:00
|
|
|
"748656edbfb2d0bb.js",
|
|
|
|
"79f882da06f88c9f.js",
|
2018-11-03 10:56:43 +03:00
|
|
|
"d28e80d99f819136.js",
|
2018-01-26 15:53:30 +03:00
|
|
|
"92b6af54adef3624.js",
|
|
|
|
"ef2d369cccc5386c.js",
|
|
|
|
// Temporarily ignore tests for using octal escape before use strict
|
|
|
|
"147fa078a7436e0e.js",
|
|
|
|
"15a6123f6b825c38.js",
|
|
|
|
"3bc2b27a7430f818.js",
|
|
|
|
];
|
|
|
|
|
|
|
|
let root = {
|
|
|
|
let mut root = Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf();
|
|
|
|
root.push("tests");
|
|
|
|
root.push("test262-parser");
|
|
|
|
root
|
|
|
|
};
|
|
|
|
|
|
|
|
eprintln!("Loading tests from {}", root.display());
|
|
|
|
|
2018-01-27 05:31:45 +03:00
|
|
|
const TYPES: &[&str] = &[
|
2018-06-02 12:01:00 +03:00
|
|
|
"fail", /* TODO
|
2019-03-08 05:36:23 +03:00
|
|
|
* "early" */
|
2018-01-26 15:53:30 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
for err_type in TYPES {
|
|
|
|
let dir = root.join(err_type);
|
2018-02-27 07:21:57 +03:00
|
|
|
let error_reference_dir = {
|
|
|
|
let mut root = Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf();
|
|
|
|
root.push("tests");
|
|
|
|
root.push("test262-error-references");
|
|
|
|
root.push(err_type);
|
|
|
|
root
|
|
|
|
};
|
2018-01-26 15:53:30 +03:00
|
|
|
|
|
|
|
for entry in read_dir(&dir)? {
|
|
|
|
let entry = entry?;
|
|
|
|
let file_name = entry
|
|
|
|
.path()
|
|
|
|
.strip_prefix(&dir)
|
2018-02-27 07:21:57 +03:00
|
|
|
.expect("failed to strip prefix")
|
2018-01-26 15:53:30 +03:00
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let input = {
|
|
|
|
let mut buf = String::new();
|
|
|
|
File::open(entry.path())?.read_to_string(&mut buf)?;
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
|
|
|
let ignore = IGNORED_ERROR_TESTS.contains(&&*file_name);
|
|
|
|
|
|
|
|
let module = file_name.contains("module");
|
|
|
|
|
2018-02-27 07:21:57 +03:00
|
|
|
let dir = dir.clone();
|
|
|
|
let error_reference_dir = error_reference_dir.clone();
|
|
|
|
let name = format!("test262::error_reporting::{}::{}", err_type, file_name);
|
2018-01-26 15:53:30 +03:00
|
|
|
add_test(tests, name, ignore, move || {
|
|
|
|
eprintln!(
|
2018-02-27 07:21:57 +03:00
|
|
|
"\n\n========== Running error reporting test {}\nSource:\n{}\n",
|
2018-01-26 15:53:30 +03:00
|
|
|
file_name, input
|
|
|
|
);
|
|
|
|
|
2018-02-27 07:21:57 +03:00
|
|
|
let path = dir.join(&file_name);
|
2018-01-26 15:53:30 +03:00
|
|
|
// Parse source
|
|
|
|
let err = if module {
|
2018-11-21 12:59:17 +03:00
|
|
|
parse_module(&path).expect_err("should fail, but parsed as")
|
2018-01-26 15:53:30 +03:00
|
|
|
} else {
|
2018-11-21 12:59:17 +03:00
|
|
|
parse_script(&path).expect_err("should fail, but parsed as")
|
2018-01-26 15:53:30 +03:00
|
|
|
};
|
|
|
|
|
2018-06-02 12:01:00 +03:00
|
|
|
if err
|
|
|
|
.compare_to_file(format!(
|
|
|
|
"{}.stderr",
|
|
|
|
error_reference_dir.join(file_name).display()
|
2018-10-25 07:17:05 +03:00
|
|
|
))
|
|
|
|
.is_err()
|
2018-02-27 07:21:57 +03:00
|
|
|
{
|
|
|
|
panic!()
|
|
|
|
}
|
2018-01-26 15:53:30 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn identity_tests(tests: &mut Vec<TestDescAndFn>) -> Result<(), io::Error> {
|
2018-01-12 10:53:06 +03:00
|
|
|
let root = {
|
|
|
|
let mut root = Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf();
|
|
|
|
root.push("tests");
|
|
|
|
root.push("test262-parser");
|
|
|
|
root
|
|
|
|
};
|
|
|
|
|
|
|
|
eprintln!("Loading tests from {}", root.display());
|
|
|
|
|
|
|
|
let pass_dir = root.join("pass");
|
|
|
|
|
|
|
|
let files = read_dir(&pass_dir)?;
|
|
|
|
|
|
|
|
for entry in files {
|
|
|
|
let entry = entry?;
|
|
|
|
let file_name = entry
|
|
|
|
.path()
|
|
|
|
.strip_prefix(&pass_dir)
|
2018-02-27 07:21:57 +03:00
|
|
|
.expect("failed to strip prefix")
|
2018-01-12 10:53:06 +03:00
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let input = {
|
|
|
|
let mut buf = String::new();
|
|
|
|
File::open(entry.path())?.read_to_string(&mut buf)?;
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
let explicit = {
|
|
|
|
let mut buf = String::new();
|
|
|
|
File::open(root.join("pass-explicit").join(&file_name))?.read_to_string(&mut buf)?;
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
|
|
|
let ignore = IGNORED_PASS_TESTS.contains(&&*file_name);
|
|
|
|
|
|
|
|
let module = file_name.contains("module");
|
|
|
|
|
2018-02-27 07:21:57 +03:00
|
|
|
let root = root.clone();
|
|
|
|
let name = format!("test262::identity::{}", file_name);
|
2018-01-12 10:53:06 +03:00
|
|
|
add_test(tests, name, ignore, move || {
|
2018-01-26 15:53:30 +03:00
|
|
|
eprintln!(
|
2018-02-27 07:21:57 +03:00
|
|
|
"\n\n\n========== Running test {}\nSource:\n{}\nExplicit:\n{}",
|
2018-01-12 10:53:06 +03:00
|
|
|
file_name, input, explicit
|
|
|
|
);
|
|
|
|
|
2018-01-26 15:53:30 +03:00
|
|
|
if module {
|
2018-11-21 12:59:17 +03:00
|
|
|
let p = |explicit| {
|
2018-02-27 07:21:57 +03:00
|
|
|
parse_module(
|
2018-11-21 12:59:17 +03:00
|
|
|
&root
|
|
|
|
.join(if explicit { "pass-explicit" } else { "pass" })
|
|
|
|
.join(&file_name),
|
2018-10-25 07:17:05 +03:00
|
|
|
)
|
|
|
|
.map(normalize)
|
2018-09-16 14:25:41 +03:00
|
|
|
.unwrap()
|
2018-01-26 15:53:30 +03:00
|
|
|
};
|
2018-11-21 12:59:17 +03:00
|
|
|
let src = p(false);
|
|
|
|
let expected = p(true);
|
2018-01-26 15:53:30 +03:00
|
|
|
assert_eq!(src, expected);
|
2019-02-24 08:12:04 +03:00
|
|
|
|
|
|
|
let json =
|
|
|
|
serde_json::to_string_pretty(&src).expect("failed to serialize module as json");
|
|
|
|
|
|
|
|
let deser = serde_json::from_str::<Module>(&json)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
panic!(
|
|
|
|
"failed to deserialize json back to module: {}\n{}",
|
|
|
|
err, json
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.fold_with(&mut Normalizer);
|
|
|
|
assert_eq!(src, deser, "JSON:\n{}", json);
|
2018-01-26 15:53:30 +03:00
|
|
|
} else {
|
2018-11-21 12:59:17 +03:00
|
|
|
let p = |explicit| {
|
2018-02-27 07:21:57 +03:00
|
|
|
parse_script(
|
2018-11-21 12:59:17 +03:00
|
|
|
&root
|
|
|
|
.join(if explicit { "pass-explicit" } else { "pass" })
|
|
|
|
.join(&file_name),
|
2018-10-25 07:17:05 +03:00
|
|
|
)
|
|
|
|
.map(normalize)
|
2018-09-16 14:25:41 +03:00
|
|
|
.unwrap()
|
2018-01-26 15:53:30 +03:00
|
|
|
};
|
2018-11-21 12:59:17 +03:00
|
|
|
let src = p(false);
|
|
|
|
let expected = p(true);
|
2018-01-26 15:53:30 +03:00
|
|
|
assert_eq!(src, expected);
|
2018-01-12 10:53:06 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-13 05:08:21 +03:00
|
|
|
fn parse_script(file_name: &Path) -> Result<Script, NormalizedOutput> {
|
2018-11-21 12:59:17 +03:00
|
|
|
with_parser(file_name, |p| p.parse_script())
|
2018-01-12 10:53:06 +03:00
|
|
|
}
|
2018-11-21 12:59:17 +03:00
|
|
|
fn parse_module<'a>(file_name: &Path) -> Result<Module, NormalizedOutput> {
|
|
|
|
with_parser(file_name, |p| p.parse_module())
|
2018-01-21 11:47:37 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:59:17 +03:00
|
|
|
fn with_parser<F, Ret>(file_name: &Path, f: F) -> Result<Ret, StdErr>
|
2018-02-27 07:21:57 +03:00
|
|
|
where
|
2019-11-06 07:14:44 +03:00
|
|
|
F: for<'a> FnOnce(&mut Parser<'a, Lexer<'a, SourceFileInput>>) -> PResult<'a, Ret>,
|
2018-02-27 07:21:57 +03:00
|
|
|
{
|
2019-01-07 13:43:47 +03:00
|
|
|
let output = ::testing::run_test(false, |cm, handler| {
|
2018-11-21 12:59:17 +03:00
|
|
|
let fm = cm
|
|
|
|
.load_file(file_name)
|
|
|
|
.unwrap_or_else(|e| panic!("failed to load {}: {}", file_name.display(), e));
|
2018-01-23 15:38:48 +03:00
|
|
|
|
2018-02-27 07:21:57 +03:00
|
|
|
let res = f(&mut Parser::new(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2018-01-23 15:38:48 +03:00
|
|
|
(&*fm).into(),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2019-01-07 13:43:47 +03:00
|
|
|
))
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
()
|
|
|
|
});
|
2018-02-27 07:21:57 +03:00
|
|
|
|
2018-11-18 08:00:07 +03:00
|
|
|
res
|
2018-02-27 07:21:57 +03:00
|
|
|
});
|
|
|
|
|
2018-11-16 14:09:17 +03:00
|
|
|
output
|
2018-01-12 10:53:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-01-26 15:53:30 +03:00
|
|
|
fn identity() {
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
let mut tests = Vec::new();
|
|
|
|
identity_tests(&mut tests).unwrap();
|
2019-10-02 07:53:56 +03:00
|
|
|
test_main(&args, tests, Some(Options::new()));
|
2018-01-26 15:53:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error() {
|
2018-01-12 10:53:06 +03:00
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
let mut tests = Vec::new();
|
2018-01-26 15:53:30 +03:00
|
|
|
error_tests(&mut tests).unwrap();
|
2019-10-02 07:53:56 +03:00
|
|
|
test_main(&args, tests, Some(Options::new()));
|
2018-01-12 10:53:06 +03:00
|
|
|
}
|
2018-10-25 07:17:05 +03:00
|
|
|
|
|
|
|
pub fn normalize<T>(t: T) -> T
|
|
|
|
where
|
|
|
|
Normalizer: Fold<T>,
|
|
|
|
{
|
|
|
|
let mut n = Normalizer;
|
|
|
|
n.fold(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Normalizer;
|
|
|
|
impl Fold<Span> for Normalizer {
|
|
|
|
fn fold(&mut self, _: Span) -> Span {
|
|
|
|
Span::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Fold<Str> for Normalizer {
|
|
|
|
fn fold(&mut self, s: Str) -> Str {
|
|
|
|
Str {
|
|
|
|
span: Default::default(),
|
|
|
|
has_escape: false,
|
|
|
|
..s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Fold<Expr> for Normalizer {
|
|
|
|
fn fold(&mut self, e: Expr) -> Expr {
|
|
|
|
let e = e.fold_children(self);
|
|
|
|
|
|
|
|
match e {
|
|
|
|
Expr::Paren(ParenExpr { expr, .. }) => *expr,
|
2019-01-07 13:43:47 +03:00
|
|
|
Expr::New(n @ NewExpr { args: None, .. }) => Expr::New(NewExpr {
|
2018-10-25 07:17:05 +03:00
|
|
|
args: Some(vec![]),
|
2019-01-07 13:43:47 +03:00
|
|
|
..n
|
2018-10-25 07:17:05 +03:00
|
|
|
}),
|
|
|
|
// Flatten comma expressions.
|
|
|
|
Expr::Seq(SeqExpr { mut exprs, span }) => {
|
|
|
|
let need_work = exprs.iter().any(|n| match **n {
|
|
|
|
Expr::Seq(..) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
|
|
|
|
if need_work {
|
|
|
|
exprs = exprs.into_iter().fold(vec![], |mut v, e| {
|
|
|
|
match *e {
|
|
|
|
Expr::Seq(SeqExpr { exprs, .. }) => v.extend(exprs),
|
|
|
|
_ => v.push(e),
|
|
|
|
}
|
|
|
|
v
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Expr::Seq(SeqExpr { exprs, span })
|
|
|
|
}
|
|
|
|
_ => e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Fold<PropName> for Normalizer {
|
|
|
|
fn fold(&mut self, n: PropName) -> PropName {
|
|
|
|
let n = n.fold_children(self);
|
|
|
|
|
|
|
|
match n {
|
|
|
|
PropName::Ident(Ident { sym, .. }) => PropName::Str(Str {
|
|
|
|
span: Default::default(),
|
|
|
|
value: sym,
|
|
|
|
has_escape: false,
|
|
|
|
}),
|
|
|
|
PropName::Num(num) => PropName::Str(Str {
|
|
|
|
span: Default::default(),
|
|
|
|
value: num.to_string().into(),
|
|
|
|
has_escape: false,
|
|
|
|
}),
|
|
|
|
_ => n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-24 08:12:04 +03:00
|
|
|
|
|
|
|
impl Fold<PatOrExpr> for Normalizer {
|
|
|
|
fn fold(&mut self, node: PatOrExpr) -> PatOrExpr {
|
|
|
|
let node = node.fold_children(self);
|
|
|
|
|
|
|
|
match node {
|
|
|
|
PatOrExpr::Pat(box Pat::Expr(e)) => PatOrExpr::Expr(e),
|
|
|
|
PatOrExpr::Expr(box Expr::Ident(i)) => PatOrExpr::Pat(box Pat::Ident(i)),
|
|
|
|
_ => node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|