Skip to content

Commit 1663b00

Browse files
committed
macros: simplify compile-fail tests
I was experimenting with my solution locally and it didn't work. My solution was wrong, but the compile-fail tests didn't actually fail. I'm not 100% sure why that was the case, but I assume it's because I've set up a cargo workspace for my own exercise solutions. This moves the target folder somewhere else, but the compile-fail tests assume a specific target directory location. Using doctests should be much more reliable, since it's using a built-in way of Rust to do this. It's a little unconventional compared to the other exercises, but so were the previous compile-fail tests. One downside to this is that users who solve the exercise locally will have to learn a new way of un-ignoring a test. I've tried to make this smooth with appropriate comments. The test-runner should have no problems with this, since it uses `cargo test -- --include-ignored` to run all tests. This works for these new doctests just like for the normal ones.
1 parent 7978fdb commit 1663b00

File tree

15 files changed

+111
-226
lines changed

15 files changed

+111
-226
lines changed

exercises/practice/macros/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"Cargo.toml"
2626
],
2727
"test": [
28-
"tests/macros.rs"
28+
"tests/macros.rs",
29+
"src/compile_fail_tests.rs"
2930
],
3031
"example": [
3132
".meta/example.rs"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/// To activate a doctest locally, remove ",ignore" from the code block.
2+
///
3+
/// # Comma separator
4+
///
5+
/// ```compile_fail,ignore
6+
/// use macros::hashmap;
7+
/// use std::collections::HashMap;
8+
///
9+
/// // using only commas is invalid
10+
/// let _hm: HashMap<_, _> = hashmap!('a', 1);
11+
/// ```
12+
///
13+
/// # Double trailing commas
14+
///
15+
/// ```compile_fail,ignore
16+
/// use macros::hashmap;
17+
/// use std::collections::HashMap;
18+
///
19+
/// // a single trailing comma is okay, but two is not
20+
/// let _hm: HashMap<_, _> = hashmap!('a' => 2, ,);
21+
/// ```
22+
///
23+
/// # Only comma
24+
///
25+
/// ```compile_fail,ignore
26+
/// use macros::hashmap;
27+
/// use std::collections::HashMap;
28+
///
29+
/// // a single random comma is not valid
30+
/// let _hm: HashMap<(), ()> = hashmap!(,);
31+
/// ```
32+
///
33+
/// # Single argument
34+
///
35+
/// ```compile_fail,ignore
36+
/// use macros::hashmap;
37+
/// use std::collections::HashMap;
38+
///
39+
/// // a single argument is invalid
40+
/// let _hm: HashMap<_, _> = hashmap!('a');
41+
/// ```
42+
///
43+
/// # Triple arguments
44+
///
45+
/// ```compile_fail,ignore
46+
/// use macros::hashmap;
47+
/// use std::collections::HashMap;
48+
///
49+
/// // three arguments are invalid
50+
/// hashmap!('a' => 1, 'b');
51+
/// ```
52+
///
53+
/// # Only arrow
54+
///
55+
/// ```compile_fail,ignore
56+
/// use macros::hashmap;
57+
/// use std::collections::HashMap;
58+
///
59+
/// // a single random arrow is not valid
60+
/// let _hm: HashMap<(), ()> = hashmap!(=>);
61+
/// ```
62+
///
63+
/// # Trailing arrow
64+
///
65+
/// ```compile_fail,ignore
66+
/// use macros::hashmap;
67+
/// use std::collections::HashMap;
68+
///
69+
/// // a trailing => isn't valid either
70+
/// hashmap!('a' => 2, =>);
71+
/// ```
72+
///
73+
/// # Leading comma
74+
///
75+
/// ```compile_fail,ignore
76+
/// use macros::hashmap;
77+
/// use std::collections::HashMap;
78+
///
79+
/// // leading commas are not valid
80+
/// let _hm: HashMap<_, _> = hashmap!(, 'a' => 2);
81+
/// ```
82+
///
83+
/// # Missing comma
84+
///
85+
/// ```compile_fail,ignore
86+
/// use macros::hashmap;
87+
/// use std::collections::HashMap;
88+
///
89+
/// // Key value pairs must be separated by commas
90+
/// let _hm: HashMap<_, _> = hashmap!('a' => 1 'b' => 2);
91+
/// ```
92+
///
93+
/// # Missing argument
94+
///
95+
/// ```compile_fail,ignore
96+
/// use macros::hashmap;
97+
/// use std::collections::HashMap;
98+
///
99+
/// // an argument should come between each pair of commas
100+
/// let _hm: HashMap<_, _> = hashmap!('a' => 1, , 'b' => 2);
101+
/// ```
102+
///
103+
const _TESTS: () = ();

exercises/practice/macros/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ macro_rules! hashmap {
44
todo!()
55
};
66
}
7+
8+
/// This module contains doctests, which allows writing tests where a code
9+
/// snippet is supposed to fail to compile. These tests also have "ignore"
10+
/// attributes, makes sure to remove them when solving this exercise locally.
11+
mod compile_fail_tests;

exercises/practice/macros/tests/invalid/Cargo.toml

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

exercises/practice/macros/tests/invalid/comma_sep.rs

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

exercises/practice/macros/tests/invalid/double_commas.rs

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

exercises/practice/macros/tests/invalid/leading_comma.rs

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

exercises/practice/macros/tests/invalid/missing_argument.rs

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

exercises/practice/macros/tests/invalid/no_comma.rs

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

exercises/practice/macros/tests/invalid/only_arrow.rs

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

0 commit comments

Comments
 (0)