Introduce a script::test module to expose the APIs needed for unit tests.

This commit is contained in:
Ms2ger 2016-06-08 11:55:47 +02:00
parent b843be4975
commit 86d59212fe
5 changed files with 68 additions and 22 deletions

View file

@ -118,6 +118,7 @@ mod serviceworker_manager;
mod serviceworkerjob;
mod stylesheet_loader;
mod task_source;
pub mod test;
pub mod textinput;
mod timers;
mod unpremultiplytable;

55
components/script/test.rs Normal file
View file

@ -0,0 +1,55 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
pub use dom::bindings::str::{ByteString, DOMString};
pub use dom::headers::normalize_value;
pub mod size_of {
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 layout_wrapper::ServoThreadSafeLayoutNode;
use std::mem::size_of;
pub fn CharacterData() -> usize {
size_of::<CharacterData>()
}
pub fn Element() -> usize {
size_of::<Element>()
}
pub fn EventTarget() -> usize {
size_of::<EventTarget>()
}
pub fn HTMLDivElement() -> usize {
size_of::<HTMLDivElement>()
}
pub fn HTMLElement() -> usize {
size_of::<HTMLElement>()
}
pub fn HTMLSpanElement() -> usize {
size_of::<HTMLSpanElement>()
}
pub fn Node() -> usize {
size_of::<Node>()
}
pub fn ServoThreadSafeLayoutNode() -> usize {
size_of::<ServoThreadSafeLayoutNode>()
}
pub fn Text() -> usize {
size_of::<Text>()
}
}

View file

@ -2,14 +2,13 @@
* 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 script::dom::bindings::str::ByteString;
use script::dom::headers;
use script::test::{ByteString, normalize_value};
#[test]
fn test_normalize_empty_bytestring() {
// empty ByteString test
let empty_bytestring = ByteString::new(vec![]);
let actual = headers::normalize_value(empty_bytestring);
let actual = normalize_value(empty_bytestring);
let expected = ByteString::new(vec![]);
assert_eq!(actual, expected);
}
@ -18,7 +17,7 @@ fn test_normalize_empty_bytestring() {
fn test_normalize_all_whitespace_bytestring() {
// All whitespace test. A horizontal tab, a line feed, a carriage return , and a space
let all_whitespace_bytestring = ByteString::new(vec![b'\t', b'\n', b'\r', b' ']);
let actual = headers::normalize_value(all_whitespace_bytestring);
let actual = normalize_value(all_whitespace_bytestring);
let expected = ByteString::new(vec![]);
assert_eq!(actual, expected);
}
@ -27,7 +26,7 @@ fn test_normalize_all_whitespace_bytestring() {
fn test_normalize_non_empty_no_whitespace_bytestring() {
// Non-empty, no whitespace ByteString test
let no_whitespace_bytestring = ByteString::new(vec![b'S', b'!']);
let actual = headers::normalize_value(no_whitespace_bytestring);
let actual = normalize_value(no_whitespace_bytestring);
let expected = ByteString::new(vec![b'S', b'!']);
assert_eq!(actual, expected);
}
@ -36,7 +35,7 @@ fn test_normalize_non_empty_no_whitespace_bytestring() {
fn test_normalize_non_empty_leading_whitespace_bytestring() {
// Non-empty, leading whitespace, no trailing whitespace ByteString test
let leading_whitespace_bytestring = ByteString::new(vec![b'\t', b'\n', b' ', b'\r', b'S', b'!']);
let actual = headers::normalize_value(leading_whitespace_bytestring);
let actual = normalize_value(leading_whitespace_bytestring);
let expected = ByteString::new(vec![b'S', b'!']);
assert_eq!(actual, expected);
}
@ -45,7 +44,7 @@ fn test_normalize_non_empty_leading_whitespace_bytestring() {
fn test_normalize_non_empty_no_leading_whitespace_trailing_whitespace_bytestring() {
// Non-empty, no leading whitespace, but with trailing whitespace ByteString test
let trailing_whitespace_bytestring = ByteString::new(vec![b'S', b'!', b'\t', b'\n', b' ', b'\r']);
let actual = headers::normalize_value(trailing_whitespace_bytestring);
let actual = normalize_value(trailing_whitespace_bytestring);
let expected = ByteString::new(vec![b'S', b'!']);
assert_eq!(actual, expected);
}
@ -55,7 +54,7 @@ fn test_normalize_non_empty_leading_and_trailing_whitespace_bytestring() {
// Non-empty, leading whitespace, and trailing whitespace ByteString test
let whitespace_sandwich_bytestring =
ByteString::new(vec![b'\t', b'\n', b' ', b'\r', b'S', b'!', b'\t', b'\n', b' ', b'\r']);
let actual = headers::normalize_value(whitespace_sandwich_bytestring);
let actual = normalize_value(whitespace_sandwich_bytestring);
let expected = ByteString::new(vec![b'S', b'!']);
assert_eq!(actual, expected);
}
@ -68,7 +67,7 @@ fn test_normalize_non_empty_leading_trailing_and_internal_whitespace_bytestring(
ByteString::new(vec![b'\t', b'\n', b' ', b'\r', b'S',
b'\t', b'\n', b' ', b'\r', b'!',
b'\t', b'\n', b' ', b'\r']);
let actual = headers::normalize_value(whitespace_bigmac_bytestring);
let actual = normalize_value(whitespace_bigmac_bytestring);
let expected = ByteString::new(vec![b'S', b'\t', b'\n', b' ', b'\r', b'!']);
assert_eq!(actual, expected);
}

View file

@ -2,25 +2,16 @@
* 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 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 script::layout_wrapper::ServoThreadSafeLayoutNode;
use std::mem::size_of;
use script::test::size_of;
// Macro so that we can stringify type names
// I'd really prefer the tests themselves to be run at plugin time,
// however rustc::middle doesn't have access to the full type data
macro_rules! sizeof_checker (
($testname: ident, $t:ty, $known_size:expr) => (
($testname: ident, $t: ident, $known_size: expr) => (
#[test]
fn $testname() {
let new = size_of::<$t>();
let new = size_of::$t();
let old = $known_size;
if new < old {
panic!("Your changes have decreased the stack size of commonly used DOM struct {} from {} to {}. \

View file

@ -13,7 +13,7 @@ use msg::constellation_msg::CONTROL;
#[cfg(target_os = "macos")]
use msg::constellation_msg::SUPER;
use script::clipboard_provider::DummyClipboardContext;
use script::dom::bindings::str::DOMString;
use script::test::DOMString;
use script::textinput::{TextInput, TextPoint, Selection, Lines, Direction, SelectionDirection};
fn text_input(lines: Lines, s: &str) -> TextInput<DummyClipboardContext> {