Skip to content

Commit 9626fa1

Browse files
committed
Test: Add test to macros::since and macros::expand
1 parent 99596c7 commit 9626fa1

32 files changed

+208
-67
lines changed

macros/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ quote = { workspace = true }
2222
semver = { workspace = true }
2323
syn = { workspace = true, features = ["full", "extra-traits"] }
2424

25+
26+
[dev-dependencies]
27+
macrotest = "1"
28+
trybuild = "1.0"
29+
30+
2531
[features]
2632

2733
# Do not add `Send` bounds.

macros/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# openraft_macros
2+
3+
This crate provides a set of macros to support Openraft.
4+
5+
# Test
6+
7+
- Macro expansions are tested with [`macrotest`] crate.
8+
- Macro compile errors are tested with [`trybuild`] crate.
9+
10+
11+
[`macrotest`]: https://crates.io/crates/macrotest
12+
[`trybuild`]: https://crates.io/crates/trybuild

macros/src/expand.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ impl Parse for Expand {
182182
{
183183
let content;
184184
let _parenthesis = parenthesized!(content in input);
185+
let content_str = content.to_string();
186+
let content_span = content.span();
185187

186188
let k = content.parse::<TypeOrExpr>()?;
187189
let mut args = vec![k.clone()];
@@ -201,6 +203,16 @@ impl Parse for Expand {
201203
args.push(v);
202204
}
203205

206+
if args.len() != b.idents.len() {
207+
return Err(syn::Error::new(
208+
content_span,
209+
format!(
210+
"Expected the same number of arguments(`{}`) as template variables(`{:?}`)",
211+
content_str, b.idents
212+
),
213+
));
214+
}
215+
204216
// Ignore duplicates if keyed
205217
if b.present_keys.contains(&k) && b.keyed {
206218
continue;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
KEYED,
4+
(K, T, V) => {let K: T = V;},
5+
);
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let a: u64 = 1;
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
KEYED,
4+
(K, T, V) => {let K: T = V;},
5+
(a, u64, 1),
6+
);
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let a: u64 = 1;
3+
let b: String = "foo".to_string();
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
KEYED,
4+
(K, T, V) => {let K: T = V;},
5+
(a, u64, 1),
6+
(b, String, "foo".to_string()),
7+
(a, u32, 2), // duplicate `a` will be ignored
8+
);
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let a: u64 = 1;
3+
let b: String = "foo".to_string();
4+
let a: u32 = 2;
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
!KEYED,
4+
(K, T, V) => {let K: T = V;},
5+
(a, u64, 1),
6+
(b, String, "foo".to_string()),
7+
(a, u32, 2),
8+
);
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
c;
3+
c;
4+
u8;
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
!KEYED,
4+
(K, T, V) => {K; T; V;},
5+
(c, , ,),
6+
(c, , u8 ),
7+
);
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let c: u8;
3+
#[allow(dead_code)]
4+
let c: u16;
5+
#[allow(dead_code)]
6+
#[allow(dead_code)]
7+
let c: u16;
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
!KEYED,
4+
(K, M, T) => {M let K: T;},
5+
(c, , u8, ),
6+
(c, #[allow(dead_code)] , u16),
7+
(c, #[allow(dead_code)] #[allow(dead_code)] , u16),
8+
);
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
!FOO,
4+
(K, T, V) => {K; T; V;},
5+
);
6+
7+
openraft_macros::expand!(
8+
FOO,
9+
(K, T, V) => {K; T; V;},
10+
);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: Expected KEYED
2+
--> tests/expand/fail/arg0_invalid_keyed.rs:3:10
3+
|
4+
3 | !FOO,
5+
| ^^^
6+
7+
error: Expected KEYED
8+
--> tests/expand/fail/arg0_invalid_keyed.rs:8:9
9+
|
10+
8 | FOO,
11+
| ^^^
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
openraft_macros::expand!(
3+
!KEYED,
4+
(K, T, V) => {K; T; V;},
5+
(c, , ),
6+
(c, , u8, ),
7+
);
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: Expected the same number of arguments(`c, ,`) as template variables(`["K", "T", "V"]`)
2+
--> tests/expand/fail/variable_absent.rs:5:10
3+
|
4+
5 | (c, , ),
5+
| ^
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Since: 1.0.0
2+
const A: i32 = 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[openraft_macros::since(version = "1.0.0")]
2+
const A: i32 = 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Since: 1.0.0, Date(2021-01-01)
2+
const A: i32 = 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[openraft_macros::since(version = "1.0.0", date = "2021-01-01")]
2+
const A: i32 = 0;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Doc
2+
///
3+
/// By default, `Send` bounds will be added to the trait and to the return bounds of any async
4+
/// functions defined withing the trait.
5+
///
6+
/// If the `singlethreaded` feature is enabled, the trait definition remains the same without any
7+
/// added `Send` bounds.
8+
///
9+
/// # Example
10+
///
11+
/// - list
12+
///
13+
/// Since: 1.0.0, Date(2021-01-01)
14+
const A: i32 = 0;

