From 38f4054f97e1947427d43072a46b0e18e6bac41b Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sun, 26 Jan 2020 17:36:16 +0800 Subject: [PATCH] Move tests (#897) --- src/format/json.rs | 24 +++ src/virtual_dom/vcomp.rs | 64 ++++++ src/virtual_dom/vlist.rs | 42 ++++ src/virtual_dom/vtag.rs | 411 +++++++++++++++++++++++++++++++++++++++ src/virtual_dom/vtext.rs | 40 ++++ tests/format_test.rs | 19 -- tests/vcomp_test.rs | 60 ------ tests/vlist_test.rs | 38 ---- tests/vtag_test.rs | 409 -------------------------------------- tests/vtext_test.rs | 36 ---- 10 files changed, 581 insertions(+), 562 deletions(-) delete mode 100644 tests/format_test.rs delete mode 100644 tests/vcomp_test.rs delete mode 100644 tests/vlist_test.rs delete mode 100644 tests/vtag_test.rs delete mode 100644 tests/vtext_test.rs diff --git a/src/format/json.rs b/src/format/json.rs index c9473d500aa..ce9f0a826c9 100644 --- a/src/format/json.rs +++ b/src/format/json.rs @@ -18,3 +18,27 @@ pub struct Json(pub T); text_format!(Json based on serde_json); binary_format!(Json based on serde_json); + +#[cfg(test)] +mod tests { + use super::*; + use crate::format::{Binary, Text}; + use serde::{Deserialize, Serialize}; + #[cfg(feature = "wasm_test")] + use wasm_bindgen_test::wasm_bindgen_test as test; + + #[test] + fn json_format() { + #[derive(Serialize, Deserialize)] + struct Data { + value: u8, + } + + let Json(data): Json> = Json::from(Ok(r#"{"value": 123}"#.to_string())); + let data = data.unwrap(); + assert_eq!(data.value, 123); + + let _stored: Text = Json(&data).into(); + let _stored: Binary = Json(&data).into(); + } +} diff --git a/src/virtual_dom/vcomp.rs b/src/virtual_dom/vcomp.rs index 0cfe773ea37..ea2c3b39eb3 100644 --- a/src/virtual_dom/vcomp.rs +++ b/src/virtual_dom/vcomp.rs @@ -293,3 +293,67 @@ impl fmt::Debug for VChild { f.write_str("VChild<_>") } } + +#[cfg(test)] +mod tests { + use crate::macros::Properties; + use crate::{html, Component, ComponentLink, Html, ShouldRender}; + #[cfg(feature = "wasm_test")] + use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; + + #[cfg(feature = "wasm_test")] + wasm_bindgen_test_configure!(run_in_browser); + + struct Comp; + + #[derive(Clone, PartialEq, Properties)] + struct Props { + field_1: u32, + field_2: u32, + } + + impl Component for Comp { + type Message = (); + type Properties = Props; + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + Comp + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!(); + } + + fn view(&self) -> Html { + unimplemented!(); + } + } + + #[test] + fn set_properties_to_component() { + html! { + + }; + + html! { + + }; + + html! { + + }; + + html! { + + }; + + let props = Props { + field_1: 1, + field_2: 1, + }; + + html! { + + }; + } +} diff --git a/src/virtual_dom/vlist.rs b/src/virtual_dom/vlist.rs index 5748d4a1b00..7e668d479af 100644 --- a/src/virtual_dom/vlist.rs +++ b/src/virtual_dom/vlist.rs @@ -115,3 +115,45 @@ impl VDiff for VList { previous_sibling } } + +#[cfg(test)] +mod tests { + use crate::{html, Component, ComponentLink, Html, ShouldRender}; + #[cfg(feature = "wasm_test")] + use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; + + #[cfg(feature = "wasm_test")] + wasm_bindgen_test_configure!(run_in_browser); + + struct Comp; + + impl Component for Comp { + type Message = (); + type Properties = (); + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + Comp + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!(); + } + + fn view(&self) -> Html { + unimplemented!(); + } + } + + #[test] + fn check_fragments() { + let fragment = html! { + <> + + }; + html! { +
+ { fragment } +
+ }; + } +} diff --git a/src/virtual_dom/vtag.rs b/src/virtual_dom/vtag.rs index cd9f9729118..d993236b1ba 100644 --- a/src/virtual_dom/vtag.rs +++ b/src/virtual_dom/vtag.rs @@ -537,3 +537,414 @@ where from.clone() } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{html, Component, ComponentLink, Html, ShouldRender}; + use stdweb::web::{document, IElement}; + #[cfg(feature = "wasm_test")] + use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; + + #[cfg(feature = "wasm_test")] + wasm_bindgen_test_configure!(run_in_browser); + + struct Comp; + + impl Component for Comp { + type Message = (); + type Properties = (); + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + Comp + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!(); + } + + fn view(&self) -> Html { + unimplemented!(); + } + } + + struct CompInt; + + impl Component for CompInt { + type Message = u32; + type Properties = (); + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + CompInt + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!(); + } + + fn view(&self) -> Html { + unimplemented!(); + } + } + + struct CompBool; + + impl Component for CompBool { + type Message = bool; + type Properties = (); + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + CompBool + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!(); + } + + fn view(&self) -> Html { + unimplemented!(); + } + } + + #[test] + fn it_compares_tags() { + let a = html! { +
+ }; + + let b = html! { +
+ }; + + let c = html! { +

+ }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_compares_text() { + let a = html! { +
{ "correct" }
+ }; + + let b = html! { +
{ "correct" }
+ }; + + let c = html! { +
{ "incorrect" }
+ }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_compares_attributes() { + let a = html! { +
+ }; + + let b = html! { +
+ }; + + let c = html! { +
+ }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_compares_children() { + let a = html! { +
+

+
+ }; + + let b = html! { +
+

+
+ }; + + let c = html! { +
+ +
+ }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_compares_classes() { + let a = html! { +
+ }; + + let b = html! { +
+ }; + + let c = html! { +
+ }; + + let d = html! { +
+ }; + + assert_eq!(a, b); + assert_ne!(a, c); + assert_eq!(c, d); + } + + #[test] + fn classes_from_local_variables() { + let a = html! { +
+ }; + + let class_2 = "class-2"; + let b = html! { +
+ }; + + let class_2_fmt = format!("class-{}", 2); + let c = html! { +
+ }; + + assert_eq!(a, b); + assert_eq!(a, c); + } + + #[test] + fn supports_multiple_classes_string() { + let a = html! { +
+ }; + + let b = html! { +
+ }; + + assert_ne!(a, b); + + if let VNode::VTag(vtag) = a { + println!("{:?}", vtag.classes); + assert!(vtag.classes.contains("class-1")); + assert!(vtag.classes.contains("class-2")); + assert!(vtag.classes.contains("class-3")); + } else { + panic!("vtag expected"); + } + } + + #[test] + fn supports_multiple_classes_vec() { + let mut classes = vec!["class-1"]; + classes.push("class-2"); + let a = html! { +
+ }; + + if let VNode::VTag(vtag) = a { + println!("{:?}", vtag.classes); + assert!(vtag.classes.contains("class-1")); + assert!(vtag.classes.contains("class-2")); + assert!(!vtag.classes.contains("class-3")); + } else { + panic!("vtag expected"); + } + } + + #[test] + fn filter_empty_string_classes_vec() { + let mut classes = vec![""]; + classes.push("class-2"); + let a = html! {
}; + let b = html! {
}; + let c = html! {
}; + + if let VNode::VTag(vtag) = a { + assert!(vtag.classes.is_empty()); + } else { + panic!("vtag expected"); + } + + if let VNode::VTag(vtag) = b { + assert!(vtag.classes.is_empty()); + } else { + panic!("vtag expected"); + } + + if let VNode::VTag(vtag) = c { + assert!(vtag.classes.is_empty()); + } else { + panic!("vtag expected"); + } + } + + fn assert_vtag(node: &mut VNode) -> &mut VTag { + if let VNode::VTag(vtag) = node { + return vtag; + } + panic!("should be vtag"); + } + + fn assert_namespace(vtag: &VTag, namespace: &'static str) { + assert_eq!( + vtag.reference.as_ref().unwrap().namespace_uri().unwrap(), + namespace + ); + } + + #[test] + fn supports_svg() { + let div_el = document().create_element("div").unwrap(); + let svg_el = document().create_element_ns(SVG_NAMESPACE, "svg").unwrap(); + + let mut g_node = html! { }; + let path_node = html! { }; + let mut svg_node = html! { {path_node} }; + + let svg_tag = assert_vtag(&mut svg_node); + svg_tag.apply(&div_el, None, None); + assert_namespace(svg_tag, SVG_NAMESPACE); + let path_tag = assert_vtag(svg_tag.children.get_mut(0).unwrap()); + assert_namespace(path_tag, SVG_NAMESPACE); + + let g_tag = assert_vtag(&mut g_node); + g_tag.apply(&div_el, None, None); + assert_namespace(g_tag, HTML_NAMESPACE); + g_tag.reference = None; + + g_tag.apply(&svg_el, None, None); + assert_namespace(g_tag, SVG_NAMESPACE); + } + + #[test] + fn keeps_order_of_classes() { + let a = html! { +
+ }; + + if let VNode::VTag(vtag) = a { + println!("{:?}", vtag.classes); + assert_eq!(vtag.classes.to_string(), "class-1 class-2 class-3"); + } + } + + #[test] + fn it_compares_values() { + let a = html! { + + }; + + let b = html! { + + }; + + let c = html! { + + }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_compares_kinds() { + let a = html! { + + }; + + let b = html! { + + }; + + let c = html! { + + }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_compares_checked() { + let a = html! { + + }; + + let b = html! { + + }; + + let c = html! { + + }; + + assert_eq!(a, b); + assert_ne!(a, c); + } + + #[test] + fn it_allows_aria_attributes() { + let a = html! { +

