Skip to content

adding tests for macro and enhance macro #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ proc-macro = true
quote = "1.0.26"
rand = "0.8.5"
syn = { version = "2.0.8", features = ["full"] }

[dev-dependencies]
trybuild = "1.0.99"
netsim-embed = { path = "..", features = ["ipc"] }
async-std = { version = "1.12.0", features = ["attributes"] }
77 changes: 57 additions & 20 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_attribute]
pub fn machine(_attrs: TokenStream, fun: TokenStream) -> TokenStream {
Expand All @@ -12,10 +13,6 @@ pub fn machine(_attrs: TokenStream, fun: TokenStream) -> TokenStream {
f.sig.constness.is_none(),
"netsim_embed::machine cannot be const"
);
assert!(
f.sig.asyncness.is_none(),
"netsim_embed::machine cannot be async"
);
assert!(
f.sig.unsafety.is_none(),
"netsim_embed::machine cannot be unsafe"
Expand All @@ -28,10 +25,6 @@ pub fn machine(_attrs: TokenStream, fun: TokenStream) -> TokenStream {
f.sig.generics.params.is_empty(),
"netsim_embed::machine cannot be generic"
);
assert!(
f.sig.inputs.len() == 1,
"netsim_embed::machine must take exactly one argument"
);
assert!(
f.sig.variadic.is_none(),
"netsim_embed::machine cannot be variadic"
Expand All @@ -41,23 +34,60 @@ pub fn machine(_attrs: TokenStream, fun: TokenStream) -> TokenStream {
"netsim_embed::machine must not declare a return type"
);

let input = match &f.sig.inputs.first().unwrap() {
syn::FnArg::Typed(input) => input,
_ => panic!("netsim_embed::machine must be a freestanding function"),
};
assert!(
input.attrs.is_empty(),
"netsim_embed::machine's only argument must not have any attributes attached"
);
let mut inputs = vec![];
for input in &f.sig.inputs {
match input {
syn::FnArg::Typed(input) => {
assert!(
input.attrs.is_empty(),
"netsim_embed::machine's only argument must not have any attributes attached"
);
inputs.push(input);
}
_ => panic!("netsim_embed::machine must be a freestanding function"),
}
}

let f_vis = f.vis;
let f_ident = f.sig.ident;
let input_ty = &input.ty;
let id: u128 = rand::random();
let input_pat = &input.pat;

let (input_ty, input_pat) = match inputs.len() {
0 => (quote! { () }, quote! {_}),
1 => {
let input = inputs.first().unwrap();
let input_ty = &input.ty;
let input_pat = &input.pat;
(quote! { #input_ty }, quote! { #input_pat })
}
_ => {
let types: Vec<_> = inputs.iter().map(|x| x.ty.clone()).collect();
let patterns: Vec<_> = inputs.iter().map(|x| x.pat.clone()).collect();

let input_ty = quote! {
(#(#types),*)
};
let input_pat = quote! {
(#(#patterns),*)
};
(input_ty, input_pat)
}
};

let f_block = f.block;
let f_block = if f.sig.asyncness.is_some() {
quote! {
{
async_std::task::block_on(async #f_block)
}
}
} else {
quote! {
#f_block
}
};

TokenStream::from(quote::quote! {
TokenStream::from(quote! {
#[allow(non_camel_case_types)]
#f_vis struct #f_ident ;

Expand All @@ -68,7 +98,14 @@ pub fn machine(_attrs: TokenStream, fun: TokenStream) -> TokenStream {
#id
}

fn call(#input_pat: #input_ty) #f_block
fn call( #input_pat : #input_ty) #f_block
}
})
}

#[test]
fn test() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/macros/failures/*.rs");
t.pass("tests/macros/success/*.rs");
}
6 changes: 6 additions & 0 deletions macros/tests/macros/failures/invalid_return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[netsim_embed_macros::machine]
fn foo() -> usize {
5
}

fn main() {}
7 changes: 7 additions & 0 deletions macros/tests/macros/failures/invalid_return.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: custom attribute panicked
--> tests/macros/failures/invalid_return.rs:1:1
|
1 | #[netsim_embed_macros::machine]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: netsim_embed::machine must not declare a return type
4 changes: 4 additions & 0 deletions macros/tests/macros/failures/unsafe_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[netsim_embed_macros::machine]
unsafe fn foo() {}

fn main() {}
7 changes: 7 additions & 0 deletions macros/tests/macros/failures/unsafe_function.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: custom attribute panicked
--> tests/macros/failures/unsafe_function.rs:1:1
|
1 | #[netsim_embed_macros::machine]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: netsim_embed::machine cannot be unsafe
15 changes: 15 additions & 0 deletions macros/tests/macros/success/async_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use netsim_embed::MachineFn;
use std::sync::OnceLock;

static DATA: OnceLock<String> = OnceLock::new();

#[netsim_embed_macros::machine]
async fn foo(bar: &'static str) {
DATA.get_or_init(|| format!("got {bar}"));
}

fn main() {
foo::call("hello world");
let data = DATA.get_or_init(|| "others".to_string());
assert_eq!(data, "got hello world");
}
15 changes: 15 additions & 0 deletions macros/tests/macros/success/empty_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use netsim_embed::MachineFn;
use std::sync::OnceLock;

static DATA: OnceLock<String> = OnceLock::new();

#[netsim_embed_macros::machine]
fn foo() {
DATA.get_or_init(|| "got it".to_string());
}

fn main() {
foo::call(());
let data = DATA.get_or_init(|| "others".to_string());
assert_eq!(data, "got it");
}
15 changes: 15 additions & 0 deletions macros/tests/macros/success/multiple_calls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use netsim_embed::MachineFn;
use std::sync::OnceLock;

static DATA: OnceLock<String> = OnceLock::new();

#[netsim_embed_macros::machine]
fn foo(bar: &'static str, baz: &'static str) {
DATA.get_or_init(|| format!("got {bar} {baz}"));
}

fn main() {
foo::call(("hello", "world"));
let data = DATA.get_or_init(|| "others".to_string());
assert_eq!(data, "got hello world");
}
Loading