Skip to main content

test_value

Macro test_value 

Source
macro_rules! test_value {
    (@recur, [$($item:tt),* $(,)?]) => { ... };
    (@recur, {$($col:tt : $val:tt),* $(,)?}) => { ... };
    (@recur, $val:expr) => { ... };
    (@col, $col:ident) => { ... };
    (@col, $col:expr) => { ... };
    ([$($item:tt),* $(,)?]) => { ... };
    ({$($col:tt : $val:tt),* $(,)?}) => { ... };
    ($val:expr) => { ... };
}
Expand description

Helper macro for constructing [Value] instances for use in tests and Exampless.

Can be used to create simple scalar values with anything implementing IntoValue:

assert_eq!(test_value!(42),   Value::test_int(42));
assert_eq!(test_value!(true), Value::test_bool(true));
assert_eq!(test_value!(()),   Value::test_nothing());

Can be used in place of test_list!:

let test =   test_value!(["abc", 42, true]);
let expected = test_list!["abc", 42, true];
assert_eq!(test, expected);

Can be used in place of test_record!, with some differences:

  • instead of fat arrows (=>), colons are used (:).
  • keys can be bare identifiers in addition to string literals and variables. (to use the value of an existing variable, wrap it in parentheses)
let key_in_var = "foo";
let test = test_value!({
    a: 1,
    "b": 2,
    (key_in_var): "bar",
});
let expected = test_record! {
    "a" => 1,
    "b" => 2,
    "foo" => "bar",
};
assert_eq!(test, expected);

The most important feature of [test_value!] is that it works recursively for all values. That makes it very powerful for constructing complex and nested values:

let test = test_value!({
    a: 1,
    b: {
        c: 2,
        d: ["e", "f", {g: 3}],
    },
});
let expected = test_record! {
    "a" => 1,
    "b" => test_record! {
        "c" => 2,
        "d" => test_list! ["e", "f", test_record! { "g" => 3 } ],
    },
};
assert_eq!(test, expected);