+
+ #[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));
+ }