Skip to content

Commit 14b6201

Browse files
authored
test(spec): add whitespace control spec tests for tilde (~) syntax (#455)
Add comprehensive spec tests for Handlebars whitespace trimming using the tilde (~) syntax. The tilde character can be placed inside mustaches to strip whitespace from the adjacent side. New spec/whitespace.yaml covers: - Basic tilde on both sides: {{~name~}} strips whitespace before/after - Left-side tilde only: {{~name}} strips whitespace before - Right-side tilde only: {{name~}} strips whitespace after - Multiline whitespace stripping in div context - Block helpers (#each, #if) with whitespace control - Preserve whitespace without tilde (baseline behavior) Extension fields (ext) already tested in metadata.yaml with test case "extension fields are parsed and added to 'ext'" which verifies that frontmatter like `myext.foo: 123` becomes accessible as `ext['myext']['foo']`. Verified across all language implementations. All 8 whitespace tests pass across: - JavaScript/TypeScript (76 spec tests total) - Python dotpromptz (79 spec tests total) - Go (spec_test.go) - Rust (SpecTest_whitespace) - Java (SpecTest_whitespace) Files changed: - spec/whitespace.yaml (new) - java/com/google/dotprompt/BUILD.bazel - python/dotpromptz/tests/dotpromptz/spec_test.py - rs/dotprompt/BUILD.bazel
1 parent f641a62 commit 14b6201

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

java/com/google/dotprompt/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ java_spec_test(
134134
spec_file = "//spec:helpers/unlessEquals.yaml",
135135
)
136136

137+
java_spec_test(
138+
name = "SpecTest_whitespace",
139+
spec_file = "//spec:whitespace.yaml",
140+
)
141+
137142
java_test(
138143
name = "ResolveToolsConcurrencyTest",
139144
srcs = ["ResolveToolsConcurrencyTest.java"],

python/dotpromptz/tests/dotpromptz/spec_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@
138138
'spec/helpers/role.yaml',
139139
'spec/helpers/unlessEquals.yaml',
140140
'spec/helpers/section.yaml',
141-
'spec/variables.yaml',
141+
'spec/metadata.yaml',
142142
'spec/partials.yaml',
143143
'spec/picoschema.yaml',
144-
'spec/metadata.yaml',
144+
'spec/variables.yaml',
145+
'spec/whitespace.yaml',
145146
]
146147

147148
# Counters for test class and test method names.

rs/dotprompt/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,9 @@ rust_spec_test(
111111
spec_file = "//spec:helpers/unlessEquals.yaml",
112112
deps = [":dotprompt"],
113113
)
114+
115+
rust_spec_test(
116+
name = "SpecTest_whitespace",
117+
spec_file = "//spec:whitespace.yaml",
118+
deps = [":dotprompt"],
119+
)

spec/whitespace.yaml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
# Tests for Handlebars whitespace control using the tilde (~) syntax.
18+
# The tilde character can be placed inside mustaches to strip whitespace
19+
# from the adjacent side of the mustache.
20+
#
21+
# Syntax:
22+
# {{~ expr }} - strips whitespace BEFORE the expression
23+
# {{ expr ~}} - strips whitespace AFTER the expression
24+
# {{~ expr ~}} - strips whitespace on BOTH sides
25+
26+
- name: whitespace_tilde_basic
27+
template: |
28+
Hello {{~name~}} World
29+
tests:
30+
- desc: strips whitespace on both sides of variable
31+
data:
32+
input: { name: "Beautiful" }
33+
expect:
34+
messages:
35+
- role: user
36+
content: [{ text: "HelloBeautifulWorld\n" }]
37+
38+
- name: whitespace_tilde_left
39+
template: |
40+
Hello {{~name}} World
41+
tests:
42+
- desc: strips whitespace before variable only
43+
data:
44+
input: { name: "Beautiful" }
45+
expect:
46+
messages:
47+
- role: user
48+
content: [{ text: "HelloBeautiful World\n" }]
49+
50+
- name: whitespace_tilde_right
51+
template: |
52+
Hello {{name~}} World
53+
tests:
54+
- desc: strips whitespace after variable only
55+
data:
56+
input: { name: "Beautiful" }
57+
expect:
58+
messages:
59+
- role: user
60+
content: [{ text: "Hello BeautifulWorld\n" }]
61+
62+
- name: whitespace_tilde_multiline
63+
template: |
64+
<div>
65+
{{~value~}}
66+
</div>
67+
tests:
68+
- desc: strips whitespace in multiline context
69+
data:
70+
input: { value: "Content" }
71+
expect:
72+
messages:
73+
- role: user
74+
content: [{ text: "<div>Content</div>\n" }]
75+
76+
- name: whitespace_tilde_block_each
77+
template: |
78+
Items:{{#each items~}}
79+
- {{this}}
80+
{{~/each}}Done
81+
tests:
82+
- desc: strips whitespace in each block
83+
data:
84+
input: { items: ["one", "two", "three"] }
85+
expect:
86+
messages:
87+
- role: user
88+
content: [{ text: "Items:- one- two- threeDone\n" }]
89+
90+
- name: whitespace_tilde_block_if
91+
template: |
92+
Start
93+
{{~#if show~}}
94+
Content
95+
{{~/if~}}
96+
End
97+
tests:
98+
- desc: strips whitespace around if block when true
99+
data:
100+
input: { show: true }
101+
expect:
102+
messages:
103+
- role: user
104+
content: [{ text: "StartContentEnd\n" }]
105+
106+
- desc: strips whitespace around if block when false
107+
data:
108+
input: { show: false }
109+
expect:
110+
messages:
111+
- role: user
112+
content: [{ text: "StartEnd\n" }]
113+
114+
- name: whitespace_no_tilde
115+
template: |
116+
Hello {{ name }} World
117+
tests:
118+
- desc: preserves whitespace without tilde
119+
data:
120+
input: { name: "Beautiful" }
121+
expect:
122+
messages:
123+
- role: user
124+
content: [{ text: "Hello Beautiful World\n" }]

0 commit comments

Comments
 (0)