Skip to content

Commit

Permalink
Added unit tests along with modified offset_to_text_point function
Browse files Browse the repository at this point in the history
  • Loading branch information
paavininanda committed Feb 4, 2018
1 parent e9a42ce commit 5a6e63e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 4 additions & 5 deletions components/script/textinput.rs
Expand Up @@ -276,7 +276,7 @@ impl<T: ClipboardProvider> TextInput<T> {
}

debug_assert!(self.edit_point.line < self.lines.len());
debug_assert!(self.edit_point.index <= max(1, self.lines[self.edit_point.line].len()));
debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len());
}

pub fn get_selection_text(&self) -> Option<String> {
Expand Down Expand Up @@ -871,13 +871,12 @@ impl<T: ClipboardProvider> TextInput<T> {
fn offset_to_text_point(&self, abs_point: usize) -> TextPoint {
let mut index = abs_point;
let mut line = 0;

let last_line_idx = self.lines.len() - 1;
self.lines.iter().enumerate().fold(0, |acc, (i, val)| {
if i != last_line_idx {
let line_end = max(val.len(), 1);
let new_acc = acc + line_end;
if abs_point > new_acc && index > line_end {
let line_end = val.len();
let new_acc = acc + line_end + 1;
if abs_point >= new_acc && index > line_end {
index -= line_end + 1;
line += 1;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/script/textinput.rs
Expand Up @@ -600,6 +600,13 @@ fn test_textinput_set_selection_with_direction() {
assert!(textinput.selection_origin.is_some());
assert_eq!(textinput.selection_origin.unwrap().line, 0);
assert_eq!(textinput.selection_origin.unwrap().index, 6);

textinput = text_input(Lines::Multiple, "\n\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(textinput.edit_point.line, 1);
assert_eq!(textinput.edit_point.index, 0);
assert_eq!(textinput.selection_direction, SelectionDirection::Forward);

}

#[test]
Expand Down Expand Up @@ -629,4 +636,11 @@ fn test_selection_bounds() {
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
assert_eq!(3, textinput.selection_start_offset());
assert_eq!(6, textinput.selection_end_offset());

textinput = text_input(Lines::Multiple, "\n\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_origin_or_edit_point());
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_start());
assert_eq!(TextPoint { line: 1, index: 0 }, textinput.selection_end());

}

0 comments on commit 5a6e63e

Please sign in to comment.