From 3d3bf5dc839db9a1ef5cdcf180fa346320196d83 Mon Sep 17 00:00:00 2001 From: YUyz <569812520@qq.com> Date: Fri, 3 Aug 2018 22:02:31 +0800 Subject: [PATCH] add test for HTMLTableElement in web-sys crate (#629) --- crates/web-sys/README.md | 2 +- crates/web-sys/tests/wasm/element.js | 16 ++ crates/web-sys/tests/wasm/main.rs | 1 + crates/web-sys/tests/wasm/table_element.rs | 179 +++++++++++++++++++++ 4 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 crates/web-sys/tests/wasm/table_element.rs diff --git a/crates/web-sys/README.md b/crates/web-sys/README.md index 9241f6824..bfd949f55 100644 --- a/crates/web-sys/README.md +++ b/crates/web-sys/README.md @@ -243,7 +243,7 @@ bindings are fully working and have full test coverage. - [ ] HTMLTableCaptionElement.webidl - [ ] HTMLTableCellElement.webidl - [ ] HTMLTableColElement.webidl - - [ ] HTMLTableElement.webidl + - [x] HTMLTableElement.webidl - [ ] HTMLTableRowElement.webidl - [ ] HTMLTableSectionElement.webidl - [ ] HTMLTemplateElement.webidl diff --git a/crates/web-sys/tests/wasm/element.js b/crates/web-sys/tests/wasm/element.js index 583fca651..ac12195e9 100644 --- a/crates/web-sys/tests/wasm/element.js +++ b/crates/web-sys/tests/wasm/element.js @@ -14,6 +14,10 @@ export function new_button() { return document.createElement("button"); } +export function new_caption() { + return document.createElement("caption"); +} + export function new_del() { return document.createElement("del"); } @@ -115,6 +119,18 @@ export function new_style() { return document.createElement("style"); } +export function new_table() { + return document.createElement("table"); +} + +export function new_tfoot() { + return document.createElement("tfoot"); +} + +export function new_thead() { + return document.createElement("thead"); +} + export function new_title() { return document.createElement("title"); } diff --git a/crates/web-sys/tests/wasm/main.rs b/crates/web-sys/tests/wasm/main.rs index 4d3864938..1d6a2a035 100644 --- a/crates/web-sys/tests/wasm/main.rs +++ b/crates/web-sys/tests/wasm/main.rs @@ -42,5 +42,6 @@ pub mod script_element; pub mod slot_element; pub mod span_element; pub mod style_element; +pub mod table_element; pub mod title_element; pub mod xpath_result; diff --git a/crates/web-sys/tests/wasm/table_element.rs b/crates/web-sys/tests/wasm/table_element.rs new file mode 100644 index 000000000..98e04b74b --- /dev/null +++ b/crates/web-sys/tests/wasm/table_element.rs @@ -0,0 +1,179 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen_test::*; +use web_sys::{HtmlTableCaptionElement, HtmlTableElement, HtmlTableSectionElement}; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern "C" { + fn new_table() -> HtmlTableElement; + fn new_caption() -> HtmlTableCaptionElement; + fn new_thead() -> HtmlTableSectionElement; + fn new_tfoot() -> HtmlTableSectionElement; +} + +#[wasm_bindgen_test] +fn test_table_element() { + let table = new_table(); + assert!( + table.caption().is_none(), + "New table element should have no caption element." + ); + + table.create_caption(); + assert!( + table.caption().is_some(), + "Table element should have caption element after create caption." + ); + + table.delete_caption(); + assert!( + table.caption().is_none(), + "Table element should have no caption element after delete caption." + ); + + table.set_caption(Some(&new_caption())); + assert!( + table.caption().is_some(), + "Table element should have caption element after set." + ); + + assert!( + table.t_head().is_none(), + "New table element should have no thead element." + ); + + table.create_t_head(); + assert!( + table.t_head().is_some(), + "Table element should have thead element after create thead." + ); + + table.delete_t_head(); + assert!( + table.t_head().is_none(), + "Table element should have no thead element after delete thead." + ); + + table.set_t_head(Some(&new_thead())); + assert!( + table.t_head().is_some(), + "Table element should have thead element after set." + ); + + assert!( + table.t_foot().is_none(), + "New table element should have no tfoot element." + ); + + table.create_t_foot(); + assert!( + table.t_foot().is_some(), + "Table element should have tfoot element after create tfoot." + ); + + table.delete_t_foot(); + assert!( + table.t_foot().is_none(), + "Table element should have no tfoot element after delete tfoot." + ); + + table.set_t_foot(Some(&new_tfoot())); + assert!( + table.t_foot().is_some(), + "Table element should have tfoot element after set." + ); + + assert!( + table.t_bodies().length() == 0, + "New table element should have no tbody element." + ); + + table.create_t_body(); + assert!( + table.t_bodies().length() == 1, + "Table element should have tbody element after create tbody." + ); + + assert!( + table.rows().length() == 0, + "New table element should have no rows." + ); + + table + .insert_row(0) + .expect("Failed to insert row at index 0"); + assert!( + table.rows().length() == 1, + "Table element should have rows after insert row." + ); + + table + .delete_row(0) + .expect("Failed to delete row at index 0"); + assert!( + table.rows().length() == 0, + "Table element should have no rows after delete row." + ); + + table.set_align("left"); + assert_eq!( + table.align(), + "left", + "Table element should have an align property of 'left'" + ); + + table.set_border("10"); + assert_eq!( + table.border(), + "10", + "Table element should have a border property of '10'" + ); + + table.set_frame("above"); + assert_eq!( + table.frame(), + "above", + "Table element should have an frame property of 'above'" + ); + + table.set_rules("none"); + assert_eq!( + table.rules(), + "none", + "Table element should have an rules property of 'none'" + ); + + table.set_summary("summary"); + assert_eq!( + table.summary(), + "summary", + "Table element should have an summary property of 'summary'" + ); + + table.set_width("1000"); + assert_eq!( + table.width(), + "1000", + "Table element should have a width property of '1000'" + ); + + table.set_bg_color("#ffffff"); + assert_eq!( + table.bg_color(), + "#ffffff", + "Table element should have an bgColor property of '#ffffff'" + ); + + table.set_cell_padding("1"); + assert_eq!( + table.cell_padding(), + "1", + "Table element should have an cellPadding property of '1'" + ); + + table.set_cell_spacing("1"); + assert_eq!( + table.cell_spacing(), + "1", + "Table element should have an cellSpacing property of '1'" + ); +}