macros/tests/since/expand/with_doc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// Doc
2+
///
3+
/// By default, `Send` bounds will be added to the trait and to the return bounds of any async
4+
/// functions defined withing the trait.
5+
///
6+
/// If the `singlethreaded` feature is enabled, the trait definition remains the same without any
7+
/// added `Send` bounds.
8+
///
9+
/// # Example
10+
///
11+
/// - list
12+
#[openraft_macros::since(version = "1.0.0", date = "2021-01-01")]
13+
const A: i32 = 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[openraft_macros::since(version = "1.0.0", date = "2021-01--")]
2+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: `date`(`2021-01--`) is not valid date string. Expected format: yyyy-mm-dd
2+
--> tests/since/fail/invalid_date.rs:1:52
3+
|
4+
1 | #[openraft_macros::since(version = "1.0.0", date = "2021-01--")]
5+
| ^^^^^^^^^^^
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[openraft_macros::since(version = "1.0.0..0")]
2+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: `version`(`1.0.0..0`) is not valid semver.
2+
--> tests/since/fail/invalid_sem_ver.rs:1:36
3+
|
4+
1 | #[openraft_macros::since(version = "1.0.0..0")]
5+
| ^^^^^^^^^^

macros/tests/test_default_types.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.

macros/tests/test_expand.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[test]
2+
fn fail() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/expand/fail/*.rs");
5+
}
6+
7+
#[test]
8+
fn pass() {
9+
macrotest::expand("tests/expand/expand/*.rs");
10+
}

macros/tests/test_since.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
use openraft_macros::since;
2-
3-
/// Doc
4-
///
5-
/// By default, `Send` bounds will be added to the trait and to the return bounds of any async
6-
/// functions defined withing the trait.
7-
///
8-
/// If the `singlethreaded` feature is enabled, the trait definition remains the same without any
9-
/// added `Send` bounds.
10-
///
11-
/// # Example
12-
///
13-
/// - list
14-
#[since(version = "1.0.0", date = "2021-01-01")]
15-
#[allow(dead_code)]
16-
const A: i32 = 0;
17-
18-
#[since(version = "1.0.0")]
19-
#[allow(dead_code)]
20-
fn foo() {}
21-
22-
/*
23-
24-
#[since(version = "1.0.0..0")]
25-
fn bad_semver() {}
26-
27-
#[since(version = "1.0.0", date = "2021-01--")]
28-
fn bad_date() {}
29-
30-
*/
1+
#[test]
2+
fn fail() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/since/fail/*.rs");
5+
}
6+
7+
#[test]
8+
fn pass() {
9+
macrotest::expand("tests/since/expand/*.rs");
10+
}

0 commit comments

Comments
 (0)