github.com/docker-library/go-dockerlibrary@v0.0.0-20200821205225-669fbe5c1d52/pkg/templatelib/lib_test.go (about)

     1  package templatelib_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"text/template"
     7  	"unsafe"
     8  
     9  	"github.com/docker-library/go-dockerlibrary/pkg/templatelib"
    10  )
    11  
    12  func TestTernaryPanic(t *testing.T) {
    13  	// one of the only places template.IsTrue will return "false" for the "ok" value is an UnsafePointer (hence this test)
    14  
    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  	}
    19  
    20  	err = tmpl.Execute(nil, unsafe.Pointer(uintptr(0)))
    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)
    26  	}
    27  }
    28  
    29  func TestJoinPanic(t *testing.T) {
    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  	}
    34  
    35  	err = tmpl.Execute(nil, nil)
    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)
    41  	}
    42  }