Skip to content

Add test_value! macro#18582

Merged
Bahex merged 8 commits into
nushell:mainfrom
Bahex:test_value_macro
Jul 13, 2026
Merged

Add test_value! macro#18582
Bahex merged 8 commits into
nushell:mainfrom
Bahex:test_value_macro

Conversation

@Bahex

@Bahex Bahex commented Jul 12, 2026

Copy link
Copy Markdown
Member

Description

Tip

tl;dr: allows us to write complex values in rust with more or less the same syntax as nu.

The rest is copied from the doc comment:

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);

User-facing changes (Release notes)

N/A

@Bahex Bahex added tests issues to add tests or fix tests notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes labels Jul 13, 2026
Comment thread crates/nu-protocol/src/value/macros.rs Outdated
@Bahex
Bahex force-pushed the test_value_macro branch from 67b5cae to e981944 Compare July 13, 2026 19:39
@Bahex
Bahex merged commit 35f2c10 into nushell:main Jul 13, 2026
14 checks passed
@github-actions github-actions Bot added this to the v0.115.0 milestone Jul 13, 2026
@cptpiepmatz cptpiepmatz mentioned this pull request Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes tests issues to add tests or fix tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants