use std::env; use std::error::Error; use std::fs::read_to_string; use std::path::PathBuf; use test_case::test_case; use yaml_rust::{Yaml, YamlEmitter, YamlLoader}; use yaml_path::Path; fn get_test_data() -> Result, Box> { let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; let mut path = PathBuf::from(manifest_dir); path.push("tests/test_path.yaml"); let yaml_str = read_to_string(path)?; Ok(YamlLoader::load_from_str(&yaml_str)?) } #[test_case("/squads/alpha", &["1.1"], true, None)] #[test_case("squads.bravo", &["2.2"], true, None)] #[test_case("/array_of_hashes/1", &["step: 2\nname: two"], true, None)] // #[test_case("aliases[&aliasAnchorOne]", &["Anchored Scalar Value"], true, None)] // #[test_case("aliases[&newAlias]", &["Not in the original data"], false, Some("Not in the original data"))] // #[test_case("aliases[0]", &["Anchored Scalar Value"], true, None)] // #[test_case("aliases.0", &["Anchored Scalar Value"], true, None; "aliases.0")] // #[test_case("(array_of_hashes.name)+(rollback_hashes.on_condition.failure.name)", &["[\"one\", \"two\", \"three\", \"four\"]"], true, None)] // #[test_case("/array_of_hashes/name", &["one", "two"], true, None)] // #[test_case("aliases[1:2]", &["[Hey, Number Two!]"], true, None)] // #[test_case("aliases[1:1]", &["[Hey, Number Two!]"], true, None)] // #[test_case("squads[bravo:charlie]", &["2.2", "3.3"], true, None)] // #[test_case("/&arrayOfHashes/1/step", &["2"], true, None)] // #[test_case("&arrayOfHashes[step=1].name", &["one"], true, None)] // #[test_case("squads[.!=\"\"][.=1.1]", &["1.1"], true, None)] // #[test_case("squads[.!=\"\"][.>1.1][.<3.3]", &["2.2"], true, None)] // #[test_case("aliases[.^Hey]", &["Hey, Number Two!"], true, None)] // #[test_case("aliases[.$Value]", &["Anchored Scalar Value"], true, None)] // #[test_case("aliases[.%Value]", &["Anchored Scalar Value"], true, None; "alises[.%value]")] // #[test_case("&arrayOfHashes[step>1].name", &["two"], true, None)] // #[test_case("&arrayOfHashes[step<2].name", &["one"], true, None)] // #[test_case("squads[.>charlie]", &["4.4"], true, None)] // #[test_case("squads[.>=charlie]", &["3.3, 4.4"], true, None)] // #[test_case("squads[.) -> Result<(), Box> { let yamlpath = Path::new(yamlpath)?; let data = get_test_data()?; let first = &data[0]; let mut count = 0; for (idx, node) in yamlpath.get_all(&first)?.into_iter().enumerate() { let mut node_as_str = String::new(); let mut emitter = YamlEmitter::new(&mut node_as_str); emitter.dump(node)?; node_as_str.drain(0..4); assert_eq!(node_as_str, results[idx]); count += 1; } assert_eq!(count, results.len()); Ok(()) }