tmpltool 1.5.0

A fast and simple command-line template rendering tool using MiniJinja templates with environment variables
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use minijinja::Environment;
use std::path::PathBuf;
use tmpltool::{TemplateContext, functions::register_all};

fn render_template(template: &str) -> String {
    let mut env = Environment::new();
    let ctx = TemplateContext::new(PathBuf::from("."), false);
    register_all(&mut env, ctx);

    let tmpl = env.template_from_str(template).unwrap();
    tmpl.render(()).unwrap()
}

// ==================== regex_replace Tests ====================

#[test]
fn test_regex_replace_basic() {
    let result = render_template(
        r#"{{ regex_replace(string="hello123world", pattern="[0-9]+", replacement="-") }}"#,
    );
    assert_eq!(result, "hello-world");
}

#[test]
fn test_regex_replace_whitespace() {
    let result = render_template(
        r#"{{ regex_replace(string="foo bar baz", pattern="\\s+", replacement="_") }}"#,
    );
    assert_eq!(result, "foo_bar_baz");
}

#[test]
fn test_regex_replace_no_match() {
    let result = render_template(
        r#"{{ regex_replace(string="hello", pattern="[0-9]+", replacement="-") }}"#,
    );
    assert_eq!(result, "hello");
}

#[test]
fn test_regex_replace_capture_groups() {
    let result = render_template(
        r#"{{ regex_replace(string="hello world", pattern="(\\w+) (\\w+)", replacement="$2 $1") }}"#,
    );
    assert_eq!(result, "world hello");
}

// ==================== regex_match Tests ====================

