@@ -178,7 +178,7 @@ fn partition_source(s: &str) -> (String, String) {
178
178
179
179
for line in s. lines ( ) {
180
180
let trimline = line. trim ( ) ;
181
- let header = trimline. chars ( ) . all ( char :: is_whitespace ) || trimline. starts_with ( "#![" ) ;
181
+ let header = trimline. is_empty ( ) || trimline. starts_with ( "#![" ) ;
182
182
if !header || after_header {
183
183
after_header = true ;
184
184
after. push_str ( line) ;
@@ -192,6 +192,51 @@ fn partition_source(s: &str) -> (String, String) {
192
192
( before, after)
193
193
}
194
194
195
+ #[ cfg( test) ]
196
+ mod partition_source_tests {
197
+ use super :: partition_source;
198
+
199
+ macro_rules! run_tests {
200
+ ( $( $name: ident, $initial: expr => ( $before: expr, $after: expr) ) ,+) => {
201
+ $(
202
+ #[ test]
203
+ fn $name( ) {
204
+ let sources = partition_source( $initial) ;
205
+ assert_eq!( sources, ( String :: from( $before) , String :: from( $after) ) ) ;
206
+ }
207
+ ) +
208
+ } ;
209
+ }
210
+
211
+ run_tests ! (
212
+ without_header, "some text" => ( "" , "some text\n " ) ,
213
+ two_string_without_header, "some\n text" => ( "" , "some\n text\n " ) ,
214
+ three_string_separated_by_empty_string, "some\n \n text" => ( "" , "some\n \n text\n " ) ,
215
+
216
+ some_empty_lines_before_text, "\n \n \n some text" => ( "\n \n \n " , "some text\n " ) ,
217
+
218
+ only_header, "#![allow(unused_variables)]" => (
219
+ "#![allow(unused_variables)]\n " ,
220
+ ""
221
+ ) ,
222
+
223
+ one_line_header, "#![allow(unused_variables)]\n #fn main() {\n let a = 5;\n #}</code></pre>" => (
224
+ "#![allow(unused_variables)]\n " ,
225
+ "#fn main() {\n let a = 5;\n #}</code></pre>\n "
226
+ ) ,
227
+
228
+ multiline_header, "#![allow(unused_variables)]\n #![allow(missing_docs)]\n #fn main() {\n let a = 5;\n #}</code></pre>" => (
229
+ "#![allow(unused_variables)]\n #![allow(missing_docs)]\n " ,
230
+ "#fn main() {\n let a = 5;\n #}</code></pre>\n "
231
+ ) ,
232
+
233
+ multiline_header_with_empty_string, "#![allow(unused_variables)]\n \n #![allow(missing_docs)]\n #fn main() {\n let a = 5;\n #}</code></pre>" => (
234
+ "#![allow(unused_variables)]\n \n #![allow(missing_docs)]\n " ,
235
+ "#fn main() {\n let a = 5;\n #}</code></pre>\n "
236
+ )
237
+ ) ;
238
+ }
239
+
195
240
#[ cfg( test) ]
196
241
mod tests {
197
242
use super :: * ;
0 commit comments