#![allow(clippy::unwrap_used, clippy::expect_used)]
use idet_core::completion::{char_to_byte, collect_words, current_prefix, current_word, matches};
use idet_core::edits::{dedent_line, indent_line, insert, newline_indent};
use idet_core::lineops::{LineMove, move_line};
use idet_core::wordops::{WordSwap, swap};
#[test]
fn move_line_swaps_and_keeps_column() {
let text = "alpha\nbeta\ngamma";
let (moved, cursor) = move_line(text, 8, LineMove::Up).unwrap();
assert_eq!(moved, "beta\nalpha\ngamma");
assert_eq!(cursor, 2);
}
#[test]
fn move_line_down_at_last_line_is_none() {
let text = "one\ntwo";
assert!(move_line(text, 5, LineMove::Down).is_none());
}
#[test]
fn move_line_up_at_first_line_is_none() {
let text = "one\ntwo";
assert!(move_line(text, 1, LineMove::Up).is_none());
}
#[test]
fn move_line_cursor_follows_its_line() {
let text = "ab\ncdef";
let (moved, cursor) = move_line(text, 1, LineMove::Down).unwrap();
assert_eq!(moved, "cdef\nab");
assert_eq!(cursor, 6);
}
#[test]
fn move_line_counts_multibyte_columns() {
let text = "äö\nxy";
let (moved, cursor) = move_line(text, 1, LineMove::Down).unwrap();
assert_eq!(moved, "xy\näö");
assert_eq!(cursor, 4);
}
#[test]
fn collect_words_dedups_and_filters_short() {
let words = collect_words("foo bar foo a bb baz_1");
assert_eq!(words, vec!["bar", "baz_1", "foo"]);
}
#[test]
fn current_prefix_reads_word_before_cursor() {
assert_eq!(current_prefix("let val", 7), "val");
assert_eq!(current_prefix("let val ", 8), "");
assert_eq!(current_prefix("hä_ll", 5), "hä_ll");
}
#[test]
fn current_word_spans_both_directions() {
assert_eq!(current_word("hello world", 3), (0, 5));
assert_eq!(current_word("hello world", 6), (6, 11));
assert_eq!(current_word("hello world", 5), (0, 5));
assert_eq!(current_word("a b", 2), (2, 2));
assert_eq!(current_word("hä_ll", 5), (0, 5));
}
#[test]
fn matches_excludes_exact_and_non_prefix() {
let words = vec!["value".to_owned(), "val".to_owned(), "other".to_owned()];
let found = matches(&words, "val");
assert_eq!(found, vec![&"value".to_owned()]);
}
#[test]
fn char_to_byte_handles_multibyte() {
assert_eq!(char_to_byte("äbc", 0), 0);
assert_eq!(char_to_byte("äbc", 1), 2);
assert_eq!(char_to_byte("äbc", 3), 4);
}
#[test]
fn swap_caret_word_with_next() {
assert_eq!(
swap("foo bar", (1, 1), WordSwap::Next).unwrap(),
("bar foo".to_owned(), (5, 5))
);
}
#[test]
fn swap_caret_word_with_prev() {
assert_eq!(
swap("foo bar", (5, 5), WordSwap::Prev).unwrap(),
("bar foo".to_owned(), (1, 1))
);
}
#[test]
fn swap_none_without_neighbour() {
assert!(swap("foo", (1, 1), WordSwap::Next).is_none());
assert!(swap(" ", (1, 1), WordSwap::Next).is_none());
}
#[test]
fn swap_caret_word_keeps_multibyte() {
assert_eq!(
swap("ä ö", (0, 0), WordSwap::Next).unwrap(),
("ö ä".to_owned(), (2, 2))
);
}
#[test]
fn swap_selection_moves_whole_span() {
assert_eq!(
swap("don't next", (0, 5), WordSwap::Next).unwrap(),
("next don't".to_owned(), (5, 10))
);
}
#[test]
fn swap_selection_prev_keeps_range() {
assert_eq!(
swap("one don't", (4, 9), WordSwap::Prev).unwrap(),
("don't one".to_owned(), (0, 5))
);
}
#[test]
fn insert_places_after_insertion() {
assert_eq!(insert("ab", 1, "XY"), ("aXYb".to_owned(), 3));
}
#[test]
fn newline_indent_copies_leading_whitespace() {
assert_eq!(newline_indent(" x", 5), (" x\n ".to_owned(), 10));
}
#[test]
fn indent_line_prepends_four_spaces() {
assert_eq!(indent_line("ab\ncd", 4), ("ab\n cd".to_owned(), 8));
}
#[test]
fn dedent_line_removes_leading_spaces() {
assert_eq!(dedent_line(" cd", 7), (" cd".to_owned(), 3));
assert_eq!(dedent_line("cd", 1), ("cd".to_owned(), 1));
}