UI tree test macro #

ui_tree! builds a UI tree fixture from RSX-like markup. It accepts a mutable tree, creates the elements and text nodes, and attaches the single top-level element to the app root.

The tree is any value with the six methods it calls: root, create_element, create_text, append_child, set_attribute, and set_style_property. A test of a running app passes the Fixture that TestApp::with_fixture lends; core's unit tests may pass a &mut UITree.

An app's tests use it as burokku::test::ui_tree, which comes with Burokku's test-support feature:

[dev-dependencies]
burokku = { path = "../../crates/burokku", features = ["test-support"] }
use burokku::test::ui_tree;

Core's own unit tests use test_macro::ui_tree, through the development dependency on the macro crate, burokku-test-macro:

[dev-dependencies]
test_macro = { package = "burokku-test-macro", path = "../test_macro" }

Example #

use crate::ui::ui_tree::{UITree, ElementTag};
use test_macro::ui_tree;

#[test]
fn builds_text_tree() {
    let mut ui_tree = UITree::new();
    let name = "Burokku";

    ui_tree!(&mut ui_tree,
        <window var=window width="800" id="main">
            <flex var=content style:flex-direction="row" style:gap="8px">
                <text var=label style:font-size="16px">
                    "Hello, " {name}
                </text>
            </flex>
        </window>
    );

    assert_eq!(ui_tree.element_tag(window), Ok(ElementTag::WINDOW));
    assert_eq!(ui_tree.parent(content), Some(window));
    assert_eq!(ui_tree.parent(label), Some(content));
    assert_eq!(ui_tree.text_content(label).unwrap(), "Hello, Burokku");
    assert_eq!(ui_tree.attribute(window, "id"), Some("main"));
}

The macro is intended for valid fixture setup. It uses the normal UITree APIs, so tests continue to inspect nodes explicitly with calls such as ui_tree.node(label).

Syntax #

Pass a mutable UI tree expression followed by exactly one top-level element:

ui_tree!(&mut ui_tree, <window />);

Any tag an extension registered works, Burokku's own and an app's alike, such as <x-panel />. The tree checks the tag when it creates the element.

Markup Behavior
var=label Introduces a caller-local NodeId named label; it is not stored in the UI tree.
style:width="20px" Calls set_style_property; the element's existing style type validates it.
id="sidebar" Calls set_attribute.
"quoted" or unquoted text Creates a text node.
{value} Creates text from value.to_string().
<!-- "comment" --> Ignored.

var accepts an identifier or string literal, such as var=panel or var="panel". Names must be valid Rust identifiers and unique within one macro invocation. Attribute and style values may be Rust expressions whose results implement ToString.

The top-level element is appended to the app root. Existing UI tree relationship rules still apply, so a complete fixture must have one window at its root.

Errors #

The macro reports these while compiling:

  • multiple top-level nodes;
  • missing attribute values;
  • invalid or duplicate var names;
  • fragments, block attributes, and unsupported node forms.

Unregistered element tags, and UI tree relationship, attribute, and style errors, panic immediately because the macro is only for valid test-fixture setup.