Skip to content

test: add shared TestHandlebars helpers and adopt across render tests#771

Merged
sunng87 merged 1 commit into
masterfrom
refactor/test-util
Jul 12, 2026
Merged

test: add shared TestHandlebars helpers and adopt across render tests#771
sunng87 merged 1 commit into
masterfrom
refactor/test-util

Conversation

@sunng87

@sunng87 sunng87 commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Why

Most render tests repeated the same Handlebars::new() / register_template_string(..).is_ok() / render(..).unwrap() / assert_eq! dance, with .unwrap() hiding the cause of failures and assert_eq! of two long strings giving no hint of which template/data produced the mismatch. This adds a small shared helper and adopts it across the render-assert test suite.

The helper

A new off-by-default testing cargo feature exposes a #[doc(hidden)] pub mod testing containing a TestHandlebars extension trait over Registry:

method maps to
register(name, template) register_template_string (panics with context on compile error)
assert_render_template(t, data, expected) render_template
assert_render(name, data, expected) render
assert_render_template_ok(t, data) assert render_template is Ok
assert_render_template_err(t, data, msg?) assert render_template errors (+ optional substring)
assert_render_err(name, data, msg?) assert render errors

Method names mirror the underlying Registry calls 1:1, so there's no second vocabulary. On failure, assertions print the template and the data, so the cause is obvious without a debugger.

The module is compiled for this crate's own tests via a self dev-dependency (handlebars = { path = ".", features = ["testing"] }), and — behind the off-by-default feature — is also available to downstream users testing their own helpers/templates.

Scope

Converted: all 12 tests/*.rs integration files, and the unit-test modules in helpers/{helper_each,helper_if,helper_with,helper_lookup,helper_extras,string_helpers}, partial, plus the clean render-assert tests in registry and render. helper_macro is now table-driven.

Intentionally left untouched (a render helper doesn't help and forcing it would hurt): template AST tests, the grammar pest-parser tests, context navigation tests, render-of-element tests, decorators/inline, and the detailed error-inspection/mechanics tests in registry/render (e.g. those asserting on column_no / reason()).

Pre-existing issues fixed along the way

  • helper_each::test_nested_each_with_path_ups was missing its #[test] attribute (dead code). It now runs and passes.
  • partial::teset_partial_context_with_both_hash_and_param typo → renamed to test_….

Verification

  • cargo test and cargo test --all-features — all green (lib unit tests went up by 1 thanks to the revived dead test, confirming nothing was dropped).
  • cargo clippy --tests [--all-features] — zero warnings.
  • cargo build --features testing --no-default-features — the public path compiles for downstream users.

Notes

  • Net −~750 lines of test boilerplate (886 insertions, 1390 deletions across 24 files + new src/testing.rs).
  • This commit is unsigned (no GPG key available in the working environment). Please re-sign/amend if the repo requires it.

Introduce an off-by-default `testing` cargo feature exposing a
`#[doc(hidden)] pub mod testing` with a `TestHandlebars` extension trait
over `Registry`: `assert_render_template`, `assert_render`, `register`,
`assert_render_template_ok`, `assert_render_template_err`, and
`assert_render_err`. Method names mirror the underlying `Registry` calls
1:1. The module is compiled for this crate's own tests via a self
dev-dependency, and is also available to downstream users testing custom
helpers/templates. On failure, assertions print the template and the data
so the cause is obvious without a debugger.

Adopt the trait across the render-assert tests in `tests/` and the
`src/` unit-test modules, removing ~750 lines of
`register_template_string(..).is_ok()` / `.unwrap()` / `assert_eq!`
boilerplate. `helper_macro` becomes table-driven. Low-level tests
(template AST, grammar parser, context navigation, render-of-elements,
and the detailed error-inspection tests in registry/render) are
intentionally left untouched.

Two pre-existing issues fixed during the pass:
- `helper_each::test_nested_each_with_path_ups` was missing its `#[test]`
  attribute (dead code); it now runs and passes.
- `partial::teset_partial_context_with_both_hash_and_param` typo renamed
  to `test_...`.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 84.495% (-0.6%) from 85.052% — refactor/test-util into master

@sunng87
sunng87 merged commit 171b2a7 into master Jul 12, 2026
9 checks passed
@sunng87
sunng87 deleted the refactor/test-util branch July 12, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants