Skip to content

Commit

Permalink
Added doctest for spreading option
Browse files Browse the repository at this point in the history
  • Loading branch information
sfisol committed Dec 18, 2024
1 parent 7875229 commit 76dc831
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/vertigo/src/fetch/lazy_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl<T: PartialEq + Clone> LazyCache<T> {
}

impl<T: JsJsonDeserialize> LazyCache<T> {
/// Helper to easily create a lazy cache of Vec<T> deserialized from provided URL base and route
/// Helper to easily create a lazy cache of `Vec<T>` deserialized from provided URL base and route
///
/// ```rust
/// use vertigo::{Computed, LazyCache, RequestBuilder, AutoJsJson, Resource};
Expand Down
20 changes: 20 additions & 0 deletions crates/vertigo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ pub use log;

/// Allows to create [DomNode] using HTML tags.
///
/// Simple DOM with a param embedded:
///
/// ```rust
/// use vertigo::dom;
///
Expand All @@ -306,6 +308,24 @@ pub use log;
/// </div>
/// };
/// ```
///
/// Mapping and embedding an `Option`:
///
/// ```rust
/// use vertigo::dom;
///
/// let name = "John";
/// let occupation = Some("Lumberjack");
///
/// dom! {
/// <div>
/// <h3>"Hello " {name} "!"</h3>
/// {..occupation.map(|occupation| dom! { <p>"Occupation: " {occupation}</p> })}
/// </div>
/// };
/// ```
///
/// Note the spread operator which utilizes the fact that `Option` is iterable in Rust.
pub use vertigo_macro::dom;

/// Allows to create [DomElement] using HTML tags.
Expand Down
22 changes: 20 additions & 2 deletions crates/vertigo/src/tests/dom/list_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::{self as vertigo, DomNode};

#[test]
fn children_from_iter() {
let list = (0..10)
.map(|i| dom! { <li>{i}</li> });
let list = (0..10).map(|i| dom! { <li>{i}</li> });

let node = dom! {
<ul>
Expand Down Expand Up @@ -35,3 +34,22 @@ fn children_from_iter_inline() {

assert_eq!(node.get_children().len(), 11);
}

#[test]
fn iter_option() {
let some_label = Some("Label".to_string());
let none_label = Option::<String>::None;

let node = dom! {
<div>
{..some_label}
{..none_label}
</div>
};

let DomNode::Node { node } = node else {
panic!("Expected DomNode::Node")
};

assert_eq!(node.get_children().len(), 1);
}

0 comments on commit 76dc831

Please sign in to comment.