use super::FieldResolver;
use std::collections::{HashMap, HashSet};
fn set(entries: &[&str]) -> HashSet<String> {
entries.iter().map(|s| (*s).to_string()).collect()
}
#[test]
fn is_array_strips_virtual_namespace_prefix() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&set(&["action_results", "final_url"]),
&set(&["action_results"]),
&HashSet::new(),
);
assert!(
resolver.is_array("action_results"),
"control: the unprefixed spelling was always classified as an array"
);
assert!(
resolver.is_array("interaction.action_results"),
"`interaction.` is a virtual label — the value it addresses is the same slice"
);
}
#[test]
fn is_array_agrees_with_result_relative_path() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&set(&["action_results"]),
&set(&["action_results"]),
&HashSet::new(),
);
let relative = resolver.result_relative_path("interaction.action_results");
assert_eq!(relative, "action_results", "the virtual prefix must be stripped");
assert!(
resolver.is_array(relative),
"the relative spelling names a declared array field"
);
assert!(
resolver.is_array("interaction.action_results"),
"the prefixed and relative spellings name one value and must classify identically"
);
}
#[test]
fn is_array_does_not_strip_a_real_nested_struct_field() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&set(&["metrics", "final_url"]),
&set(&["total_lines"]),
&HashSet::new(),
);
assert!(
!resolver.is_array("metrics.total_lines"),
"`metrics` is a declared result field, so it is a real struct step, not a virtual label"
);
}
#[test]
fn is_array_does_not_strip_when_result_fields_is_unconfigured() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&HashSet::new(),
&set(&["action_results"]),
&HashSet::new(),
);
assert!(
!resolver.is_array("interaction.action_results"),
"without result_fields the prefix is indistinguishable from a real nested field"
);
}
#[test]
fn is_array_still_rejects_a_stripped_path_that_is_not_an_array() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&set(&["final_url", "action_results"]),
&set(&["action_results"]),
&HashSet::new(),
);
assert!(
!resolver.is_array("interaction.final_url"),
"final_url is a declared result field but not an array"
);
}
#[test]
fn is_array_terminates_when_the_stripped_root_is_known_only_through_fields_array() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&set(&["content"]),
&set(&["items"]),
&HashSet::new(),
);
assert!(
resolver.is_array("ns.items"),
"items is a declared array field, so the virtual `ns.` label must strip"
);
}
#[test]
fn is_array_resolves_an_alias_to_its_array_target() {
let mut aliases = HashMap::new();
aliases.insert("results".to_string(), "action_results".to_string());
let resolver = FieldResolver::new(
&aliases,
&HashSet::new(),
&set(&["action_results"]),
&set(&["action_results"]),
&HashSet::new(),
);
assert!(
resolver.is_array("results"),
"the alias names the same slice as its target"
);
}