Merge pull request #607 from twilco/master

Add unit tests for various `web-sys` HTML element bindings
This commit is contained in:
Nick Fitzgerald 2018-08-01 16:54:25 -07:00 committed by GitHub
commit d90802a40c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 230 additions and 20 deletions

View File

@ -226,17 +226,17 @@ bindings are fully working and have full test coverage.
- [ ] HTMLOListElement.webidl
- [ ] HTMLOptGroupElement.webidl
- [ ] HTMLOptionElement.webidl
- [ ] HTMLOptionsCollection.webidl
- [ ] HTMLOutputElement.webidl
- [ ] HTMLParagraphElement.webidl
- [ ] HTMLParamElement.webidl
- [x] HTMLOptionsCollection.webidl
- [x] HTMLOutputElement.webidl
- [x] HTMLParagraphElement.webidl
- [x] HTMLParamElement.webidl
- [ ] HTMLPictureElement.webidl
- [ ] HTMLPreElement.webidl
- [ ] HTMLProgressElement.webidl
- [ ] HTMLQuoteElement.webidl
- [x] HTMLPreElement.webidl
- [x] HTMLProgressElement.webidl
- [x] HTMLQuoteElement.webidl
- [x] HTMLScriptElement.webidl
- [x] HTMLSelectElement.webidl
- [ ] HTMLSlotElement.webidl
- [x] HTMLSlotElement.webidl
- [ ] HTMLSourceElement.webidl
- [ ] HTMLSpanElement.webidl
- [x] HTMLStyleElement.webidl

View File

@ -1,7 +1,3 @@
export function new_div() {
return document.createElement("div");
}
export function new_a() {
return document.createElement("a");
}
@ -18,6 +14,10 @@ export function new_button() {
return document.createElement("button");
}
export function new_div() {
return document.createElement("div");
}
export function new_form() {
return document.createElement("form");
}
@ -26,6 +26,10 @@ export function new_head() {
return document.createElement("head");
}
export function new_heading() {
return document.createElement("h1");
}
export function new_hr() {
return document.createElement("hr");
}
@ -34,6 +38,38 @@ export function new_html() {
return document.createElement("html");
}
export function new_input() {
return document.createElement("input");
}
export function new_food_options_collection() {
return new_select_with_food_opts().options;
}
export function new_output() {
return document.createElement("output");
}
export function new_paragraph() {
return document.createElement("p");
}
export function new_param() {
return document.createElement("param");
}
export function new_pre() {
return document.createElement("pre");
}
export function new_progress() {
return document.createElement("progress");
}
export function new_quote() {
return document.createElement("q");
}
export function new_script() {
return document.createElement("script");
}
@ -51,6 +87,10 @@ export function new_select_with_food_opts() {
return select;
}
export function new_slot() {
return document.createElement("slot");
}
export function new_span() {
return document.createElement("span");
}
@ -59,18 +99,10 @@ export function new_style() {
return document.createElement("style");
}
export function new_input() {
return document.createElement("input");
}
export function new_title() {
return document.createElement("title");
}
export function new_heading() {
return document.createElement("h1");
}
export function new_xpath_result() {
let xmlDoc = new DOMParser().parseFromString("<root><value>tomato</value></root>", "application/xml");
let xpathResult = xmlDoc.evaluate("/root//value", xmlDoc, null, XPathResult.ANY_TYPE, null);

View File

@ -25,9 +25,17 @@ pub mod hr_element;
pub mod html_element;
pub mod html_html_element;
pub mod input_element;
pub mod options_collection;
pub mod output_element;
pub mod paragraph_element;
pub mod param_element;
pub mod pre_element;
pub mod progress_element;
pub mod quote_element;
pub mod response;
pub mod select_element;
pub mod script_element;
pub mod slot_element;
pub mod span_element;
pub mod style_element;
pub mod title_element;

View File

@ -0,0 +1,23 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlOptionsCollection;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_food_options_collection() -> HtmlOptionsCollection;
}
#[wasm_bindgen_test]
fn test_options_collection() {
let opt_collection = new_food_options_collection();
assert!(opt_collection.length() == 4, "Our option collection should have four options.");
assert!(opt_collection.remove(0).is_ok(), "We should be able to successfully remove an element from an option collection.");
assert!(opt_collection.length() == 3, "Our option collection should have three options after removing one.");
assert!(opt_collection.set_selected_index(1).is_ok(), "Should be able to set the selected index of an option collection if it is valid.");
assert_eq!(opt_collection.selected_index().unwrap(), 1, "The second option should be selected in our option collection.");
opt_collection.set_length(1234);
assert_eq!(opt_collection.length(), 1234, "Our option collections length should update after being set to 1234.");
}

