Skip to content

Commit a6cb9c6

Browse files
committed
test(animation): directly test uncovered AnimationEngine helpers
1 parent 4eddc44 commit a6cb9c6

1 file changed

Lines changed: 250 additions & 0 deletions

File tree

‎src/animation.rs‎

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,4 +1798,254 @@ mod tests {
17981798
Some("src/lib.rs")
17991799
));
18001800
}
1801+
1802+
#[test]
1803+
fn switch_file_uses_default_speed_and_empty_content_fallbacks() {
1804+
let mut engine = AnimationEngine::new(42);
1805+
engine.set_speed_rules(vec![speed_rule("*.rs:5")]);
1806+
1807+
engine.execute_step(switch_file("notes.txt", "", ""));
1808+
1809+
assert_eq!(engine.speed_ms, 42);
1810+
assert_eq!(engine.buffer.lines, vec![String::new()]);
1811+
assert_eq!(engine.buffer.old_content_lines, vec![String::new()]);
1812+
assert_eq!(engine.buffer.new_content_lines, vec![String::new()]);
1813+
assert_eq!(engine.buffer.old_content_line_offsets, vec![0]);
1814+
assert_eq!(engine.buffer.new_content_line_offsets, vec![0]);
1815+
assert!(engine.buffer.cached_highlights.is_empty());
1816+
}
1817+
1818+
#[test]
1819+
fn generate_cursor_movement_covers_short_medium_and_long_distances() {
1820+
let mut short_engine = AnimationEngine::new(20);
1821+
1822+
assert_eq!(short_engine.generate_cursor_movement(2, 2, 0, &[]), 2);
1823+
assert!(short_engine.steps.is_empty());
1824+
1825+
let short_lines = ["root", " one", " two"];
1826+
assert_eq!(
1827+
short_engine.generate_cursor_movement(0, 2, 2, &short_lines),
1828+
2
1829+
);
1830+
assert!(matches!(
1831+
short_engine.steps.first(),
1832+
Some(AnimationStep::MoveCursor { line: 1, col: 1 })
1833+
));
1834+
assert!(matches!(
1835+
short_engine.steps.get(2),
1836+
Some(AnimationStep::MoveCursor { line: 2, col: 2 })
1837+
));
1838+
assert!(short_engine.steps.iter().all(|step| match step {
1839+
AnimationStep::Pause { multiplier } => {
1840+
(*multiplier - CURSOR_MOVE_PAUSE * CURSOR_MOVE_SHORT_MULTIPLIER).abs()
1841+
< f64::EPSILON
1842+
}
1843+
_ => true,
1844+
}));
1845+
1846+
let medium_lines = (0..=60)
1847+
.map(|line| format!("{}line {line}", " ".repeat(line % 4)))
1848+
.collect::<Vec<_>>();
1849+
let medium_refs = medium_lines.iter().map(String::as_str).collect::<Vec<_>>();
1850+
let mut medium_engine = AnimationEngine::new(20);
1851+
assert_eq!(
1852+
medium_engine.generate_cursor_movement(60, 0, 60, &medium_refs),
1853+
0
1854+
);
1855+
let medium_moves = medium_engine
1856+
.steps
1857+
.iter()
1858+
.filter_map(|step| match step {
1859+
AnimationStep::MoveCursor { line, .. } => Some(*line),
1860+
_ => None,
1861+
})
1862+
.collect::<Vec<_>>();
1863+
assert!(medium_moves.len() < 60);
1864+
assert_eq!(medium_moves.last(), Some(&0));
1865+
assert!(medium_moves.windows(2).all(|pair| pair[0] > pair[1]));
1866+
assert!(medium_engine.steps.iter().all(|step| match step {
1867+
AnimationStep::Pause { multiplier } => {
1868+
(*multiplier - CURSOR_MOVE_PAUSE * CURSOR_MOVE_MEDIUM_MULTIPLIER).abs()
1869+
< f64::EPSILON
1870+
}
1871+
_ => true,
1872+
}));
1873+
1874+
let long_lines = (0..=250)
1875+
.map(|line| format!("{}item {line}", " ".repeat(line % 3)))
1876+
.collect::<Vec<_>>();
1877+
let long_refs = long_lines.iter().map(String::as_str).collect::<Vec<_>>();
1878+
let mut long_engine = AnimationEngine::new(20);
1879+
assert_eq!(
1880+
long_engine.generate_cursor_movement(250, 0, 250, &long_refs),
1881+
0
1882+
);
1883+
let long_moves = long_engine
1884+
.steps
1885+
.iter()
1886+
.filter_map(|step| match step {
1887+
AnimationStep::MoveCursor { line, .. } => Some(*line),
1888+
_ => None,
1889+
})
1890+
.collect::<Vec<_>>();
1891+
assert!(long_moves.len() <= MAX_SCROLL_STEPS);
1892+
assert_eq!(long_moves.last(), Some(&0));
1893+
assert!(long_engine.steps.iter().all(|step| match step {
1894+
AnimationStep::Pause { multiplier } => {
1895+
(*multiplier - CURSOR_MOVE_PAUSE * CURSOR_MOVE_LONG_MULTIPLIER).abs() < f64::EPSILON
1896+
}
1897+
_ => true,
1898+
}));
1899+
}
1900+
1901+
#[test]
1902+
fn generate_steps_for_hunk_handles_context_deletion_and_addition() {
1903+
let mut engine = AnimationEngine::new(30);
1904+
let hunk = hunk(
1905+
1,
1906+
vec![
1907+
line_change(LineChangeType::Context, " keep"),
1908+
line_change(LineChangeType::Deletion, "removed"),
1909+
line_change(LineChangeType::Addition, " added();"),
1910+
line_change(LineChangeType::Context, "tail"),
1911+
],
1912+
);
1913+
1914+
assert_eq!(engine.generate_steps_for_hunk(&hunk, 0, 2), (4, 5));
1915+
assert!(matches!(
1916+
engine.steps.first(),
1917+
Some(AnimationStep::MoveCursor { line: 2, col: 2 })
1918+
));
1919+
assert!(matches!(
1920+
engine.steps.get(2),
1921+
Some(AnimationStep::DeleteLine { line: 3 })
1922+
));
1923+
assert!(matches!(
1924+
engine.steps.get(4),
1925+
Some(AnimationStep::InsertLine { line: 3, content }) if content == " "
1926+
));
1927+
assert!(matches!(
1928+
engine.steps.get(5),
1929+
Some(AnimationStep::InsertChar {
1930+
line: 3,
1931+
col: 4,
1932+
ch: 'a'
1933+
})
1934+
));
1935+
assert!(matches!(
1936+
engine.steps.get(13),
1937+
Some(AnimationStep::Pause { multiplier })
1938+
if (*multiplier - INSERT_LINE_PAUSE).abs() < f64::EPSILON
1939+
));
1940+
assert!(matches!(
1941+
engine.steps.last(),
1942+
Some(AnimationStep::Pause { multiplier })
1943+
if (*multiplier - CURSOR_MOVE_PAUSE).abs() < f64::EPSILON
1944+
));
1945+
}
1946+
1947+
#[test]
1948+
fn execute_step_updates_dialog_terminal_and_editor_cursor_state() {
1949+
let mut engine = AnimationEngine::new(30);
1950+
1951+
engine.execute_step(AnimationStep::OpenFileDialogStart);
1952+
engine.execute_step(AnimationStep::DialogTypeChar { ch: 's' });
1953+
engine.execute_step(AnimationStep::TerminalPrompt);
1954+
engine.execute_step(AnimationStep::TerminalTypeChar { ch: 'g' });
1955+
engine.buffer = EditorBuffer::from_content("line\n kept");
1956+
engine.execute_step(AnimationStep::DeleteLine { line: 0 });
1957+
engine.execute_step(AnimationStep::MoveCursor { line: 0, col: 2 });
1958+
1959+
assert_eq!(engine.dialog_title.as_deref(), Some("Open File..."));
1960+
assert_eq!(engine.dialog_typing_text, "s");
1961+
assert_eq!(engine.terminal_lines, vec!["~ g".to_string()]);
1962+
assert_eq!(engine.buffer.lines, vec![" kept".to_string()]);
1963+
assert_eq!(engine.buffer.cursor_line, 0);
1964+
assert_eq!(engine.buffer.cursor_col, 2);
1965+
assert_eq!(engine.line_offset, -1);
1966+
assert_eq!(engine.active_pane, ActivePane::Editor);
1967+
}
1968+
1969+
#[test]
1970+
fn tick_honors_manual_pause_timed_pause_and_finished_state() {
1971+
let mut engine = AnimationEngine::new(1);
1972+
engine.state = AnimationState::Playing;
1973+
engine.pending_metadata = Some(metadata("abcdef1", "Tick", vec![]));
1974+
engine.steps = vec![
1975+
AnimationStep::ResetState,
1976+
AnimationStep::Pause { multiplier: 1.0 },
1977+
AnimationStep::TerminalOutput {
1978+
text: "done".to_string(),
1979+
},
1980+
];
1981+
engine.frame_interval_ms = 0;
1982+
engine.next_step_delay = 0;
1983+
engine.last_update = Instant::now() - Duration::from_millis(10);
1984+
engine.last_frame = Instant::now() - Duration::from_millis(10);
1985+
engine.cursor_blink_timer = Instant::now() - Duration::from_millis(600);
1986+
1987+
assert!(engine.tick());
1988+
assert!(!engine.cursor_visible);
1989+
assert_eq!(engine.current_step, 1);
1990+
assert!(matches!(
1991+
engine.current_metadata.as_ref(),
1992+
Some(current) if current.hash == "abcdef1"
1993+
));
1994+
1995+
engine.pause();
1996+
assert!(engine.tick());
1997+
assert_eq!(engine.current_step, 1);
1998+
1999+
engine.resume();
2000+
engine.last_update = Instant::now() - Duration::from_millis(10);
2001+
engine.last_frame = Instant::now() - Duration::from_millis(10);
2002+
assert!(engine.tick());
2003+
assert_eq!(engine.current_step, 2);
2004+
assert!(engine.pause_until.is_some());
2005+
2006+
assert!(engine.tick());
2007+
assert_eq!(engine.current_step, 2);
2008+
2009+
engine.pause_until = Some(Instant::now() - Duration::from_millis(1));
2010+
engine.last_update = Instant::now() - Duration::from_millis(10);
2011+
engine.last_frame = Instant::now() - Duration::from_millis(10);
2012+
assert!(engine.tick());
2013+
assert_eq!(engine.terminal_lines, vec!["done".to_string()]);
2014+
assert_eq!(engine.state, AnimationState::Finished);
2015+
2016+
assert!(!engine.tick());
2017+
}
2018+
2019+
#[test]
2020+
fn update_scroll_handles_zero_dimensions_middle_and_bottom_alignment() {
2021+
let mut engine = AnimationEngine::new(30);
2022+
2023+
assert_eq!(engine.calculate_line_display_height("wrapped"), 1);
2024+
2025+
engine.content_width = 6;
2026+
assert_eq!(engine.calculate_line_display_height("wrapped"), 1);
2027+
2028+
engine.buffer.lines = vec![
2029+
"line 0".to_string(),
2030+
"line 1".to_string(),
2031+
"line 2".to_string(),
2032+
"line 3".to_string(),
2033+
"line 4".to_string(),
2034+
"line 5".to_string(),
2035+
];
2036+
engine.viewport_height = 0;
2037+
engine.buffer.cursor_line = 4;
2038+
engine.update_scroll();
2039+
assert_eq!(engine.buffer.scroll_offset, 0);
2040+
2041+
engine.content_width = 30;
2042+
engine.viewport_height = 2;
2043+
engine.buffer.cursor_line = 3;
2044+
engine.update_scroll();
2045+
assert_eq!(engine.buffer.scroll_offset, 2);
2046+
2047+
engine.buffer.cursor_line = 5;
2048+
engine.update_scroll();
2049+
assert_eq!(engine.buffer.scroll_offset, 4);
2050+
}
18012051
}

0 commit comments

Comments
 (0)