+ + +

+

+ }; + if let VNode::VTag(vtag) = a { + assert!(vtag.attributes.contains_key("aria-controls")); + assert_eq!( + vtag.attributes.get("aria-controls"), + Some(&"it-works".into()) + ); + } else { + panic!("vtag expected"); + } + } + + #[test] + fn it_checks_mixed_closing_tags() { + let a = html! {
}; + let b = html! {
}; + assert_eq!(a, b); + } + + #[test] + fn it_checks_misleading_gt() { + html! {
::default()>
}; + html! {
::default()>
}; + + html! { }; + html! { }; + } +} diff --git a/src/virtual_dom/vtext.rs b/src/virtual_dom/vtext.rs index 97357fcd5eb..40866c72f0d 100644 --- a/src/virtual_dom/vtext.rs +++ b/src/virtual_dom/vtext.rs @@ -101,3 +101,43 @@ impl PartialEq for VText { self.text == other.text } } + +#[cfg(test)] +mod test { + use crate::{html, Component, ComponentLink, Html, ShouldRender}; + #[cfg(feature = "wasm_test")] + use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; + + #[cfg(feature = "wasm_test")] + wasm_bindgen_test_configure!(run_in_browser); + + struct Comp; + + impl Component for Comp { + type Message = (); + type Properties = (); + + fn create(_: Self::Properties, _: ComponentLink) -> Self { + Comp + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!(); + } + + fn view(&self) -> Html { + unimplemented!(); + } + } + + #[test] + fn text_as_root() { + html! { + "Text Node As Root" + }; + + html! { + { "Text Node As Root" } + }; + } +} diff --git a/tests/format_test.rs b/tests/format_test.rs deleted file mode 100644 index 6892a9ba8f9..00000000000 --- a/tests/format_test.rs +++ /dev/null @@ -1,19 +0,0 @@ -use serde::{Deserialize, Serialize}; -#[cfg(feature = "wasm_test")] -use wasm_bindgen_test::wasm_bindgen_test as test; -use yew::format::{Binary, Json, Text}; - -#[test] -fn json_format() { - #[derive(Serialize, Deserialize)] - struct Data { - value: u8, - } - - let Json(data): Json> = Json::from(Ok(r#"{"value": 123}"#.to_string())); - let data = data.unwrap(); - assert_eq!(data.value, 123); - - let _stored: Text = Json(&data).into(); - let _stored: Binary = Json(&data).into(); -} diff --git a/tests/vcomp_test.rs b/tests/vcomp_test.rs deleted file mode 100644 index f3e99470014..00000000000 --- a/tests/vcomp_test.rs +++ /dev/null @@ -1,60 +0,0 @@ -#[cfg(feature = "wasm_test")] -use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; -use yew::macros::Properties; -use yew::{html, Component, ComponentLink, Html, ShouldRender}; - -#[cfg(feature = "wasm_test")] -wasm_bindgen_test_configure!(run_in_browser); - -struct Comp; - -#[derive(Clone, PartialEq, Properties)] -struct Props { - field_1: u32, - field_2: u32, -} - -impl Component for Comp { - type Message = (); - type Properties = Props; - - fn create(_: Self::Properties, _: ComponentLink) -> Self { - Comp - } - - fn update(&mut self, _: Self::Message) -> ShouldRender { - unimplemented!(); - } - - fn view(&self) -> Html { - unimplemented!(); - } -} - -#[test] -fn set_properties_to_component() { - html! { - - }; - - html! { - - }; - - html! { - - }; - - html! { - - }; - - let props = Props { - field_1: 1, - field_2: 1, - }; - - html! { - - }; -} diff --git a/tests/vlist_test.rs b/tests/vlist_test.rs deleted file mode 100644 index 02bee958017..00000000000 --- a/tests/vlist_test.rs +++ /dev/null @@ -1,38 +0,0 @@ -#[cfg(feature = "wasm_test")] -use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; -use yew::{html, Component, ComponentLink, Html, ShouldRender}; - -#[cfg(feature = "wasm_test")] -wasm_bindgen_test_configure!(run_in_browser); - -struct Comp; - -impl Component for Comp { - type Message = (); - type Properties = (); - - fn create(_: Self::Properties, _: ComponentLink) -> Self { - Comp - } - - fn update(&mut self, _: Self::Message) -> ShouldRender { - unimplemented!(); - } - - fn view(&self) -> Html { - unimplemented!(); - } -} - -#[test] -fn check_fragments() { - let fragment = html! { - <> - - }; - html! { -
- { fragment } -
- }; -} diff --git a/tests/vtag_test.rs b/tests/vtag_test.rs deleted file mode 100644 index 94c6b687cb2..00000000000 --- a/tests/vtag_test.rs +++ /dev/null @@ -1,409 +0,0 @@ -#![recursion_limit = "128"] -use stdweb::web::{document, IElement}; -#[cfg(feature = "wasm_test")] -use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; -use yew::virtual_dom::vtag::{VTag, HTML_NAMESPACE, SVG_NAMESPACE}; -use yew::virtual_dom::{VDiff, VNode}; -use yew::{html, Component, ComponentLink, Html, ShouldRender}; - -#[cfg(feature = "wasm_test")] -wasm_bindgen_test_configure!(run_in_browser); - -struct Comp; - -impl Component for Comp { - type Message = (); - type Properties = (); - - fn create(_: Self::Properties, _: ComponentLink) -> Self { - Comp - } - - fn update(&mut self, _: Self::Message) -> ShouldRender { - unimplemented!(); - } - - fn view(&self) -> Html { - unimplemented!(); - } -} - -struct CompInt; - -impl Component for CompInt { - type Message = u32; - type Properties = (); - - fn create(_: Self::Properties, _: ComponentLink) -> Self { - CompInt - } - - fn update(&mut self, _: Self::Message) -> ShouldRender { - unimplemented!(); - } - - fn view(&self) -> Html { - unimplemented!(); - } -} - -struct CompBool; - -impl Component for CompBool { - type Message = bool; - type Properties = (); - - fn create(_: Self::Properties, _: ComponentLink) -> Self { - CompBool - } - - fn update(&mut self, _: Self::Message) -> ShouldRender { - unimplemented!(); - } - - fn view(&self) -> Html { - unimplemented!(); - } -} - -#[test] -fn it_compares_tags() { - let a = html! { -
- }; - - let b = html! { -
- }; - - let c = html! { -

- }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_compares_text() { - let a = html! { -
{ "correct" }
- }; - - let b = html! { -
{ "correct" }
- }; - - let c = html! { -
{ "incorrect" }
- }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_compares_attributes() { - let a = html! { -
- }; - - let b = html! { -
- }; - - let c = html! { -
- }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_compares_children() { - let a = html! { -
-

-
- }; - - let b = html! { -
-

-
- }; - - let c = html! { -
- -
- }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_compares_classes() { - let a = html! { -
- }; - - let b = html! { -
- }; - - let c = html! { -
- }; - - let d = html! { -
- }; - - assert_eq!(a, b); - assert_ne!(a, c); - assert_eq!(c, d); -} - -#[test] -fn classes_from_local_variables() { - let a = html! { -
- }; - - let class_2 = "class-2"; - let b = html! { -
- }; - - let class_2_fmt = format!("class-{}", 2); - let c = html! { -
- }; - - assert_eq!(a, b); - assert_eq!(a, c); -} - -#[test] -fn supports_multiple_classes_string() { - let a = html! { -
- }; - - let b = html! { -
- }; - - assert_ne!(a, b); - - if let VNode::VTag(vtag) = a { - println!("{:?}", vtag.classes); - assert!(vtag.classes.contains("class-1")); - assert!(vtag.classes.contains("class-2")); - assert!(vtag.classes.contains("class-3")); - } else { - panic!("vtag expected"); - } -} - -#[test] -fn supports_multiple_classes_vec() { - let mut classes = vec!["class-1"]; - classes.push("class-2"); - let a = html! { -
- }; - - if let VNode::VTag(vtag) = a { - println!("{:?}", vtag.classes); - assert!(vtag.classes.contains("class-1")); - assert!(vtag.classes.contains("class-2")); - assert!(!vtag.classes.contains("class-3")); - } else { - panic!("vtag expected"); - } -} - -#[test] -fn filter_empty_string_classes_vec() { - let mut classes = vec![""]; - classes.push("class-2"); - let a = html! {
}; - let b = html! {
}; - let c = html! {
}; - - if let VNode::VTag(vtag) = a { - assert!(vtag.classes.is_empty()); - } else { - panic!("vtag expected"); - } - - if let VNode::VTag(vtag) = b { - assert!(vtag.classes.is_empty()); - } else { - panic!("vtag expected"); - } - - if let VNode::VTag(vtag) = c { - assert!(vtag.classes.is_empty()); - } else { - panic!("vtag expected"); - } -} - -fn assert_vtag(node: &mut VNode) -> &mut VTag { - if let VNode::VTag(vtag) = node { - return vtag; - } - panic!("should be vtag"); -} - -fn assert_namespace(vtag: &VTag, namespace: &'static str) { - assert_eq!( - vtag.reference.as_ref().unwrap().namespace_uri().unwrap(), - namespace - ); -} - -#[test] -fn supports_svg() { - let div_el = document().create_element("div").unwrap(); - let svg_el = document().create_element_ns(SVG_NAMESPACE, "svg").unwrap(); - - let mut g_node = html! { }; - let path_node = html! { }; - let mut svg_node = html! { {path_node} }; - - let svg_tag = assert_vtag(&mut svg_node); - svg_tag.apply(&div_el, None, None); - assert_namespace(svg_tag, SVG_NAMESPACE); - let path_tag = assert_vtag(svg_tag.children.get_mut(0).unwrap()); - assert_namespace(path_tag, SVG_NAMESPACE); - - let g_tag = assert_vtag(&mut g_node); - g_tag.apply(&div_el, None, None); - assert_namespace(g_tag, HTML_NAMESPACE); - g_tag.reference = None; - - g_tag.apply(&svg_el, None, None); - assert_namespace(g_tag, SVG_NAMESPACE); -} - -#[test] -fn keeps_order_of_classes() { - let a = html! { -
- }; - - if let VNode::VTag(vtag) = a { - println!("{:?}", vtag.classes); - assert_eq!(vtag.classes.to_string(), "class-1 class-2 class-3"); - } -} - -#[test] -fn it_compares_values() { - let a = html! { - - }; - - let b = html! { - - }; - - let c = html! { - - }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_compares_kinds() { - let a = html! { - - }; - - let b = html! { - - }; - - let c = html! { - - }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_compares_checked() { - let a = html! { - - }; - - let b = html! { - - }; - - let c = html! { - - }; - - assert_eq!(a, b); - assert_ne!(a, c); -} - -#[test] -fn it_allows_aria_attributes() { - let a = html! { -

