#!/usr/bin/env bats # tests/unit/template_vars.bats # Unit tests for template variable substitution patterns. # # These tests replicate the escape_sed_pattern_literal logic from trbldoc.sh # and verify that: # - the canonical {{ var }} format (two brackets, one space each side) is matched # - the old {{varname}} format (two brackets, no spaces) is NOT matched # - the layout-specific patterns ({{ title }}, {{ nav }}, {{ yield }}, # {{ breadcrumb }}) work correctly in sed address and substitution position load "../helpers/common.bash" setup() { setup_workspace } teardown() { teardown_workspace } # Replicate escape_sed_pattern_literal from trbldoc.sh. escape_pat() { printf '%s' "$1" | sed -e 's/[][\\.^$*+?(){}|]/\\&/g' } # ── chunk template variables ────────────────────────────────────────────────── @test "template vars: {{ src_repo }} is matched and substituted" { local pat result pat=$(escape_pat '{{ src_repo }}') result=$(printf '%s' 'Link: {{ src_repo }}/file.c' | sed -E "s|$pat|https://example.com|g") assert_equal "$result" 'Link: https://example.com/file.c' } @test "template vars: {{src_repo}} without spaces is NOT matched" { local pat result pat=$(escape_pat '{{ src_repo }}') result=$(printf '%s' 'Old: {{src_repo}}' | sed -E "s|$pat|https://example.com|g") assert_equal "$result" 'Old: {{src_repo}}' } @test "template vars: {{ toc }} is matched and substituted" { local pat result pat=$(escape_pat '{{ toc }}') result=$(printf '%s' 'Nav: {{ toc }}' | sed -E "s|$pat|
body content
\n' > "$tmpbody" printf '{{ yield }}\n' > "$tmplayout" output=$(sed -E \ -e "/\\{\\{ yield \\}\\}/r $tmpbody" \ -e "/\\{\\{ yield \\}\\}/d" \ "$tmplayout") rm -f "$tmpbody" "$tmplayout" assert_equal "$output" 'body content
' } @test "template vars: layout old {{{yield}}} triple-brace is NOT matched by {{ yield }} pattern" { local tmpbody tmplayout output tmpbody=$(mktemp) tmplayout=$(mktemp) printf 'body content
\n' > "$tmpbody" printf '{{{yield}}}\n' > "$tmplayout" output=$(sed -E \ -e "/\\{\\{ yield \\}\\}/r $tmpbody" \ -e "/\\{\\{ yield \\}\\}/d" \ "$tmplayout") rm -f "$tmpbody" "$tmplayout" # {{{yield}}} should remain unchanged — the new pattern only matches {{ yield }} assert_equal "$output" '{{{yield}}}' } @test "template vars: substitution value containing special sed chars is safe" { # Verify that replacement values with & and \ don't corrupt the output # (this exercises the escape_sed_replacement_literal path) local pat esc_repl result pat=$(escape_pat '{{ src_repo }}') # Simulate escape_sed_replacement_literal for a URL containing & esc_repl=$(printf '%s' 'https://example.com/a&b' | sed -e 's/[\\&|]/\\&/g') result=$(printf '%s' '{{ src_repo }}/path' | sed -E "s|$pat|$esc_repl|g") assert_equal "$result" 'https://example.com/a&b/path' }