+ rewrite_string("eq_", &fmt, 2);
+ }
+
+ #[test]
+ fn line_break_at_valid_points_test() {
+ let string = "[TheName](Dont::break::my::type::That::would::be::very::nice) break here";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(
+ break_string(20, false, "", &graphemes[..]),
+ SnippetState::LineEnd(
+ "[TheName](Dont::break::my::type::That::would::be::very::nice) ".to_string(),
+ 62
+ )
+ );
+ }
+
+ #[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, 27);
+ 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, 25);
+ 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, 25);
+ 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();
+ let fmt = StringFormat {
+ opener: "",
+ closer: "",
+ line_start: "// ",
+ line_end: "",
+ shape: Shape::legacy(100, Indent::from_width(&config, 4)),
+ trim_end: true,
+ config: &config,
+ };
+
+ let rewritten_string = rewrite_string(string, &fmt, 100);
+ assert_eq!(
+ rewritten_string,
+ Some("Vivamus id mi.\n // Vivamus id mi.".to_string())
+ );
+ }
+
+ #[test]
+ fn overflow_in_non_string_content() {
+ let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
+ let config: Config = Default::default();
+ let fmt = StringFormat {
+ opener: "",
+ closer: "",
+ line_start: "// ",
+ line_end: "",
+ shape: Shape::legacy(30, Indent::from_width(&config, 8)),
+ trim_end: true,
+ config: &config,
+ };
+
+ assert_eq!(
+ rewrite_string(comment, &fmt, 30),
+ Some(
+ "Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor"
+ .to_string()
+ )
+ );
+ }
+
+ #[test]
+ fn overflow_in_non_string_content_with_line_end() {
+ let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
+ let config: Config = Default::default();
+ let fmt = StringFormat {
+ opener: "",
+ closer: "",
+ line_start: "// ",
+ line_end: "@",
+ shape: Shape::legacy(30, Indent::from_width(&config, 8)),
+ trim_end: true,
+ config: &config,
+ };
+
+ assert_eq!(
+ rewrite_string(comment, &fmt, 30),
+ Some(
+ "Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor"
+ .to_string()
+ )
+ );
+ }
+
+ #[test]
+ fn blank_line_with_non_empty_line_start() {
+ let config: Config = Default::default();
+ let mut fmt = StringFormat {
+ opener: "",
+ closer: "",
+ line_start: "// ",
+ line_end: "",
+ shape: Shape::legacy(30, Indent::from_width(&config, 4)),
+ trim_end: true,
+ config: &config,
+ };
+
+ let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 30),
+ Some(
+ "Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string()
+ )
+ );
+
+ fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4));
+ let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 15),
+ Some(
+ r#"Aenean
+ //
+ // metus. Vestibulum
+ // ac lacus. Vivamus
+ // porttitor"#
+ .to_string()
+ )
+ );
+ }
+
+ #[test]
+ fn retain_blank_lines() {
+ let config: Config = Default::default();
+ let fmt = StringFormat {
+ opener: "",
+ closer: "",
+ line_start: "// ",
+ line_end: "",
+ shape: Shape::legacy(20, Indent::from_width(&config, 4)),
+ trim_end: true,
+ config: &config,
+ };
+
+ let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 20),
+ Some(
+ "Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string()
+ )
+ );
+
+ let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 20),
+ Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string())
+ );
+
+ let comment = "Aenean\n \nmetus. Vestibulum ac lacus.";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 20),
+ Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string())
+ );
+ }
+
+ #[test]
+ fn boundary_on_edge() {
+ let config: Config = Default::default();
+ let mut fmt = StringFormat {
+ opener: "",
+ closer: "",
+ line_start: "// ",
+ line_end: "",
+ shape: Shape::legacy(13, Indent::from_width(&config, 4)),
+ trim_end: true,
+ config: &config,
+ };
+
+ let comment = "Aenean metus. Vestibulum ac lacus.";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 13),
+ Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string())
+ );
+
+ fmt.trim_end = false;
+ let comment = "Vestibulum ac lacus.";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 13),
+ Some("Vestibulum \n // ac lacus.".to_string())
+ );
+
+ fmt.trim_end = true;
+ fmt.line_end = "\\";
+ let comment = "Vestibulum ac lacus.";
+ assert_eq!(
+ rewrite_string(comment, &fmt, 13),
+ Some("Vestibulum\\\n // ac lacus.".to_string())
+ );
+ }
+
+ #[test]
+ fn detect_urls() {
+ let string = "aaa http://example.org something";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(detect_url(&graphemes, 8), Some(21));
+
+ let string = "https://example.org something";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(detect_url(&graphemes, 0), Some(18));
+
+ let string = "aaa ftp://example.org something";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(detect_url(&graphemes, 8), Some(20));
+
+ let string = "aaa file://example.org something";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(detect_url(&graphemes, 8), Some(21));
+
+ let string = "aaa http not an url";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(detect_url(&graphemes, 6), None);
+
+ let string = "aaa file://example.org";
+ let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
+ assert_eq!(detect_url(&graphemes, 8), Some(21));