diff --git a/Cargo.lock b/Cargo.lock index 1384dc0d0d..95b698ea52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -417,6 +417,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dir-test" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "035f15b4ae5cc91ca448fe9668799e3d9b09fd7381a7004e232769ff0efabb79" +dependencies = [ + "dir-test-macros", +] + +[[package]] +name = "dir-test-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15699f408d3f58a1259b624d3c127721a442555136933d33cf387b81f5c9c89c" +dependencies = [ + "glob", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dot2" version = "0.1.0" @@ -638,7 +659,6 @@ dependencies = [ "fe-common", "fe-driver", "fe-test-files", - "fe-test-utils-macros", "fe-yulc", "getrandom 0.2.8", "hex", @@ -745,11 +765,14 @@ dependencies = [ name = "fe-parser2" version = "0.20.0-alpha" dependencies = [ + "dir-test", "fe-compiler-test-utils", "fxhash", "lazy_static", "logos", "rowan", + "wasm-bindgen", + "wasm-bindgen-test", ] [[package]] @@ -760,15 +783,6 @@ dependencies = [ "include_dir", ] -[[package]] -name = "fe-test-utils-macros" -version = "0.20.0-alpha" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "fe-yulc" version = "0.20.0-alpha" @@ -857,6 +871,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "half" version = "1.8.2" @@ -1080,12 +1100,6 @@ version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" -[[package]] -name = "libm" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" - [[package]] name = "linked-hash-map" version = "0.5.6" @@ -1195,7 +1209,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg", - "libm", ] [[package]] @@ -1402,9 +1415,9 @@ dependencies = [ [[package]] name = "proptest" -version = "1.1.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29f1b898011ce9595050a68e60f90bad083ff2987a695a42357134c8381fba70" +checksum = "1e0d9cc07f18492d879586c92b485def06bc850da3118075cd45d50e9c95b0e5" dependencies = [ "bitflags", "byteorder", @@ -1415,7 +1428,6 @@ dependencies = [ "rand_chacha 0.3.1", "rand_xorshift", "regex-syntax", - "unarray", ] [[package]] @@ -2021,12 +2033,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "unarray" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" - [[package]] name = "unescape" version = "0.1.0" diff --git a/crates/analyzer/src/namespace/items.rs b/crates/analyzer/src/namespace/items.rs index 05a446ae3d..8179feb4bd 100644 --- a/crates/analyzer/src/namespace/items.rs +++ b/crates/analyzer/src/namespace/items.rs @@ -1665,8 +1665,8 @@ impl ImplId { || other == Type::SelfType(TraitOrType::TypeId(self.receiver(db))).id(db) } - /// Returns `true` if the `type_in_impl` can stand in for the `type_in_trait` as a type used - /// for a parameter or as a return type + /// Returns `true` if the `type_in_impl` can stand in for the + /// `type_in_trait` as a type used for a parameter or as a return type pub fn can_stand_in_for( &self, db: &dyn AnalyzerDb, diff --git a/crates/parser2/Cargo.toml b/crates/parser2/Cargo.toml index 42c872ba2d..5a1955b25a 100644 --- a/crates/parser2/Cargo.toml +++ b/crates/parser2/Cargo.toml @@ -16,4 +16,9 @@ fxhash = "0.2.1" lazy_static = "1.4.0" [dev-dependencies] -fe-compiler-test-utils = { path = "../test-utils" } \ No newline at end of file +fe-compiler-test-utils = { path = "../test-utils" } +dir-test = "0.1" +wasm-bindgen-test = "0.3" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = "0.2" \ No newline at end of file diff --git a/crates/parser2/test_files/syntax_node/exprs/if_.fe b/crates/parser2/test_files/syntax_node/exprs/if.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/exprs/if_.fe rename to crates/parser2/test_files/syntax_node/exprs/if.fe diff --git a/crates/parser2/test_files/syntax_node/exprs/if_.snap b/crates/parser2/test_files/syntax_node/exprs/if.snap similarity index 98% rename from crates/parser2/test_files/syntax_node/exprs/if_.snap rename to crates/parser2/test_files/syntax_node/exprs/if.snap index 1513eb3dd8..7930ec57ee 100644 --- a/crates/parser2/test_files/syntax_node/exprs/if_.snap +++ b/crates/parser2/test_files/syntax_node/exprs/if.snap @@ -1,6 +1,7 @@ --- source: crates/parser2/tests/syntax_node.rs -expression: snapshot +expression: node +input_file: crates/parser2/test_files/syntax_node/exprs/if.fe --- Root@0..279 IfExpr@0..15 diff --git a/crates/parser2/test_files/syntax_node/exprs/match_.fe b/crates/parser2/test_files/syntax_node/exprs/match.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/exprs/match_.fe rename to crates/parser2/test_files/syntax_node/exprs/match.fe diff --git a/crates/parser2/test_files/syntax_node/exprs/match_.snap b/crates/parser2/test_files/syntax_node/exprs/match.snap similarity index 99% rename from crates/parser2/test_files/syntax_node/exprs/match_.snap rename to crates/parser2/test_files/syntax_node/exprs/match.snap index 80a8790a4f..b5cbf36cdd 100644 --- a/crates/parser2/test_files/syntax_node/exprs/match_.snap +++ b/crates/parser2/test_files/syntax_node/exprs/match.snap @@ -1,6 +1,7 @@ --- source: crates/parser2/tests/syntax_node.rs -expression: snapshot +expression: node +input_file: crates/parser2/test_files/syntax_node/exprs/match.fe --- Root@0..516 MatchExpr@0..10 diff --git a/crates/parser2/test_files/syntax_node/items/const_.fe b/crates/parser2/test_files/syntax_node/items/const.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/items/const_.fe rename to crates/parser2/test_files/syntax_node/items/const.fe diff --git a/crates/parser2/test_files/syntax_node/items/const_.snap b/crates/parser2/test_files/syntax_node/items/const.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/items/const_.snap rename to crates/parser2/test_files/syntax_node/items/const.snap diff --git a/crates/parser2/test_files/syntax_node/items/extern_.fe b/crates/parser2/test_files/syntax_node/items/extern.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/items/extern_.fe rename to crates/parser2/test_files/syntax_node/items/extern.fe diff --git a/crates/parser2/test_files/syntax_node/items/extern_.snap b/crates/parser2/test_files/syntax_node/items/extern.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/items/extern_.snap rename to crates/parser2/test_files/syntax_node/items/extern.snap diff --git a/crates/parser2/test_files/syntax_node/items/impl_.fe b/crates/parser2/test_files/syntax_node/items/impl.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/items/impl_.fe rename to crates/parser2/test_files/syntax_node/items/impl.fe diff --git a/crates/parser2/test_files/syntax_node/items/impl_.snap b/crates/parser2/test_files/syntax_node/items/impl.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/items/impl_.snap rename to crates/parser2/test_files/syntax_node/items/impl.snap diff --git a/crates/parser2/test_files/syntax_node/items/trait_.fe b/crates/parser2/test_files/syntax_node/items/trait.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/items/trait_.fe rename to crates/parser2/test_files/syntax_node/items/trait.fe diff --git a/crates/parser2/test_files/syntax_node/items/trait_.snap b/crates/parser2/test_files/syntax_node/items/trait.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/items/trait_.snap rename to crates/parser2/test_files/syntax_node/items/trait.snap diff --git a/crates/parser2/test_files/syntax_node/items/type_.fe b/crates/parser2/test_files/syntax_node/items/type.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/items/type_.fe rename to crates/parser2/test_files/syntax_node/items/type.fe diff --git a/crates/parser2/test_files/syntax_node/items/type_.snap b/crates/parser2/test_files/syntax_node/items/type.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/items/type_.snap rename to crates/parser2/test_files/syntax_node/items/type.snap diff --git a/crates/parser2/test_files/syntax_node/items/use_.fe b/crates/parser2/test_files/syntax_node/items/use.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/items/use_.fe rename to crates/parser2/test_files/syntax_node/items/use.fe diff --git a/crates/parser2/test_files/syntax_node/items/use_.snap b/crates/parser2/test_files/syntax_node/items/use.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/items/use_.snap rename to crates/parser2/test_files/syntax_node/items/use.snap diff --git a/crates/parser2/test_files/syntax_node/stmts/for_.fe b/crates/parser2/test_files/syntax_node/stmts/for.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/stmts/for_.fe rename to crates/parser2/test_files/syntax_node/stmts/for.fe diff --git a/crates/parser2/test_files/syntax_node/stmts/for_.snap b/crates/parser2/test_files/syntax_node/stmts/for.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/stmts/for_.snap rename to crates/parser2/test_files/syntax_node/stmts/for.snap diff --git a/crates/parser2/test_files/syntax_node/stmts/let_.fe b/crates/parser2/test_files/syntax_node/stmts/let.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/stmts/let_.fe rename to crates/parser2/test_files/syntax_node/stmts/let.fe diff --git a/crates/parser2/test_files/syntax_node/stmts/let_.snap b/crates/parser2/test_files/syntax_node/stmts/let.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/stmts/let_.snap rename to crates/parser2/test_files/syntax_node/stmts/let.snap diff --git a/crates/parser2/test_files/syntax_node/stmts/while_.fe b/crates/parser2/test_files/syntax_node/stmts/while.fe similarity index 100% rename from crates/parser2/test_files/syntax_node/stmts/while_.fe rename to crates/parser2/test_files/syntax_node/stmts/while.fe diff --git a/crates/parser2/test_files/syntax_node/stmts/while_.snap b/crates/parser2/test_files/syntax_node/stmts/while.snap similarity index 100% rename from crates/parser2/test_files/syntax_node/stmts/while_.snap rename to crates/parser2/test_files/syntax_node/stmts/while.snap diff --git a/crates/parser2/tests/error_recovery.rs b/crates/parser2/tests/error_recovery.rs index 1154c0a2ad..18635f1e91 100644 --- a/crates/parser2/tests/error_recovery.rs +++ b/crates/parser2/tests/error_recovery.rs @@ -1,64 +1,78 @@ -use fe_parser2::{ - parser::{expr::parse_expr, item::ItemListScope, stmt::parse_stmt}, - syntax_node::SyntaxNode, -}; +use dir_test::{dir_test, Fixture}; + +use fe_compiler_test_utils::snap_test; + mod test_runner; use test_runner::*; -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/error_recovery/items", - "parser2/test_files/error_recovery/items", - test_item_list -} -fn test_item_list(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - parser.parse(ItemListScope::default(), None); - }, - false, - ); - runner.run(input) -} -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/error_recovery/exprs", - "parser2/test_files/error_recovery/exprs", - test_expr +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/error_recovery/items", + glob: "*.fe" +)] +fn test_item_list(fixture: Fixture<&str>) { + let runner = TestRunner::item_list(false); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fn test_expr(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - parser.set_newline_as_trivia(false); - bump_newlines(parser); - while parser.current_kind().is_some() { - bump_newlines(parser); - parse_expr(parser); - bump_newlines(parser); - } - }, - false, - ); - runner.run(input) +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/error_recovery/stmts", + glob: "*.fe" +)] +fn test_stmt(fixture: Fixture<&str>) { + let runner = TestRunner::stmt_list(false); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/error_recovery/stmts", - "parser2/test_files/error_recovery/stmts", - test_stmt +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/error_recovery/exprs", + glob: "*.fe" +)] +fn test_expr(fixture: Fixture<&str>) { + let runner = TestRunner::expr_list(false); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fn test_stmt(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - parser.set_newline_as_trivia(false); - bump_newlines(parser); - while parser.current_kind().is_some() { - bump_newlines(parser); - parse_stmt(parser, None); - bump_newlines(parser); - } - }, - false, - ); - runner.run(input) +#[cfg(target_family = "wasm")] +mod wasm { + use super::*; + use wasm_bindgen_test::wasm_bindgen_test; + + #[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/error_recovery/items", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_item_list(fixture: Fixture<&str>) { + TestRunner::item_list(false).run(fixture.content()); + } + + #[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/error_recovery/stmts", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_stmt(fixture: Fixture<&str>) { + TestRunner::stmt_list(false).run(fixture.content()); + } + + #[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/error_recovery/exprs", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_expr(fixture: Fixture<&str>) { + TestRunner::expr_list(false).run(fixture.content()); + } } diff --git a/crates/parser2/tests/syntax_node.rs b/crates/parser2/tests/syntax_node.rs index 8c645760b5..c6068ef988 100644 --- a/crates/parser2/tests/syntax_node.rs +++ b/crates/parser2/tests/syntax_node.rs @@ -1,89 +1,122 @@ -use fe_parser2::{ - parser::{expr::parse_expr, item::ItemListScope, parse_pat, stmt::parse_stmt}, - syntax_node::SyntaxNode, -}; +use dir_test::{dir_test, Fixture}; + +use fe_compiler_test_utils::snap_test; mod test_runner; use test_runner::*; -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/syntax_node/structs", - "parser2/test_files/syntax_node/structs", - test_item_list -} -fn test_item_list(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - parser.parse(ItemListScope::default(), None); - }, - true, - ); - runner.run(input) +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/items", + glob: "*.fe" +)] +fn test_item_list(fixture: Fixture<&str>) { + let runner = TestRunner::item_list(true); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/syntax_node/pats", - "parser2/test_files/syntax_node/pats", - test_pat -} -fn test_pat(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - while parser.current_kind().is_some() { - parse_pat(parser); - } - }, - true, - ); - runner.run(input) +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/structs", + glob: "*.fe" +)] +fn test_struct(fixture: Fixture<&str>) { + let runner = TestRunner::item_list(true); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/syntax_node/exprs", - "parser2/test_files/syntax_node/exprs", - test_expr +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/stmts", + glob: "*.fe" +)] +fn test_stmt(fixture: Fixture<&str>) { + let runner = TestRunner::stmt_list(true); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fn test_expr(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - parser.set_newline_as_trivia(false); - bump_newlines(parser); - while parser.current_kind().is_some() { - bump_newlines(parser); - parse_expr(parser); - bump_newlines(parser); - } - }, - true, - ); - runner.run(input) +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/exprs", + glob: "*.fe" +)] +fn test_expr(fixture: Fixture<&str>) { + let runner = TestRunner::expr_list(true); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fe_compiler_test_utils::build_debug_snap_tests! { - "parser2/test_files/syntax_node/stmts", - "parser2/test_files/syntax_node/stmts", - test_stmt +#[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/pats", + glob: "*.fe" +)] +fn test_pat(fixture: Fixture<&str>) { + let runner = TestRunner::pat_list(true); + let node = format! {"{:#?}", runner.run(fixture.content())}; + snap_test!(node, fixture.path()); } -fn test_stmt(input: &str) -> SyntaxNode { - let runner = TestRunner::new( - |parser| { - parser.set_newline_as_trivia(false); - bump_newlines(parser); - while parser.current_kind().is_some() { - bump_newlines(parser); - parse_stmt(parser, None); - bump_newlines(parser); - } - }, - true, - ); - runner.run(input) -} +#[cfg(target_family = "wasm")] +mod wasm { + use super::*; + use wasm_bindgen_test::wasm_bindgen_test; + + #[dir_test::dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/items", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_item_list(fixture: dir_test::Fixture<&str>) { + TestRunner::item_list(true).run(fixture.content()); + } -fe_compiler_test_utils::build_debug_snap_tests!( - "parser2/test_files/syntax_node/items", - "parser2/test_files/syntax_node/items", - test_item_list -); + #[dir_test::dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/structs", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_struct(fixture: dir_test::Fixture<&str>) { + TestRunner::item_list(true).run(fixture.content()); + } + + #[dir_test::dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/stmts", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_stmt(fixture: dir_test::Fixture<&str>) { + TestRunner::stmt_list(true).run(fixture.content()); + } + + #[dir_test::dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/exprs", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_expr(fixture: dir_test::Fixture<&str>) { + TestRunner::expr_list(true).run(fixture.content()); + } + + #[dir_test( + dir: "$CARGO_MANIFEST_DIR/test_files/syntax_node/pats", + glob: "*.fe" + postfix: "wasm" + )] + #[dir_test_attr( + #[wasm_bindgen_test] + )] + fn test_pat(fixture: Fixture<&str>) { + TestRunner::pat_list(true).run(fixture.content()); + } +} diff --git a/crates/parser2/tests/test_runner.rs b/crates/parser2/tests/test_runner.rs index 88cac2888c..bc84c5c261 100644 --- a/crates/parser2/tests/test_runner.rs +++ b/crates/parser2/tests/test_runner.rs @@ -2,25 +2,81 @@ use fe_parser2::{ lexer, - parser::{Parser, RootScope}, + parser::{ + expr::parse_expr, item::ItemListScope, parse_pat, stmt::parse_stmt, Parser, RootScope, + }, syntax_node::SyntaxNode, SyntaxKind, }; -pub struct TestRunner -where - F: Fn(&mut Parser), -{ - f: F, +type BoxedParseFn = Box)>; +pub struct TestRunner { + f: BoxedParseFn, should_success: bool, } -impl TestRunner -where - F: Fn(&mut Parser), -{ - pub fn new(f: F, should_success: bool) -> Self { - Self { f, should_success } +impl TestRunner { + /// Constructs a new test runner. + pub fn new(f: F, should_success: bool) -> Self + where + F: Fn(&mut Parser) + 'static, + { + Self { + f: Box::new(f), + should_success, + } + } + + /// Constructs a test runner for parsing a list of expressions. + pub fn item_list(should_success: bool) -> Self { + fn parse(parser: &mut Parser) { + parser.parse(ItemListScope::default(), None); + } + + Self::new(parse, should_success) + } + + /// Constructs a test runner for parsing a list of statements. + pub fn stmt_list(should_success: bool) -> Self { + fn parse(parser: &mut Parser) { + parser.set_newline_as_trivia(false); + + bump_newlines(parser); + while parser.current_kind().is_some() { + bump_newlines(parser); + parse_stmt(parser, None); + bump_newlines(parser); + } + } + + Self::new(parse, should_success) + } + + /// Constructs a test runner for parsing a list of expressions. + pub fn expr_list(should_success: bool) -> Self { + fn parse(parser: &mut Parser) { + parser.set_newline_as_trivia(false); + + bump_newlines(parser); + while parser.current_kind().is_some() { + bump_newlines(parser); + parse_expr(parser); + bump_newlines(parser); + } + } + + Self::new(parse, should_success) + } + + /// Constructs a test runner for parsing a list of patterns. + pub fn pat_list(should_success: bool) -> Self { + fn parse(parser: &mut Parser) { + while parser.current_kind().is_some() { + parse_pat(parser); + } + } + + Self::new(parse, should_success) } pub fn run(&self, input: &str) -> SyntaxNode { diff --git a/crates/test-utils/Cargo.toml b/crates/test-utils/Cargo.toml index e9ecb879d3..beceb82903 100644 --- a/crates/test-utils/Cargo.toml +++ b/crates/test-utils/Cargo.toml @@ -15,7 +15,6 @@ fe-driver = {path = "../driver", version = "^0.20.0-alpha"} fe-yulc = {path = "../yulc", version = "^0.20.0-alpha", optional = true, features = ["solc-backend"]} fe-analyzer = {path = "../analyzer", version = "^0.20.0-alpha"} test-files = {path = "../test-files", package = "fe-test-files" } -fe-test-utils-macros = {path = "macros", version = "^0.20.0-alpha"} hex = "0.4" primitive-types = {version = "0.12", default-features = false, features = ["rlp"]} serde_json = "1.0.64" diff --git a/crates/test-utils/macros/Cargo.toml b/crates/test-utils/macros/Cargo.toml deleted file mode 100644 index 691c864b6b..0000000000 --- a/crates/test-utils/macros/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "fe-test-utils-macros" -version = "0.20.0-alpha" -edition = "2021" - -[lib] -proc_macro = true - -[dependencies] -syn = { version = "1.0", features = ["full"] } -proc-macro2 = "1.0" -quote = "1.0" \ No newline at end of file diff --git a/crates/test-utils/macros/src/lib.rs b/crates/test-utils/macros/src/lib.rs deleted file mode 100644 index 78a677e332..0000000000 --- a/crates/test-utils/macros/src/lib.rs +++ /dev/null @@ -1,185 +0,0 @@ -use std::{ - fs, - path::{Path, PathBuf}, -}; - -use quote::quote; - -type Error = syn::Error; -type Result = syn::Result; - -#[proc_macro] -pub fn build_snap_tests(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - match expand(input) { - Ok(ts) => ts, - Err(err) => err.to_compile_error().into(), - } -} - -fn expand(input: proc_macro::TokenStream) -> Result { - let args: Args = syn::parse(input)?; - - let builder = SnapTestBuilder::from_args(args)?; - builder.build().map(|ts| ts.into()) -} - -struct SnapTestBuilder { - fixture_dir: PathBuf, - snapshot_dir: PathBuf, - target_fn: syn::Path, - debug_snap: bool, -} - -impl SnapTestBuilder { - fn from_args(args: Args) -> Result { - let workspace_root = cargo_workspace_dir(); - - let fixture_dir: PathBuf = workspace_root.join(args.fixture_dir.value()); - let snapshot_dir: PathBuf = workspace_root.join(args.snapshot_dir.value()); - - if !fixture_dir.is_dir() | !fixture_dir.exists() { - return Err(Error::new_spanned( - args.fixture_dir, - format! {"invalid path for `fixture_dir`: `{}` is invalid path", - fixture_dir.display()}, - )); - } else if !snapshot_dir.is_dir() { - return Err(Error::new_spanned( - args.snapshot_dir, - format! {"invalid path for `snapshot_dir`: `{}` is invalid path", - snapshot_dir.display()}, - )); - } - - Ok(Self { - fixture_dir, - snapshot_dir, - target_fn: args.target_fn, - debug_snap: args.debug_snap.value(), - }) - } - - fn build(&self) -> Result { - let mut tests = Vec::new(); - - let dir = fs::read_dir(&self.fixture_dir).unwrap(); - for fixture in dir.flatten() { - let fixture_path = fixture.path(); - if fixture_path.is_file() - && fixture_path.extension().and_then(|ext| ext.to_str()) == Some("fe") - { - tests.push(self.build_test(&fixture_path)); - } - } - - Ok(quote! { - #(#tests)* - }) - } - - fn build_test(&self, fixture_file: &Path) -> proc_macro2::TokenStream { - let file_name = fixture_file.file_name().unwrap().to_str().unwrap(); - let file_stem_name = fixture_file.file_stem().unwrap().to_str().unwrap(); - let test_fn_ident = syn::Ident::new(file_stem_name, proc_macro2::Span::call_site()); - let fixture_file = fixture_file.to_str().unwrap(); - - let snapshot_dir = self.snapshot_dir.to_str().unwrap(); - let target_fn = &self.target_fn; - let snapshot = syn::Ident::new("snapshot", proc_macro2::Span::call_site()); - let get_snapshot = if self.debug_snap { - quote! { - let #snapshot = format!("{:#?}", #target_fn(&input)); - } - } else { - quote! { - let #snapshot = format!("{}", #target_fn(&input)); - } - }; - - quote! { - #[test] - fn #test_fn_ident() { - let input = ::std::fs::read_to_string(#fixture_file).unwrap(); - #get_snapshot - let mut settings = ::fe_compiler_test_utils::_macro_support::_insta::Settings::new(); - settings.set_snapshot_path(#snapshot_dir); - settings.set_input_file(#file_name); - settings.set_prepend_module_to_snapshot(false); - - - ::fe_compiler_test_utils::_insta_assert_snapshot!{ - #snapshot, - settings - } - } - } - } -} - -// FIXME: This is quite hacky and should be removed when `span::source_file` is -// stabilized. -// See [`Tracking issue for proc_macro::Span inspection APIs #54725`](https://github.com/rust-lang/rust/issues/54725) for more information. -fn cargo_workspace_dir() -> PathBuf { - let mut cargo_workspace_dir: PathBuf = env!["CARGO_MANIFEST_DIR"].into(); - - for _ in 0..2 { - cargo_workspace_dir.pop(); - } - - cargo_workspace_dir -} -struct Args { - fixture_dir: syn::LitStr, - snapshot_dir: syn::LitStr, - target_fn: syn::Path, - debug_snap: syn::LitBool, -} - -impl syn::parse::Parse for Args { - fn parse(input: syn::parse::ParseStream) -> Result { - let error_msg = "expected `build_snap_tests! { - fixture_dir: .., - snapshot_dir: .., - target_fn: .., - debug_snap: .. - }`"; - - let ident = input.parse::()?; - if ident != "fixture_dir" { - return Err(Error::new_spanned(ident, error_msg)); - } - input.parse::()?; - let fixture_dir = input.parse::()?; - input.parse::()?; - - let ident = input.parse::()?; - if ident != "snapshot_dir" { - return Err(Error::new_spanned(ident, error_msg)); - } - input.parse::()?; - let snapshot_dir = input.parse::()?; - input.parse::()?; - - let ident = input.parse::()?; - if ident != "target_fn" { - return Err(Error::new_spanned(ident, error_msg)); - } - input.parse::()?; - let target_fn = input.parse::()?; - input.parse::()?; - - let ident = input.parse::()?; - if ident != "debug_snap" { - return Err(Error::new_spanned(ident, error_msg)); - } - input.parse::()?; - let debug_snap = input.parse::()?; - - Ok(Self { - fixture_dir, - snapshot_dir, - target_fn, - debug_snap, - }) - } -} diff --git a/crates/test-utils/src/_macro_support.rs b/crates/test-utils/src/_macro_support.rs index b46e00043f..13b387142a 100644 --- a/crates/test-utils/src/_macro_support.rs +++ b/crates/test-utils/src/_macro_support.rs @@ -1,35 +1,26 @@ #[doc(hidden)] pub use insta as _insta; -// NOTE: Borrowed from `insta` implementation from -// [here](https://docs.rs/insta/1.26/src/insta/macros.rs.html#2-16) -/// Utility macro to return the name of the current function. -#[doc(hidden)] +/// A macro to assert that a value matches a snapshot. +/// If the snapshot does not exist, it will be created in the same directory as +/// the test file. #[macro_export] -macro_rules! _function_name { - () => {{ - fn f() {} - fn type_name_of_val(_: T) -> &'static str { - std::any::type_name::() - } - let mut name = type_name_of_val(f).strip_suffix("::f").unwrap_or(""); - while let Some(rest) = name.strip_suffix("::{{closure}}") { - name = rest; - } - name - }}; -} +macro_rules! snap_test { + ($value:expr, $fixture_path: expr) => { + let mut settings = $crate::_macro_support::_insta::Settings::new(); + let fixture_path = ::std::path::Path::new($fixture_path); + let fixture_dir = fixture_path.parent().unwrap(); + let fixture_name = fixture_path.file_stem().unwrap().to_str().unwrap(); -#[doc(hidden)] -#[macro_export] -macro_rules! _insta_assert_snapshot { - ($value: expr, $setting: expr) => { - $setting.bind(|| { + settings.set_snapshot_path(fixture_dir); + settings.set_input_file($fixture_path); + settings.set_prepend_module_to_snapshot(false); + settings.bind(|| { $crate::_macro_support::_insta::_macro_support::assert_snapshot( $crate::_macro_support::_insta::_macro_support::AutoName.into(), &$value, env!("CARGO_MANIFEST_DIR"), - $crate::_function_name!(), + fixture_name, module_path!(), file!(), line!(), @@ -39,33 +30,3 @@ macro_rules! _insta_assert_snapshot { }) }; } - -/// Build a set of snapshot tests from a directory of fixtures. -/// `fixture_dir` and `snapshot_dir` should be relative to the workspace root. -/// `target_fn` should take `&str` and return ``. -#[macro_export] -macro_rules! build_snap_tests { - ($fixture_dir: literal, $snapshot_dir: literal, $target_fn: path) => { - fe_compiler_test_utils::_build_snap_tests! { - fixture_dir: $fixture_dir, - snapshot_dir: $snapshot_dir, - target_fn: $target_fn, - debug_snap: false - } - }; -} - -/// Build a set of snapshot tests from a directory of fixtures. -/// `fixture_dir` and `snapshot_dir` should be relative to the workspace root. -/// `target_fn` should take `&str` and return ``. -#[macro_export] -macro_rules! build_debug_snap_tests { - ($fixture_dir: literal, $snapshot_dir: literal, $target_fn: path) => { - fe_compiler_test_utils::_build_snap_tests! { - fixture_dir: $fixture_dir, - snapshot_dir: $snapshot_dir, - target_fn: $target_fn, - debug_snap: true - } - }; -} diff --git a/crates/test-utils/src/lib.rs b/crates/test-utils/src/lib.rs index b8f385f655..9f721601d5 100644 --- a/crates/test-utils/src/lib.rs +++ b/crates/test-utils/src/lib.rs @@ -1,9 +1,6 @@ #[doc(hidden)] pub mod _macro_support; -#[doc(hidden)] -pub use fe_test_utils_macros::build_snap_tests as _build_snap_tests; - use evm_runtime::{ExitReason, Handler}; use fe_common::diagnostics::print_diagnostics; use fe_common::utils::keccak; diff --git a/crates/tests/Cargo.toml b/crates/tests/Cargo.toml index b008d72d77..77749d658e 100644 --- a/crates/tests/Cargo.toml +++ b/crates/tests/Cargo.toml @@ -33,7 +33,7 @@ wasm-bindgen-test = "0.3.24" solc-backend = ["fe-yulc/solc-backend", "fe-compiler-test-utils/solc-backend"] [dev-dependencies.proptest] -version = "1.0.0" +version = "=1.0.0" # The default feature set includes things like process forking which are not # supported in Web Assembly. default-features = false