Skip to content

Commit

Permalink
Add parse_export tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan committed Jul 28, 2017
1 parent a5681f9 commit 2539f17
Showing 1 changed file with 114 additions and 2 deletions.
116 changes: 114 additions & 2 deletions parcel/src/es6.rs
Expand Up @@ -15,15 +15,15 @@ macro_rules! expected {
}};
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum Export<'s> {
Default(&'s str),
AllFrom(Cow<'s, str>),
Named(Vec<ExportSpec<'s>>),
NamedFrom(Vec<ExportSpec<'s>>, Cow<'s, str>),
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExportSpec<'s> {
bind: &'s str,
name: &'s str,
Expand Down Expand Up @@ -979,4 +979,116 @@ mod test {
"')'",
).unwrap();
}

fn assert_export_form(source: &str, result: Export, out: &str) {
let mut lexer = lex::Lexer::new_unnamed(source);
assert_eq!(lexer.advance().tt, Tt::Export);
let mut output = String::new();
assert_eq!(parse_export(&mut lexer, &mut output).unwrap(), result);
assert_eq!(output, out);
}

fn assert_export_form_panic(source: &str) {
let mut lexer = lex::Lexer::new_unnamed(source);
assert_eq!(lexer.advance().tt, Tt::Export);
let mut output = String::new();
parse_export(&mut lexer, &mut output).unwrap_err();
}

#[test]
fn test_export_default() {
assert_export_form(
"export //\ndefault /* comment */ 0 _next",
Export::Default("__default"),
// " //\n /* comment */ 0",
" //\nconst __default = ",
);
assert_export_form(
"export default class Test {} _next",
Export::Default("Test"),
// " class Test {}",
" class Test",
);
assert_export_form(
"export default function test() {} _next",
Export::Default("test"),
// " function test() {}",
" function test",
);
assert_export_form(
"export default function* testGen() {} _next",
Export::Default("testGen"),
// " function* testGen() {}",
" function* testGen",
);
}

#[test]
fn test_export_default_exprs_panic() {
assert_export_form_panic("export default class {} _next");
assert_export_form_panic("export default function() {} _next");
assert_export_form_panic("export default function*() {} _next");
}

#[test]
fn test_export_binding() {
assert_export_form(
"export var asdf _next",
Export::Named(vec![ExportSpec::same("asdf")]),
" var asdf",
);
assert_export_form(
"export let a = 1, b = (1, 2), c = 3, d = (za, zb) => b, e _next",
Export::Named(vec![
ExportSpec::same("a"),
ExportSpec::same("b"),
ExportSpec::same("c"),
ExportSpec::same("d"),
ExportSpec::same("e"),
]),
" let a = 1, b = (1, 2), c = 3, d = (za, zb) => b, e",
);
assert_export_form(
"export const j = class A extends B(c, d) {}, k = 1 _next",
Export::Named(vec![
ExportSpec::same("j"),
ExportSpec::same("k"),
]),
" const j = class A extends B(c, d) {}, k = 1",
);
}

#[test]
fn test_export_hoistable_declaration() {
assert_export_form(
"export class Test2 {} _next",
Export::Named(vec![ExportSpec::same("Test2")]),
// " class Test2 {}",
" class Test2",
);
assert_export_form(
"export function test2() {} _next",
Export::Named(vec![ExportSpec::same("test2")]),
// " function test2() {}",
" function test2",
);
assert_export_form(
"export function* testGen2() {} _next",
Export::Named(vec![ExportSpec::same("testGen2")]),
// " function* testGen2() {}",
" function* testGen2",
);
}

#[test]
fn test_export_list() {
assert_export_form(
"export {va as vaz, vb} _next",
Export::Named(vec![
ExportSpec::new("va", "vaz"),
ExportSpec::same("vb"),
]),
" ",
);
}
}

0 comments on commit 2539f17

Please sign in to comment.