about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarcus Klaas <mail@marcusklaas.nl>2015-09-09 23:13:37 +0200
committerMarcus Klaas <mail@marcusklaas.nl>2015-09-11 00:53:01 +0200
commitabe8e7de990b88e2b3d7349bc1fc240b4eb49bad (patch)
tree12737f9e45f8596e526c846b3a4c924b20370b7a
parent749a9689be022f2737820a87fe09d930907435a5 (diff)
Add tests for chain expressions
-rw-r--r--src/chains.rs9
-rw-r--r--src/comment.rs27
-rw-r--r--tests/source/chains.rs33
-rw-r--r--tests/system.rs3
-rw-r--r--tests/target/chains.rs38
5 files changed, 78 insertions, 32 deletions
diff --git a/src/chains.rs b/src/chains.rs
index 4f395df0487..e5d2a43840b 100644
--- a/src/chains.rs
+++ b/src/chains.rs
@@ -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))
diff --git a/src/comment.rs b/src/comment.rs
index 313510f02b7..290fc62e15a 100644
--- a/src/comment.rs
+++ b/src/comment.rs
@@ -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
diff --git a/tests/source/chains.rs b/tests/source/chains.rs
new file mode 100644
index 00000000000..7e4bda0671e
--- /dev/null
+++ b/tests/source/chains.rs
@@ -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)
+}
diff --git a/tests/system.rs b/tests/system.rs
index 9cfe9101986..fb877993daf 100644
--- a/tests/system.rs
+++ b/tests/system.rs
@@ -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."))
 }
 
 
diff --git a/tests/target/chains.rs b/tests/target/chains.rs
new file mode 100644
index 00000000000..2ed91a2eb8e
--- /dev/null
+++ b/tests/target/chains.rs
@@ -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)
+}