use super::FieldResolver;
use std::collections::{HashMap, HashSet};
fn resolver_with_optional(field: &str) -> FieldResolver {
let optional: HashSet<String> = [field.to_string()].into_iter().collect();
FieldResolver::new(
&HashMap::new(),
&optional,
&HashSet::new(),
&HashSet::new(),
&HashSet::new(),
)
}
#[test]
fn indexing_past_an_optional_field_narrows_it_first() {
let resolver = resolver_with_optional("choices[0].message.tool_calls");
let rendered = resolver.accessor("choices[0].message.tool_calls[0].function.name", "python", "result");
assert_eq!(
rendered,
"(result.choices[0].message.tool_calls[0].function.name if result.choices[0].message.tool_calls else None)",
"the optional collection must be narrowed before it is subscripted"
);
}
#[test]
fn indexing_a_non_optional_field_is_unguarded() {
let resolver = FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&HashSet::new(),
&HashSet::new(),
&HashSet::new(),
);
assert_eq!(
resolver.accessor("choices[0].message.tool_calls[0].function.name", "python", "result"),
"result.choices[0].message.tool_calls[0].function.name"
);
}
#[test]
fn a_terminal_optional_field_is_left_unguarded() {
let resolver = resolver_with_optional("summary");
assert_eq!(resolver.accessor("summary", "python", "result"), "result.summary");
}