Skip to content

Commit

Permalink
Get text tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Mar 6, 2019
1 parent a78936c commit 09104b4
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 147 deletions.
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Writing html!](./html-macro/README.md)
- [Writing html!](./html-macro/html-macro.md)
- [Compile Time Errors](./html-macro/compile-time-errors.md)
- [Working with Text](./html-macro/text/README.md)
- [Virtual DOM](./virtual-dom/README.md)
- [Unit Testing your Views](./virtual-dom/unit-testing-views.md)
- [Server Side Rendering (SSR)](./views/server-side-rendering/README.md)
Expand Down
10 changes: 10 additions & 0 deletions book/src/html-macro/text/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Working with Text

Rather than needing you to wrap your text in `"` quotation marks,
the `html-macro` will work with raw unquoted text.

Here are some examples of how the `html!` macro works with text:

```rust
{{ #include ../../../../crates/html-macro-test/src/text.rs }}
```
128 changes: 2 additions & 126 deletions crates/html-macro-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![feature(proc_macro_hygiene)]
#![cfg(test)]

use html_macro::html;
use std::collections::HashMap;
use virtual_node::{IterableNodes, VElement, VirtualNode};

mod text;

struct HtmlMacroTest<'a> {
desc: &'a str,
generated: VirtualNode,
Expand Down Expand Up @@ -209,137 +210,12 @@ fn vec_of_nodes() {
.test();
}

// FIXME: Move text tests into a separate file and import into book under
// html! section

#[test]
fn text_root_node() {
HtmlMacroTest {
desc: "Text as root node",
generated: html! { some text },
expected: VirtualNode::text("some text"),
}
.test()
}

/// Just make sure that this compiles since type is a keyword
#[test]
fn type_attribute() {
html! { <link rel="stylesheet" type="text/css" href="/app.css" /> };
}

#[test]
fn text_variable_root() {
let text = "hello world";

HtmlMacroTest {
desc: "Text variable root",
generated: html! { { text } },
expected: VirtualNode::text("hello world"),
}
.test()
}

#[test]
fn text_variable_child() {
let text = "world";

assert_eq!(
&html! { <div>{ text }</div> }.to_string(),
"<div>world</div>"
)
}

#[test]
fn text_space_after_start_tag() {
assert_eq!(
&html! { <div> After Start Tag</div> }.to_string(),
"<div> After Start Tag</div>"
)
}

#[test]
fn text_space_before_end_tag() {
assert_eq!(
&html! { <div>Before End Tag </div> }.to_string(),
"<div>Before End Tag </div>"
)
}

#[test]
fn text_space_before_block() {
let text = "Before Block";

assert_eq!(
&html! { <div> {text}</div> }.to_string(),
"<div> Before Block</div>"
)
}

#[test]
fn text_space_after_block() {
let text = "Hello";

assert_eq!(
&html! { <div>{text} </div> }.to_string(),
"<div>Hello </div>"
)
}

#[test]
fn text_space_in_block_ignored() {
let text = "Hello";

assert_eq!(
&html! { <div>{ text }</div> }.to_string(),
"<div>Hello</div>"
)
}

#[test]
fn text_multiple_text_no_space_between() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div>{ hello }{ world }</div> }.to_string(),
"<div>HelloWorld</div>"
)
}

#[test]
fn text_multiple_text_space_between() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div>{ hello } { world }</div> }.to_string(),
"<div>Hello World</div>"
)
}

#[test]
fn text_multiple_text_space_around() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div> { hello }{ world } </div> }.to_string(),
"<div> HelloWorld </div>"
)
}

#[test]
fn text_multiple_text_space_between_around() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div> { hello } { world } </div> }.to_string(),
"<div> Hello World </div>"
)
}

// Verify that all of our self closing tags work as both.
// Self closing tags can be written as either <tag> and <tag />
#[test]
Expand Down
161 changes: 161 additions & 0 deletions crates/html-macro-test/src/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#![feature(proc_macro_hygiene)]

use crate::HtmlMacroTest;
use html_macro::html;
use std::collections::HashMap;
use virtual_node::{IterableNodes, VElement, VirtualNode};

#[test]
fn text_root_node() {
HtmlMacroTest {
desc: "Text as root node",
generated: html! { some text },
expected: VirtualNode::text("some text"),
}
.test()
}

