зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 08:25:51 +03:00
add CursorMove tests for multi-byte characters
Этот коммит содержится в:
113
tests/cursor.rs
113
tests/cursor.rs
@@ -30,9 +30,10 @@ fn empty_textarea() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn forward() {
|
fn forward() {
|
||||||
let mut t = TextArea::from(["abc", "def"]);
|
for (text, positions) in [
|
||||||
|
(
|
||||||
for pos in [
|
["abc", "def"],
|
||||||
|
[
|
||||||
(0, 1),
|
(0, 1),
|
||||||
(0, 2),
|
(0, 2),
|
||||||
(0, 3),
|
(0, 3),
|
||||||
@@ -41,18 +42,37 @@ fn forward() {
|
|||||||
(1, 2),
|
(1, 2),
|
||||||
(1, 3),
|
(1, 3),
|
||||||
(1, 3),
|
(1, 3),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
["あいう", "🐶🐱👪"],
|
||||||
|
[
|
||||||
|
(0, 1),
|
||||||
|
(0, 2),
|
||||||
|
(0, 3),
|
||||||
|
(1, 0),
|
||||||
|
(1, 1),
|
||||||
|
(1, 2),
|
||||||
|
(1, 3),
|
||||||
|
(1, 3),
|
||||||
|
],
|
||||||
|
),
|
||||||
] {
|
] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
|
|
||||||
|
for pos in positions {
|
||||||
t.move_cursor(CursorMove::Forward);
|
t.move_cursor(CursorMove::Forward);
|
||||||
assert_eq!(t.cursor(), pos);
|
assert_eq!(t.cursor(), pos, "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn back() {
|
fn back() {
|
||||||
let mut t = TextArea::from(["abc", "def"]);
|
for (text, positions) in [
|
||||||
t.move_cursor(BOTTOM_RIGHT);
|
(
|
||||||
|
["abc", "def"],
|
||||||
for pos in [
|
[
|
||||||
(1, 2),
|
(1, 2),
|
||||||
(1, 1),
|
(1, 1),
|
||||||
(1, 0),
|
(1, 0),
|
||||||
@@ -61,92 +81,135 @@ fn back() {
|
|||||||
(0, 1),
|
(0, 1),
|
||||||
(0, 0),
|
(0, 0),
|
||||||
(0, 0),
|
(0, 0),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
["あいう", "🐶🐱👪"],
|
||||||
|
[
|
||||||
|
(1, 2),
|
||||||
|
(1, 1),
|
||||||
|
(1, 0),
|
||||||
|
(0, 3),
|
||||||
|
(0, 2),
|
||||||
|
(0, 1),
|
||||||
|
(0, 0),
|
||||||
|
(0, 0),
|
||||||
|
],
|
||||||
|
),
|
||||||
] {
|
] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
|
t.move_cursor(BOTTOM_RIGHT);
|
||||||
|
|
||||||
|
for pos in positions {
|
||||||
t.move_cursor(CursorMove::Back);
|
t.move_cursor(CursorMove::Back);
|
||||||
assert_eq!(t.cursor(), pos);
|
assert_eq!(t.cursor(), pos, "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn up() {
|
fn up() {
|
||||||
let mut t = TextArea::from(["abc", "def", "ghi"]);
|
for text in [
|
||||||
|
["abc", "def", "ghi"],
|
||||||
|
["あいう", "🐶🐱🐰", "👪🤟🏿👩🏻❤️💋👨🏾"],
|
||||||
|
] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
|
|
||||||
for col in 0..=3 {
|
for col in 0..=3 {
|
||||||
let mut row = 2;
|
let mut row = 2;
|
||||||
|
|
||||||
t.move_cursor(CursorMove::Jump(2 as u16, col as u16));
|
t.move_cursor(CursorMove::Jump(2, col as u16));
|
||||||
assert_eq!(t.cursor(), (row, col));
|
assert_eq!(t.cursor(), (row, col), "{:?}", t.lines());
|
||||||
|
|
||||||
while row > 0 {
|
while row > 0 {
|
||||||
t.move_cursor(CursorMove::Up);
|
t.move_cursor(CursorMove::Up);
|
||||||
row -= 1;
|
row -= 1;
|
||||||
assert_eq!(t.cursor(), (row, col));
|
assert_eq!(t.cursor(), (row, col), "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn up_trim() {
|
fn up_trim() {
|
||||||
let mut t = TextArea::from(["", "a", "bcd", "efgh"]);
|
for text in [["", "a", "bcd", "efgh"], ["", "👪", "🐶!🐱", "あ?い!"]] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
t.move_cursor(CursorMove::Jump(3, 3));
|
t.move_cursor(CursorMove::Jump(3, 3));
|
||||||
|
|
||||||
for expected in [(2, 3), (1, 1), (0, 0)] {
|
for expected in [(2, 3), (1, 1), (0, 0)] {
|
||||||
t.move_cursor(CursorMove::Up);
|
t.move_cursor(CursorMove::Up);
|
||||||
assert_eq!(t.cursor(), expected);
|
assert_eq!(t.cursor(), expected, "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn down() {
|
fn down() {
|
||||||
let mut t = TextArea::from(["abc", "def", "ghi"]);
|
for text in [
|
||||||
|
["abc", "def", "ghi"],
|
||||||
|
["あいう", "🐶🐱🐰", "👪🤟🏿👩🏻❤️💋👨🏾"],
|
||||||
|
] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
|
|
||||||
for col in 0..=3 {
|
for col in 0..=3 {
|
||||||
let mut row = 0;
|
let mut row = 0;
|
||||||
|
|
||||||
t.move_cursor(CursorMove::Jump(0, col as u16));
|
t.move_cursor(CursorMove::Jump(0, col as u16));
|
||||||
assert_eq!(t.cursor(), (row, col));
|
assert_eq!(t.cursor(), (row, col), "{:?}", t.lines());
|
||||||
|
|
||||||
while row < 2 {
|
while row < 2 {
|
||||||
t.move_cursor(CursorMove::Down);
|
t.move_cursor(CursorMove::Down);
|
||||||
row += 1;
|
row += 1;
|
||||||
assert_eq!(t.cursor(), (row, col));
|
assert_eq!(t.cursor(), (row, col), "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn down_trim() {
|
fn down_trim() {
|
||||||
let mut t = TextArea::from(["abcd", "efg", "h", ""]);
|
for text in [["abcd", "efg", "h", ""], ["あ?い!", "🐶!🐱", "👪", ""]] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
t.move_cursor(CursorMove::Jump(0, 3));
|
t.move_cursor(CursorMove::Jump(0, 3));
|
||||||
|
|
||||||
for expected in [(1, 3), (2, 1), (3, 0)] {
|
for expected in [(1, 3), (2, 1), (3, 0)] {
|
||||||
t.move_cursor(CursorMove::Down);
|
t.move_cursor(CursorMove::Down);
|
||||||
assert_eq!(t.cursor(), expected);
|
assert_eq!(t.cursor(), expected, "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn head() {
|
fn head() {
|
||||||
let mut t = TextArea::from(["efg", "h", ""]);
|
for text in [["efg", "h", ""], ["あいう", "👪", ""]] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
for row in 0..t.lines().len() {
|
for row in 0..t.lines().len() {
|
||||||
let len = t.lines()[row].len();
|
let len = t.lines()[row].len();
|
||||||
for col in [0, len / 2, len] {
|
for col in [0, len / 2, len] {
|
||||||
t.move_cursor(CursorMove::Jump(row as u16, col as u16));
|
t.move_cursor(CursorMove::Jump(row as u16, col as u16));
|
||||||
t.move_cursor(CursorMove::Head);
|
t.move_cursor(CursorMove::Head);
|
||||||
assert_eq!(t.cursor(), (row, 0));
|
assert_eq!(t.cursor(), (row, 0), "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn end() {
|
fn end() {
|
||||||
let mut t = TextArea::from(["efg", "h", ""]);
|
for text in [["efg", "h", ""], ["あいう", "👪", ""]] {
|
||||||
|
let mut t = TextArea::from(text);
|
||||||
for row in 0..t.lines().len() {
|
for row in 0..t.lines().len() {
|
||||||
let len = t.lines()[row].len();
|
let len = match row {
|
||||||
|
0 => 3,
|
||||||
|
1 => 1,
|
||||||
|
2 => 0,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
for col in [0, len / 2, len] {
|
for col in [0, len / 2, len] {
|
||||||
t.move_cursor(CursorMove::Jump(row as u16, col as u16));
|
t.move_cursor(CursorMove::Jump(row as u16, col as u16));
|
||||||
t.move_cursor(CursorMove::End);
|
t.move_cursor(CursorMove::End);
|
||||||
assert_eq!(t.cursor(), (row, len));
|
assert_eq!(t.cursor(), (row, len), "{:?}", t.lines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user