diagnostics_assertions

Macro assert_data_tree

Source
macro_rules! assert_data_tree {
    ($diagnostics_hierarchy:expr, $($rest:tt)+) => { ... };
}
Expand description

Macro to simplify tree matching in tests. The first argument is the actual tree passed as a DiagnosticsHierarchyGetter (e.g. a DiagnosticsHierarchy or an Inspector). The second argument is given to tree_assertion! which creates a TreeAssertion to validate the tree.

Each leaf value must be a type that implements either PropertyAssertion or TreeAssertion.

Example:

// Actual tree
let diagnostics_hierarchy = DiagnosticsHierarchy {
    name: "key".to_string(),
    properties: vec![
        Property::String("sub".to_string(), "sub_value".to_string()),
        Property::String("sub2".to_string(), "sub2_value".to_string()),
    ],
    children: vec![
       DiagnosticsHierarchy {
           name: "child1".to_string(),
           properties: vec![
               Property::Int("child1_sub".to_string(), 10i64),
           ],
           children: vec![],
       },
       DiagnosticsHierarchy {
           name: "child2".to_string(),
           properties: vec![
               Property::Uint("child2_sub".to_string(), 20u64),
           ],
           children: vec![],
       },
   ],
};

assert_data_tree!(
    diagnostics_hierarchy,
    key: {
        sub: AnyProperty,   // only verify that `sub` is a property of `key`
        sub2: "sub2_value",
        child1: {
            child1_sub: 10i64,
        },
        child2: {
            child2_sub: 20u64,
        },
    }
);

In order to do a partial match on a tree, use the contains keyword:

assert_data_tree!(diagnostics_hierarchy, key: contains {
    sub: "sub_value",
    child1: contains {},
});

In order to do a match on a tree where the keys need to be computed (they are some expression), you’ll need to use => instead of ::

assert_data_tree!(diagnostics_hierarchy, key: {
    key_fn() => "value",
})

Note that key_fn has to return a String.

The first argument can be an Inspector, in which case the whole tree is read from the Inspector and matched against:

let inspector = Inspector::default();
assert_data_tree!(inspector, root: {});

TreeAssertions made elsewhere can be included bodily in the macro, but must always be followed by a trailing comma: assert_data_tree!( diagnostics_hierarchy, key: { make_child_tree_assertion(), // required trailing comma } );

A tree may contain multiple properties or children with the same name. This macro does not support matching against them, and will throw an error if it detects duplicates. This is to provide warning for users who accidentally log the same name multiple times, as the behavior for reading properties or children with duplicate names is not well defined.