Skip to content

Commit

Permalink
export xtest inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
devrandom committed Sep 25, 2024
1 parent 5f75443 commit 49a529e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ext-functional-test-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
println!("Hello, world!");
println!("{} tests were exported", lightning::get_xtests().len());
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions ext-test-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ proc-macro = true
syn = { version = "1.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"

[dev-dependencies]
inventory = "0.3"
42 changes: 42 additions & 0 deletions ext-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,18 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
.into();
}

let fn_name = &item_fn.sig.ident;
let fn_name_str = fn_name.to_string();
quote! {
#cfg_attr
#item_fn

inventory::submit! {
crate::XTestItem {
test_fn: #fn_name,
test_name: #fn_name_str,
}
}
}
},
_ => {
Expand All @@ -91,3 +100,36 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {

TokenStream::from(expanded)
}

#[proc_macro]
pub fn xtest_inventory(_input: TokenStream) -> TokenStream {
let expanded = quote! {
pub struct XTestItem {
pub test_fn: fn(),
pub test_name: &'static str,
}

inventory::collect!(XTestItem);

pub fn get_xtests() -> Vec<&'static XTestItem> {
inventory::iter::<XTestItem>
.into_iter()
.collect()
}

#[macro_export]
macro_rules! xtest_inventory {
($test_fn:expr, $test_name:expr) => {
inventory::submit! {
XTestItem {
test_fn: $test_fn,
test_name: $test_name,
}
}
};
}
};

TokenStream::from(expanded)
}

4 changes: 3 additions & 1 deletion lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
# Internal test utilities exposed to other repo crates
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "ext-test-macro", "lightning-types/_test_utils"]
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "ext-test-macro", "lightning-types/_test_utils", "inventory"]
# Unlog messages superior at targeted level.
max_level_off = []
max_level_error = []
Expand Down Expand Up @@ -51,11 +51,13 @@ backtrace = { version = "0.3", optional = true }
libm = { version = "0.2", default-features = false }
delegate = "0.12.0"
ext-test-macro = { path = "../ext-test-macro", optional = true }
inventory = { version = "0.3", optional = true }

[dev-dependencies]
regex = "1.5.6"
lightning-types = { version = "0.1.0", path = "../lightning-types", features = ["_test_utils"] }
ext-test-macro = { path = "../ext-test-macro" }
inventory = { version = "0.3" }

[dev-dependencies.bitcoin]
version = "0.32.2"
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ mod prelude {
extern crate backtrace;

mod sync;

#[cfg(feature = "_test_utils")]
ext_test_macro::xtest_inventory!();

0 comments on commit 49a529e

Please sign in to comment.