Split assert_eq! if any arguments are not simple

This commit is contained in:
David Wood 2017-12-27 21:19:42 +00:00
parent 212a8a3c29
commit 39e2f43f91
4 changed files with 36 additions and 51 deletions

View file

@ -2082,18 +2082,16 @@ where
tactic = default_tactic();
if tactic == DefinitiveListTactic::Vertical {
if let Some((all_simple_before, all_simple_after, num_args_before)) =
if let Some((all_simple, num_args_before)) =
maybe_get_args_offset(callee_str, args)
{
let one_line_before = all_simple_before
let one_line = all_simple
&& definitive_tactic(
&item_vec[..num_args_before],
ListTactic::HorizontalVertical,
Separator::Comma,
nested_shape.width,
) == DefinitiveListTactic::Horizontal;
let one_line_after = all_simple_after
) == DefinitiveListTactic::Horizontal
&& definitive_tactic(
&item_vec[num_args_before + 1..],
ListTactic::HorizontalVertical,
@ -2101,11 +2099,9 @@ where
nested_shape.width,
) == DefinitiveListTactic::Horizontal;
tactic = DefinitiveListTactic::SpecialMacro(
one_line_before,
one_line_after,
num_args_before,
);
if one_line {
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
};
}
}
}
@ -2141,18 +2137,14 @@ fn is_every_args_simple<T: ToExpr>(lists: &[&T]) -> bool {
}
/// In case special-case style is required, returns an offset from which we start horizontal layout.
fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<(bool, bool, usize)> {
fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<(bool, usize)> {
if let Some(&(_, num_args_before)) = SPECIAL_MACRO_WHITELIST
.iter()
.find(|&&(s, _)| s == callee_str)
{
let all_simple_before =
args.len() >= num_args_before && is_every_args_simple(&args[..num_args_before]);
let all_simple = args.len() >= num_args_before && is_every_args_simple(args);
let all_simple_after =
args.len() >= num_args_before + 1 && is_every_args_simple(&args[num_args_before + 1..]);
Some((all_simple_before, all_simple_after, num_args_before))
Some((all_simple, num_args_before))
} else {
None
}

View file

@ -161,7 +161,7 @@ pub enum DefinitiveListTactic {
Horizontal,
Mixed,
/// Special case tactic for `format!()`, `write!()` style macros.
SpecialMacro(bool, bool, usize),
SpecialMacro(usize),
}
impl DefinitiveListTactic {
@ -311,30 +311,16 @@ where
DefinitiveListTactic::Horizontal if !first => {
result.push(' ');
}
DefinitiveListTactic::SpecialMacro(
one_line_before,
one_line_after,
num_args_before,
) => {
DefinitiveListTactic::SpecialMacro(num_args_before) => {
if i == 0 {
// Nothing
} else if i < num_args_before {
if one_line_before {
result.push(' ');
} else {
result.push('\n');
result.push_str(indent_str);
}
result.push(' ');
} else if i <= num_args_before + 1 {
result.push('\n');
result.push_str(indent_str);
} else {
if one_line_after {
result.push(' ');
} else {
result.push('\n');
result.push_str(indent_str);
}
result.push(' ');
}
}
DefinitiveListTactic::Vertical if !first => {

View file

@ -267,21 +267,21 @@ fn special_case_macros() {
warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
assert!(result == 42, "Ahoy there, {}!", target);
assert!(result == 42, "Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')", result, input, expected);
assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert!(result == 42, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
assert_eq!(left, right, "Ahoy there, {}!", target);
assert_eq!(left, right, "Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')", result, input, expected);
assert_eq!(first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line, second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line, "Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')", result, input, expected);
assert_eq!(left + 42, right, "Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')", result, input, expected);
assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert_eq!(first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line, second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert_eq!(left, right, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
write!(&mut s, "Ahoy there, {}!", target);
write!(&mut s, "Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')", result, input, expected);
write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
write!(&mut s, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
writeln!(&mut s, "Ahoy there, {}!", target);
writeln!(&mut s, "Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')", result, input, expected);
writeln!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
writeln!(&mut s, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
}

View file

@ -694,8 +694,10 @@ fn special_case_macros() {
assert!(result == 42, "Ahoy there, {}!", target);
assert!(
result == 42,
"Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')",
result, input, expected
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
result,
input,
expected
);
assert!(
result == 42,
@ -731,23 +733,28 @@ fn special_case_macros() {
assert_eq!(left, right, "Ahoy there, {}!", target);
assert_eq!(
left, right,
"Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')",
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
result, input, expected
);
assert_eq!(
first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line,
second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line,
"Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')",
result, input, expected
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
result,
input,
expected
);
assert_eq!(
left + 42,
right,
"Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')",
result, input, expected
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
result,
input,
expected
);
assert_eq!(
left, right,
left,
right,
"{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
1,
2,
@ -780,7 +787,7 @@ fn special_case_macros() {
write!(&mut s, "Ahoy there, {}!", target);
write!(
&mut s,
"Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')",
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
result, input, expected
);
write!(
@ -817,7 +824,7 @@ fn special_case_macros() {
writeln!(&mut s, "Ahoy there, {}!", target);
writeln!(
&mut s,
"Arr! Batten down the hatches, we got '{}' but not '{}' (we expected '{}')",
"Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
result, input, expected
);
writeln!(