Update tests for new Selection API

This commit is contained in:
Joe Wilm 2017-06-17 10:29:59 -07:00 committed by Joe Wilm
parent 6b081dcc95
commit f12fd880fe
3 changed files with 53 additions and 39 deletions

View File

@ -496,6 +496,7 @@ mod tests {
use event::{Mouse, ClickState};
use config::{self, Config, ClickHandler};
use index::{Point, Side};
use selection::Selection;
use super::{Action, Binding, Processor};
@ -510,7 +511,7 @@ mod tests {
struct ActionContext<'a> {
pub terminal: &'a mut Term,
pub selection: Option<&'a mut Selection>,
pub selection: &'a mut Option<Selection>,
pub size_info: &'a SizeInfo,
pub mouse: &'a mut Mouse,
pub last_action: MultiClick,
@ -533,11 +534,9 @@ mod tests {
// STUBBED
}
fn clear_selection(&mut self) { }
fn update_selection(&mut self, point: Point, side: Side) {
self.selection.update(point, side);
}
fn clear_selection(&mut self) {}
fn update_selection(&mut self, _point: Point, _side: Side) {}
fn simple_selection(&mut self, _point: Point, _side: Side) {}
fn semantic_selection(&mut self, _point: Point) {
// set something that we can check for here
@ -578,16 +577,16 @@ mod tests {
padding_y: 0.0,
};
use ::ansi::TermInfo;
let mut terminal = Term::new(&config, size);
let mut mouse = Mouse::default();
mouse.click_state = $initial_state;
let mut selection = None;
let context = ActionContext {
terminal: &mut terminal,
selection: None,
selection: &mut selection,
mouse: &mut mouse,
size_info: &size,
last_action: MultiClick::None,

View File

@ -408,6 +408,27 @@ mod test {
use index::{Line, Column, Side, Point};
use super::{Selection, Span, SpanType};
struct Dimensions(Point);
impl<'a> super::Dimensions for &'a Dimensions {
fn dimensions(&self) -> Point {
self.0
}
}
impl Dimensions {
pub fn new(line: usize, col: usize) -> Self {
Dimensions(Point {
line: Line(line),
col: Column(col)
})
}
}
impl<'a> super::SemanticSearch for &'a Dimensions {
fn semantic_search_left(&self, _: Point) -> Point { unimplemented!(); }
fn semantic_search_right(&self, _: Point) -> Point { unimplemented!(); }
}
/// Test case of single cell selection
///
/// 1. [ ]
@ -416,11 +437,11 @@ mod test {
#[test]
fn single_cell_left_to_right() {
let location = Point { line: Line(0), col: Column(0) };
let mut selection = Selection::new(Line(1), Column(1));
selection.update(location, Side::Left);
let mut selection = Selection::simple(location, Side::Left);
selection.update(location, Side::Right);
assert_eq!(selection.span().unwrap(), Span {
assert_eq!(selection.to_span(&Dimensions::new(1, 1)).unwrap(), Span {
cols: Column(1),
ty: SpanType::Inclusive,
front: location,
tail: location
@ -435,11 +456,11 @@ mod test {
#[test]
fn single_cell_right_to_left() {
let location = Point { line: Line(0), col: Column(0) };
let mut selection = Selection::new(Line(1), Column(1));
selection.update(location, Side::Right);
let mut selection = Selection::simple(location, Side::Right);
selection.update(location, Side::Left);
assert_eq!(selection.span().unwrap(), Span {
assert_eq!(selection.to_span(&Dimensions::new(1, 1)).unwrap(), Span {
cols: Column(1),
ty: SpanType::Inclusive,
front: location,
tail: location
@ -453,11 +474,10 @@ mod test {
/// 3. [ B][E ]
#[test]
fn between_adjacent_cells_left_to_right() {
let mut selection = Selection::new(Line(1), Column(2));
selection.update(Point::new(Line(0), Column(0)), Side::Right);
let mut selection = Selection::simple(Point::new(Line(0), Column(0)), Side::Right);
selection.update(Point::new(Line(0), Column(1)), Side::Left);
assert_eq!(selection.span(), None);
assert_eq!(selection.to_span(&Dimensions::new(1, 2)), None);
}
/// Test adjacent cell selection from right to left
@ -467,11 +487,10 @@ mod test {
/// 3. [ E][B ]
#[test]
fn between_adjacent_cells_right_to_left() {
let mut selection = Selection::new(Line(1), Column(2));
selection.update(Point::new(Line(0), Column(1)), Side::Left);
let mut selection = Selection::simple(Point::new(Line(0), Column(1)), Side::Left);
selection.update(Point::new(Line(0), Column(0)), Side::Right);
assert_eq!(selection.span(), None);
assert_eq!(selection.to_span(&Dimensions::new(1, 2)), None);
}
/// Test selection across adjacent lines
@ -485,11 +504,11 @@ mod test {
/// [XX][XB][ ][ ][ ]
#[test]
fn across_adjacent_lines_upward_final_cell_exclusive() {
let mut selection = Selection::new(Line(2), Column(5));
selection.update(Point::new(Line(1), Column(1)), Side::Right);
let mut selection = Selection::simple(Point::new(Line(1), Column(1)), Side::Right);
selection.update(Point::new(Line(0), Column(1)), Side::Right);
assert_eq!(selection.span().unwrap(), Span {
assert_eq!(selection.to_span(&Dimensions::new(2, 5)).unwrap(), Span {
cols: Column(5),
front: Point::new(Line(0), Column(1)),
tail: Point::new(Line(1), Column(1)),
ty: SpanType::ExcludeFront
@ -509,12 +528,12 @@ mod test {
/// [XE][ ][ ][ ][ ]
#[test]
fn selection_bigger_then_smaller() {
let mut selection = Selection::new(Line(2), Column(5));
selection.update(Point::new(Line(0), Column(1)), Side::Right);
let mut selection = Selection::simple(Point::new(Line(0), Column(1)), Side::Right);
selection.update(Point::new(Line(1), Column(1)), Side::Right);
selection.update(Point::new(Line(1), Column(0)), Side::Right);
assert_eq!(selection.span().unwrap(), Span {
assert_eq!(selection.to_span(&Dimensions::new(2, 5)).unwrap(), Span {
cols: Column(5),
front: Point::new(Line(0), Column(1)),
tail: Point::new(Line(1), Column(0)),
ty: SpanType::ExcludeFront

View File

@ -1841,21 +1841,18 @@ mod tests {
mem::swap(&mut term.semantic_escape_chars, &mut escape_chars);
{
let mut selection = Selection::new(grid.num_lines(), grid.num_cols());
term.semantic_selection(&mut selection, Point { line: Line(0), col: Column(1) });
assert_eq!(term.string_from_selection(&selection.span().unwrap()), "aa");
let selection = Selection::semantic(Point { line: Line(0), col: Column(1) }, &term);
assert_eq!(term.string_from_selection(&selection.to_span(&term).unwrap()), "aa");
}
{
let mut selection = Selection::new(grid.num_lines(), grid.num_cols());
term.semantic_selection(&mut selection, Point { line: Line(0), col: Column(4) });
assert_eq!(term.string_from_selection(&selection.span().unwrap()), "aaa");
let selection = Selection::semantic(Point { line: Line(0), col: Column(4) }, &term);
assert_eq!(term.string_from_selection(&selection.to_span(&term).unwrap()), "aaa");
}
{
let mut selection = Selection::new(grid.num_lines(), grid.num_cols());
term.semantic_selection(&mut selection, Point { line: Line(1), col: Column(1) });
assert_eq!(term.string_from_selection(&selection.span().unwrap()), "aaa");
let selection = Selection::semantic(Point { line: Line(1), col: Column(1) }, &term);
assert_eq!(term.string_from_selection(&selection.to_span(&term).unwrap()), "aaa");
}
}
@ -1880,9 +1877,8 @@ mod tests {
mem::swap(&mut term.grid, &mut grid);
let mut selection = Selection::new(grid.num_lines(), grid.num_cols());
term.line_selection(&mut selection, Point { line: Line(0), col: Column(3) });
match selection.span() {
let selection = Selection::lines(Point { line: Line(0), col: Column(3) });
match selection.to_span(&term) {
Some(span) => assert_eq!(term.string_from_selection(&span), "\"aa\"a"),
_ => ()
}