Skip to content

Commit 12fbcd8

Browse files
authored
macros: attach doctests to separate items (#2162)
Cargo emits the item name in the test output for doctests. In the future, we can use this in the test runner to display a better name. [no important files changed]
1 parent faba793 commit 12fbcd8

2 files changed

Lines changed: 22 additions & 34 deletions

File tree

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,91 @@
1-
/// To activate a doctest locally, remove ",ignore" from the code block.
2-
///
3-
/// # Comma separator
4-
///
1+
// To activate a doctest locally, remove ",ignore" from the code block.
2+
53
/// ```compile_fail,ignore
64
/// use macros::hashmap;
75
/// use std::collections::HashMap;
86
///
97
/// // using only commas is invalid
108
/// let _hm: HashMap<_, _> = hashmap!('a', 1);
119
/// ```
12-
///
13-
/// # Double trailing commas
14-
///
10+
const _COMMA_SEPARATOR: () = ();
11+
1512
/// ```compile_fail,ignore
1613
/// use macros::hashmap;
1714
/// use std::collections::HashMap;
1815
///
1916
/// // a single trailing comma is okay, but two is not
2017
/// let _hm: HashMap<_, _> = hashmap!('a' => 2, ,);
2118
/// ```
22-
///
23-
/// # Only comma
24-
///
19+
const _DOUBLE_TRAILING_COMMAS: () = ();
20+
2521
/// ```compile_fail,ignore
2622
/// use macros::hashmap;
2723
/// use std::collections::HashMap;
2824
///
2925
/// // a single random comma is not valid
3026
/// let _hm: HashMap<(), ()> = hashmap!(,);
3127
/// ```
32-
///
33-
/// # Single argument
34-
///
28+
const _ONLY_COMMA: () = ();
29+
3530
/// ```compile_fail,ignore
3631
/// use macros::hashmap;
3732
/// use std::collections::HashMap;
3833
///
3934
/// // a single argument is invalid
4035
/// let _hm: HashMap<_, _> = hashmap!('a');
4136
/// ```
42-
///
43-
/// # Triple arguments
44-
///
37+
const _SINGLE_ARGUMENT: () = ();
38+
4539
/// ```compile_fail,ignore
4640
/// use macros::hashmap;
4741
/// use std::collections::HashMap;
4842
///
4943
/// // three arguments are invalid
5044
/// hashmap!('a' => 1, 'b');
5145
/// ```
52-
///
53-
/// # Only arrow
54-
///
46+
const _TRIPLE_ARGUMENTS: () = ();
47+
5548
/// ```compile_fail,ignore
5649
/// use macros::hashmap;
5750
/// use std::collections::HashMap;
5851
///
5952
/// // a single random arrow is not valid
6053
/// let _hm: HashMap<(), ()> = hashmap!(=>);
6154
/// ```
62-
///
63-
/// # Trailing arrow
64-
///
55+
const _ONLY_ARROW: () = ();
56+
6557
/// ```compile_fail,ignore
6658
/// use macros::hashmap;
6759
/// use std::collections::HashMap;
6860
///
6961
/// // a trailing => isn't valid either
7062
/// hashmap!('a' => 2, =>);
7163
/// ```
72-
///
73-
/// # Leading comma
74-
///
64+
const _TRAILING_ARROW: () = ();
65+
7566
/// ```compile_fail,ignore
7667
/// use macros::hashmap;
7768
/// use std::collections::HashMap;
7869
///
7970
/// // leading commas are not valid
8071
/// let _hm: HashMap<_, _> = hashmap!(, 'a' => 2);
8172
/// ```
82-
///
83-
/// # Missing comma
84-
///
73+
const _LEADING_COMMA: () = ();
74+
8575
/// ```compile_fail,ignore
8676
/// use macros::hashmap;
8777
/// use std::collections::HashMap;
8878
///
8979
/// // Key value pairs must be separated by commas
9080
/// let _hm: HashMap<_, _> = hashmap!('a' => 1 'b' => 2);
9181
/// ```
92-
///
93-
/// # Missing argument
94-
///
82+
const _MISSING_COMMA: () = ();
83+
9584
/// ```compile_fail,ignore
9685
/// use macros::hashmap;
9786
/// use std::collections::HashMap;
9887
///
9988
/// // an argument should come between each pair of commas
10089
/// let _hm: HashMap<_, _> = hashmap!('a' => 1, , 'b' => 2);
10190
/// ```
102-
///
103-
const _TESTS: () = ();
91+
const _MISSING_ARGUMENT: () = ();

exercises/practice/macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ macro_rules! hashmap {
77

88
/// This module contains doctests, which allows writing tests where a code
99
/// snippet is supposed to fail to compile. These tests also have "ignore"
10-
/// attributes, makes sure to remove them when solving this exercise locally.
10+
/// attributes, make sure to remove them when solving this exercise locally.
1111
pub mod compile_fail_tests;

0 commit comments

Comments
 (0)