From b7dd34a9b260f4c47a2e9193c3c5a6519582cf0f Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 3 Apr 2017 12:57:27 -0700 Subject: [PATCH] Update coverage in "pkg/templatelib" to 100% --- pkg/templatelib/lib_test.go | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pkg/templatelib/lib_test.go diff --git a/pkg/templatelib/lib_test.go b/pkg/templatelib/lib_test.go new file mode 100644 index 0000000..9143aa2 --- /dev/null +++ b/pkg/templatelib/lib_test.go @@ -0,0 +1,45 @@ +package templatelib_test + +import ( + "testing" + "text/template" + "unsafe" + + "github.com/docker-library/go-dockerlibrary/pkg/templatelib" +) + +func TestTernaryPanic(t *testing.T) { + // one of the only places template.IsTrue will return "false" for the "ok" value is an UnsafePointer (hence this test) + + defer func() { + if r := recover(); r == nil { + t.Errorf("Expected panic, executed successfully instead") + } else if errText, ok := r.(string); !ok || errText != `template.IsTrue() says things are NOT OK` { + t.Errorf("Unexpected panic: %v", errText) + } + }() + + tmpl, err := template.New("unsafe-pointer").Funcs(templatelib.FuncMap).Parse(`{{ ternary "true" "false" . }}`) + + err = tmpl.Execute(nil, unsafe.Pointer(uintptr(0))) + if err != nil { + t.Errorf("Expected panic, got error instead: %v", err) + } +} + +func TestJoinPanic(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("Expected panic, executed successfully instead") + } else if errText, ok := r.(string); !ok || errText != `"join" requires at least one argument` { + t.Errorf("Unexpected panic: %v", r) + } + }() + + tmpl, err := template.New("join-no-arg").Funcs(templatelib.FuncMap).Parse(`{{ join }}`) + + err = tmpl.Execute(nil, nil) + if err != nil { + t.Errorf("Expected panic, got error instead: %v", err) + } +}