2018-12-06 11:42:03 -06:00
|
|
|
use std::fmt;
|
2018-08-17 08:04:34 -05:00
|
|
|
|
|
|
|
use itertools::Itertools;
|
2018-10-15 16:44:23 -05:00
|
|
|
use text_unit::{TextRange, TextUnit};
|
2018-08-17 08:04:34 -05:00
|
|
|
|
2018-12-06 11:42:03 -06:00
|
|
|
pub use difference::Changeset as __Changeset;
|
2018-08-12 10:50:16 -05:00
|
|
|
|
2018-10-31 14:34:31 -05:00
|
|
|
pub const CURSOR_MARKER: &str = "<|>";
|
|
|
|
|
2018-08-12 10:50:16 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_eq_text {
|
|
|
|
($expected:expr, $actual:expr) => {{
|
|
|
|
let expected = $expected;
|
|
|
|
let actual = $actual;
|
|
|
|
if expected != actual {
|
|
|
|
let changeset = $crate::__Changeset::new(actual, expected, "\n");
|
|
|
|
println!("Expected:\n{}\n\nActual:\n{}\nDiff:{}\n", expected, actual, changeset);
|
|
|
|
panic!("text differs");
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
($expected:expr, $actual:expr, $($tt:tt)*) => {{
|
|
|
|
let expected = $expected;
|
|
|
|
let actual = $actual;
|
|
|
|
if expected != actual {
|
|
|
|
let changeset = $crate::__Changeset::new(actual, expected, "\n");
|
|
|
|
println!("Expected:\n{}\n\nActual:\n{}\n\nDiff:\n{}\n", expected, actual, changeset);
|
|
|
|
println!($($tt)*);
|
|
|
|
panic!("text differs");
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
2018-08-17 08:04:34 -05:00
|
|
|
|
|
|
|
pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) {
|
|
|
|
let actual = format!("{:?}", actual);
|
|
|
|
let expected = expected.lines().map(|l| l.trim()).join(" ");
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2018-08-25 06:30:54 -05:00
|
|
|
|
|
|
|
pub fn extract_offset(text: &str) -> (TextUnit, String) {
|
2018-10-13 14:33:15 -05:00
|
|
|
match try_extract_offset(text) {
|
2018-08-25 06:30:54 -05:00
|
|
|
None => panic!("text should contain cursor marker"),
|
2018-10-13 14:33:15 -05:00
|
|
|
Some(result) => result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> {
|
2018-10-31 14:34:31 -05:00
|
|
|
let cursor_pos = text.find(CURSOR_MARKER)?;
|
|
|
|
let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len());
|
2018-08-25 06:30:54 -05:00
|
|
|
new_text.push_str(&text[..cursor_pos]);
|
2018-10-31 14:34:31 -05:00
|
|
|
new_text.push_str(&text[cursor_pos + CURSOR_MARKER.len()..]);
|
2018-08-25 06:30:54 -05:00
|
|
|
let cursor_pos = TextUnit::from(cursor_pos as u32);
|
2018-10-13 14:33:15 -05:00
|
|
|
Some((cursor_pos, new_text))
|
2018-08-25 06:30:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extract_range(text: &str) -> (TextRange, String) {
|
2018-10-13 14:33:15 -05:00
|
|
|
match try_extract_range(text) {
|
|
|
|
None => panic!("text should contain cursor marker"),
|
|
|
|
Some(result) => result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
|
|
|
|
let (start, text) = try_extract_offset(text)?;
|
|
|
|
let (end, text) = try_extract_offset(&text)?;
|
|
|
|
Some((TextRange::from_to(start, end), text))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extract_ranges(text: &str) -> (Vec<TextRange>, String) {
|
|
|
|
let mut ranges = Vec::new();
|
|
|
|
let mut text = String::from(text);
|
|
|
|
while let Some((range, new_text)) = try_extract_range(&text) {
|
|
|
|
text = new_text;
|
|
|
|
ranges.push(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
(ranges, text)
|
2018-08-25 06:30:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_cursor(text: &str, offset: TextUnit) -> String {
|
|
|
|
let offset: u32 = offset.into();
|
|
|
|
let offset: usize = offset as usize;
|
|
|
|
let mut res = String::new();
|
|
|
|
res.push_str(&text[..offset]);
|
|
|
|
res.push_str("<|>");
|
|
|
|
res.push_str(&text[offset..]);
|
|
|
|
res
|
|
|
|
}
|
2018-10-31 13:37:32 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FixtureEntry {
|
|
|
|
pub meta: String,
|
|
|
|
pub text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses text wich looks like this:
|
|
|
|
///
|
|
|
|
/// ```notrust
|
|
|
|
/// //- some meta
|
|
|
|
/// line 1
|
|
|
|
/// line 2
|
|
|
|
/// // - other meta
|
|
|
|
/// ```
|
|
|
|
pub fn parse_fixture(fixture: &str) -> Vec<FixtureEntry> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut meta: Option<&str> = None;
|
|
|
|
|
|
|
|
macro_rules! flush {
|
|
|
|
() => {
|
|
|
|
if let Some(meta) = meta {
|
2018-10-31 15:41:43 -05:00
|
|
|
res.push(FixtureEntry {
|
|
|
|
meta: meta.to_string(),
|
|
|
|
text: buf.clone(),
|
|
|
|
});
|
2018-10-31 13:37:32 -05:00
|
|
|
buf.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2018-10-31 15:41:43 -05:00
|
|
|
let margin = fixture
|
|
|
|
.lines()
|
2018-10-31 14:34:31 -05:00
|
|
|
.filter(|it| it.trim_start().starts_with("//-"))
|
|
|
|
.map(|it| it.len() - it.trim_start().len())
|
2018-10-31 15:41:43 -05:00
|
|
|
.next()
|
|
|
|
.expect("empty fixture");
|
|
|
|
let lines = fixture.lines().filter_map(|line| {
|
|
|
|
if line.len() >= margin {
|
|
|
|
assert!(line[..margin].trim().is_empty());
|
|
|
|
Some(&line[margin..])
|
|
|
|
} else {
|
|
|
|
assert!(line.trim().is_empty());
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
2018-10-31 14:34:31 -05:00
|
|
|
|
|
|
|
for line in lines {
|
2018-10-31 13:37:32 -05:00
|
|
|
if line.starts_with("//-") {
|
|
|
|
flush!();
|
|
|
|
buf.clear();
|
|
|
|
meta = Some(line["//-".len()..].trim());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
buf.push_str(line);
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
flush!();
|
|
|
|
res
|
|
|
|
}
|