#[test]
fn text_variable_root() {
let text = "hello world";

HtmlMacroTest {
desc: "Text variable root",
generated: html! { { text } },
expected: VirtualNode::text("hello world"),
}
.test()
}

#[test]
fn text_variable_child() {
let text = "world";

assert_eq!(
&html! { <div>{ text }</div> }.to_string(),
"<div>world</div>"
)
}

#[test]
fn text_space_after_start_tag() {
assert_eq!(
&html! { <div> After Start Tag</div> }.to_string(),
"<div> After Start Tag</div>"
)
}

#[test]
fn text_space_before_end_tag() {
assert_eq!(
&html! { <div>Before End Tag </div> }.to_string(),
"<div>Before End Tag </div>"
)
}

#[test]
fn text_space_before_block() {
let text = "Before Block";

assert_eq!(
&html! { <div> {text}</div> }.to_string(),
"<div> Before Block</div>"
)
}

#[test]
fn text_space_after_block() {
let text = "Hello";

assert_eq!(
&html! { <div>{text} </div> }.to_string(),
"<div>Hello </div>"
)
}

#[test]
fn text_space_in_block_ignored() {
let text = "Hello";

assert_eq!(
&html! { <div>{ text }</div> }.to_string(),
"<div>Hello</div>"
)
}

#[test]
fn text_multiple_text_no_space_between() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div>{ hello }{ world }</div> }.to_string(),
"<div>HelloWorld</div>"
)
}

#[test]
fn text_multiple_text_space_between() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div>{ hello } { world }</div> }.to_string(),
"<div>Hello World</div>"
)
}

#[test]
fn text_multiple_text_space_around() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div> { hello }{ world } </div> }.to_string(),
"<div> HelloWorld </div>"
)
}

#[test]
fn text_multiple_text_space_between_around() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div> { hello } { world } </div> }.to_string(),
"<div> Hello World </div>"
)
}

#[test]
fn text_tokens_in_between_vars_without_space() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div>{ hello }NoSpace{ world }</div> }.to_string(),
"<div>HelloNoSpaceWorld</div>"
)
}

#[test]
fn text_tokens_in_between_vars_with_space() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div>{ hello } Space { world }</div> }.to_string(),
"<div>Hello Space World</div>"
)
}

#[test]
fn text_tokens_in_between_vars_space_around_between() {
let hello = "Hello";
let world = "World";

assert_eq!(
&html! { <div> { hello } Space { world } </div> }.to_string(),
"<div> Hello Space World </div>"
)
}
2 changes: 1 addition & 1 deletion crates/html-macro/src/parser/braced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl HtmlParser {
if stmt_idx == 0 && !last_tag_kind_was_text && !last_tag_kind_was_brace {
if let Some(open_tag_end) = open_tag_end {
if open_tag_end.0 != brace_span.start().line
|| brace_span.start().column - open_tag_end.1 > 1
|| brace_span.start().column - open_tag_end.1 > 0
{
// FIXME BEFORE MERGE: Add a method to generate a new node that
// increments our node_idx. Pass a span to that method
Expand Down
2 changes: 1 addition & 1 deletion crates/html-macro/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl HtmlParser {
attrs,
closing_bracket_span,
} => {
self.parse_open_tag(name, attrs);
self.parse_open_tag(name, closing_bracket_span, attrs);
self.last_tag_kind = Some(TagKind::Open);
}
Tag::Close {
Expand Down
6 changes: 3 additions & 3 deletions crates/html-macro/src/parser/open_tag.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::parser::{is_self_closing, HtmlParser};
use crate::tag::Attr;
use proc_macro2::Ident;
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::Expr;

impl HtmlParser {
/// Parse an incoming Tag::Open
pub(crate) fn parse_open_tag(&mut self, name: &Ident, attrs: &Vec<Attr>) {
self.set_most_recent_open_tag_end(&name.span());
pub(crate) fn parse_open_tag(&mut self, name: &Ident, closing_span: &Span, attrs: &Vec<Attr>) {
self.set_most_recent_open_tag_end(closing_span);

let idx = &mut self.current_node_idx;
let parent_to_children = &mut self.parent_to_children;
Expand Down
Loading

0 comments on commit 09104b4

Please sign in to comment.