use super::assertions::render_assertion;
use crate::e2e::field_access::FieldResolver;
use crate::e2e::fixture::Assertion;
use std::collections::{HashMap, HashSet};
fn optional_parent_array_resolver() -> FieldResolver {
let optional: HashSet<String> = ["data".to_string()].into_iter().collect();
let array: HashSet<String> = ["data.children".to_string()].into_iter().collect();
FieldResolver::new(&HashMap::new(), &optional, &HashSet::new(), &array, &HashSet::new())
}
fn plain_array_resolver() -> FieldResolver {
let array: HashSet<String> = ["items".to_string()].into_iter().collect();
FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&HashSet::new(),
&array,
&HashSet::new(),
)
}
fn assertion(assertion_type: &str, field: &str) -> Assertion {
Assertion {
assertion_type: assertion_type.to_string(),
field: Some(field.to_string()),
..Assertion::default()
}
}
fn render(resolver: &FieldResolver, assertion: &Assertion) -> String {
let mut out = String::new();
render_assertion(
&mut out,
assertion,
"result",
resolver,
false,
false,
false,
false,
&HashMap::new(),
false,
false,
);
out
}
#[test]
fn is_empty_on_array_field_through_optional_parent_coalesces_to_bool() {
let out = render(
&optional_parent_array_resolver(),
&assertion("is_empty", "data.children"),
);
assert_eq!(
out, " XCTAssertTrue((result.data()?.children().isEmpty ?? true), \"expected empty value\")\n",
"got: {out}"
);
assert!(
!out.contains("isEmpty, \"expected"),
"must not emit the bare Bool? form: {out}"
);
}
#[test]
fn not_empty_on_array_field_through_optional_parent_coalesces_to_bool() {
let out = render(
&optional_parent_array_resolver(),
&assertion("not_empty", "data.children"),
);
assert_eq!(
out, " XCTAssertTrue(result.data()?.children().isEmpty == false, \"expected non-empty value\")\n",
"got: {out}"
);
}
#[test]
fn is_empty_on_plain_array_field_is_unchanged() {
let out = render(&plain_array_resolver(), &assertion("is_empty", "items"));
assert_eq!(
out, " XCTAssertTrue(result.items().isEmpty, \"expected empty value\")\n",
"got: {out}"
);
}
#[test]
fn not_empty_on_plain_array_field_is_unchanged() {
let out = render(&plain_array_resolver(), &assertion("not_empty", "items"));
assert_eq!(
out, " XCTAssertTrue(!result.items().isEmpty, \"expected non-empty value\")\n",
"got: {out}"
);
}