From e6fdd4140bc81b64508bb65a00b521cb66ede39a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Feb 2018 20:46:59 -0800 Subject: [PATCH] add more tests --- term/src/terminalstate.rs | 15 ++++++++ term/src/test/c0.rs | 29 +++++++++++++++ term/src/test/c1.rs | 16 +++++++++ term/src/test/mod.rs | 76 ++++++++++++++++++--------------------- 4 files changed, 95 insertions(+), 41 deletions(-) create mode 100644 term/src/test/c0.rs create mode 100644 term/src/test/c1.rs diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 0afbd8aae..daa944b10 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -763,6 +763,19 @@ impl TerminalState { } } + /// Moves the cursor down one line in the same column. + /// If the cursor is at the bottom margin, the page scrolls up. + fn c1_index(&mut self) { + let y = self.cursor.y; + let y = if y == self.scroll_region.end - 1 { + self.scroll_up(1); + y + } else { + y + 1 + }; + self.set_cursor_pos(&Position::Relative(0), &Position::Absolute(y as i64)); + } + /// Move the cursor up 1 line. If the position is at the top scroll margin, /// scroll the region down. fn reverse_index(&mut self) { @@ -1083,6 +1096,8 @@ impl vte::Perform for TerminalState { } // Reverse Index (RI) (b'M', &[], &[]) => self.reverse_index(), + // Index (IND) + (b'D', &[], &[]) => self.c1_index(), // Enable alternate character set mode (smacs) (b'0', &[b'('], &[]) => {} diff --git a/term/src/test/c0.rs b/term/src/test/c0.rs new file mode 100644 index 000000000..c3a42d8f7 --- /dev/null +++ b/term/src/test/c0.rs @@ -0,0 +1,29 @@ +//! Testing C0 control characters + +use super::*; + +#[test] +fn test_bs() { + let mut term = TestTerm::new(3, 4, 0); + term.assert_cursor_pos(0, 0, None); + term.print("\x08"); + term.assert_cursor_pos(0, 0, Some("cannot move left of the margin")); + term.print("ab\x08"); + term.assert_cursor_pos(1, 0, None); + // TODO: when we can set the left margin, we should test that here +} + +#[test] +fn test_lf() { + let mut term = TestTerm::new(3, 10, 0); + term.print("hello\n"); + term.assert_cursor_pos(0, 1, Some("LF moves to new line")); +} + +#[test] +fn test_cr() { + let mut term = TestTerm::new(3, 10, 0); + term.print("hello\r"); + term.assert_cursor_pos(0, 0, Some("CR moves to left margin on current line")); + // TODO: when we can set the left margin, we should test that here +} diff --git a/term/src/test/c1.rs b/term/src/test/c1.rs new file mode 100644 index 000000000..b1493f5d5 --- /dev/null +++ b/term/src/test/c1.rs @@ -0,0 +1,16 @@ +//! Testing C1 control sequences + +use super::*; + +#[test] +fn test_ind() { + let mut term = TestTerm::new(4, 4, 0); + term.print("a\nb\x1bD"); + term.assert_cursor_pos(1, 2, None); + assert_visible_contents(&term, &["a ", "b ", " ", " "]); + term.print("\x1bD"); + term.assert_cursor_pos(1, 3, None); + term.print("\x1bD"); + term.assert_cursor_pos(1, 3, None); + assert_visible_contents(&term, &["b ", " ", " ", " "]); +} diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs index d369ccc69..512dd18a1 100644 --- a/term/src/test/mod.rs +++ b/term/src/test/mod.rs @@ -3,6 +3,8 @@ use super::*; mod selection; +mod c0; +mod c1; #[derive(Default, Debug)] struct TestHost { @@ -44,9 +46,9 @@ struct TestTerm { } impl TestTerm { - fn new(width: usize, height: usize, scrollback: usize) -> Self { + fn new(height: usize, width: usize, scrollback: usize) -> Self { Self { - term: Terminal::new(width, height, scrollback), + term: Terminal::new(height, width, scrollback), host: TestHost::new(), } } @@ -175,6 +177,27 @@ impl TestTerm { modifiers: KeyModifiers::default(), }).unwrap(); } + + fn assert_cursor_pos(&self, x: usize, y: i64, reason: Option<&str>) { + let cursor = self.cursor_pos(); + let expect = CursorPosition { x, y }; + assert_eq!( + cursor, + expect, + "actual cursor (left) didn't match expected cursor (right) reason={:?}", + reason + ); + } + + fn assert_dirty_lines(&self, expected: &[usize], reason: Option<&str>) { + let dirty_indices: Vec = self.get_dirty_lines().iter().map(|&(i, ..)| i).collect(); + assert_eq!( + &dirty_indices, + &expected, + "actual dirty lines (left) didn't match expected dirty lines (right) reason={:?}", + reason + ); + } } impl Deref for TestTerm { @@ -191,35 +214,6 @@ impl DerefMut for TestTerm { } } -macro_rules! assert_cursor_pos { - ($term:expr, $x:expr, $y:expr) => { - assert_cursor_pos!($term, $x, $y, - "actual cursor (left) didn't match expected cursor (right)"); - }; - - ($term:expr, $x:expr, $y:expr, $($reason:tt)*) => { - { - let cursor = $term.cursor_pos(); - let expect = CursorPosition { x: $x, y: $y }; - assert_eq!(cursor, expect, $($reason)*); - } - }; -} - -macro_rules! assert_dirty_lines { - ($term:expr, $expected:expr) => { - assert_dirty_lines!($term, $expected, - "actual dirty lines (left) didn't match expected dirty lines (right)"); - }; - - ($term:expr, $expected:expr, $($reason:tt)*) => { - let dirty_indices: Vec = $term.get_dirty_lines() - .iter() - .map(|&(i, _, _)| i).collect(); - assert_eq!(&dirty_indices, $expected, $($reason)*); - }; -} - /// Asserts that both line slices match according to the /// selected flags. fn assert_lines_equal(lines: &[Line], expect_lines: &[Line], compare: Compare) { @@ -356,18 +350,18 @@ fn cursor_movement_damage() { term.print("fooo."); assert_visible_contents(&term, &["foo", "o. "]); - assert_cursor_pos!(&term, 2, 1); - assert_dirty_lines!(&term, &[0, 1]); + term.assert_cursor_pos(2, 1, None); + term.assert_dirty_lines(&[0, 1], None); term.cup(0, 1); term.clean_dirty_lines(); term.print("\x08"); - assert_cursor_pos!(&term, 0, 1, "BS doesn't change the line"); - assert_dirty_lines!(&term, &[1]); + term.assert_cursor_pos(0, 1, Some("BS doesn't change the line")); + term.assert_dirty_lines(&[1], None); term.clean_dirty_lines(); term.cup(0, 0); - assert_dirty_lines!(&term, &[0, 1], "cursor movement dirties old and new lines"); + term.assert_dirty_lines(&[0, 1], Some("cursor movement dirties old and new lines")); } /// Replicates a bug I initially found via: @@ -380,14 +374,14 @@ fn test_delete_lines() { term.print("111\r\n222\r\n333\r\n444\r\n555"); assert_visible_contents(&term, &["111", "222", "333", "444", "555"]); - assert_dirty_lines!(&term, &[0, 1, 2, 3, 4]); + term.assert_dirty_lines(&[0, 1, 2, 3, 4], None); term.cup(0, 1); term.clean_dirty_lines(); - assert_dirty_lines!(&term, &[]); + term.assert_dirty_lines(&[], None); term.delete_lines(2); assert_visible_contents(&term, &["111", "444", "555", " ", " "]); - assert_dirty_lines!(&term, &[1, 2, 3, 4]); + term.assert_dirty_lines(&[1, 2, 3, 4], None); term.clean_dirty_lines(); term.cup(0, 3); @@ -401,7 +395,7 @@ fn test_delete_lines() { term.delete_lines(1); assert_visible_contents(&term, &["111", "555", "aaa", " ", "bbb"]); - assert_dirty_lines!(&term, &[1, 2, 3]); + term.assert_dirty_lines(&[1, 2, 3], None); // expand the scroll region to fill the screen term.set_scroll_region(0, 4); @@ -409,7 +403,7 @@ fn test_delete_lines() { term.delete_lines(1); assert_visible_contents(&term, &["111", "aaa", " ", "bbb", " "]); - assert_dirty_lines!(&term, &[1, 2, 3, 4]); + term.assert_dirty_lines(&[1, 2, 3, 4], None); } #[test]