@@ -124,21 +124,61 @@ r[attributes.testing.should_panic]
124
124
## The `should_panic ` attribute
125
125
126
126
r [attributes . testing. should_panic. intro]
127
- A function annotated with the ` test ` attribute that returns `()` can also be annotated with the ` should_panic ` attribute .
127
+ The * ` should_panic ` [ attribute ][ attributes ] * changes a [ test function ][ attributes . testing . test] so that it passes only if it panics .
128
128
129
- r [attributes . testing. should_panic. behavior]
130
- The * `should_panic ` attribute * makes the test only pass if it actually panics .
129
+ > [! EXAMPLE ]
130
+ > ```rust
131
+ > #[test]
132
+ > #[should_panic(expected = " values don't match" )]
133
+ > fn mytest () {
134
+ > assert_eq! (1 , 2 , " values don't match" );
135
+ > }
136
+ > ```
131
137
132
138
r [attributes . testing. should_panic. syntax]
133
- The `should_panic ` attribute may optionally take an input string that must appear within the panic message . If the string is not found in the message , then the test will fail . The string may be passed using the [MetaNameValueStr ] syntax or the [MetaListNameValueStr ] syntax with an `expected ` field .
134
-
135
- ```rust
136
- #[test]
137
- #[should_panic(expected = " values don't match" )]
138
- fn mytest () {
139
- assert_eq! (1 , 2 , " values don't match" );
140
- }
141
- ```
139
+ The `should_panic ` attribute is specified with one of the following forms :
140
+
141
+ - [MetaWord ]
142
+ > [! EXAMPLE ]
143
+ > ```rust
144
+ > #[test]
145
+ > #[should_panic]
146
+ > fn mytest () { panic! (" some message" ); }
147
+ > ```
148
+
149
+ - [MetaNameValueStr ] --- This indicates that the given string should appear within the panic message .
150
+ > [! EXAMPLE ]
151
+ > ```rust
152
+ > #[test]
153
+ > #[should_panic = " some message" ]
154
+ > fn mytest () { panic! (" some message" ); }
155
+ > ```
156
+
157
+ - [MetaListNameValueStr ] --- Specified with the key `expected `. Same behavior as [MetaNameValueStr ], just with an explicit key .
158
+ > [! EXAMPLE ]
159
+ > ```rust
160
+ > #[test]
161
+ > #[should_panic(expected = " some message" )]
162
+ > fn mytest () { panic! (" some message" ); }
163
+ > ```
164
+
165
+ r [attributes . testing. should_panic. allowed- positions ]
166
+ The `should_panic ` attribute may be applied to functions annotated with the `test ` attribute .
167
+
168
+ > [! NOTE ]
169
+ > `rustc ` currently warns in some other positions . This may become a hard error in the future .
170
+
171
+ r [attributes . testing. should_panic. duplicates]
172
+ Only the first instance of `should_panic ` on a function is honored . Subsequent `should_panic ` attributes are ignored .
173
+
174
+ > [! NOTE ]
175
+ > `rustc ` currently ignores subsequent duplicate `should_panic ` attributes . This may become an error in the future .
176
+
177
+ r [attributes . testing. should_panic. expected]
178
+ The string specified with the [MetaNameValueStr ] form or the `expected ` key in [MetaListNameValueStr ] indicates that the string must appear somewhere within the panic message . If the string is not found in the message , then the test will fail .
179
+
180
+ r [attributes . testing. should_panic. return ]
181
+ The return type of the test function must be `()`.
142
182
143
183
[`Termination `]: std :: process :: Termination
144
184
[`report `]: std :: process :: Termination :: report
0 commit comments