mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transformer): add unit tests and test coverage (#1001)
This commit is contained in:
parent
cc6c52e536
commit
c060621512
5 changed files with 79 additions and 3 deletions
|
|
@ -33,12 +33,11 @@ fn main() {
|
||||||
println!("Original:\n");
|
println!("Original:\n");
|
||||||
println!("{printed}");
|
println!("{printed}");
|
||||||
|
|
||||||
let program = allocator.alloc(ret.program);
|
let semantic = SemanticBuilder::new(&source_text, source_type).build(&ret.program).semantic;
|
||||||
|
|
||||||
let semantic = SemanticBuilder::new(&source_text, source_type).build(program).semantic;
|
|
||||||
let (symbols, _scope_tree) = semantic.into_symbol_table_and_scope_tree();
|
let (symbols, _scope_tree) = semantic.into_symbol_table_and_scope_tree();
|
||||||
let symbols = Rc::new(RefCell::new(symbols));
|
let symbols = Rc::new(RefCell::new(symbols));
|
||||||
|
|
||||||
|
let program = allocator.alloc(ret.program);
|
||||||
let transform_options = TransformOptions {
|
let transform_options = TransformOptions {
|
||||||
target: TransformTarget::ES2015,
|
target: TransformTarget::ES2015,
|
||||||
react: Some(TransformReactOptions::default()),
|
react: Some(TransformReactOptions::default()),
|
||||||
|
|
|
||||||
|
|
@ -255,3 +255,21 @@ fn gather_node_parts(expr: &Expression, parts: &mut std::vec::Vec<Atom>) {
|
||||||
_ => parts.push(Atom::from("ref")),
|
_ => 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);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ mod es2022;
|
||||||
mod options;
|
mod options;
|
||||||
mod react_jsx;
|
mod react_jsx;
|
||||||
mod regexp;
|
mod regexp;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tester;
|
||||||
mod typescript;
|
mod typescript;
|
||||||
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
|
||||||
50
crates/oxc_transformer/src/tester.rs
Normal file
50
crates/oxc_transformer/src/tester.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -15,6 +15,13 @@ use oxc_span::{SourceType, VALID_EXTENSIONS};
|
||||||
use oxc_tasks_common::{normalize_path, project_root};
|
use oxc_tasks_common::{normalize_path, project_root};
|
||||||
use oxc_transformer::{TransformOptions, TransformReactOptions, TransformTarget, Transformer};
|
use oxc_transformer::{TransformOptions, TransformReactOptions, TransformTarget, Transformer};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(coverage, coverage_nightly))]
|
||||||
|
fn test() {
|
||||||
|
babel(&BabelOptions::default());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct BabelOptions {
|
pub struct BabelOptions {
|
||||||
pub filter: Option<String>,
|
pub filter: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue