Skip to content

Commit e97ed34

Browse files
committed
test(test2): Add UI tests for macro error output
The tests use the `trybuild` crate to check that compilation fails for the incorrect macro use. The test will also fail if the error output changes. These were added directly to the `tests` directory (as opposed to being part of the `testsuite` subdirectory) because the automatic check and overwrite functionality provided by the `trybuild` crate seemed to not work properly when placed there.
1 parent 233f8ad commit e97ed34

17 files changed

+272
-0
lines changed

Cargo.lock

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/libtest2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ escargot = "0.5.8"
3939
once_cell_polyfill = "1.56.0"
4040
pathdiff = "0.2.1"
4141
snapbox = { version = "0.6.0", features = ["json"] }
42+
trybuild = "1.0.111"
4243

4344
[lints]
4445
workspace = true

crates/libtest2/tests/ui.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[test]
2+
fn ui() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/ui/*.rs");
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[libtest2::main]
2+
fn main() {}
3+
4+
#[libtest2::test]
5+
#[ignore(reason = "invalid syntax right here")]
6+
fn test(_: &libtest2::TestContext) {
7+
panic!("We just need to compile this");
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: unknown attribute 'ignore(reason = "invalid syntax right here")'
2+
--> tests/ui/ignore_incorrect_usage.rs:4:1
3+
|
4+
4 | #[libtest2::test]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[libtest2::main]
2+
fn main() {}
3+
4+
#[libtest2::test]
5+
#[should_panic(expect = "something")]
6+
fn test(_: &libtest2::TestContext) {
7+
panic!("the correct attribute is 'expected = \"...\"'");
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: unknown attribute 'should_panic(expect = "something")'
2+
--> tests/ui/should_panic_attribute_typo.rs:4:1
3+
|
4+
4 | #[libtest2::test]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[libtest2::main]
2+
fn main() {}
3+
4+
#[libtest2::test]
5+
#[should_panic]
6+
#[should_panic]
7+
fn test_1(_: &libtest2::TestContext) {}
8+
9+
#[libtest2::test]
10+
#[should_panic]
11+
#[should_panic(reason = "something")]
12+
fn test_2(_: &libtest2::TestContext) {}
13+
14+
#[libtest2::test]
15+
#[should_panic = "anything"]
16+
#[should_panic]
17+
fn test_3(_: &libtest2::TestContext) {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: annotating a test with multiple 'should_panic' attributes is not allowed
2+
--> tests/ui/should_panic_multiple_attributes.rs:4:1
3+
|
4+
4 | #[libtest2::test]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: annotating a test with multiple 'should_panic' attributes is not allowed
10+
--> tests/ui/should_panic_multiple_attributes.rs:9:1
11+
|
12+
9 | #[libtest2::test]
13+
| ^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: annotating a test with multiple 'should_panic' attributes is not allowed
18+
--> tests/ui/should_panic_multiple_attributes.rs:14:1
19+
|
20+
14 | #[libtest2::test]
21+
| ^^^^^^^^^^^^^^^^^
22+
|
23+
= note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[libtest2::main]
2+
fn main() {}
3+
4+
#[libtest2::test]
5+
#[unsafe(no_mangle)] // we just need some unknown attribute here
6+
fn test(_: &libtest2::TestContext) {}

0 commit comments

Comments
 (0)