1
1
package templatelib_test
2
2
3
3
import (
4
+ "strings"
4
5
"testing"
5
6
"text/template"
6
7
"unsafe"
@@ -11,35 +12,31 @@ import (
11
12
func TestTernaryPanic (t * testing.T ) {
12
13
// one of the only places template.IsTrue will return "false" for the "ok" value is an UnsafePointer (hence this test)
13
14
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
-
22
15
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
+ }
23
19
24
20
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 )
27
26
}
28
27
}
29
28
30
29
func 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
-
39
30
tmpl , err := template .New ("join-no-arg" ).Funcs (templatelib .FuncMap ).Parse (`{{ join }}` )
31
+ if err != nil {
32
+ t .Errorf ("Unexpected error: %v" , err )
33
+ }
40
34
41
35
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 )
44
41
}
45
42
}
0 commit comments