#[test]
fn test_regex_match_found() {
    let result = render_template(r#"{{ regex_match(string="hello123", pattern="[0-9]+") }}"#);
    assert_eq!(result, "true");
}

#[test]
fn test_regex_match_not_found() {
    let result = render_template(r#"{{ regex_match(string="hello", pattern="[0-9]+") }}"#);
    assert_eq!(result, "false");
}

#[test]
fn test_regex_match_email() {
    let result = render_template(
        r#"{{ regex_match(string="test@example.com", pattern="^[\\w.-]+@[\\w.-]+\\.\\w+$") }}"#,
    );
    assert_eq!(result, "true");
}

// ==================== regex_find_all Tests ====================

#[test]
fn test_regex_find_all_numbers() {
    let result =
        render_template(r#"{{ regex_find_all(string="a1b2c3", pattern="[0-9]+") | tojson }}"#);
    assert_eq!(result, r#"["1","2","3"]"#);
}

#[test]
fn test_regex_find_all_words() {
    let result =
        render_template(r#"{{ regex_find_all(string="hello world", pattern="\\w+") | tojson }}"#);
    assert_eq!(result, r#"["hello","world"]"#);
}

#[test]
fn test_regex_find_all_no_match() {
    let result =
        render_template(r#"{{ regex_find_all(string="hello", pattern="[0-9]+") | tojson }}"#);
    assert_eq!(result, "[]");
}

// ==================== substring Tests ====================

#[test]
fn test_substring_with_length() {
    let result = render_template(r#"{{ substring(string="hello world", start=0, length=5) }}"#);
    assert_eq!(result, "hello");
}

#[test]
fn test_substring_without_length() {
    let result = render_template(r#"{{ substring(string="hello world", start=6) }}"#);
    assert_eq!(result, "world");
}

#[test]
fn test_substring_negative_start() {
    let result = render_template(r#"{{ substring(string="hello world", start=-5) }}"#);
    assert_eq!(result, "world");
}

#[test]
fn test_substring_out_of_bounds() {
    let result = render_template(r#"{{ substring(string="hello", start=10) }}"#);
    assert_eq!(result, "");
}

// ==================== contains Tests ====================

#[test]
fn test_contains_found() {
    let result = render_template(r#"{{ contains(string="hello world", substring="world") }}"#);
    assert_eq!(result, "true");
}

#[test]
fn test_contains_not_found() {
    let result = render_template(r#"{{ contains(string="hello world", substring="foo") }}"#);
    assert_eq!(result, "false");
}

#[test]
fn test_contains_empty_substring() {
    let result = render_template(r#"{{ contains(string="hello", substring="") }}"#);
    assert_eq!(result, "true");
}

// ==================== index_of Tests ====================

#[test]
fn test_index_of_found() {
    let result = render_template(r#"{{ index_of(string="hello world", substring="world") }}"#);
    assert_eq!(result, "6");
}

#[test]
fn test_index_of_not_found() {
    let result = render_template(r#"{{ index_of(string="hello world", substring="foo") }}"#);
    assert_eq!(result, "-1");
}

#[test]
fn test_index_of_at_start() {
    let result = render_template(r#"{{ index_of(string="hello", substring="hello") }}"#);
    assert_eq!(result, "0");
}

// ==================== count_occurrences Tests ====================

#[test]
fn test_count_occurrences_multiple() {
    let result = render_template(
        r#"{{ count_occurrences(string="hello hello hello", substring="hello") }}"#,
    );
    assert_eq!(result, "3");
}

#[test]
fn test_count_occurrences_none() {
    let result = render_template(r#"{{ count_occurrences(string="hello", substring="world") }}"#);
    assert_eq!(result, "0");
}

#[test]
fn test_count_occurrences_single_char() {
    let result = render_template(r#"{{ count_occurrences(string="aaa", substring="a") }}"#);
    assert_eq!(result, "3");
}

// ==================== truncate Tests ====================

#[test]
fn test_truncate_with_ellipsis() {
    let result = render_template(r#"{{ truncate(string="Hello World", length=8) }}"#);
    assert_eq!(result, "Hello...");
}

#[test]
fn test_truncate_custom_suffix() {
    let result = render_template(r#"{{ truncate(string="Hello World", length=8, suffix=">>") }}"#);
    assert_eq!(result, "Hello >>");
}

#[test]
fn test_truncate_no_truncation_needed() {
    let result = render_template(r#"{{ truncate(string="Hi", length=10) }}"#);
    assert_eq!(result, "Hi");
}

#[test]
fn test_truncate_exact_length() {
    let result = render_template(r#"{{ truncate(string="Hello", length=5) }}"#);
    assert_eq!(result, "Hello");
}

// ==================== word_count Tests ====================

#[test]
fn test_word_count_simple() {
    let result = render_template(r#"{{ word_count(string="Hello World") }}"#);
    assert_eq!(result, "2");
}

#[test]
fn test_word_count_multiple_spaces() {
    let result = render_template(r#"{{ word_count(string="  one   two   three  ") }}"#);
    assert_eq!(result, "3");
}

#[test]
fn test_word_count_empty() {
    let result = render_template(r#"{{ word_count(string="") }}"#);
    assert_eq!(result, "0");
}

#[test]
fn test_word_count_single() {
    let result = render_template(r#"{{ word_count(string="hello") }}"#);
    assert_eq!(result, "1");
}

// ==================== split_lines Tests ====================

#[test]
fn test_split_lines_basic() {
    let result = render_template("{{ split_lines(string=\"line1\\nline2\\nline3\") | tojson }}");
    assert_eq!(result, r#"["line1","line2","line3"]"#);
}

#[test]
fn test_split_lines_single() {
    let result = render_template(r#"{{ split_lines(string="single line") | tojson }}"#);
    assert_eq!(result, r#"["single line"]"#);
}

#[test]
fn test_split_lines_empty() {
    let result = render_template(r#"{{ split_lines(string="") | tojson }}"#);
    assert_eq!(result, "[]");
}

// ==================== Error Cases ====================

#[test]
fn test_regex_replace_invalid_pattern() {
    let mut env = Environment::new();
    let ctx = TemplateContext::new(PathBuf::from("."), false);
    register_all(&mut env, ctx);

    let tmpl = env
        .template_from_str(
            r#"{{ regex_replace(string="test", pattern="[invalid", replacement="x") }}"#,
        )
        .unwrap();
    let result = tmpl.render(());
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Invalid regex"));
}

#[test]
fn test_count_occurrences_empty_substring() {
    let mut env = Environment::new();
    let ctx = TemplateContext::new(PathBuf::from("."), false);
    register_all(&mut env, ctx);

    let tmpl = env
        .template_from_str(r#"{{ count_occurrences(string="test", substring="") }}"#)
        .unwrap();
    let result = tmpl.render(());
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("cannot be empty"));
}

// ==================== wrap Tests ====================

#[test]
fn test_wrap_basic() {
    let result = render_template(
        r#"{{ wrap(string="The quick brown fox jumps over the lazy dog", width=20) }}"#,
    );
    assert!(result.contains("The quick brown fox"));
    assert!(result.contains("\n"));
}

#[test]
fn test_wrap_with_indent() {
    let result =
        render_template(r#"{{ wrap(string="Hello World Example Test", width=10, indent="  ") }}"#);
    assert!(result.contains("  "));
}

#[test]
fn test_wrap_single_word() {
    let result = render_template(r#"{{ wrap(string="Hello", width=20) }}"#);
    assert_eq!(result, "Hello");
}

#[test]
fn test_wrap_zero_width_error() {
    let mut env = Environment::new();
    let ctx = TemplateContext::new(PathBuf::from("."), false);
    register_all(&mut env, ctx);

    let tmpl = env
        .template_from_str(r#"{{ wrap(string="test", width=0) }}"#)
        .unwrap();
    let result = tmpl.render(());
    assert!(result.is_err());
}

// ==================== center Tests ====================

#[test]
fn test_center_basic() {
    let result = render_template(r#"{{ center(string="hello", width=11) }}"#);
    assert_eq!(result, "   hello   ");
}

#[test]
fn test_center_custom_char() {
    let result = render_template(r#"{{ center(string="hi", width=10, char="-") }}"#);
    assert_eq!(result, "----hi----");
}

#[test]
fn test_center_string_longer_than_width() {
    let result = render_template(r#"{{ center(string="hello world", width=5) }}"#);
    assert_eq!(result, "hello world");
}

#[test]
fn test_center_odd_padding() {
    let result = render_template(r#"{{ center(string="hi", width=7, char="*") }}"#);
    assert_eq!(result, "**hi***");
}

// ==================== sentence_case Tests ====================

#[test]
fn test_sentence_case_lowercase() {
    let result = render_template(r#"{{ sentence_case(string="hello world") }}"#);
    assert_eq!(result, "Hello world");
}

#[test]
fn test_sentence_case_uppercase() {
    let result = render_template(r#"{{ sentence_case(string="HELLO WORLD") }}"#);
    assert_eq!(result, "Hello world");
}

#[test]
fn test_sentence_case_mixed() {
    let result = render_template(r#"{{ sentence_case(string="hELLO wORLD") }}"#);
    assert_eq!(result, "Hello world");
}

#[test]
fn test_sentence_case_empty() {
    let result = render_template(r#"{{ sentence_case(string="") }}"#);
    assert_eq!(result, "");
}

// ==================== strip_html Tests ====================

#[test]
fn test_strip_html_basic() {
    let result = render_template(r#"{{ strip_html(string="<p>Hello World</p>") }}"#);
    assert_eq!(result, "Hello World");
}

#[test]
fn test_strip_html_nested() {
    let result = render_template(r#"{{ strip_html(string="<p>Hello <b>World</b></p>") }}"#);
    assert_eq!(result, "Hello World");
}

#[test]
fn test_strip_html_with_attributes() {
    let result = render_template(r#"{{ strip_html(string="<div class='test'>Content</div>") }}"#);
    assert_eq!(result, "Content");
}

#[test]
fn test_strip_html_no_tags() {
    let result = render_template(r#"{{ strip_html(string="No tags here") }}"#);
    assert_eq!(result, "No tags here");
}

// ==================== strip_ansi Tests ====================

#[test]
fn test_strip_ansi_color() {
    let result = render_template(r#"{{ strip_ansi(string="\x1b[31mRed\x1b[0m") }}"#);
    assert_eq!(result, "Red");
}

#[test]
fn test_strip_ansi_bold() {
    let result = render_template(r#"{{ strip_ansi(string="\x1b[1mBold\x1b[0m") }}"#);
    assert_eq!(result, "Bold");
}

#[test]
fn test_strip_ansi_no_codes() {
    let result = render_template(r#"{{ strip_ansi(string="Plain text") }}"#);
    assert_eq!(result, "Plain text");
}

// ==================== normalize_whitespace Tests ====================

#[test]
fn test_normalize_whitespace_spaces() {
    let result = render_template(r#"{{ normalize_whitespace(string="  hello   world  ") }}"#);
    assert_eq!(result, "hello world");
}

#[test]
fn test_normalize_whitespace_tabs() {
    let result = render_template("{{ normalize_whitespace(string=\"hello\\t\\tworld\") }}");
    assert_eq!(result, "hello world");
}

#[test]
fn test_normalize_whitespace_newlines() {
    let result = render_template("{{ normalize_whitespace(string=\"line1\\n\\nline2\") }}");
    assert_eq!(result, "line1 line2");
}

#[test]
fn test_normalize_whitespace_mixed() {
    let result = render_template("{{ normalize_whitespace(string=\"  a  \\t b \\n c  \") }}");
    assert_eq!(result, "a b c");
}

// ==================== to_constant_case Tests ====================

#[test]
fn test_to_constant_case_spaces() {
    let result = render_template(r#"{{ to_constant_case(string="hello world") }}"#);
    assert_eq!(result, "HELLO_WORLD");
}

#[test]
fn test_to_constant_case_camel() {
    let result = render_template(r#"{{ to_constant_case(string="helloWorld") }}"#);
    assert_eq!(result, "HELLO_WORLD");
}

#[test]
fn test_to_constant_case_kebab() {
    let result = render_template(r#"{{ to_constant_case(string="hello-world-test") }}"#);
    assert_eq!(result, "HELLO_WORLD_TEST");
}

#[test]
fn test_to_constant_case_snake() {
    let result = render_template(r#"{{ to_constant_case(string="hello_world") }}"#);
    assert_eq!(result, "HELLO_WORLD");
}

#[test]
fn test_to_constant_case_empty() {
    let result = render_template(r#"{{ to_constant_case(string="") }}"#);
    assert_eq!(result, "");
}

// ==================== pluralize Tests ====================

#[test]
fn test_pluralize_singular() {
    let result = render_template(r#"{{ pluralize(count=1, singular="item") }}"#);
    assert_eq!(result, "item");
}

#[test]
fn test_pluralize_plural_default() {
    let result = render_template(r#"{{ pluralize(count=5, singular="item") }}"#);
    assert_eq!(result, "items");
}

#[test]
fn test_pluralize_zero() {
    let result = render_template(r#"{{ pluralize(count=0, singular="item") }}"#);
    assert_eq!(result, "items");
}

#[test]
fn test_pluralize_custom_plural() {
    let result =
        render_template(r#"{{ pluralize(count=2, singular="child", plural="children") }}"#);
    assert_eq!(result, "children");
}

#[test]
fn test_pluralize_custom_singular() {
    let result = render_template(r#"{{ pluralize(count=1, singular="person", plural="people") }}"#);
    assert_eq!(result, "person");
}

// ==================== Direct Unit Tests ====================
// These tests call the functions directly to ensure coverage of all code paths

mod unit_tests {
    use minijinja::Value;
    use minijinja::value::Kwargs;
    // Note: center_fn, normalize_whitespace_fn, regex_replace_fn, split_lines_fn, strip_ansi_fn,
    // strip_html_fn, substring_fn, truncate_fn, word_count_fn, wrap_fn have been moved to
    // filter_functions/string.rs with dual function+filter syntax support.
    use tmpltool::functions::Function;
    use tmpltool::functions::string::{
        Contains, CountOccurrences, IndexOf, Pluralize, RegexFindAll, RegexMatch, SentenceCase,
        ToConstantCase,
    };

    // Note: regex_replace_fn tests removed - function now in filter_functions/string.rs

    #[test]
    fn test_regex_match_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello123")),
            ("pattern", Value::from("[0-9]+")),
        ]);
        let result = RegexMatch::call(kwargs).unwrap();
        assert!(result.is_true());
    }

    #[test]
    fn test_regex_match_invalid_pattern_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello")),
            ("pattern", Value::from("[invalid")),
        ]);
        let result = RegexMatch::call(kwargs);
        assert!(result.is_err());
    }

    #[test]
    fn test_regex_find_all_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("a1b2c3")),
            ("pattern", Value::from("[0-9]+")),
        ]);
        let result = RegexFindAll::call(kwargs).unwrap();
        let json: serde_json::Value = serde_json::to_value(&result).unwrap();
        assert_eq!(json, serde_json::json!(["1", "2", "3"]));
    }

    #[test]
    fn test_regex_find_all_invalid_pattern_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello")),
            ("pattern", Value::from("[invalid")),
        ]);
        let result = RegexFindAll::call(kwargs);
        assert!(result.is_err());
    }

    // Note: substring_fn tests removed - function now in filter_functions/string.rs

    #[test]
    fn test_contains_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello world")),
            ("substring", Value::from("world")),
        ]);
        let result = Contains::call(kwargs).unwrap();
        assert!(result.is_true());
    }

    #[test]
    fn test_index_of_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello world")),
            ("substring", Value::from("world")),
        ]);
        let result = IndexOf::call(kwargs).unwrap();
        assert_eq!(result.as_i64().unwrap(), 6);
    }

    #[test]
    fn test_count_occurrences_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello hello hello")),
            ("substring", Value::from("hello")),
        ]);
        let result = CountOccurrences::call(kwargs).unwrap();
        assert_eq!(result.as_i64().unwrap(), 3);
    }

    #[test]
    fn test_count_occurrences_empty_error_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("string", Value::from("hello")),
            ("substring", Value::from("")),
        ]);
        let result = CountOccurrences::call(kwargs);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("cannot be empty"));
    }

    // Note: truncate_fn, word_count_fn, split_lines_fn, wrap_fn, center_fn tests removed
    // - these functions are now in filter_functions/string.rs with dual function+filter syntax.

    #[test]
    fn test_sentence_case_direct() {
        let kwargs = Kwargs::from_iter(vec![("string", Value::from("HELLO WORLD"))]);
        let result = SentenceCase::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "Hello world");
    }

    #[test]
    fn test_sentence_case_empty_direct() {
        let kwargs = Kwargs::from_iter(vec![("string", Value::from(""))]);
        let result = SentenceCase::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "");
    }

    // Note: strip_html_fn, strip_ansi_fn, normalize_whitespace_fn tests removed
    // - these functions are now in filter_functions/string.rs with dual function+filter syntax.

    #[test]
    fn test_to_constant_case_direct() {
        let kwargs = Kwargs::from_iter(vec![("string", Value::from("helloWorld"))]);
        let result = ToConstantCase::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "HELLO_WORLD");
    }

    #[test]
    fn test_to_constant_case_empty_direct() {
        let kwargs = Kwargs::from_iter(vec![("string", Value::from(""))]);
        let result = ToConstantCase::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "");
    }

    #[test]
    fn test_to_constant_case_trailing_separator() {
        let kwargs = Kwargs::from_iter(vec![("string", Value::from("hello-"))]);
        let result = ToConstantCase::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "HELLO");
    }

    #[test]
    fn test_pluralize_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("count", Value::from(5)),
            ("singular", Value::from("item")),
        ]);
        let result = Pluralize::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "items");
    }

    #[test]
    fn test_pluralize_with_custom_plural_direct() {
        let kwargs = Kwargs::from_iter(vec![
            ("count", Value::from(0)),
            ("singular", Value::from("child")),
            ("plural", Value::from("children")),
        ]);
        let result = Pluralize::call(kwargs).unwrap();
        assert_eq!(result.as_str().unwrap(), "children");
    }
}