Skip to main content

ndslice/selection/
test_utils.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9use nom::Parser as _;
10
11use crate::selection::Selection;
12
13/// Parse an input string to a selection.
14pub fn parse(input: &str) -> Selection {
15    use nom::combinator::all_consuming;
16
17    use crate::selection::parse::expression;
18
19    let (_, selection) = all_consuming(expression).parse(input).unwrap();
20    selection
21}
22
23#[macro_export]
24macro_rules! assert_structurally_eq {
25    ($expected:expr, $actual:expr) => {{
26        let expected = &$expected;
27        let actual = &$actual;
28        assert!(
29            $crate::selection::structurally_equal(expected, actual),
30            "Selections do not match.\nExpected: {:#?}\nActual:   {:#?}",
31            expected,
32            actual,
33        );
34    }};
35}
36
37#[macro_export]
38macro_rules! assert_round_trip {
39    ($selection:expr) => {{
40        let selection: Selection = $selection; // take ownership
41        // Convert `Selection` to representation as compact
42        // syntax.
43        let compact = $crate::selection::pretty::compact(&selection).to_string();
44        // Parse a `Selection` from the compact syntax
45        // representation.
46        let parsed = $crate::selection::test_utils::parse(&compact);
47        // Check that the input and parsed `Selection`s are
48        // structurally equivalent.
49        assert!(
50            $crate::selection::structurally_equal(&selection, &parsed),
51            "input: {} \n compact: {}\n parsed: {}",
52            selection,
53            compact,
54            parsed
55        );
56    }};
57}