Skip to content

Commit 371c280

Browse files
committed
externally visible test macro
1 parent a0d0f02 commit 371c280

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"lightning-custom-message",
1616
"lightning-transaction-sync",
1717
"possiblyrandom",
18+
"ext-test-macro",
1819
]
1920

2021
exclude = [

ext-test-macro/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "ext-test-macro"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
syn = { version = "1.0", features = ["full"] }
13+
quote = "1.0"
14+
proc-macro2 = "1.0"

ext-test-macro/src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use proc_macro::TokenStream;
2+
use proc_macro2::TokenStream as TokenStream2;
3+
use quote::quote;
4+
use syn::{parse_macro_input, Item};
5+
6+
/// An exposed test. This is a test that will run locally and also be
7+
/// made available to other crates that want to run it in their own context.
8+
///
9+
/// For example:
10+
/// ```rust
11+
/// use ext_test_macro::xtest;
12+
///
13+
/// fn f1() {}
14+
///
15+
/// #[xtest(feature = "_test_utils")]
16+
/// pub fn test_f1() {
17+
/// f1();
18+
/// }
19+
/// ```
20+
///
21+
/// May also be applied to modules, like so:
22+
///
23+
/// ```rust
24+
/// use ext_test_macro::xtest;
25+
///
26+
/// #[xtest(feature = "_test_utils")]
27+
/// pub mod tests {
28+
/// use super::*;
29+
///
30+
/// fn f1() {}
31+
///
32+
/// #[xtest]
33+
/// pub fn test_f1() {
34+
/// f1();
35+
/// }
36+
/// }
37+
/// ```
38+
///
39+
/// Which will include the module if we are testing or the `_test_utils` feature
40+
/// is on.
41+
#[proc_macro_attribute]
42+
pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
43+
let attrs = parse_macro_input!(attrs as TokenStream2);
44+
let input = parse_macro_input!(item as Item);
45+
46+
let expanded = match input {
47+
Item::Mod(item_mod) => {
48+
let cfg = if attrs.is_empty() {
49+
quote! { #[cfg(test)] }
50+
} else {
51+
quote! { #[cfg(any(test, #attrs))] }
52+
};
53+
quote! {
54+
#cfg
55+
#item_mod
56+
}
57+
},
58+
Item::Fn(item_fn) => {
59+
let cfg_attr = if attrs.is_empty() {
60+
quote! { #[cfg(test)] }
61+
} else {
62+
quote! { #[cfg(any(test, #attrs))] }
63+
};
64+
quote! {
65+
#cfg_attr
66+
#item_fn
67+
}
68+
},
69+
_ => {
70+
return syn::Error::new_spanned(
71+
input,
72+
"xtest can only be applied to functions or modules",
73+
)
74+
.to_compile_error()
75+
.into();
76+
},
77+
};
78+
79+
TokenStream::from(expanded)
80+
}

lightning/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ libm = { version = "0.2", default-features = false }
5353
[dev-dependencies]
5454
regex = "1.5.6"
5555
lightning-types = { version = "0.1.0", path = "../lightning-types", features = ["_test_utils"] }
56+
ext-test-macro = { path = "../ext-test-macro" }
5657

5758
[dev-dependencies.bitcoin]
5859
version = "0.32.2"

0 commit comments

Comments
 (0)