package salix import ( "strings" "testing" ) func TestIf(t *testing.T) { const tmplStr = `#if(weather.Temp > 30):

It's a hot day!

#elif(weather.Temp < 0):

It's freezing!

#else:

The temperature is #(weather.Temp)

#!if` type weather struct { Temp int } res := execStr(t, tmplStr, map[string]any{ "weather": weather{Temp: 40}, }) res = strings.TrimSpace(res) if res != "

It's a hot day!

" { t.Errorf("Expected %q, got %q", "

It's a hot day!

", res) } res = execStr(t, tmplStr, map[string]any{ "weather": weather{Temp: -1}, }) res = strings.TrimSpace(res) if res != "

It's freezing!

" { t.Errorf("Expected %q, got %q", "

It's freezing!

", res) } res = execStr(t, tmplStr, map[string]any{ "weather": weather{Temp: 25}, }) res = strings.TrimSpace(res) if res != "

The temperature is 25

" { t.Errorf("Expected %q, got %q", "

The temperature is 25

", res) } } func TestFor(t *testing.T) { const tmplStr = `#for(item in items): #(item) #!for` res := execStr(t, tmplStr, map[string]any{ "items": []any{1.2, 3, "Hello", 4 + 2i, "-2", false, nil}, }) expected := `1.2 3 Hello (4+2i) -2 false ` if res != expected { t.Errorf("Expected %q, got %q", expected, res) } } func TestForTwoArgs(t *testing.T) { const tmplStr = `#for(i, item in items): #(i) #(item) #!for` res := execStr(t, tmplStr, map[string]any{ "items": []any{1.2, 3, "Hello", 4 + 2i, "-2", false, nil}, }) expected := `0 1.2 1 3 2 Hello 3 (4+2i) 4 -2 5 false 6 ` if res != expected { t.Errorf("Expected %q, got %q", expected, res) } }