[
    {
        "cases": [
            {
                "comment": "Matching a literal",
                "expression": "foo[?name == 'a']",
                "is_deeply": 1,
                "result": [
                    {
                        "name": "a"
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "name": "a"
                },
                {
                    "name": "b"
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Matching a literal",
                "expression": "*[?[0] == `0`]",
                "is_deeply": 1,
                "result": [
                    [],
                    []
                ]
            }
        ],
        "given": {
            "bar": [
                2,
                3
            ],
            "foo": [
                0,
                1
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Matching an expression",
                "expression": "foo[?first == last]",
                "is_deeply": 1,
                "result": [
                    {
                        "first": "foo",
                        "last": "foo"
                    }
                ]
            },
            {
                "comment": "Verify projection created from filter",
                "expression": "foo[?first == last].first",
                "is_deeply": 1,
                "result": [
                    "foo"
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "first": "foo",
                    "last": "bar"
                },
                {
                    "first": "foo",
                    "last": "foo"
                },
                {
                    "first": "foo",
                    "last": "baz"
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Greater than with a number",
                "expression": "foo[?age > `25`]",
                "is_deeply": 1,
                "result": [
                    {
                        "age": 30
                    }
                ]
            },
            {
                "expression": "foo[?age >= `25`]",
                "is_deeply": 1,
                "result": [
                    {
                        "age": 25
                    },
                    {
                        "age": 30
                    }
                ]
            },
            {
                "comment": "Greater than with a number",
                "expression": "foo[?age > `30`]",
                "is_deeply": 1,
                "result": []
            },
            {
                "comment": "Greater than with a number",
                "expression": "foo[?age < `25`]",
                "is_deeply": 1,
                "result": [
                    {
                        "age": 20
                    }
                ]
            },
            {
                "comment": "Greater than with a number",
                "expression": "foo[?age <= `25`]",
                "is_deeply": 1,
                "result": [
                    {
                        "age": 20
                    },
                    {
                        "age": 25
                    }
                ]
            },
            {
                "comment": "Greater than with a number",
                "expression": "foo[?age < `20`]",
                "is_deeply": 1,
                "result": []
            },
            {
                "expression": "foo[?age == `20`]",
                "is_deeply": 1,
                "result": [
                    {
                        "age": 20
                    }
                ]
            },
            {
                "expression": "foo[?age != `20`]",
                "is_deeply": 1,
                "result": [
                    {
                        "age": 25
                    },
                    {
                        "age": 30
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "age": 20
                },
                {
                    "age": 25
                },
                {
                    "age": 30
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Filter with subexpression",
                "expression": "foo[?top.name == 'a']",
                "is_deeply": 1,
                "result": [
                    {
                        "top": {
                            "name": "a"
                        }
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "top": {
                        "name": "a"
                    }
                },
                {
                    "top": {
                        "name": "b"
                    }
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Matching an expression",
                "expression": "foo[?top.first == top.last]",
                "is_deeply": 1,
                "result": [
                    {
                        "top": {
                            "first": "foo",
                            "last": "foo"
                        }
                    }
                ]
            },
            {
                "comment": "Matching a JSON array",
                "expression": "foo[?top == `{\"first\": \"foo\", \"last\": \"bar\"}`]",
                "is_deeply": 1,
                "result": [
                    {
                        "top": {
                            "first": "foo",
                            "last": "bar"
                        }
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "top": {
                        "first": "foo",
                        "last": "bar"
                    }
                },
                {
                    "top": {
                        "first": "foo",
                        "last": "foo"
                    }
                },
                {
                    "top": {
                        "first": "foo",
                        "last": "baz"
                    }
                }
            ]
        }
    },
    {
        "cases": [
            {
                "expression": "foo[?key == `true`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    }
                ]
            },
            {
                "expression": "foo[?key == `false`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": false
                    }
                ]
            },
            {
                "expression": "foo[?key == `0`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": 0
                    }
                ]
            },
            {
                "expression": "foo[?key == `1`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": 1
                    }
                ]
            },
            {
                "expression": "foo[?key == `[0]`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": [
                            0
                        ]
                    }
                ]
            },
            {
                "expression": "foo[?key == `{\"bar\": [0]}`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key == `null`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": null
                    }
                ]
            },
            {
                "expression": "foo[?key == `[1]`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": [
                            1
                        ]
                    }
                ]
            },
            {
                "expression": "foo[?key == `{\"a\":2}`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`true` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    }
                ]
            },
            {
                "expression": "foo[?`false` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": false
                    }
                ]
            },
            {
                "expression": "foo[?`0` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": 0
                    }
                ]
            },
            {
                "expression": "foo[?`1` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": 1
                    }
                ]
            },
            {
                "expression": "foo[?`[0]` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": [
                            0
                        ]
                    }
                ]
            },
            {
                "expression": "foo[?`{\"bar\": [0]}` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`null` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": null
                    }
                ]
            },
            {
                "expression": "foo[?`[1]` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": [
                            1
                        ]
                    }
                ]
            },
            {
                "expression": "foo[?`{\"a\":2}` == key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `true`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `false`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `0`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `1`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `null`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `[1]`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?key != `{\"a\":2}`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    }
                ]
            },
            {
                "expression": "foo[?`true` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`false` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`0` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`1` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`null` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": [
                            1
                        ]
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`[1]` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": {
                            "a": 2
                        }
                    }
                ]
            },
            {
                "expression": "foo[?`{\"a\":2}` != key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": false
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "bar": [
                                0
                            ]
                        }
                    },
                    {
                        "key": null
                    },
                    {
                        "key": [
                            1
                        ]
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "key": true
                },
                {
                    "key": false
                },
                {
                    "key": 0
                },
                {
                    "key": 1
                },
                {
                    "key": [
                        0
                    ]
                },
                {
                    "key": {
                        "bar": [
                            0
                        ]
                    }
                },
                {
                    "key": null
                },
                {
                    "key": [
                        1
                    ]
                },
                {
                    "key": {
                        "a": 2
                    }
                }
            ]
        }
    },
    {
        "cases": [
            {
                "expression": "reservations[].instances[?bar==`1`]",
                "is_deeply": 1,
                "result": [
                    [
                        {
                            "bar": 1,
                            "foo": 2
                        }
                    ]
                ]
            },
            {
                "expression": "reservations[*].instances[?bar==`1`]",
                "is_deeply": 1,
                "result": [
                    [
                        {
                            "bar": 1,
                            "foo": 2
                        }
                    ]
                ]
            },
            {
                "expression": "reservations[].instances[?bar==`1`][]",
                "is_deeply": 1,
                "result": [
                    {
                        "bar": 1,
                        "foo": 2
                    }
                ]
            }
        ],
        "given": {
            "reservations": [
                {
                    "instances": [
                        {
                            "bar": 2,
                            "foo": 1
                        },
                        {
                            "bar": 3,
                            "foo": 1
                        },
                        {
                            "bar": 2,
                            "foo": 1
                        },
                        {
                            "bar": 1,
                            "foo": 2
                        }
                    ]
                }
            ]
        }
    },
    {
        "cases": [
            {
                "expression": "foo[?bar==`1`].bar[0]",
                "is_deeply": 1,
                "result": []
            }
        ],
        "given": {
            "baz": "other",
            "foo": [
                {
                    "bar": 1
                },
                {
                    "bar": 2
                },
                {
                    "bar": 3
                },
                {
                    "bar": 4
                },
                {
                    "bar": 1,
                    "baz": 2
                }
            ]
        }
    },
    {
        "cases": [
            {
                "expression": "foo[?a==`1`].b.c",
                "is_deeply": 1,
                "result": [
                    "x",
                    "y",
                    "z"
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "a": 1,
                    "b": {
                        "c": "x"
                    }
                },
                {
                    "a": 1,
                    "b": {
                        "c": "y"
                    }
                },
                {
                    "a": 1,
                    "b": {
                        "c": "z"
                    }
                },
                {
                    "a": 2,
                    "b": {
                        "c": "z"
                    }
                },
                {
                    "a": 1,
                    "baz": 2
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Filter with or expression",
                "expression": "foo[?name == 'a' || name == 'b']",
                "is_deeply": 1,
                "result": [
                    {
                        "name": "a"
                    },
                    {
                        "name": "b"
                    }
                ]
            },
            {
                "expression": "foo[?name == 'a' || name == 'e']",
                "is_deeply": 1,
                "result": [
                    {
                        "name": "a"
                    }
                ]
            },
            {
                "expression": "foo[?name == 'a' || name == 'b' || name == 'c']",
                "is_deeply": 1,
                "result": [
                    {
                        "name": "a"
                    },
                    {
                        "name": "b"
                    },
                    {
                        "name": "c"
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "name": "a"
                },
                {
                    "name": "b"
                },
                {
                    "name": "c"
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Filter with and expression",
                "expression": "foo[?a == `1` && b == `2`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2
                    }
                ]
            },
            {
                "expression": "foo[?a == `1` && b == `4`]",
                "is_deeply": 1,
                "result": []
            }
        ],
        "given": {
            "foo": [
                {
                    "a": 1,
                    "b": 2
                },
                {
                    "a": 1,
                    "b": 3
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Filter with Or and And expressions",
                "expression": "foo[?c == `3` || a == `1` && b == `4`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2,
                        "c": 3
                    }
                ]
            },
            {
                "expression": "foo[?b == `2` || a == `3` && b == `4`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2,
                        "c": 3
                    },
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            },
            {
                "expression": "foo[?a == `3` && b == `4` || b == `2`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2,
                        "c": 3
                    },
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            },
            {
                "expression": "foo[?(a == `3` && b == `4`) || b == `2`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2,
                        "c": 3
                    },
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            },
            {
                "expression": "foo[?((a == `3` && b == `4`)) || b == `2`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2,
                        "c": 3
                    },
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            },
            {
                "expression": "foo[?a == `3` && (b == `4` || b == `2`)]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            },
            {
                "expression": "foo[?a == `3` && ((b == `4` || b == `2`))]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "a": 1,
                    "b": 2,
                    "c": 3
                },
                {
                    "a": 3,
                    "b": 4
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Verify precedence of or/and expressions",
                "expression": "foo[?a == `1` || b ==`2` && c == `5`]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 1,
                        "b": 2,
                        "c": 3
                    }
                ]
            },
            {
                "comment": "Parentheses can alter precedence",
                "expression": "foo[?(a == `1` || b ==`2`) && c == `5`]",
                "is_deeply": 1,
                "result": []
            },
            {
                "comment": "Not expressions combined with and/or",
                "expression": "foo[?!(a == `1` || b ==`2`)]",
                "is_deeply": 1,
                "result": [
                    {
                        "a": 3,
                        "b": 4
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "a": 1,
                    "b": 2,
                    "c": 3
                },
                {
                    "a": 3,
                    "b": 4
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Unary filter expression",
                "expression": "foo[?key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": true
                    },
                    {
                        "key": [
                            0
                        ]
                    },
                    {
                        "key": {
                            "a": "b"
                        }
                    },
                    {
                        "key": 0
                    },
                    {
                        "key": 1
                    }
                ]
            },
            {
                "comment": "Unary not filter expression",
                "expression": "foo[?!key]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": false
                    },
                    {
                        "key": []
                    },
                    {
                        "key": {}
                    },
                    {
                        "key": null
                    },
                    {
                        "notkey": true
                    }
                ]
            },
            {
                "comment": "Equality with null RHS",
                "expression": "foo[?key == `null`]",
                "is_deeply": 1,
                "result": [
                    {
                        "key": null
                    },
                    {
                        "notkey": true
                    }
                ]
            }
        ],
        "given": {
            "foo": [
                {
                    "key": true
                },
                {
                    "key": false
                },
                {
                    "key": []
                },
                {
                    "key": {}
                },
                {
                    "key": [
                        0
                    ]
                },
                {
                    "key": {
                        "a": "b"
                    }
                },
                {
                    "key": 0
                },
                {
                    "key": 1
                },
                {
                    "key": null
                },
                {
                    "notkey": true
                }
            ]
        }
    },
    {
        "cases": [
            {
                "comment": "Using @ in a filter expression",
                "expression": "foo[?@ < `5`]",
                "is_deeply": 1,
                "result": [
                    0,
                    1,
                    2,
                    3,
                    4
                ]
            },
            {
                "comment": "Using @ in a filter expression",
                "expression": "foo[?`5` > @]",
                "is_deeply": 1,
                "result": [
                    0,
                    1,
                    2,
                    3,
                    4
                ]
            },
            {
                "comment": "Using @ in a filter expression",
                "expression": "foo[?@ == @]",
                "is_deeply": 1,
                "result": [
                    0,
                    1,
                    2,
                    3,
                    4,
                    5,
                    6,
                    7,
                    8,
                    9
                ]
            }
        ],
        "given": {
            "foo": [
                0,
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9
            ]
        }
    }
]