use claims::assert_some_eq;
use super::*;
use crate::assert_empty;
fn history_item_to_string(item: &HistoryItem) -> String {
let range = if item.start_index == item.end_index {
item.start_index.to_string()
}
else {
format!("{}-{}", item.start_index, item.end_index)
};
format!(
"{:?}[{range}] {}",
item.operation,
item.lines.iter().map(Line::to_text).collect::<Vec<String>>().join(", ")
)
}
fn _assert_history_items(actual: &[HistoryItem], expected: &[HistoryItem]) {
let actual_strings: Vec<String> = actual
.iter()
.filter(|item| item.operation != Operation::Load)
.map(history_item_to_string)
.collect();
let expected_strings: Vec<String> = expected.iter().map(history_item_to_string).collect();
pretty_assertions::assert_str_eq!(actual_strings.join("\n"), expected_strings.join("\n"));
}
macro_rules! assert_history_items {
($history_items:expr, $($arg:expr),*) => {
let expected = &vec![$( $arg, )*];
_assert_history_items(&Vec::from($history_items), &expected);
};
}
fn create_lines() -> Vec<Line> {
vec![
Line::parse("pick aaa c1").unwrap(),
Line::parse("pick bbb c2").unwrap(),
Line::parse("pick ccc c3").unwrap(),
Line::parse("pick ddd c4").unwrap(),
Line::parse("pick eee c5").unwrap(),
]
}
macro_rules! assert_todo_lines {
($lines:expr, $($arg:expr),*) => {
let expected = vec![$( Line::parse($arg).unwrap(), )*];
pretty_assertions::assert_str_eq!(
$lines.iter().map(Line::to_text).collect::<Vec<String>>().join("\n"),
expected.iter().map(Line::to_text).collect::<Vec<String>>().join("\n")
);
};
}
#[test]
fn new() {
let mut history = History::new(100);
assert_eq!(history.limit, 100);
assert_eq!(history.undo_history.len(), 1);
assert_some_eq!(history.undo_history.pop_back(), HistoryItem::new_load());
assert_empty!(history.redo_history);
}
#[test]
fn record_history() {
let mut history = History::new(5);
history.redo_history.push_front(HistoryItem::new_add(1, 1));
history.record(HistoryItem::new_add(1, 1));
assert_history_items!(history.undo_history, HistoryItem::new_add(1, 1));
assert_empty!(history.redo_history);
}
#[test]
fn record_history_overflow_limit() {
let mut history = History::new(3);
history.record(HistoryItem::new_add(1, 1));
history.record(HistoryItem::new_add(2, 2));
history.record(HistoryItem::new_add(3, 3));
history.record(HistoryItem::new_add(4, 4));
assert_history_items!(
history.undo_history,
HistoryItem::new_add(2, 2),
HistoryItem::new_add(3, 3),
HistoryItem::new_add(4, 4)
);
assert_empty!(history.redo_history);
}
#[test]
fn undo_at_load() {
let mut history = History::new(10);
let mut lines = create_lines();
assert_some_eq!(history.undo(&mut lines), (Operation::Load, 0, 0));
assert_todo_lines!(
lines,
"pick aaa c1",
"pick bbb c2",
"pick ccc c3",
"pick ddd c4",
"pick eee c5"
);
assert_some_eq!(history.undo_history.pop_back(), HistoryItem::new_load());
}
#[test]
fn undo_redo_add_start() {
let mut history = History::new(10);
history.record(HistoryItem::new_add(0, 0));
let mut lines = create_lines();
assert_some_eq!(history.undo(&mut lines), (Operation::Add, 0, 0));
assert_todo_lines!(lines, "pick bbb c2", "pick ccc c3", "pick ddd c4", "pick eee c5");
assert_some_eq!(history.redo(&mut lines), (Operation::Remove, 0, 0));
assert_todo_lines!(
lines,
"pick aaa c1",
"pick bbb c2",
"pick ccc c3",
"pick ddd c4",
"pick eee c5"
);
}
#[test]
fn undo_redo_add_end() {
let mut history = History::new(10);
history.