feat(transformer): add unit tests and test coverage (#1001)

This commit is contained in:
Boshen 2023-10-16 14:07:29 +08:00 committed by GitHub
parent cc6c52e536
commit c060621512
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 3 deletions

View file

@ -33,12 +33,11 @@ fn main() {
println!("Original:\n");
println!("{printed}");
let program = allocator.alloc(ret.program);
let semantic = SemanticBuilder::new(&source_text, source_type).build(program).semantic;
let semantic = SemanticBuilder::new(&source_text, source_type).build(&ret.program).semantic;
let (symbols, _scope_tree) = semantic.into_symbol_table_and_scope_tree();
let symbols = Rc::new(RefCell::new(symbols));
let program = allocator.alloc(ret.program);
let transform_options = TransformOptions {
target: TransformTarget::ES2015,
react: Some(TransformReactOptions::default()),

View file

@ -255,3 +255,21 @@ fn gather_node_parts(expr: &Expression, parts: &mut std::vec::Vec<Atom>) {
_ => parts.push(Atom::from("ref")),
}
}
#[test]
fn test() {
use crate::{
options::{TransformOptions, TransformTarget},
tester::Tester,
};
let options =
TransformOptions { target: TransformTarget::ES2015, ..TransformOptions::default() };
let tests = &[(
"let x = {}; let y = 0; let z = 0; x[z++] **= y;",
"var _ref; let x = {}; let y = 0; let z = 0; _ref = z++,x[_ref] = Math.pow(x[_ref], y);",
)];
Tester::new("test.js", options).test(tests);
}

View file

@ -15,6 +15,8 @@ mod es2022;
mod options;
mod react_jsx;
mod regexp;
#[cfg(test)]
mod tester;
mod typescript;
use std::{cell::RefCell, rc::Rc};

View file

@ -0,0 +1,50 @@
use std::{cell::RefCell, rc::Rc};
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use crate::{TransformOptions, Transformer};
pub struct Tester {
source_type: SourceType,
options: TransformOptions,
allocator: Allocator,
}
impl Tester {
pub fn new(filename: &str, options: TransformOptions) -> Self {
let source_type = SourceType::from_path(filename).unwrap();
Self { source_type, options, allocator: Allocator::default() }
}
pub fn test(&self, tests: &[(&str, &str)]) {
for (source_text, expected) in tests {
let transformed = self.transform(source_text);
let expected = self.codegen(expected);
assert_eq!(transformed, expected, "{source_text}");
}
}
fn transform(&self, source_text: &str) -> String {
let program = Parser::new(&self.allocator, source_text, self.source_type).parse().program;
let semantic = SemanticBuilder::new(source_text, self.source_type).build(&program).semantic;
let (symbols, _scope_tree) = semantic.into_symbol_table_and_scope_tree();
let symbols = Rc::new(RefCell::new(symbols));
let program = self.allocator.alloc(program);
Transformer::new(&self.allocator, self.source_type, &symbols, self.options.clone())
.build(program);
Codegen::<false>::new(source_text.len(), CodegenOptions).build(program)
}
fn codegen(&self, source_text: &str) -> String {
let program = Parser::new(&self.allocator, source_text, self.source_type).parse().program;
Codegen::<false>::new(source_text.len(), CodegenOptions).build(&program)
}
}

View file

@ -15,6 +15,13 @@ use oxc_span::{SourceType, VALID_EXTENSIONS};
use oxc_tasks_common::{normalize_path, project_root};
use oxc_transformer::{TransformOptions, TransformReactOptions, TransformTarget, Transformer};
#[test]
#[cfg(any(coverage, coverage_nightly))]
fn test() {
babel(&BabelOptions::default());
}
#[derive(Default)]
pub struct BabelOptions {
pub filter: Option<String>,
}