diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-10-14 09:52:53 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-10-14 09:52:53 +0200 |
commit | 95f66dc8b4a8cb4c40aa6e4b231bafde516a68bc (patch) | |
tree | 651e51e486ad0eaef0d488eff6f9f6d00dbe7723 /src/util/location.rs | |
parent | 930630506c6d42250b5fe6b883ac65b2e9ba668b (diff) | |
download | markdown-rs-95f66dc8b4a8cb4c40aa6e4b231bafde516a68bc.tar.gz markdown-rs-95f66dc8b4a8cb4c40aa6e4b231bafde516a68bc.tar.bz2 markdown-rs-95f66dc8b4a8cb4c40aa6e4b231bafde516a68bc.zip |
Fix positional info of cr+lf in mdx
Diffstat (limited to 'src/util/location.rs')
-rw-r--r-- | src/util/location.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/util/location.rs b/src/util/location.rs index 0c9c426..deff307 100644 --- a/src/util/location.rs +++ b/src/util/location.rs @@ -32,6 +32,7 @@ impl Location { if bytes[index] == b'\r' { if index + 1 < bytes.len() && bytes[index + 1] == b'\n' { location_index.indices.push(index + 2); + index += 1; } else { location_index.indices.push(index + 1); } @@ -109,3 +110,90 @@ impl Location { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_location_lf() { + let location = Location::new("ab\nc".as_bytes()); + assert_eq!( + location.to_point(0), // `a` + Some(Point::new(1, 1, 0)), + "should support some points (1)" + ); + assert_eq!( + location.to_point(1), // `b` + Some(Point::new(1, 2, 1)), + "should support some points (2)" + ); + assert_eq!( + location.to_point(2), // `\n` + Some(Point::new(1, 3, 2)), + "should support some points (3)" + ); + assert_eq!( + location.to_point(3), // `c` + Some(Point::new(2, 1, 3)), + "should support some points (4)" + ); + assert_eq!( + location.to_point(4), // EOF + // Still gets a point, so that we can represent positions of things + // that end at the last character, the `c` end at `2:2`. + Some(Point::new(2, 2, 4)), + "should support some points (5)" + ); + assert_eq!( + location.to_point(5), // Out of bounds + None, + "should support some points (6)" + ); + } + + #[test] + fn test_location_cr() { + let location = Location::new("a\rb".as_bytes()); + assert_eq!( + location.to_point(0), // `a` + Some(Point::new(1, 1, 0)), + "should support some points (1)" + ); + assert_eq!( + location.to_point(1), // `\r` + Some(Point::new(1, 2, 1)), + "should support some points (2)" + ); + assert_eq!( + location.to_point(2), // `b` + Some(Point::new(2, 1, 2)), + "should support some points (3)" + ); + } + + #[test] + fn test_location_cr_lf() { + let location = Location::new("a\r\nb".as_bytes()); + assert_eq!( + location.to_point(0), // `a` + Some(Point::new(1, 1, 0)), + "should support some points (1)" + ); + assert_eq!( + location.to_point(1), // `\r` + Some(Point::new(1, 2, 1)), + "should support some points (2)" + ); + assert_eq!( + location.to_point(2), // `\n` + Some(Point::new(1, 3, 2)), + "should support some points (3)" + ); + assert_eq!( + location.to_point(3), // `b` + Some(Point::new(2, 1, 3)), + "should support some points (4)" + ); + } +} |