Repository navigation
Expand file tree
/
Copy pathconvert_test.go
More file actions
393 lines (371 loc) · 11.8 KB
/
Copy pathconvert_test.go
File metadata and controls
393 lines (371 loc) · 11.8 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package calcmark
import (
"strings"
"testing"
)
// simpleTemplate is a minimal Go template for testing template wrapping.
const simpleTemplate = `<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
{{if .Content}}
<div class="embedded">{{.Content}}</div>
{{else}}
{{range .Blocks}}
{{if eq .Type "calculation"}}
<div class="calc">
{{range .SourceLines}}<div>{{.Source}}{{if .Result}} = {{.Result}}{{end}}</div>
{{end}}
</div>
{{else}}
<div class="text">{{.HTML}}</div>
{{end}}
{{end}}
{{end}}
</body>
</html>`
// T1: cm mode, markdown output (existing behavior, must not regress)
func TestConvert_CM_Markdown(t *testing.T) {
input := "price = 100 USD\ntax = 8.5%\ntotal = price + tax"
result, err := Convert(input, Options{
Format: "md",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result == "" {
t.Fatal("expected non-empty result")
}
// Markdown output should contain the variable names and results
if !strings.Contains(result, "price") {
t.Error("expected result to contain 'price'")
}
if !strings.Contains(result, "total") {
t.Error("expected result to contain 'total'")
}
}
// T2: cm mode, html output with template
func TestConvert_CM_HTMLWithTemplate(t *testing.T) {
input := "price = 100 USD"
result, err := Convert(input, Options{
Format: "html",
Template: simpleTemplate,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should be a full HTML document using our template
if !strings.Contains(result, "<!DOCTYPE html>") {
t.Error("expected full HTML document with DOCTYPE")
}
if !strings.Contains(result, "<title>Test</title>") {
t.Error("expected template title")
}
if !strings.Contains(result, "price") {
t.Error("expected result to contain 'price'")
}
}
// T3: cm mode, html output without template → fragment
func TestConvert_CM_HTMLFragment(t *testing.T) {
input := "price = 100 USD"
result, err := Convert(input, Options{
Format: "html",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result == "" {
t.Fatal("expected non-empty result")
}
// Default template includes DOCTYPE — a fragment would not.
// However, the default go-calcmark HTML template IS a full document.
// The plan says "fragment by default" means no template = use default template.
// Actually, re-reading the plan: "HTML output returns fragments by default —
// no <html>/<head>/<body>. An optional Go template wraps fragments into full documents."
// So with no template, we should get a fragment.
if strings.Contains(result, "<!DOCTYPE") {
t.Error("expected HTML fragment (no DOCTYPE), got full document")
}
if strings.Contains(result, "<html") {
t.Error("expected HTML fragment (no <html> tag)")
}
// Should still contain the formatted content
if !strings.Contains(result, "price") {
t.Error("expected result to contain 'price'")
}
}
// T10: locale option works in both modes
func TestConvert_CM_Locale(t *testing.T) {
input := "price = 1234.56 EUR"
result, err := Convert(input, Options{
Format: "md",
Locale: "de-DE",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// German locale should use comma as decimal separator
// and period as thousands separator: 1.234,56
if !strings.Contains(result, "1.234,56") {
t.Errorf("expected German-formatted number '1.234,56' in result, got:\n%s", result)
}
}
// T3b: cm mode, default format should be html
func TestConvert_CM_DefaultFormat(t *testing.T) {
input := "x = 42"
result, err := Convert(input, Options{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Default format is html, no template = fragment
if result == "" {
t.Fatal("expected non-empty result")
}
}
// Test that empty input returns an error
func TestConvert_EmptyInput(t *testing.T) {
_, err := Convert("", Options{Format: "md"})
if err == nil {
t.Error("expected error for empty input")
}
}
// Test that whitespace-only input returns an error
func TestConvert_WhitespaceInput(t *testing.T) {
_, err := Convert(" \n\t\n ", Options{Format: "md"})
if err == nil {
t.Error("expected error for whitespace-only input")
}
}
// Test that unknown format returns an error
func TestConvert_UnknownFormat(t *testing.T) {
_, err := Convert("x = 1", Options{Format: "xml"})
if err == nil {
t.Error("expected error for unknown format")
}
if !strings.Contains(err.Error(), "unknown format") {
t.Errorf("expected 'unknown format' error, got: %v", err)
}
}
// --- Embedded mode tests ---
// T4: embedded mode, markdown output (blocks replaced, prose unchanged)
func TestConvert_Embedded_Markdown(t *testing.T) {
input := "# Budget\n\n```cm\nprice = 100 USD\n```\n\nSome prose.\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "md",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Prose should be preserved
if !strings.Contains(result, "# Budget") {
t.Error("expected heading to be preserved")
}
if !strings.Contains(result, "Some prose.") {
t.Error("expected prose to be preserved")
}
// CalcMark block should be replaced with evaluated output
if strings.Contains(result, "```cm") {
t.Error("expected cm fence to be replaced with evaluated output")
}
// Should contain the evaluated result (price = 100 USD → table or annotation)
if !strings.Contains(result, "price") {
t.Error("expected evaluated output to contain 'price'")
}
}
// T8: embedded mode, block with evaluation error → inline error blockquote
func TestConvert_Embedded_BlockError(t *testing.T) {
input := "# Test\n\n```cm\nx = unknown_var + 1\n```\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "md",
})
// Error should be non-nil (signals block failures)
if err == nil {
t.Error("expected non-nil error for block with evaluation issues")
}
// But result should still contain output (partial success)
if result == "" {
t.Fatal("expected non-empty result even with block errors")
}
// Should contain inline error blockquote
if !strings.Contains(result, "# Test") {
t.Error("expected heading to be preserved")
}
}
// T9: embedded mode preserves frontmatter passthrough
func TestConvert_Embedded_FrontmatterPreserved(t *testing.T) {
input := "---\ntitle: Test Doc\n---\n\n# Doc\n\n```cm\nx = 42\n```\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "md",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Frontmatter should be preserved as-is
if !strings.Contains(result, "---\ntitle: Test Doc\n---") {
t.Errorf("expected frontmatter to be preserved, got:\n%s", result)
}
// Heading should be preserved
if !strings.Contains(result, "# Doc") {
t.Error("expected heading to be preserved")
}
}
// T5: embedded mode, html fragment (new)
func TestConvert_Embedded_HTMLFragment(t *testing.T) {
input := "# Budget\n\n```cm\nprice = 100 USD\n```\n\nSome prose.\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "html",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should be an HTML fragment (no DOCTYPE, no <html> tag)
if strings.Contains(result, "<!DOCTYPE") {
t.Error("expected HTML fragment, got full document")
}
// Should contain goldmark-rendered heading
if !strings.Contains(result, "<h1") {
t.Error("expected <h1> heading in goldmark output")
}
// Should contain prose as paragraph
if !strings.Contains(result, "Some prose.") {
t.Error("expected prose paragraph in output")
}
// Should contain evaluated CalcMark output (rendered as HTML table or similar)
if !strings.Contains(result, "price") {
t.Error("expected evaluated CalcMark content")
}
}
// T6: embedded mode, html with template (what Lark uses)
func TestConvert_Embedded_HTMLWithTemplate(t *testing.T) {
input := "# Budget\n\n```cm\nprice = 100 USD\n```\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "html",
Template: simpleTemplate,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should be a full HTML document using our template
if !strings.Contains(result, "<!DOCTYPE html>") {
t.Error("expected full HTML document with DOCTYPE")
}
if !strings.Contains(result, "<title>Test</title>") {
t.Error("expected template title")
}
// Content should be inside the embedded div
if !strings.Contains(result, `class="embedded"`) {
t.Error("expected embedded content wrapper from template")
}
// Should contain goldmark-rendered content
if !strings.Contains(result, "<h1") {
t.Error("expected <h1> heading in goldmark output")
}
}
// T7: embedded mode, no calcmark blocks → passthrough
func TestConvert_Embedded_NoBlocks(t *testing.T) {
input := "# Just Markdown\n\nNo calcmark here.\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "html",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should contain goldmark-rendered heading and paragraph
if !strings.Contains(result, "<h1") {
t.Error("expected <h1> heading")
}
if !strings.Contains(result, "No calcmark here.") {
t.Error("expected prose content")
}
}
// T9b: embedded mode, frontmatter stripped from HTML output
func TestConvert_Embedded_FrontmatterStrippedFromHTML(t *testing.T) {
input := "---\ntitle: Test Doc\n---\n\n# Doc\n\n```cm\nx = 42\n```\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "html",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Frontmatter should NOT appear in the HTML output
if strings.Contains(result, "title: Test Doc") {
t.Error("expected frontmatter to be stripped from HTML output")
}
// The heading should still be there
if !strings.Contains(result, "<h1") {
t.Error("expected heading in HTML output")
}
}
// T4b: embedded mode with multiple blocks
func TestConvert_Embedded_MultipleBlocks(t *testing.T) {
input := "# Report\n\n```cm\na = 10\n```\n\nMiddle text.\n\n```cm\nb = 20\n```\n\nEnd.\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "md",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(result, "# Report") {
t.Error("expected heading preserved")
}
if !strings.Contains(result, "Middle text.") {
t.Error("expected middle prose preserved")
}
if !strings.Contains(result, "End.") {
t.Error("expected end prose preserved")
}
// Both cm fences should be gone
if strings.Contains(result, "```cm") {
t.Error("expected all cm fences to be replaced")
}
}
// Footnotes are standard Markdown and must render as proper HTML links.
func TestConvert_Embedded_Footnotes(t *testing.T) {
input := "Text with a footnote[^1].\n\n[^1]: This is the footnote content.\n"
result, err := Convert(input, Options{
Mode: Embedded,
Format: "html",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Footnote reference should become a link, not literal "[^1]"
if strings.Contains(result, "[^1]") {
t.Error("footnote not rendered — literal [^1] found in HTML output")
}
// Should contain a footnote backref or footnote section
if !strings.Contains(result, "footnote") {
t.Error("expected footnote HTML elements in output")
}
}
// Cross-block footnote resolution (issue #129). Reference and
// definition live in DIFFERENT text blocks split by a calc block in
// the default (CM) flow; the reference must still resolve to a
// clickable link rather than render as literal `[^note1]`.
func TestConvert_CM_FootnotesAcrossCalcBlocks(t *testing.T) {
// CalcMark document shape: text block, then a calc block (a
// bare assignment line is one), then the footnote definition
// in a second text block.
input := "Reference here.[^note1]\n\nx = 42\n\n[^note1]: Cross-block footnote definition.\n"
result, err := Convert(input, Options{
Mode: CM,
Format: "html",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if strings.Contains(result, "[^note1]") {
t.Errorf("footnote ref unresolved — literal [^note1] in output:\n%s", result)
}
if !strings.Contains(result, "footnote") {
t.Errorf("expected a footnote section/backref in output:\n%s", result)
}
}