From 73fed12ba860d3247a8f1a5c13f72e8d28e4848c Mon Sep 17 00:00:00 2001 From: Charles Johnson Date: Sun, 7 Nov 2021 22:09:54 +0900 Subject: [PATCH 1/3] Setup tests for website --- zia-lang.org/crate/Cargo.toml | 7 ++++ zia-lang.org/crate/src/lib.rs | 56 +++++++++++++++++++++++++++++++ zia-lang.org/crate/tests/test.rs | 7 ---- zia-lang.org/crate/webdriver.json | 9 +++++ 4 files changed, 72 insertions(+), 7 deletions(-) delete mode 100644 zia-lang.org/crate/tests/test.rs create mode 100644 zia-lang.org/crate/webdriver.json diff --git a/zia-lang.org/crate/Cargo.toml b/zia-lang.org/crate/Cargo.toml index 902c870c..60de6d1f 100644 --- a/zia-lang.org/crate/Cargo.toml +++ b/zia-lang.org/crate/Cargo.toml @@ -26,6 +26,13 @@ features = [ "KeyboardEvent" ] +[dev-dependencies.web-sys] +features = [ + "Window", + "Document", + "Element" +] + [profile.release] lto = true opt-level = 's' diff --git a/zia-lang.org/crate/src/lib.rs b/zia-lang.org/crate/src/lib.rs index 31764609..b56fb9af 100644 --- a/zia-lang.org/crate/src/lib.rs +++ b/zia-lang.org/crate/src/lib.rs @@ -144,3 +144,59 @@ pub fn run() { log!("App started."); } + +#[cfg(test)] +mod test { + extern crate wasm_bindgen_test; + use super::run; + use seed::window; + use wasm_bindgen_test::*; + + wasm_bindgen_test_configure!(run_in_browser); + + fn load_page() { + // Need to insert an element with id="app" to mimic the loading screen + // defined in ../../entries/index.hbs + let document = window().document().unwrap(); + let section = document.create_element("section").unwrap(); + section.set_id("app"); + document.body().unwrap().append_with_node_1(§ion).unwrap(); + + // Now we can run the WASM part + run(); + } + + #[wasm_bindgen_test] + fn keyboard_input_is_displayed_textarea_element() { + load_page() + // TODO: simulate keyboard input + + // TODO: assert that text is displayed in textarea element + } + + #[wasm_bindgen_test] + fn submitted_command_moves_from_textarea_to_history() { + load_page() + // TODO: focus textarea + + // TODO: simulate typing text + + // TODO: simulate pressing "Enter" + + // TODO: assert textarea is empty + + // TODO: assert text submitted appears in an element above textarea + } + + #[wasm_bindgen_test] + fn height_of_textarea_does_not_change_when_typing_one_line() { + load_page() + // TODO: get the height of the textarea element + + // TODO: simulate typing text + + // TODO: get the height of the textarea element + + // TODO: assert height of textarea element has not changed + } +} diff --git a/zia-lang.org/crate/tests/test.rs b/zia-lang.org/crate/tests/test.rs deleted file mode 100644 index 3ce8516a..00000000 --- a/zia-lang.org/crate/tests/test.rs +++ /dev/null @@ -1,7 +0,0 @@ -extern crate wasm_bindgen_test; -use wasm_bindgen_test::*; - -#[wasm_bindgen_test] -fn pass() { - assert_eq!(1, 1); -} diff --git a/zia-lang.org/crate/webdriver.json b/zia-lang.org/crate/webdriver.json new file mode 100644 index 00000000..76e9221e --- /dev/null +++ b/zia-lang.org/crate/webdriver.json @@ -0,0 +1,9 @@ +{ + "moz:firefoxOptions": { + "prefs": { + "media.navigator.streams.fake": true, + "media.navigator.permission.disabled": true + }, + "args": [] + } +} \ No newline at end of file From 503893ab7913aba4b8cef9fa2d61bfad9766fb23 Mon Sep 17 00:00:00 2001 From: Charles Johnson Date: Thu, 18 Nov 2021 21:54:57 +0900 Subject: [PATCH 2/3] Add required lifetime bound for GATs --- zia/src/concepts/trait.rs | 8 ++++++-- zia/src/context_snap_shot/concept.rs | 11 ++++++++--- zia/src/snap_shot.rs | 4 +++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/zia/src/concepts/trait.rs b/zia/src/concepts/trait.rs index 625b0ff6..cfb660ae 100644 --- a/zia/src/concepts/trait.rs +++ b/zia/src/concepts/trait.rs @@ -18,8 +18,12 @@ use super::{ pub trait Concept: Sized { type Id: Copy + Display + Eq + Hash + Debug; - type IdPairIterator<'a>: Iterator; - type IdIterator<'a>: Iterator; + type IdPairIterator<'a>: Iterator + where + Self: 'a; + type IdIterator<'a>: Iterator + where + Self: 'a; fn id(&self) -> Self::Id; fn maybe_composition(&self) -> Option>; diff --git a/zia/src/context_snap_shot/concept.rs b/zia/src/context_snap_shot/concept.rs index 250aff6b..5de49afd 100644 --- a/zia/src/context_snap_shot/concept.rs +++ b/zia/src/context_snap_shot/concept.rs @@ -61,9 +61,14 @@ impl<'a, 'b> From<&'a NewDirectConceptDelta> impl<'a> ConceptTrait for Mixed<'a> { type Id = ConceptId; - type IdIterator<'b> = Box + 'b>; - type IdPairIterator<'b> = - Box + 'b>; + type IdIterator<'b> + where + Self: 'b, + = Box + 'b>; + type IdPairIterator<'b> + where + Self: 'b, + = Box + 'b>; fn id(&self) -> Self::Id { match self { diff --git a/zia/src/snap_shot.rs b/zia/src/snap_shot.rs index a4640880..f99d3c77 100644 --- a/zia/src/snap_shot.rs +++ b/zia/src/snap_shot.rs @@ -37,7 +37,9 @@ where + Debug + for<'b> From< &'b NewDirectConceptDelta, - >; + > + where + Self: 'a; fn get_concept( &self, concept_id: Self::ConceptId, From 6fef2ac2f5c59c82c71e695459f516056a4c3c3e Mon Sep 17 00:00:00 2001 From: Charles Johnson Date: Thu, 18 Nov 2021 21:56:39 +0900 Subject: [PATCH 3/3] Try to test keyboard input appears in textarea --- zia-lang.org/crate/Cargo.toml | 8 +++++++- zia-lang.org/crate/src/lib.rs | 27 ++++++++++++++++++++------- zia-lang.org/crate/src/page/home.rs | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/zia-lang.org/crate/Cargo.toml b/zia-lang.org/crate/Cargo.toml index 60de6d1f..5371f5b1 100644 --- a/zia-lang.org/crate/Cargo.toml +++ b/zia-lang.org/crate/Cargo.toml @@ -27,10 +27,16 @@ features = [ ] [dev-dependencies.web-sys] +version = "^0.3.45" features = [ "Window", "Document", - "Element" + "Element", + "Event", + "EventTarget", + "EventInit", + "KeyboardEventInit", + "HtmlTextAreaElement" ] [profile.release] diff --git a/zia-lang.org/crate/src/lib.rs b/zia-lang.org/crate/src/lib.rs index b56fb9af..c7ff396d 100644 --- a/zia-lang.org/crate/src/lib.rs +++ b/zia-lang.org/crate/src/lib.rs @@ -149,8 +149,9 @@ pub fn run() { mod test { extern crate wasm_bindgen_test; use super::run; - use seed::window; + use seed::{prelude::JsCast, window}; use wasm_bindgen_test::*; + use web_sys::{HtmlTextAreaElement, KeyboardEvent, KeyboardEventInit}; wasm_bindgen_test_configure!(run_in_browser); @@ -168,15 +169,27 @@ mod test { #[wasm_bindgen_test] fn keyboard_input_is_displayed_textarea_element() { - load_page() - // TODO: simulate keyboard input - - // TODO: assert that text is displayed in textarea element + load_page(); + let keyboard_event = KeyboardEvent::new_with_keyboard_event_init_dict( + "KeyboardEvent", + KeyboardEventInit::new().key("x"), + ) + .unwrap(); + window().document().unwrap().dispatch_event(&keyboard_event).unwrap(); + let command_input: HtmlTextAreaElement = JsCast::dyn_into( + window() + .document() + .unwrap() + .get_element_by_id("command_input") + .unwrap(), + ) + .unwrap(); + assert_eq!(command_input.value(), "x"); } #[wasm_bindgen_test] fn submitted_command_moves_from_textarea_to_history() { - load_page() + load_page(); // TODO: focus textarea // TODO: simulate typing text @@ -190,7 +203,7 @@ mod test { #[wasm_bindgen_test] fn height_of_textarea_does_not_change_when_typing_one_line() { - load_page() + load_page(); // TODO: get the height of the textarea element // TODO: simulate typing text diff --git a/zia-lang.org/crate/src/page/home.rs b/zia-lang.org/crate/src/page/home.rs index bc1e3511..45068906 100644 --- a/zia-lang.org/crate/src/page/home.rs +++ b/zia-lang.org/crate/src/page/home.rs @@ -82,7 +82,7 @@ pub fn view(model: &Model) -> impl IntoNodes { C.outline_none, C.overflow_hidden ], - attrs! {At::Type => "text", At::Name => "input"}, + attrs! {At::Type => "text", At::Name => "input", At::Id => "command_input"}, style! {St::Resize => "none", St::Height => model.command_input.get().map_or_else( // flatten textarea on first render to prevent it being // too tall on subsequent renders