- - -

-

- }; - if let VNode::VTag(vtag) = a { - assert!(vtag.attributes.contains_key("aria-controls")); - assert_eq!( - vtag.attributes.get("aria-controls"), - Some(&"it-works".into()) - ); - } else { - panic!("vtag expected"); - } -} - -#[test] -fn it_checks_mixed_closing_tags() { - let a = html! {
}; - let b = html! {
}; - assert_eq!(a, b); -} - -#[test] -fn it_checks_misleading_gt() { - html! {
::default()>
}; - html! {
::default()>
}; - - html! { }; - html! { }; -} diff --git a/tests/vtext_test.rs b/tests/vtext_test.rs deleted file mode 100644 index 25d85a9baeb..00000000000 --- a/tests/vtext_test.rs +++ /dev/null @@ -1,36 +0,0 @@ -#[cfg(feature = "wasm_test")] -use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; -use yew::{html, Component, ComponentLink, Html, ShouldRender}; - -#[cfg(feature = "wasm_test")] -wasm_bindgen_test_configure!(run_in_browser); - -struct Comp; - -impl Component for Comp { - type Message = (); - type Properties = (); - - fn create(_: Self::Properties, _: ComponentLink) -> Self { - Comp - } - - fn update(&mut self, _: Self::Message) -> ShouldRender { - unimplemented!(); - } - - fn view(&self) -> Html { - unimplemented!(); - } -} - -#[test] -fn text_as_root() { - html! { - "Text Node As Root" - }; - - html! { - { "Text Node As Root" } - }; -}