leptosfmt_formatter/formatter/
fragment.rs

1use rstml::node::NodeFragment;
2
3use crate::formatter::Formatter;
4
5impl Formatter<'_> {
6    pub fn fragment(&mut self, fragment: &NodeFragment) {
7        self.printer.word("<>");
8        self.children(&fragment.children, 0);
9        self.printer.word("</>");
10    }
11}
12
13#[cfg(test)]
14mod tests {
15    use crate::{
16        formatter::FormatterSettings,
17        test_helpers::{format_with, fragment},
18    };
19
20    macro_rules! format_fragment {
21        ($($tt:tt)*) => {{
22            let fragment = fragment! { $($tt)* };
23            let settings = FormatterSettings { max_width: 40, ..Default::default() };
24            format_with(settings, |formatter| {
25                formatter.fragment(&fragment);
26            })
27        }};
28    }
29
30    #[test]
31    fn fragment_no_children() {
32        let formatted = format_fragment! { <> </> };
33        insta::assert_snapshot!(formatted, @"<></>");
34    }
35
36    #[test]
37    fn fragment_child_element() {
38        let formatted = format_fragment! { <><span>"hello"</span></> };
39        insta::assert_snapshot!(formatted, @r#"
40        <>
41            <span>"hello"</span>
42        </>
43        "#);
44    }
45
46    #[test]
47    fn fragment_child_element_single_textual() {
48        let formatted = format_fragment! { <>"hello"</> };
49        insta::assert_snapshot!(formatted, @r#"<>"hello"</>"#);
50    }
51
52    #[test]
53    fn fragment_child_element_two_textual() {
54        let formatted = format_fragment! { <>"The count is" {count}</> };
55        insta::assert_snapshot!(formatted, @r#"<>"The count is" {count}</>"#);
56    }
57
58    #[test]
59    fn fragment_child_element_many_textual() {
60        let formatted = format_fragment! { <>"The current count is: " {count} ". Increment by one is this: " {count + 1}</> };
61        insta::assert_snapshot!(formatted, @r#"
62        <>
63            "The current count is: " {count}
64            ". Increment by one is this: " {count + 1}
65        </>
66        "#);
67    }
68}