use std::fmt::Write as FmtWrite;
pub(super) fn render_not_error(out: &mut String, result_var: &str, may_assert_presence: bool, is_streaming: bool) {
if is_streaming {
let _ = writeln!(
out,
" assertTrue(chunks.isNotEmpty(), \"expected at least one streamed chunk\")"
);
} else if may_assert_presence {
let _ = writeln!(out, " assertNotNull({result_var}, \"expected non-null result\")");
} else {
let _ = writeln!(
out,
" // not_error: covered by the bare Optional's own assertion"
);
}
}
#[cfg(test)]
mod tests {
use super::render_not_error;
use crate::e2e::codegen::not_error_presence::may_assert_presence;
use crate::e2e::fixture::{Assertion, Fixture};
fn fixture_with(assertion_types: &[&str]) -> Fixture {
Fixture {
assertions: assertion_types
.iter()
.map(|assertion_type| Assertion {
assertion_type: (*assertion_type).to_string(),
..Default::default()
})
.collect(),
..Default::default()
}
}
#[test]
fn non_streaming_renders_a_real_assertion_on_the_result_variable() {
let mut out = String::new();
render_not_error(&mut out, "result", true, false);
assert_eq!(out, " assertNotNull(result, \"expected non-null result\")\n");
}
#[test]
fn streaming_asserts_on_the_drained_chunks_list_not_the_result_variable() {
let mut out = String::new();
render_not_error(&mut out, "result", true, true);
assert_eq!(
out,
" assertTrue(chunks.isNotEmpty(), \"expected at least one streamed chunk\")\n"
);
assert!(
!out.contains("result"),
"streaming must not reference result_var: got {out}"
);
}
#[test]
fn presence_not_permitted_emits_no_not_null_assertion() {
let mut out = String::new();
render_not_error(&mut out, "result", false, false);
assert!(
!out.contains("assertNotNull(result"),
"may_assert_presence: false must not assert non-null from not_error: got {out}"
);
}
#[test]
fn sole_not_error_on_an_option_result_via_the_shared_decision_stays_inert() {
let fixture = fixture_with(&["not_error"]);
let may_assert = may_assert_presence(&fixture, true);
let mut out = String::new();
render_not_error(&mut out, "result", may_assert, false);
assert!(
!out.contains("assertNotNull(result"),
"bare Option<T> result must not assert non-null from not_error even as the sole \
assertion: got {out}"
);
}
}