Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LocatedSpan::get_line(). #66

Merged
merged 10 commits into from
Oct 18, 2020
93 changes: 93 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,96 @@ fn it_should_display_hex() {
"00000000\t61 62 63 \tabc\n".to_owned()
);
}

#[test]
fn line_of_empty_span_is_empty() {
assert_eq!(
StrSpan::new("").get_line(),
Some("".as_ref()),
);
}

#[test]
fn line_of_single_line_start_is_whole() {
assert_eq!(
StrSpan::new("A single line").get_line(),
Some("A single line".as_ref()),
);
}
#[test]
fn line_of_single_line_end_is_whole() {
let data = "A single line";
assert_eq!(
StrSpan::new(data).slice(data.len()..).get_line(),
Some("A single line".as_ref()),
);
}

#[test]
fn line_of_start_is_first() {
assert_eq!(
StrSpan::new(
"One line of text\
\nFollowed by a second\
\nand a third\n"
).get_line(),
Some("One line of text".as_ref()),
);
}

#[test]
fn line_of_nl_is_before() {
let data =
"One line of text\
\nFollowed by a second\
\nand a third\n";
assert_eq!(
StrSpan::new(data).slice(data.find('\n').unwrap()..).get_line(),
Some("One line of text".as_ref()),
);
}

#[test]
fn line_of_end_after_nl_is_empty() {
let data =
"One line of text\
\nFollowed by a second\
\nand a third\n";
assert_eq!(
StrSpan::new(data).slice(data.len()..).get_line(),
Some("".as_ref()),
);
}

#[test]
fn line_of_end_no_nl_is_last() {
let data =
"One line of text\
\nFollowed by a second\
\nand a third";
assert_eq!(
StrSpan::new(data).slice(data.len()..).get_line(),
Some("and a third".as_ref()),
);
}

#[test]
fn line_for_non_ascii_chars() {
// I don't really know if this Oriya text makes sense.
kaj marked this conversation as resolved.
Show resolved Hide resolved
let data = StrSpan::new(
"Några rader text på Svenska.\
\nFörra raden var först, den här är i mitten\
\noch här är sista raden.\n");
let s = data.slice(data.find_substring("först").unwrap()..);
assert_eq!(
format!(
"{line_no:3}: {line_text}\n {0:>lpos$}^- The match\n",
"",
line_no = s.location_line(),
line_text = core::str::from_utf8(s.get_line().unwrap()).unwrap(),
lpos = s.get_utf8_column(),
),
" 2: Förra raden var först, den här är i mitten\
\n ^- The match\n",
);
}