Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep match (double-asterisk) should recurse down into arrays #687

Open
adamchalmers opened this issue Nov 18, 2024 · 0 comments
Open

Deep match (double-asterisk) should recurse down into arrays #687

adamchalmers opened this issue Nov 18, 2024 · 0 comments

Comments

@adamchalmers
Copy link

Background

I have a deeply recursive structure I am trying to snapshot test. The nodes in the tree have many arrays of other nodes, and many maps where the values are other nodes. It's not really feasible to determine the structure ahead of time. Some of the primitive/terminal/leaf nodes in this tree are f64s.

Problem

f64 math produces different results on different target triples, so I need to round the numbers before serializing them. I figured I'd use the rounded_redactions(n) feature and just round-redact all numbers in the tree. But there's no clear way to do this. Here's an example.

#[derive(serde::Serialize, serde::Deserialize)]
struct Node {
    data: f64,
    nodes: Vec<Node>,
}

#[test]
fn test_recurse() {
    // Build the test data
    let data = Node {
        data: f64::tan(1.0),
        nodes: vec![
            Node {
                data: f64::tan(2.0),
                nodes: vec![Node {
                    data: f64::tan(4.0),
                    nodes: vec![Node {
                        data: f64::tan(5.0),
                        nodes: vec![],
                    }],
                }],
            },
            Node {
                data: f64::tan(3.0),
                nodes: vec![],
            },
        ],
    };

    // Try redacting.
    insta::assert_yaml_snapshot!("data", data, {
        // Matches the root node, but none of its children.
        ".**" => insta::rounded_redaction(3),
        // Matches the children of root, but requires knowing the name of the key you want to redact.
        // ".**[].data" => insta::rounded_redaction(3),
    });
}

This redacts the root node, so tan(1.0) gets rounded, but none of its children do. I think this is because ** doesn't recurse down into a child if the child is an array. Only to child keys.

I can add another redaction for ".**[].data" and then it matches all the children (and the children of children). That's great, but it requires that I know the data key ahead of time. For some reason ".**[].*" doesn't work either, it fails to find any of the child data keys.

Is there any simple way to say "match every single child and element of array"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant