Skip to content

Commit dde5e00

Browse files
committed
added tests for partition_source
1 parent 28e464f commit dde5e00

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/renderer/html_handlebars/hbs_wrapper.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn partition_source(s: &str) -> (String, String) {
178178

179179
for line in s.lines() {
180180
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("#![");
182182
if !header || after_header {
183183
after_header = true;
184184
after.push_str(line);
@@ -192,6 +192,51 @@ fn partition_source(s: &str) -> (String, String) {
192192
(before, after)
193193
}
194194

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\ntext" => ("", "some\ntext\n"),
214+
three_string_separated_by_empty_string, "some\n\ntext" => ("", "some\n\ntext\n"),
215+
216+
some_empty_lines_before_text, "\n\n\nsome 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() {\nlet a = 5;\n#}</code></pre>" => (
224+
"#![allow(unused_variables)]\n",
225+
"#fn main() {\nlet a = 5;\n#}</code></pre>\n"
226+
),
227+
228+
multiline_header, "#![allow(unused_variables)]\n#![allow(missing_docs)]\n#fn main() {\nlet a = 5;\n#}</code></pre>" => (
229+
"#![allow(unused_variables)]\n#![allow(missing_docs)]\n",
230+
"#fn main() {\nlet 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() {\nlet a = 5;\n#}</code></pre>" => (
234+
"#![allow(unused_variables)]\n\n#![allow(missing_docs)]\n",
235+
"#fn main() {\nlet a = 5;\n#}</code></pre>\n"
236+
)
237+
);
238+
}
239+
195240
#[cfg(test)]
196241
mod tests {
197242
use super::*;

0 commit comments

Comments
 (0)