+ let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
+ rewrite_string("eq_", &fmt);
+ }
+
+ #[test]
+ fn should_break_on_whitespace() {
+ let string = "Placerat felis. Mauris porta ante sagittis purus.";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(20, false, "", &graphemes[..]),
+ SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
+ );
+ assert_eq!(
+ break_string(20, true, "", &graphemes[..]),
+ SnippetState::LineEnd("Placerat felis.".to_string(), 16)
+ );
+ }
+
+ #[test]
+ fn should_break_on_punctuation() {
+ let string = "Placerat_felis._Mauris_porta_ante_sagittis_purus.";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(20, false, "", &graphemes[..]),
+ SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
+ );
+ }
+
+ #[test]
+ fn should_break_forward() {
+ let string = "Venenatis_tellus_vel_tellus. Aliquam aliquam dolor at justo.";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(20, false, "", &graphemes[..]),
+ SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
+ );
+ assert_eq!(
+ break_string(20, true, "", &graphemes[..]),
+ SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
+ );
+ }
+
+ #[test]
+ fn nothing_to_break() {
+ let string = "Venenatis_tellus_vel_tellus";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(20, false, "", &graphemes[..]),
+ SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
+ );
+ }
+
+ #[test]
+ fn significant_whitespaces() {
+ let string = "Neque in sem. \n Pellentesque tellus augue.";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(15, false, "", &graphemes[..]),
+ SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
+ );
+ assert_eq!(
+ break_string(25, false, "", &graphemes[..]),
+ SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
+ );
+
+ assert_eq!(
+ break_string(15, true, "", &graphemes[..]),
+ SnippetState::LineEnd("Neque in sem.".to_string(), 19)
+ );
+ assert_eq!(
+ break_string(25, true, "", &graphemes[..]),
+ SnippetState::EndWithLineFeed("Neque in sem.\n".to_string(), 20)
+ );
+ }
+
+ #[test]
+ fn big_whitespace() {
+ let string = "Neque in sem. Pellentesque tellus augue.";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(20, false, "", &graphemes[..]),
+ SnippetState::LineEnd("Neque in sem. ".to_string(), 25)
+ );
+ assert_eq!(
+ break_string(20, true, "", &graphemes[..]),
+ SnippetState::LineEnd("Neque in sem.".to_string(), 25)
+ );
+ }
+
+ #[test]
+ fn newline_in_candidate_line() {
+ let string = "Nulla\nconsequat erat at massa. Vivamus id mi.";
+
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(25, false, "", &graphemes[..]),
+ SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
+ );
+ assert_eq!(
+ break_string(25, true, "", &graphemes[..]),
+ SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
+ );
+
+ let mut config: Config = Default::default();
+ config.set().max_width(27);
+ let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
+ let rewritten_string = rewrite_string(string, &fmt);
+ assert_eq!(
+ rewritten_string,
+ Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string())
+ );
+ }
+
+ #[test]
+ fn last_line_fit_with_trailing_whitespaces() {
+ let string = "Vivamus id mi. ";
+ let config: Config = Default::default();
+ let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
+
+ fmt.trim_end = true;
+ let rewritten_string = rewrite_string(string, &fmt);
+ assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string()));
+
+ fmt.trim_end = false; // default value of trim_end
+ let rewritten_string = rewrite_string(string, &fmt);
+ assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string()));
+ }
+
+ #[test]
+ fn last_line_fit_with_newline() {
+ let string = "Vivamus id mi.\nVivamus id mi.";
+ let config: Config = Default::default();