From 1d26d9c1469279b99385d49c334a119a1fa8932f Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Thu, 23 May 2024 11:18:45 -0400 Subject: [PATCH] Add html macro open tag parse tag (#198) This commit adds a test to the `html-macro`. The test verifies that we parse opening tags correctly, such as ```
``` This test was introduced to help others understand how to test the macro's internals. Being able to test the more complex aspects of the macro's internals helps with getting quicker feedback when working on the internals. --- .cargo/{config => config.toml} | 0 .github/workflows/ci-tests.yml | 62 +++-- crates/html-macro-test/src/tests/all_tests.rs | 164 +++++++------ crates/html-macro-test/src/tests/text.rs | 228 +++++++++--------- crates/html-macro/Cargo.toml | 4 +- crates/html-macro/src/parser/statement.rs | 4 +- crates/html-macro/src/tag.rs | 97 +++++++- crates/percy-css-macro/Cargo.toml | 4 +- crates/percy-css-macro/src/lib.rs | 2 +- crates/percy-dom/tests/text.rs | 36 ++- .../src/book_example.rs | 4 +- crates/percy-router-macro/Cargo.toml | 6 +- .../src/create_routes_macro.rs | 2 +- crates/percy-router-macro/src/route_macro.rs | 10 +- .../virtual-node/src/event/virtual_events.rs | 6 +- examples/isomorphic/server/Cargo.toml | 10 +- .../isomorphic/server/src/actix_server.rs | 21 +- examples/isomorphic/server/src/main.rs | 5 +- examples/unit-testing-components/src/main.rs | 2 +- 19 files changed, 400 insertions(+), 267 deletions(-) rename .cargo/{config => config.toml} (100%) diff --git a/.cargo/config b/.cargo/config.toml similarity index 100% rename from .cargo/config rename to .cargo/config.toml diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 9e79ce44..c303fd2b 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -11,41 +11,39 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - uses: actions-rs/cargo@v1 + - uses: actions/checkout@v2 - - name: Latest Rust Nightly - uses: actions-rs/toolchain@v1 - with: + - uses: actions-rs/toolchain@v1 + with: toolchain: nightly override: true # Used when testing with `wasm-pack test` target: wasm32-unknown-unknown - - name: Rust Version Info - run: rustc --version && cargo --version && echo $CARGO_HOME - - - name: Install wasm-pack - run: > - curl -L https://github.com/rustwasm/wasm-pack/releases/download/v0.10.0/wasm-pack-v0.10.0-x86_64-unknown-linux-musl.tar.gz - | tar --strip-components=1 --wildcards -xzf - "*/wasm-pack" - && chmod +x wasm-pack - && mv wasm-pack $HOME/.cargo/bin/ - - - name: Browser versions - run: wasm-pack --version && firefox --version - - - uses: actions/cache@v2 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - target - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - - name: Run all tests - run: ./test.sh - - # NOTE: We can start running the full test suite on stable after https://github.com/rust-lang/rust/issues/54725 - - name: Check that Stable can compile - run: cargo +stable check --all + - name: Rust Version Info + run: rustc --version && cargo --version && echo $CARGO_HOME + + - name: Install wasm-pack + run: > + curl -L https://github.com/rustwasm/wasm-pack/releases/download/v0.10.0/wasm-pack-v0.10.0-x86_64-unknown-linux-musl.tar.gz + | tar --strip-components=1 --wildcards -xzf - "*/wasm-pack" + && chmod +x wasm-pack + && mv wasm-pack $HOME/.cargo/bin/ + + - name: Browser versions + run: wasm-pack --version && firefox --version + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Run all tests + run: ./test.sh + + # NOTE: We can start running the full test suite on stable after https://github.com/rust-lang/rust/issues/54725 + - name: Check that Stable can compile + run: cargo +stable check --all diff --git a/crates/html-macro-test/src/tests/all_tests.rs b/crates/html-macro-test/src/tests/all_tests.rs index 39fab197..dcbac239 100644 --- a/crates/html-macro-test/src/tests/all_tests.rs +++ b/crates/html-macro-test/src/tests/all_tests.rs @@ -9,6 +9,7 @@ use html_macro::html; use std::collections::HashMap; use virtual_node::{IterableNodes, VElement, VText, View, VirtualNode}; +#[must_use] pub(crate) struct HtmlMacroTest { pub generated: VirtualNode, pub expected: VirtualNode, @@ -17,7 +18,7 @@ pub(crate) struct HtmlMacroTest { impl HtmlMacroTest { /// Ensure that the generated and the expected virtual node are equal. pub fn test(self) { - assert_eq!(self.expected, self.generated); + assert_eq!(self.generated, self.expected); } } @@ -27,7 +28,7 @@ fn empty_div() { generated: html! {
}, expected: VirtualNode::element("div"), } - .test(); + .test(); } #[test] @@ -42,7 +43,7 @@ fn one_attr() { generated: html! {
}, expected: expected.into(), } - .test(); + .test(); } #[test] @@ -54,7 +55,7 @@ fn child_node() { generated: html! {
}, expected: expected.into(), } - .test(); + .test(); } #[test] @@ -66,7 +67,7 @@ fn sibling_child_nodes() { generated: html! {
}, expected: expected.into(), } - .test(); + .test(); } /// Nested 3 nodes deep @@ -82,20 +83,22 @@ fn three_nodes_deep() { generated: html! {
}, expected: expected.into(), } - .test() + .test() } -#[test] -fn sibling_text_nodes() { - let mut expected = VElement::new("div"); - expected.children = vec![VirtualNode::text("This is a text node")]; - HtmlMacroTest { - generated: html! {
This is a text node
}, - expected: expected.into(), - } - .test(); -} +// TODO: Requires proc macro APIs that are currently unstable - https://github.com/rust-lang/rust/issues/54725 +// #[test] +// fn sibling_text_nodes() { +// let mut expected = VElement::new("div"); +// expected.children = vec![VirtualNode::text("This is a text node")]; +// +// HtmlMacroTest { +// generated: html! {
This is a text node
}, +// expected: expected.into(), +// } +// .test(); +// } #[test] fn nested_macro() { @@ -113,7 +116,7 @@ fn nested_macro() { }, expected: expected.into(), } - .test(); + .test(); } /// If the first thing we see is a block then we grab whatever is inside it. @@ -129,44 +132,46 @@ fn block_root() { }, expected, } - .test(); -} - -/// Text followed by a block -#[test] -fn text_next_to_block() { - let child = html! { }; - - let mut expected = VElement::new("div"); - expected.children = vec![ - VirtualNode::text(" A bit of text "), - VirtualNode::element("ul"), - ]; - - HtmlMacroTest { - generated: html! { -
- A bit of text - { child } -
- }, - expected: expected.into(), - } - .test(); + .test(); } -/// Ensure that we maintain the correct spacing around punctuation tokens, since -/// they resolve into a separate TokenStream during parsing. -#[test] -fn punctuation_token() { - let text = "Hello, World"; - - HtmlMacroTest { - generated: html! { Hello, World }, - expected: VirtualNode::text(text), - } - .test() -} +// TODO: Requires proc macro APIs that are currently unstable - https://github.com/rust-lang/rust/issues/54725 +// /// Text followed by a block +// #[test] +// fn text_next_to_block() { +// let child = html! { }; +// +// let mut expected = VElement::new("div"); +// expected.children = vec![ +// VirtualNode::text(" A bit of text "), +// VirtualNode::element("ul"), +// ]; +// +// HtmlMacroTest { +// generated: html! { +//
+// A bit of text +// { child } +//
+// }, +// expected: expected.into(), +// } +// .test(); +// } + +// TODO: Requires proc macro APIs that are currently unstable - https://github.com/rust-lang/rust/issues/54725 +// /// Ensure that we maintain the correct spacing around punctuation tokens, since +// /// they resolve into a separate TokenStream during parsing. +// #[test] +// fn punctuation_token() { +// let text = "Hello, World"; +// +// HtmlMacroTest { +// generated: html! { Hello, World }, +// expected: VirtualNode::text(text), +// } +// .test() +// } #[test] fn vec_of_nodes() { @@ -179,17 +184,22 @@ fn vec_of_nodes() { generated: html! {
{ children }
}, expected: expected.into(), } - .test(); + .test(); } /// Just make sure that this compiles since as, async, for, loop, and type are keywords #[test] fn keyword_attribute() { - html! { }; - html! {