View File

@ -0,0 +1,41 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlOutputElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_output() -> HtmlOutputElement;
}
#[wasm_bindgen_test]
fn test_output_element() {
let output = new_output();
assert!(output.html_for().length() == 0, "Our basic <output> should have no html associated with it.");
assert!(output.form().is_none(), "Our basic <output> should have no form associated with it.");
output.set_name("Calculation result");
assert_eq!(output.name(), "Calculation result", "Output name should be 'Calculation result'.");
assert_eq!(output.type_(), "output", "Our basic <output> should have an type of 'output'.");
output.set_default_value("27");
assert_eq!(output.default_value(), "27", "Default output value should be '27'.");
output.set_value("49");
assert_eq!(output.value(), "49", "Output value should be '49'.");
// TODO: Fails in Chrome, but not in Firefox.
//assert!(output.will_validate(), "Output should validate by default (maybe browser dependent?)");
assert!(output.validity().valid(), "Our <output>s validity should be true.");
assert!(output.validation_message().is_ok(), "We should be able to retrieve some validation message from our <output>.");
assert!(output.check_validity(), "Our <output> should be valid.");
assert!(output.report_validity(), "Our <output> should report valid.");
output.set_custom_validity("Some scary error message.");
assert!(output.labels().length() == 0, "Our basic <output> shouldn't have any labels associated with it.");
}

View File

@ -0,0 +1,15 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlParagraphElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_paragraph() -> HtmlParagraphElement;
}
#[wasm_bindgen_test]
fn test_paragraph_element() {
let paragraph = new_paragraph();
paragraph.set_align("right");
assert_eq!(paragraph.align(), "right", "Paragraph should be aligned 'right'.");
}

View File

@ -0,0 +1,24 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlParamElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_param() -> HtmlParamElement;
}
#[wasm_bindgen_test]
fn test_param_element() {
let param = new_param();
param.set_name("color");
assert_eq!(param.name(), "color", "Name of param should be 'color'.");
param.set_value("purple");
assert_eq!(param.value(), "purple", "Value of param should be 'purple'.");
param.set_value_type("ref");
assert_eq!(param.value_type(), "ref", "Value type of param should be 'ref'.");
param.set_type("text/plain");
assert_eq!(param.type_(), "text/plain", "Value of param should be 'text/plain'.");
}

View File

@ -0,0 +1,15 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlPreElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_pre() -> HtmlPreElement;
}
#[wasm_bindgen_test]
fn test_pre_element() {
let pre = new_pre();
pre.set_width(150);
assert_eq!(pre.width(), 150, "Pre width should be 150.");
}

View File

@ -0,0 +1,21 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlProgressElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_progress() -> HtmlProgressElement;
}
#[wasm_bindgen_test]
fn test_progress_element() {
let progress = new_progress();
progress.set_max(150.5);
assert_eq!(progress.max(), 150.5, "Maximum progress value should be 150.5.");
progress.set_value(22.);
assert_eq!(progress.value(), 22., "Progress value should be 22 units.");
assert_eq!(progress.position(), (22. / 150.5), "Progress position should be 22 divided by the max possible value.");
assert!(progress.labels().length() == 0, "Our simple progress bar shouldn't be associated with any labels.");
}

View File

@ -0,0 +1,15 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlQuoteElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_quote() -> HtmlQuoteElement;
}
#[wasm_bindgen_test]
fn test_quote_element() {
let quote = new_quote();
quote.set_cite("https://en.wikipedia.org/wiki/Rust_(programming_language)");
assert_eq!(quote.cite(), "https://en.wikipedia.org/wiki/Rust_(programming_language)");
}

View File

@ -0,0 +1,16 @@
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::HtmlSlotElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_slot() -> HtmlSlotElement;
}
#[wasm_bindgen_test]
fn test_slot_element() {
let _slot = new_slot();
// TODO: Test fails in Firefox, but not in Chrome. Error in Firefox is 'ReferenceError: HTMLSlotElement is not defined'. https://w3c-test.org/shadow-dom/HTMLSlotElement-interface.html
// slot.set_name("root_separator");
// assert_eq!(slot.name(), "root_separator", "Slot name should 'root_separator'.");
}