From dc431c9bdb7a746b36a3084a80743df6700df9c8 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 7 Apr 2015 18:35:18 +0200 Subject: [PATCH] Move script crate unit tests into the unit_tests crate. --- components/script/lib.rs | 3 - components/script/textinput.rs | 188 ++---------------- components/servo/Cargo.lock | 1 + tests/unit/Cargo.toml | 3 + tests/unit/lib.rs | 2 + tests/unit/script/mod.rs | 2 + .../tests.rs => tests/unit/script/size_of.rs | 16 +- tests/unit/script/textinput.rs | 158 +++++++++++++++ 8 files changed, 190 insertions(+), 183 deletions(-) create mode 100644 tests/unit/script/mod.rs rename components/script/tests.rs => tests/unit/script/size_of.rs (85%) create mode 100644 tests/unit/script/textinput.rs diff --git a/components/script/lib.rs b/components/script/lib.rs index 75699c419cc..be9ec362473 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -65,6 +65,3 @@ pub mod script_task; mod timers; pub mod textinput; mod devtools; - -#[cfg(all(test, target_pointer_width = "64"))] -mod tests; diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 8239c08c58f..c26549bff73 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -15,18 +15,18 @@ use std::default::Default; use std::num::SignedInt; #[derive(Copy, PartialEq)] -enum Selection { +pub enum Selection { Selected, NotSelected } #[jstraceable] #[derive(Copy)] -struct TextPoint { +pub struct TextPoint { /// 0-based line number - line: usize, + pub line: usize, /// 0-based column number - index: usize, + pub index: usize, } /// Encapsulated state for handling keyboard input in a single or multiline text input control. @@ -35,7 +35,7 @@ pub struct TextInput { /// Current text input content, split across lines without trailing '\n' lines: Vec, /// Current cursor input point - edit_point: TextPoint, + pub edit_point: TextPoint, /// Beginning of selection range with edit_point as end that can span multiple lines. selection_begin: Option, /// Is this a multiline input? @@ -67,7 +67,7 @@ pub enum Lines { /// The direction in which to delete a character. #[derive(PartialEq)] -enum DeleteDir { +pub enum DeleteDir { Forward, Backward } @@ -99,7 +99,7 @@ impl TextInput { } /// Remove a character at the current editing point - fn delete_char(&mut self, dir: DeleteDir) { + pub fn delete_char(&mut self, dir: DeleteDir) { if self.selection_begin.is_none() { self.adjust_horizontal(if dir == DeleteDir::Forward { 1 @@ -111,14 +111,14 @@ impl TextInput { } /// Insert a character at the current editing point - fn insert_char(&mut self, ch: char) { + pub fn insert_char(&mut self, ch: char) { if self.selection_begin.is_none() { self.selection_begin = Some(self.edit_point); } self.replace_selection(ch.to_string()); } - fn get_sorted_selection(&self) -> (TextPoint, TextPoint) { + pub fn get_sorted_selection(&self) -> (TextPoint, TextPoint) { let begin = self.selection_begin.unwrap(); let end = self.edit_point; @@ -129,7 +129,7 @@ impl TextInput { } } - fn replace_selection(&mut self, insert: String) { + pub fn replace_selection(&mut self, insert: String) { let (begin, end) = self.get_sorted_selection(); self.clear_selection(); @@ -166,13 +166,13 @@ impl TextInput { } /// Return the length of the current line under the editing point. - fn current_line_length(&self) -> usize { + pub fn current_line_length(&self) -> usize { self.lines[self.edit_point.line].chars().count() } /// Adjust the editing point position by a given of lines. The resulting column is /// as close to the original column position as possible. - fn adjust_vertical(&mut self, adjust: isize, select: Selection) { + pub fn adjust_vertical(&mut self, adjust: isize, select: Selection) { if !self.multiline { return; } @@ -206,7 +206,7 @@ impl TextInput { /// Adjust the editing point position by a given number of columns. If the adjustment /// requested is larger than is available in the current line, the editing point is /// adjusted vertically and the process repeats with the remaining adjustment requested. - fn adjust_horizontal(&mut self, adjust: isize, select: Selection) { + pub fn adjust_horizontal(&mut self, adjust: isize, select: Selection) { if select == Selection::Selected { if self.selection_begin.is_none() { self.selection_begin = Some(self.edit_point); @@ -244,7 +244,7 @@ impl TextInput { } /// Deal with a newline input. - fn handle_return(&mut self) -> KeyReaction { + pub fn handle_return(&mut self) -> KeyReaction { if !self.multiline { return KeyReaction::TriggerDefaultAction; } @@ -253,7 +253,7 @@ impl TextInput { } /// Select all text in the input control. - fn select_all(&mut self) { + pub fn select_all(&mut self) { self.selection_begin = Some(TextPoint { line: 0, index: 0, @@ -264,7 +264,7 @@ impl TextInput { } /// Remove the current selection. - fn clear_selection(&mut self) { + pub fn clear_selection(&mut self) { self.selection_begin = None; } @@ -361,159 +361,3 @@ impl TextInput { self.edit_point.index = min(self.edit_point.index, self.current_line_length()); } } - -#[test] -fn test_textinput_delete_char() { - let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); - textinput.adjust_horizontal(2, Selection::NotSelected); - textinput.delete_char(DeleteDir::Backward); - assert_eq!(textinput.get_content(), "acdefg"); - - textinput.delete_char(DeleteDir::Forward); - assert_eq!(textinput.get_content(), "adefg"); - - textinput.adjust_horizontal(2, Selection::Selected); - textinput.delete_char(DeleteDir::Forward); - assert_eq!(textinput.get_content(), "afg"); -} - -#[test] -fn test_textinput_insert_char() { - let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); - textinput.adjust_horizontal(2, Selection::NotSelected); - textinput.insert_char('a'); - assert_eq!(textinput.get_content(), "abacdefg"); - - textinput.adjust_horizontal(2, Selection::Selected); - textinput.insert_char('b'); - assert_eq!(textinput.get_content(), "ababefg"); -} - -#[test] -fn test_textinput_get_sorted_selection() { - let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); - textinput.adjust_horizontal(2, Selection::NotSelected); - textinput.adjust_horizontal(2, Selection::Selected); - let (begin, end) = textinput.get_sorted_selection(); - assert_eq!(begin.index, 2); - assert_eq!(end.index, 4); - - textinput.clear_selection(); - - textinput.adjust_horizontal(-2, Selection::Selected); - let (begin, end) = textinput.get_sorted_selection(); - assert_eq!(begin.index, 2); - assert_eq!(end.index, 4); -} - -#[test] -fn test_textinput_replace_selection() { - let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); - textinput.adjust_horizontal(2, Selection::NotSelected); - textinput.adjust_horizontal(2, Selection::Selected); - - textinput.replace_selection("xyz".to_owned()); - assert_eq!(textinput.get_content(), "abxyzefg"); -} - -#[test] -fn test_textinput_current_line_length() { - let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); - assert_eq!(textinput.current_line_length(), 3); - - textinput.adjust_vertical(1, Selection::NotSelected); - assert_eq!(textinput.current_line_length(), 2); - - textinput.adjust_vertical(1, Selection::NotSelected); - assert_eq!(textinput.current_line_length(), 1); -} - -#[test] -fn test_textinput_adjust_vertical() { - let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); - textinput.adjust_horizontal(3, Selection::NotSelected); - textinput.adjust_vertical(1, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 1); - assert_eq!(textinput.edit_point.index, 2); - - textinput.adjust_vertical(-1, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 0); - assert_eq!(textinput.edit_point.index, 2); - - textinput.adjust_vertical(2, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 2); - assert_eq!(textinput.edit_point.index, 1); -} - -#[test] -fn test_textinput_adjust_horizontal() { - let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); - textinput.adjust_horizontal(4, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 1); - assert_eq!(textinput.edit_point.index, 0); - - textinput.adjust_horizontal(1, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 1); - assert_eq!(textinput.edit_point.index, 1); - - textinput.adjust_horizontal(2, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 2); - assert_eq!(textinput.edit_point.index, 0); - - textinput.adjust_horizontal(-1, Selection::NotSelected); - assert_eq!(textinput.edit_point.line, 1); - assert_eq!(textinput.edit_point.index, 2); -} - -#[test] -fn test_textinput_handle_return() { - let mut single_line_textinput = TextInput::new(Lines::Single, "abcdef".to_owned()); - single_line_textinput.adjust_horizontal(3, Selection::NotSelected); - single_line_textinput.handle_return(); - assert_eq!(single_line_textinput.get_content(), "abcdef"); - - let mut multi_line_textinput = TextInput::new(Lines::Multiple, "abcdef".to_owned()); - multi_line_textinput.adjust_horizontal(3, Selection::NotSelected); - multi_line_textinput.handle_return(); - assert_eq!(multi_line_textinput.get_content(), "abc\ndef"); -} - -#[test] -fn test_textinput_select_all() { - let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); - assert_eq!(textinput.edit_point.line, 0); - assert_eq!(textinput.edit_point.index, 0); - - textinput.select_all(); - assert_eq!(textinput.edit_point.line, 2); - assert_eq!(textinput.edit_point.index, 1); -} - -#[test] -fn test_textinput_get_content() { - let single_line_textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); - assert_eq!(single_line_textinput.get_content(), "abcdefg"); - - let multi_line_textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); - assert_eq!(multi_line_textinput.get_content(), "abc\nde\nf"); -} - -#[test] -fn test_textinput_set_content() { - let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); - assert_eq!(textinput.get_content(), "abc\nde\nf"); - - textinput.set_content("abc\nf".to_owned()); - assert_eq!(textinput.get_content(), "abc\nf"); - - assert_eq!(textinput.edit_point.line, 0); - assert_eq!(textinput.edit_point.index, 0); - textinput.adjust_horizontal(3, Selection::Selected); - assert_eq!(textinput.edit_point.line, 0); - assert_eq!(textinput.edit_point.index, 3); - textinput.set_content("de".to_owned()); - assert_eq!(textinput.get_content(), "de"); - assert_eq!(textinput.edit_point.line, 0); - assert_eq!(textinput.edit_point.index, 2); -} - diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 81ea1acb18e..7e16ce2a800 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -932,6 +932,7 @@ dependencies = [ "net 0.0.1", "net_traits 0.0.1", "profile 0.0.1", + "script 0.0.1", "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_plugin 0.0.0 (git+https://github.com/servo/string-cache)", diff --git a/tests/unit/Cargo.toml b/tests/unit/Cargo.toml index 3a241ecb871..5f0d1125796 100644 --- a/tests/unit/Cargo.toml +++ b/tests/unit/Cargo.toml @@ -26,6 +26,9 @@ path = "../../components/profile" [dependencies.style] path = "../../components/style" +[dependencies.script] +path = "../../components/script" + [dependencies.geom] git = "https://github.com/servo/rust-geom" diff --git a/tests/unit/lib.rs b/tests/unit/lib.rs index d0c8c7bc612..cf55e0050c4 100644 --- a/tests/unit/lib.rs +++ b/tests/unit/lib.rs @@ -9,6 +9,7 @@ extern crate gfx; extern crate net; extern crate net_traits; extern crate profile; +extern crate script; extern crate selectors; extern crate string_cache; extern crate style; @@ -17,5 +18,6 @@ extern crate url; #[cfg(test)] #[path="gfx/mod.rs"] mod gfx_tests; #[cfg(test)] #[path="net/mod.rs"] mod net_tests; +#[cfg(test)] #[path="script/mod.rs"] mod script_tests; #[cfg(test)] #[path="style/mod.rs"] mod style_tests; #[cfg(test)] #[path="util/mod.rs"] mod util_tests; diff --git a/tests/unit/script/mod.rs b/tests/unit/script/mod.rs new file mode 100644 index 00000000000..22bc57d12df --- /dev/null +++ b/tests/unit/script/mod.rs @@ -0,0 +1,2 @@ +#[cfg(target_pointer_width = "64")] mod size_of; +mod textinput; diff --git a/components/script/tests.rs b/tests/unit/script/size_of.rs similarity index 85% rename from components/script/tests.rs rename to tests/unit/script/size_of.rs index a0f82628cd6..d32f2bf4375 100644 --- a/components/script/tests.rs +++ b/tests/unit/script/size_of.rs @@ -2,14 +2,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use dom::characterdata::CharacterData; -use dom::element::Element; -use dom::eventtarget::EventTarget; -use dom::htmldivelement::HTMLDivElement; -use dom::htmlelement::HTMLElement; -use dom::htmlspanelement::HTMLSpanElement; -use dom::node::Node; -use dom::text::Text; +use script::dom::characterdata::CharacterData; +use script::dom::element::Element; +use script::dom::eventtarget::EventTarget; +use script::dom::htmldivelement::HTMLDivElement; +use script::dom::htmlelement::HTMLElement; +use script::dom::htmlspanelement::HTMLSpanElement; +use script::dom::node::Node; +use script::dom::text::Text; use std::mem::size_of; diff --git a/tests/unit/script/textinput.rs b/tests/unit/script/textinput.rs new file mode 100644 index 00000000000..a07886dce9d --- /dev/null +++ b/tests/unit/script/textinput.rs @@ -0,0 +1,158 @@ +use script::textinput::{TextInput, Selection, Lines, DeleteDir}; +use std::borrow::ToOwned; + +#[test] +fn test_textinput_delete_char() { + let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); + textinput.adjust_horizontal(2, Selection::NotSelected); + textinput.delete_char(DeleteDir::Backward); + assert_eq!(textinput.get_content(), "acdefg"); + + textinput.delete_char(DeleteDir::Forward); + assert_eq!(textinput.get_content(), "adefg"); + + textinput.adjust_horizontal(2, Selection::Selected); + textinput.delete_char(DeleteDir::Forward); + assert_eq!(textinput.get_content(), "afg"); +} + +#[test] +fn test_textinput_insert_char() { + let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); + textinput.adjust_horizontal(2, Selection::NotSelected); + textinput.insert_char('a'); + assert_eq!(textinput.get_content(), "abacdefg"); + + textinput.adjust_horizontal(2, Selection::Selected); + textinput.insert_char('b'); + assert_eq!(textinput.get_content(), "ababefg"); +} + +#[test] +fn test_textinput_get_sorted_selection() { + let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); + textinput.adjust_horizontal(2, Selection::NotSelected); + textinput.adjust_horizontal(2, Selection::Selected); + let (begin, end) = textinput.get_sorted_selection(); + assert_eq!(begin.index, 2); + assert_eq!(end.index, 4); + + textinput.clear_selection(); + + textinput.adjust_horizontal(-2, Selection::Selected); + let (begin, end) = textinput.get_sorted_selection(); + assert_eq!(begin.index, 2); + assert_eq!(end.index, 4); +} + +#[test] +fn test_textinput_replace_selection() { + let mut textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); + textinput.adjust_horizontal(2, Selection::NotSelected); + textinput.adjust_horizontal(2, Selection::Selected); + + textinput.replace_selection("xyz".to_owned()); + assert_eq!(textinput.get_content(), "abxyzefg"); +} + +#[test] +fn test_textinput_current_line_length() { + let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); + assert_eq!(textinput.current_line_length(), 3); + + textinput.adjust_vertical(1, Selection::NotSelected); + assert_eq!(textinput.current_line_length(), 2); + + textinput.adjust_vertical(1, Selection::NotSelected); + assert_eq!(textinput.current_line_length(), 1); +} + +#[test] +fn test_textinput_adjust_vertical() { + let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); + textinput.adjust_horizontal(3, Selection::NotSelected); + textinput.adjust_vertical(1, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 1); + assert_eq!(textinput.edit_point.index, 2); + + textinput.adjust_vertical(-1, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 2); + + textinput.adjust_vertical(2, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 2); + assert_eq!(textinput.edit_point.index, 1); +} + +#[test] +fn test_textinput_adjust_horizontal() { + let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); + textinput.adjust_horizontal(4, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 1); + assert_eq!(textinput.edit_point.index, 0); + + textinput.adjust_horizontal(1, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 1); + assert_eq!(textinput.edit_point.index, 1); + + textinput.adjust_horizontal(2, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 2); + assert_eq!(textinput.edit_point.index, 0); + + textinput.adjust_horizontal(-1, Selection::NotSelected); + assert_eq!(textinput.edit_point.line, 1); + assert_eq!(textinput.edit_point.index, 2); +} + +#[test] +fn test_textinput_handle_return() { + let mut single_line_textinput = TextInput::new(Lines::Single, "abcdef".to_owned()); + single_line_textinput.adjust_horizontal(3, Selection::NotSelected); + single_line_textinput.handle_return(); + assert_eq!(single_line_textinput.get_content(), "abcdef"); + + let mut multi_line_textinput = TextInput::new(Lines::Multiple, "abcdef".to_owned()); + multi_line_textinput.adjust_horizontal(3, Selection::NotSelected); + multi_line_textinput.handle_return(); + assert_eq!(multi_line_textinput.get_content(), "abc\ndef"); +} + +#[test] +fn test_textinput_select_all() { + let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 0); + + textinput.select_all(); + assert_eq!(textinput.edit_point.line, 2); + assert_eq!(textinput.edit_point.index, 1); +} + +#[test] +fn test_textinput_get_content() { + let single_line_textinput = TextInput::new(Lines::Single, "abcdefg".to_owned()); + assert_eq!(single_line_textinput.get_content(), "abcdefg"); + + let multi_line_textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); + assert_eq!(multi_line_textinput.get_content(), "abc\nde\nf"); +} + +#[test] +fn test_textinput_set_content() { + let mut textinput = TextInput::new(Lines::Multiple, "abc\nde\nf".to_owned()); + assert_eq!(textinput.get_content(), "abc\nde\nf"); + + textinput.set_content("abc\nf".to_owned()); + assert_eq!(textinput.get_content(), "abc\nf"); + + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 0); + textinput.adjust_horizontal(3, Selection::Selected); + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 3); + textinput.set_content("de".to_owned()); + assert_eq!(textinput.get_content(), "de"); + assert_eq!(textinput.edit_point.line, 0); + assert_eq!(textinput.edit_point.index, 2); +} +