test: add shared TestHandlebars helpers and adopt across render tests#771
Merged
Conversation
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_...`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 andassert_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
testingcargo feature exposes a#[doc(hidden)] pub mod testingcontaining aTestHandlebarsextension trait overRegistry:register(name, template)register_template_string(panics with context on compile error)assert_render_template(t, data, expected)render_templateassert_render(name, data, expected)renderassert_render_template_ok(t, data)render_templateisOkassert_render_template_err(t, data, msg?)render_templateerrors (+ optional substring)assert_render_err(name, data, msg?)rendererrorsMethod names mirror the underlying
Registrycalls 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/*.rsintegration files, and the unit-test modules inhelpers/{helper_each,helper_if,helper_with,helper_lookup,helper_extras,string_helpers},partial, plus the clean render-assert tests inregistryandrender.helper_macrois 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 inregistry/render(e.g. those asserting oncolumn_no/reason()).Pre-existing issues fixed along the way
helper_each::test_nested_each_with_path_upswas missing its#[test]attribute (dead code). It now runs and passes.partial::teset_partial_context_with_both_hash_and_paramtypo → renamed totest_….Verification
cargo testandcargo 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
src/testing.rs).