aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/template_vars.bats
blob: 5d95667859676855ad9889f5092c34f71afda318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/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|<ul><li>nav</li></ul>|g")
  assert_equal "$result" 'Nav: <ul><li>nav</li></ul>'
}

@test "template vars: {{toc}} without spaces is NOT matched" {
  local pat result
  pat=$(escape_pat '{{ toc }}')
  result=$(printf '%s' 'Old: {{toc}}' | sed -E "s|$pat|<ul><li>nav</li></ul>|g")
  assert_equal "$result" 'Old: {{toc}}'
}

@test "template vars: {{ ref_string }} is matched and substituted" {
  local pat result
  pat=$(escape_pat '{{ ref_string }}')
  result=$(printf '%s' "See: {{ ref_string }}." | sed -E "s|$pat|<a href='/x'>x</a>|g")
  assert_equal "$result" "See: <a href='/x'>x</a>."
}

@test "template vars: {{ref_string}} without spaces is NOT matched" {
  local pat result
  pat=$(escape_pat '{{ ref_string }}')
  result=$(printf '%s' 'Old: {{ref_string}}' | sed -E "s|$pat|<a href='/x'>x</a>|g")
  assert_equal "$result" 'Old: {{ref_string}}'
}

@test "template vars: multiple {{ src_repo }} occurrences are all substituted" {
  local pat result
  pat=$(escape_pat '{{ src_repo }}')
  result=$(printf '%s' '{{ src_repo }}/a and {{ src_repo }}/b' | sed -E "s|$pat|https://r|g")
  assert_equal "$result" 'https://r/a and https://r/b'
}

# ── layout template variables ─────────────────────────────────────────────────

@test "template vars: layout {{ title }} is matched and substituted" {
  local result
  result=$(printf '%s' '<title>{{ title }}</title>' \
    | sed -E "s|\\{\\{ title \\}\\}|My Page|g")
  assert_equal "$result" '<title>My Page</title>'
}

@test "template vars: layout {{title}} without spaces is NOT matched" {
  local result
  result=$(printf '%s' '<title>{{title}}</title>' \
    | sed -E "s|\\{\\{ title \\}\\}|My Page|g")
  assert_equal "$result" '<title>{{title}}</title>'
}

@test "template vars: layout {{ breadcrumb }} is matched and substituted" {
  local result
  result=$(printf '%s' '<h1>{{ breadcrumb }}</h1>' \
    | sed -E "s|\\{\\{ breadcrumb \\}\\}|Section|g")
  assert_equal "$result" '<h1>Section</h1>'
}

@test "template vars: layout {{breadcrumb}} without spaces is NOT matched" {
  local result
  result=$(printf '%s' '{{breadcrumb}}' \
    | sed -E "s|\\{\\{ breadcrumb \\}\\}|Section|g")
  assert_equal "$result" '{{breadcrumb}}'
}

@test "template vars: layout {{ nav }} is matched and substituted" {
  local result
  result=$(printf '%s' '<ul>{{ nav }}</ul>' \
    | sed -E "s|\\{\\{ nav \\}\\}|<li>item</li>|g")
  assert_equal "$result" '<ul><li>item</li></ul>'
}

@test "template vars: layout old {{> nav}} partial syntax is NOT matched by {{ nav }} pattern" {
  local result
  result=$(printf '%s' '<ul>{{> nav}}</ul>' \
    | sed -E "s|\\{\\{ nav \\}\\}|<li>item</li>|g")
  assert_equal "$result" '<ul>{{> nav}}</ul>'
}

@test "template vars: layout {{ yield }} works as sed address to include a file" {
  local tmpbody tmplayout output
  tmpbody=$(mktemp)
  tmplayout=$(mktemp)
  printf '<p>body content</p>\n' > "$tmpbody"
  printf '{{ yield }}\n' > "$tmplayout"
  output=$(sed -E \
    -e "/\\{\\{ yield \\}\\}/r $tmpbody" \
    -e "/\\{\\{ yield \\}\\}/d" \
    "$tmplayout")
  rm -f "$tmpbody" "$tmplayout"
  assert_equal "$output" '<p>body content</p>'
}

@test "template vars: layout old {{{yield}}} triple-brace is NOT matched by {{ yield }} pattern" {
  local tmpbody tmplayout output
  tmpbody=$(mktemp)
  tmplayout=$(mktemp)
  printf '<p>body content</p>\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'
}