use plannotator_tui_review::Side;
use super::*;
const BAR: char = '\u{258e}';
fn two_hunks(tag: &str) -> Fixture {
let fixture = Fixture::new(tag);
let lines = (1..=40).fold(String::new(), |text, n| text + "line " + &n.to_string() + "\n");
fixture.write("long.txt", &lines);
fixture.git(&["add", "long.txt"]);
fixture.git(&["-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-qm", "long"]);
let changed = lines.replace("line 12\n", "line twelve\n").replace("line 33\n", "line 33a\n");
fixture.write("long.txt", &changed);
fixture
}
fn on(screen: &Screen) -> Option<(String, Side, u32)> {
let lines = screen.diff.cursor().expect("cursor")?;
Some((lines.path, lines.side, lines.first))
}
fn bar_on(screen: &Screen, rows: &[String], text: &str) -> bool {
diff_side(screen, row_with(rows, text)).starts_with(BAR)
}
#[test]
fn hunk_jumps_land_on_the_hunks_first_line_and_draw_the_cursor_bar_there() {
let fixture = two_hunks("hunk-jump");
let mut screen = fixture.open();
let (cols, rows) = (100, 40);
frame(&mut screen, cols, rows);
screen.diff.reveal("long.txt", Side::New, 13).expect("line 13");
keys(&mut screen, "]");
let shown = frame(&mut screen, cols, rows);
assert!(bar_on(&screen, &shown, "line 30"), "] to the next hunk's first line: {shown:#?}");
assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 30)));
assert!(shown.iter().any(|r| r.contains("@@ -30,7 +30,7 @@")), "its heading is shown: {shown:#?}");
keys(&mut screen, "[");
let shown = frame(&mut screen, cols, rows);
assert!(bar_on(&screen, &shown, "line 9"), "[ to the hunk before: {shown:#?}");
assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 9)));
screen.diff.reveal("long.txt", Side::New, 35).expect("line 35");
keys(&mut screen, "[");
let shown = frame(&mut screen, cols, rows);
assert!(bar_on(&screen, &shown, "line 30"), "[ to its own hunk's first line: {shown:#?}");
assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 30)));
keys(&mut screen, "]");
assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 30)));
}
#[test]
fn a_hunk_jump_with_no_hunk_that_way_moves_nothing_past_a_file_with_none() {
let fixture = Fixture::new("hunk-jump-none");
std::fs::write(fixture.repo().join("aa.bin"), [0u8, 1, 2, 255]).expect("binary");
let mut screen = fixture.open();
assert_eq!(screen.diff.paths(), ["aa.bin", "app.py"], "a file with no hunks first");
keys(&mut screen, "n");
let heading = screen.diff.cursor_row().expect("cursor");
assert_eq!((heading.path.as_str(), heading.kind), ("app.py", diffs_rs::engine::DiffRowKind::Header));
for n in 0..3 {
keys(&mut screen, "[");
assert_eq!(screen.diff.cursor_row().as_ref(), Some(&heading), "[ {n}: no hunk before app.py's");
}
}