{#- Ruby test_function template (it block)
Context variables:
- test_name: sanitized fixture ID
- description: fixture description as a Ruby string literal (already quoted)
- expects_error: bool
- raise_error_clause: the `raise_error(...)` matcher expression, fully rendered
(plain `raise_error(RuntimeError)` when no error value is declared, or that plus
a message/class-name block when one is)
- setup_lines: list of setup line strings
- call_expr: the function call expression
- result_var: variable to store result
- assertions_rendered: pre-rendered assertion blocks
- has_usable: bool - whether the rendered `assertions_rendered` body contains a
line that can execute (false when the fixture's only assertion is
`not_error`/`error`, or every field assertion resolved to a skip comment).
Derived from the rendered text, never guessed from the fixture. Drives the
`expect(result).not_to be_nil` fallback below, and — for `returns_void` calls,
which bind no result to assert on — the `expect { .. }.not_to raise_error`
wrapper that makes "the call does not raise" a real, failable expectation
instead of an implicit one no runner can see.
- returns_void: bool - whether the call returns unit (Rust `()` → Ruby nil)
- client_factory: optional client factory expression
- fixture_id: fixture ID (for client factory)
- call_receiver: module or class name for the call
- is_clear_op: bool - whether this is a clear operation
- post_clear_list_call: expression to call the corresponding list function (empty if not a clear op)
- teardown_lines: list of teardown statements emitted after assertions (e.g. trait-bridge unregister calls)
#}
it {{ description }} do
{% if client_factory %}
{% if has_mock_and_key %}
api_key = ENV['{{ api_key_var }}']
if api_key && !api_key.empty?
warn "{{ test_name }}: using real API ({{ api_key_var }} is set)"
client = {{ call_receiver }}.{{ client_factory }}(api_key)
else
warn "{{ test_name }}: using mock server ({{ api_key_var }} not set)"
mock_url = "#{ENV['MOCK_SERVER_URL']}/fixtures/{{ fixture_id }}"
client = {{ call_receiver }}.{{ client_factory }}('test-key', mock_url)
end
{% elif has_mock %}
client = {{ call_receiver }}.{{ client_factory }}('test-key', ENV.fetch('MOCK_SERVER_URL') + '/fixtures/{{ fixture_id }}')
{% elif api_key_var %}
api_key = ENV['{{ api_key_var }}']
skip '{{ api_key_var }} not set' unless api_key
client = {{ call_receiver }}.{{ client_factory }}(api_key)
{% else %}
client = {{ call_receiver }}.{{ client_factory }}('test-key')
{% endif %}
{% endif %}
{% if expects_error %}
expect {
{% for line in setup_lines %}
{{ line }}
{% endfor %}
{{ call_expr }}
}.to {{ raise_error_clause }}
{% else %}
{% for line in setup_lines %}
{{ line }}
{% endfor %}
{% if returns_void %}
{% if has_usable %}
{{ call_expr }}
{% else %}
expect { {{ call_expr }} }.not_to raise_error
{% endif %}
{% else %}
{{ result_var }} = {{ call_expr }}
{% endif %}
{{ assertions_rendered }}
{% if is_clear_op %}
# Verify the list is empty after clear
list_result = {{ post_clear_list_call }}
expect(list_result).to be_empty
{% elif not has_usable and not returns_void %}
expect({{ result_var }}).not_to be_nil
{% endif %}
{% endif %}
{% for line in teardown_lines %}
{{ line }}
{% endfor %}
end