use super::render_assertion;
use crate::e2e::field_access::FieldResolver;
use crate::e2e::fixture::Assertion;
use std::collections::HashSet;
fn resolver_with_collection_root(field: &str) -> FieldResolver {
let array_fields: HashSet<String> = [format!("{field}[0]")].into_iter().collect();
FieldResolver::new(
&std::collections::HashMap::new(),
&std::collections::HashSet::new(),
&std::collections::HashSet::new(),
&array_fields,
&std::collections::HashSet::new(),
)
}
fn is_empty_on_field(field: &str) -> Assertion {
Assertion {
assertion_type: "is_empty".to_string(),
field: Some(field.to_string()),
..Default::default()
}
}
#[test]
fn is_empty_on_collection_root_field_uses_assert_empty_not_tostring() {
let resolver = resolver_with_collection_root("children");
let assertion = is_empty_on_field("children");
let mut out = String::new();
render_assertion(
&mut out,
&assertion,
"result",
"SampleClass",
"SampleException",
&resolver,
false,
false,
false,
false,
&std::collections::HashMap::new(),
false,
);
assert!(
out.contains("Assert.Empty("),
"expected Assert.Empty for a collection-root field; got: {out}"
);
assert!(
!out.contains("ToString()"),
"collection field must not fall back to the ToString()-based empty check; got: {out}"
);
}
#[test]
fn is_empty_on_an_undeclared_collection_root_field_uses_ir_classification_not_tostring() {
use crate::core::ir::{FieldDef, TypeDef, TypeRef};
let type_defs = vec![TypeDef {
name: "SampleClass".into(),
fields: vec![FieldDef {
name: "children".into(),
ty: TypeRef::Vec(Box::new(TypeRef::Named("DataNode".into()))),
..FieldDef::default()
}],
..TypeDef::default()
}];
let resolver = FieldResolver::new(
&std::collections::HashMap::new(),
&std::collections::HashSet::new(),
&std::collections::HashSet::new(),
&std::collections::HashSet::new(),
&std::collections::HashSet::new(),
)
.with_ir_collection_map(
FieldResolver::ir_collection_fields(&type_defs),
Some("SampleClass".to_string()),
);
let assertion = is_empty_on_field("children");
let mut out = String::new();
render_assertion(
&mut out,
&assertion,
"result",
"SampleClass",
"SampleException",
&resolver,
false,
false,
false,
false,
&std::collections::HashMap::new(),
false,
);
assert!(
out.contains("Assert.Empty("),
"expected Assert.Empty for an IR-proven collection-root field with zero config; got: {out}"
);
assert!(
!out.contains("ToString()"),
"an undeclared collection field must not fall back to the ToString()-based empty check; got: {out}"
);
}