use std::fmt::Write as FmtWrite;
use crate::e2e::codegen::mock_assertions::{build_path_and_query, mock_capture, parse_mock_field};
use crate::e2e::escape::escape_ruby_single;
use crate::e2e::fixture::Assertion;
const MOCK_SERVER_URL_EXPR: &str = "ENV.fetch('MOCK_SERVER_URL')";
pub(super) fn try_render_mock_capture_assertion(out: &mut String, assertion: &Assertion) -> bool {
let Some(field) = assertion.field.as_deref() else {
return false;
};
let Ok(query) = parse_mock_field(field) else {
return false;
};
let capture = mock_capture("ruby").unwrap_or_else(|_| {
panic!("Ruby e2e generator: mock.* field '{field}' has no ruby capture-table entry (mock_assertions::snippets)")
});
let path_and_query = build_path_and_query(&query);
let expr = format!("{}({MOCK_SERVER_URL_EXPR}, \"{path_and_query}\")", capture.helper_name);
render_mock_capture_comparison(out, assertion, field, &expr);
true
}
fn render_mock_capture_comparison(out: &mut String, assertion: &Assertion, field: &str, expr: &str) {
let assertion_type = assertion.assertion_type.as_str();
let Some(val) = &assertion.value else {
panic!(
"Ruby e2e generator: mock.* field '{field}' assertion '{assertion_type}' requires a numeric value in the fixture"
);
};
let Some(n) = val.as_u64() else {
panic!(
"Ruby e2e generator: mock.* field '{field}' assertion '{assertion_type}' requires a numeric value in the fixture, got {val:?}"
);
};
let escaped_field = escape_ruby_single(field);
match assertion_type {
"equals" | "count_equals" => {
let _ = writeln!(out, " expect({expr}).to eq({n}), 'expected {escaped_field} == {n}'");
}
"not_equals" => {
let _ = writeln!(
out,
" expect({expr}).not_to eq({n}), 'expected {escaped_field} != {n}'"
);
}
"greater_than" => {
let _ = writeln!(out, " expect({expr}).to be > {n}, 'expected {escaped_field} > {n}'");
}
"greater_than_or_equal" | "count_min" => {
let _ = writeln!(
out,
" expect({expr}).to be >= {n}, 'expected {escaped_field} >= {n}'"
);
}
"less_than" => {
let _ = writeln!(out, " expect({expr}).to be < {n}, 'expected {escaped_field} < {n}'");
}
"less_than_or_equal" => {
let _ = writeln!(
out,
" expect({expr}).to be <= {n}, 'expected {escaped_field} <= {n}'"
);
}
other => {
panic!("Ruby e2e generator: unsupported assertion type '{other}' on mock.* field '{field}'");
}
}
}
#[cfg(test)]
#[path = "assertion_mock_capture/tests.rs"]
mod tests;