Add tests for chain expressions

This commit is contained in:
Marcus Klaas 2015-09-09 23:13:37 +02:00
parent 749a9689be
commit abe8e7de99
5 changed files with 78 additions and 32 deletions

View File

@ -34,8 +34,11 @@ pub fn rewrite_chain(orig_expr: &ast::Expr,
}
let parent_rewrite = try_opt!(expr.rewrite(context, width, offset));
let total_width = rewrites.iter().fold(0, |a, b| a + b.len()) + parent_rewrite.len();
let fits_single_line = total_width <= width && rewrites.iter().all(|s| !s.contains('\n'));
if rewrites.len() == 1 {
if rewrites.len() == 1 && !fits_single_line &&
(is_continuable(expr) || parent_rewrite.len() <= context.config.tab_spaces) {
let extra_offset = extra_offset(&parent_rewrite, offset);
let offset = offset + extra_offset;
let max_width = try_opt!(width.checked_sub(extra_offset));
@ -47,9 +50,7 @@ pub fn rewrite_chain(orig_expr: &ast::Expr,
return Some(format!("{}{}", parent_rewrite, try_opt!(rerewrite)));
}
let total_width = rewrites.iter().fold(0, |a, b| a + b.len()) + parent_rewrite.len();
let connector = if total_width <= width && rewrites.iter().all(|s| !s.contains('\n')) {
let connector = if fits_single_line {
String::new()
} else {
format!("\n{}", make_indent(indent))

View File

@ -145,18 +145,6 @@ pub fn find_comment_end(s: &str) -> Option<usize> {
}
}
#[test]
fn comment_end() {
assert_eq!(Some(6), find_comment_end("// hi\n"));
assert_eq!(Some(9), find_comment_end("/* sup */ "));
assert_eq!(Some(9), find_comment_end("/*/**/ */ "));
assert_eq!(Some(6), find_comment_end("/*/ */ weird!"));
assert_eq!(None, find_comment_end("/* hi /* test */"));
assert_eq!(None, find_comment_end("// hi /* test */"));
assert_eq!(Some(9), find_comment_end("// hi /*\n."));
}
/// Returns true if text contains any comment.
pub fn contains_comment(text: &str) -> bool {
CharClasses::new(text.chars()).any(|(kind, _)| kind == CodeCharKind::Comment)
@ -173,21 +161,6 @@ pub fn uncommented(text: &str) -> String {
.collect()
}
#[test]
fn test_uncommented() {
assert_eq!(&uncommented("abc/*...*/"), "abc");
assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"), "..ac\n");
assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
}
#[test]
fn test_contains_comment() {
assert_eq!(contains_comment("abc"), false);
assert_eq!(contains_comment("abc // qsdf"), true);
assert_eq!(contains_comment("abc /* kqsdf"), true);
assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
}
struct CharClasses<T>
where T: Iterator,
T::Item: RichChar

33
tests/source/chains.rs Normal file
View File

@ -0,0 +1,33 @@
// Test chain formatting.
fn main() {
let a = b.c
.d
.1
.foo(|x| x + 1);
bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc
.ddddddddddddddddddddddddddd();
bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc.ddddddddddddddddddddddddddd.eeeeeeee();
x()
.y(|| match cond() { true => (), false => () });
loong_func()
.quux(move || if true {
1
} else {
2
});
let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum = xxxxxxx
.map(|x| x + 5)
.map(|x| x / 2)
.fold(0, |acc, x| acc + x);
aaaaaaaaaaaaaaaa.map(|x| {
x += 1;
x
}).filter(some_mod::some_filter)
}

View File

@ -121,7 +121,8 @@ pub fn idempotent_check(filename: String) -> Result<(), HashMap<String, Vec<Mism
// multithreaded rustfmt
thread::catch_panic(move || {
run(args, WriteMode::Return(HANDLE_RESULT), config);
}).map_err(|any| *any.downcast().ok().expect("Downcast failed."))
})
.map_err(|any| *any.downcast().ok().expect("Downcast failed."))
}

38
tests/target/chains.rs Normal file
View File

@ -0,0 +1,38 @@
// Test chain formatting.
fn main() {
let a = b.c.d.1.foo(|x| x + 1);
bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc.ddddddddddddddddddddddddddd();
bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc
.ddddddddddddddddddddddddddd
.eeeeeeee();
x().y(|| {
match cond() {
true => (),
false => (),
}
});
loong_func()
.quux(move || {
if true {
1
} else {
2
}
});
let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum = xxxxxxx.map(|x| x + 5)
.map(|x| x / 2)
.fold(0, |acc, x| acc + x);
aaaaaaaaaaaaaaaa
.map(|x| {
x += 1;
x
})
.filter(some_mod::some_filter)
}