use super::{Comment, find_trailing_comment, normalise_comment_text};
#[test]
fn normalise_strips_markers_and_decoration() {
assert_eq!(normalise_comment_text("// hello"), "hello");
assert_eq!(normalise_comment_text("// hello "), "hello");
assert_eq!(normalise_comment_text("//-- hello"), "hello");
assert_eq!(normalise_comment_text("//== hello"), "hello");
assert_eq!(normalise_comment_text("//* hello"), "hello");
assert_eq!(normalise_comment_text("//--hello"), "--hello");
assert_eq!(normalise_comment_text("// > quoted"), "> quoted");
}
#[test]
fn normalise_collapses_empty_and_whitespace_only_comments() {
assert_eq!(normalise_comment_text("//"), "");
assert_eq!(normalise_comment_text("// "), "");
assert_eq!(normalise_comment_text("//\t"), "");
}
#[test]
fn normalise_collapses_all_decoration_comments() {
assert_eq!(normalise_comment_text("//----------"), "");
assert_eq!(normalise_comment_text("//=========="), "");
assert_eq!(normalise_comment_text("//**********"), "");
assert_eq!(normalise_comment_text("//-=-=-=-="), "");
assert_eq!(normalise_comment_text("//--- "), "");
}
fn assert_comment(actual: Option<Comment>, expected_text: &str) {
let comment = actual.expect("expected a comment match");
assert_eq!(comment.text, expected_text);
}
#[test]
fn trailing_simple() {
let source = "#[allow(foo)] // hello\n";
let attr_hi = source.find(']').unwrap() + 1;
assert_comment(find_trailing_comment(source, attr_hi), "hello");
}
#[test]
fn trailing_skips_doc_marker() {
let source = "#[allow(foo)] /// hello\n";
let attr_hi = source.find(']').unwrap() + 1;
assert!(find_trailing_comment(source, attr_hi).is_none());
}
#[test]
fn trailing_skips_inner_doc_marker() {
let source = "#[allow(foo)] //! hello\n";
let attr_hi = source.find(']').unwrap() + 1;
assert!(find_trailing_comment(source, attr_hi).is_none());
}
#[test]
fn trailing_empty_normalised_returns_none() {
for source in [
"#[allow(foo)] //\n",
"#[allow(foo)] // \n",
"#[allow(foo)] //----------\n",
] {
let attr_hi = source.find(']').unwrap() + 1;
assert!(
find_trailing_comment(source, attr_hi).is_none(),
"expected no match for {source:?}",
);
}
}
#[test]
fn trailing_accepts_quadruple_slash() {
let source = "#[allow(foo)] //// hello\n";
let attr_hi = source.find(']').unwrap() + 1;
assert_comment(find_trailing_comment(source, attr_hi), "// hello");
}
#[test]
fn trailing_no_comment_on_same_line() {
let source = "#[allow(foo)]\n// next line\n";
let attr_hi = source.find(']').unwrap() + 1;
assert!(find_trailing_comment(source, attr_hi).is_none());
}
#[test]
fn trailing_handles_crlf() {
let source = "#[allow(foo)] // hello\r\n";
let attr_hi = source.find(']').unwrap() + 1;
assert_comment(find_trailing_comment(source, attr_hi), "hello");
}
#[test]
fn trailing_crlf_delete_range_excludes_carriage_return() {
let source = "#[allow(foo)] // hello\r\n";
let attr_hi = source.find(']').unwrap() + 1;
let comment = find_trailing_comment(source, attr_hi).expect("expected a match");
assert_eq!(&source[comment.delete_end..], "\r\n");
}