Skip to content

Commit 0a29d64

Browse files
committed
export xtest inventory
1 parent f6c2d83 commit 0a29d64

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

ext-functional-test-demo/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
println!("Hello, world!");
2+
println!("{} tests were exported", lightning::get_xtests().len());
33
}
44

55
#[cfg(test)]

ext-test-macro/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ proc-macro = true
1212
syn = { version = "1.0", features = ["full"] }
1313
quote = "1.0"
1414
proc-macro2 = "1.0"
15+
16+
[dev-dependencies]
17+
inventory = "0.3"

ext-test-macro/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,18 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
7474
.into();
7575
}
7676

77+
let fn_name = &item_fn.sig.ident;
78+
let fn_name_str = fn_name.to_string();
7779
quote! {
7880
#cfg_attr
7981
#item_fn
82+
83+
inventory::submit! {
84+
crate::XTestItem {
85+
test_fn: #fn_name,
86+
test_name: #fn_name_str,
87+
}
88+
}
8089
}
8190
},
8291
_ => {
@@ -91,3 +100,36 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
91100

92101
TokenStream::from(expanded)
93102
}
103+
104+
#[proc_macro]
105+
pub fn xtest_inventory(_input: TokenStream) -> TokenStream {
106+
let expanded = quote! {
107+
pub struct XTestItem {
108+
pub test_fn: fn(),
109+
pub test_name: &'static str,
110+
}
111+
112+
inventory::collect!(XTestItem);
113+
114+
pub fn get_xtests() -> Vec<&'static XTestItem> {
115+
inventory::iter::<XTestItem>
116+
.into_iter()
117+
.collect()
118+
}
119+
120+
#[macro_export]
121+
macro_rules! xtest_inventory {
122+
($test_fn:expr, $test_name:expr) => {
123+
inventory::submit! {
124+
XTestItem {
125+
test_fn: $test_fn,
126+
test_name: $test_name,
127+
}
128+
}
129+
};
130+
}
131+
};
132+
133+
TokenStream::from(expanded)
134+
}
135+

lightning/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1717

1818
[features]
1919
# Internal test utilities exposed to other repo crates
20-
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "ext-test-macro", "lightning-types/_test_utils"]
20+
_test_utils = ["regex", "bitcoin/bitcoinconsensus", "ext-test-macro", "lightning-types/_test_utils", "inventory"]
2121
# Unlog messages superior at targeted level.
2222
max_level_off = []
2323
max_level_error = []
@@ -51,6 +51,7 @@ backtrace = { version = "0.3", optional = true }
5151
libm = { version = "0.2", default-features = false }
5252
delegate = "0.12.0"
5353
ext-test-macro = { path = "../ext-test-macro", optional = true }
54+
inventory = { version = "0.3", optional = true }
5455

5556
[dev-dependencies]
5657
regex = "1.5.6"

lightning/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,6 @@ mod prelude {
149149
extern crate backtrace;
150150

151151
mod sync;
152+
153+
#[cfg(feature = "_test_utils")]
154+
ext_test_macro::xtest_inventory!();

0 commit comments

Comments
 (0)