11package templatelib_test
22
33import (
4+ "strings"
45 "testing"
56 "text/template"
67 "unsafe"
@@ -11,35 +12,31 @@ import (
1112func TestTernaryPanic (t * testing.T ) {
1213 // one of the only places template.IsTrue will return "false" for the "ok" value is an UnsafePointer (hence this test)
1314
14- defer func () {
15- if r := recover (); r == nil {
16- t .Errorf ("Expected panic, executed successfully instead" )
17- } else if errText , ok := r .(string ); ! ok || errText != `template.IsTrue(<nil>) says things are NOT OK` {
18- t .Errorf ("Unexpected panic: %v" , errText )
19- }
20- }()
21-
2215 tmpl , err := template .New ("unsafe-pointer" ).Funcs (templatelib .FuncMap ).Parse (`{{ ternary "true" "false" . }}` )
16+ if err != nil {
17+ t .Errorf ("Unexpected error: %v" , err )
18+ }
2319
2420 err = tmpl .Execute (nil , unsafe .Pointer (uintptr (0 )))
25- if err != nil {
26- t .Errorf ("Expected panic, got error instead: %v" , err )
21+ if err == nil {
22+ t .Errorf ("Expected error, executed successfully instead" )
23+ }
24+ if ! strings .HasSuffix (err .Error (), `template.IsTrue(<nil>) says things are NOT OK` ) {
25+ t .Errorf ("Expected specific error, got: %v" , err )
2726 }
2827}
2928
3029func TestJoinPanic (t * testing.T ) {
31- defer func () {
32- if r := recover (); r == nil {
33- t .Errorf ("Expected panic, executed successfully instead" )
34- } else if errText , ok := r .(string ); ! ok || errText != `"join" requires at least one argument` {
35- t .Errorf ("Unexpected panic: %v" , r )
36- }
37- }()
38-
3930 tmpl , err := template .New ("join-no-arg" ).Funcs (templatelib .FuncMap ).Parse (`{{ join }}` )
31+ if err != nil {
32+ t .Errorf ("Unexpected error: %v" , err )
33+ }
4034
4135 err = tmpl .Execute (nil , nil )
42- if err != nil {
43- t .Errorf ("Expected panic, got error instead: %v" , err )
36+ if err == nil {
37+ t .Errorf ("Expected error, executed successfully instead" )
38+ }
39+ if ! strings .HasSuffix (err .Error (), `"join" requires at least one argument` ) {
40+ t .Errorf ("Expected specific error, got: %v" , err )
4441 }
4542}
0 commit comments