@@ -416,6 +416,87 @@ func TestIssue209(t *testing.T) {
416416 }
417417}
418418
419+ func TestIssue342 (t * testing.T ) {
420+ // Test that adding a numeric string and a number results in arithmetic addition.
421+ // Bug: In v6, "10" + 5 returns "105" (string concatenation).
422+ // Expected: "10" + 5 should return 15 (arithmetic addition) as in v4.
423+ // See: https://github.com/flosch/pongo2/issues/342
424+
425+ tests := []struct {
426+ name string
427+ template string
428+ context pongo2.Context
429+ expected string
430+ }{
431+ {
432+ name : "numeric string + integer" ,
433+ template : "{{ a + b }}" ,
434+ context : pongo2.Context {"a" : "10" , "b" : 5 },
435+ expected : "15" ,
436+ },
437+ {
438+ name : "integer + numeric string" ,
439+ template : "{{ a + b }}" ,
440+ context : pongo2.Context {"a" : 5 , "b" : "10" },
441+ expected : "15" ,
442+ },
443+ {
444+ name : "numeric string + float" ,
445+ template : "{{ a + b }}" ,
446+ context : pongo2.Context {"a" : "10.5" , "b" : 2.5 },
447+ expected : "13.000000" ,
448+ },
449+ {
450+ name : "float + numeric string" ,
451+ template : "{{ a + b }}" ,
452+ context : pongo2.Context {"a" : 2.5 , "b" : "10.5" },
453+ expected : "13.000000" ,
454+ },
455+ {
456+ name : "non-numeric string + integer stays string concatenation" ,
457+ template : "{{ a + b }}" ,
458+ context : pongo2.Context {"a" : "hello" , "b" : 5 },
459+ expected : "hello5" ,
460+ },
461+ {
462+ name : "integer + non-numeric string stays string concatenation" ,
463+ template : "{{ a + b }}" ,
464+ context : pongo2.Context {"a" : 5 , "b" : "hello" },
465+ expected : "5hello" ,
466+ },
467+ {
468+ name : "two strings remain concatenation" ,
469+ template : "{{ a + b }}" ,
470+ context : pongo2.Context {"a" : "hello" , "b" : "world" },
471+ expected : "helloworld" ,
472+ },
473+ {
474+ name : "two numeric strings remain concatenation" ,
475+ template : "{{ a + b }}" ,
476+ context : pongo2.Context {"a" : "10" , "b" : "5" },
477+ expected : "105" ,
478+ },
479+ }
480+
481+ for _ , tt := range tests {
482+ t .Run (tt .name , func (t * testing.T ) {
483+ tpl , err := pongo2 .FromString (tt .template )
484+ if err != nil {
485+ t .Fatalf ("failed to parse template: %v" , err )
486+ }
487+
488+ result , err := tpl .Execute (tt .context )
489+ if err != nil {
490+ t .Fatalf ("failed to execute template: %v" , err )
491+ }
492+
493+ if result != tt .expected {
494+ t .Errorf ("expected %q, got %q" , tt .expected , result )
495+ }
496+ })
497+ }
498+ }
499+
419500func TestIssue237 (t * testing.T ) {
420501 // Test that ifchanged with else clause works correctly when content doesn't change.
421502 // Bug: ifchanged without watched expressions would panic or not render else block
0 commit comments