add some tests.... and fix some bugs!

This commit is contained in:
Sébastien Besnier 2020-11-14 18:51:43 +01:00
parent 2000948765
commit 6be94c5f29
3 changed files with 40 additions and 17 deletions

View File

@ -814,10 +814,8 @@ pub fn fmt_record<'a>(
format_field_multiline(buf, &field.value, field_indent, "");
}
if final_comments.iter().any(|s| s.is_comment()) {
newline(buf, field_indent);
fmt_final_comments_spaces(buf, final_comments.iter(), field_indent);
}
fmt_final_comments_spaces(buf, final_comments.iter(), field_indent);
newline(buf, indent);
} else
// is_multiline == false
@ -885,17 +883,12 @@ fn format_field_multiline<'a, T>(
buf.push(',');
}
AssignedField::SpaceBefore(sub_field, spaces) => {
if spaces.iter().any(|s| s.is_comment()) {
fmt_condition_spaces(buf, spaces.iter(), indent);
}
fmt_final_comments_spaces(buf, spaces.iter(), indent);
format_field_multiline(buf, sub_field, indent, separator_prefix);
}
AssignedField::SpaceAfter(sub_field, spaces) => {
format_field_multiline(buf, sub_field, indent, separator_prefix);
if spaces.iter().any(|s| s.is_comment()) {
newline(buf, indent);
fmt_condition_spaces(buf, spaces.iter(), indent);
}
fmt_final_comments_spaces(buf, spaces.iter(), indent);
}
Malformed(raw) => {
buf.push_str(raw);

View File

@ -107,18 +107,14 @@ where
match space {
Newline => {}
LineComment(comment) => {
newline(buf, indent);
buf.push('#');
buf.push_str(comment);
if let Some(true) = iter.peek().map(|s| s.is_comment()) {
newline(buf, indent);
}
}
DocComment(docs) => {
newline(buf, indent);
buf.push_str("##");
buf.push_str(docs);
if let Some(true) = iter.peek().map(|s| s.is_comment()) {
newline(buf, indent);
}
}
}
}

View File

@ -682,6 +682,40 @@ mod test_fmt {
);
}
#[test]
fn comments_with_newlines_in_records() {
expr_formats_to(
indoc!(
r#"
{
z: 44 #comment 0
,
y: 41, # comment 1
# comment 2
x: 42
# comment 3
# comment 4
}"#
),
indoc!(
r#"
{
z: 44,
#comment 0
y: 41,
# comment 1
# comment 2
x: 42,
# comment 3
# comment 4
}"#
),
);
}
#[test]
fn multiple_final_comments_with_comma_in_records() {
expr_formats_to(