From 7f26d8b108799caa5e1f4eca422951d7a021d53d Mon Sep 17 00:00:00 2001 From: maciektr Date: Thu, 22 Feb 2024 15:04:30 +0100 Subject: [PATCH] Add remove/replace proc macro e2e tests commit-id:ecc87fd5 --- scarb/tests/build_cairo_plugin.rs | 95 +++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/scarb/tests/build_cairo_plugin.rs b/scarb/tests/build_cairo_plugin.rs index dc960d56a..41915c69f 100644 --- a/scarb/tests/build_cairo_plugin.rs +++ b/scarb/tests/build_cairo_plugin.rs @@ -310,3 +310,98 @@ fn can_emit_plugin_diagnostics() { error: could not compile `hello` due to previous error "#}); } + +#[test] +fn can_remove_original_node() { + let temp = TempDir::new().unwrap(); + let t = temp.child("some"); + simple_project_with_code( + &t, + indoc! {r#" + use cairo_lang_macro::{ProcMacroResult, TokenStream, attribute_macro}; + + #[attribute_macro] + pub fn some_macro(token_stream: TokenStream) -> ProcMacroResult { + let _code = token_stream.to_string(); + ProcMacroResult::Remove { diagnostics: Vec::new() } + } + "#}, + ); + let project = temp.child("hello"); + ProjectBuilder::start() + .name("hello") + .version("1.0.0") + .dep("some", &t) + .lib_cairo(indoc! {r#" + #[some] + fn main() -> felt252 { 12 } + + fn main() -> felt252 { 34 } + + #[some] + fn main() -> felt252 { 56 } + "#}) + .build(&project); + + Scarb::quick_snapbox() + .arg("cairo-run") + // Disable output from Cargo. + .env("CARGO_TERM_QUIET", "true") + .current_dir(&project) + .assert() + .success() + .stdout_matches(indoc! {r#" + [..] Compiling some v1.0.0 ([..]Scarb.toml) + [..] Compiling hello v1.0.0 ([..]Scarb.toml) + [..]Finished release target(s) in [..] + [..]Running hello + Run completed successfully, returning [34] + "#}); +} + +#[test] +fn can_replace_original_node() { + let temp = TempDir::new().unwrap(); + let t = temp.child("some"); + simple_project_with_code( + &t, + indoc! {r#" + use cairo_lang_macro::{ProcMacroResult, TokenStream, attribute_macro}; + + #[attribute_macro] + pub fn some_macro(token_stream: TokenStream) -> ProcMacroResult { + let _code = token_stream.to_string(); + ProcMacroResult::Replace { + token_stream: TokenStream::new("fn main() -> felt252 { 34 }".to_string()), + aux_data: None, + diagnostics: Vec::new() + } + } + "#}, + ); + let project = temp.child("hello"); + ProjectBuilder::start() + .name("hello") + .version("1.0.0") + .dep("some", &t) + .lib_cairo(indoc! {r#" + #[some] + fn main() -> felt252 { 12 } + "#}) + .build(&project); + + Scarb::quick_snapbox() + .arg("cairo-run") + // Disable output from Cargo. + .env("CARGO_TERM_QUIET", "true") + .current_dir(&project) + .assert() + .success() + .stdout_matches(indoc! {r#" + [..] Compiling some v1.0.0 ([..]Scarb.toml) + [..] Compiling hello v1.0.0 ([..]Scarb.toml) + [..]Finished release target(s) in [..] + [..]Running hello + Run completed successfully, returning [34] + "#}); +}