From d7b399111b60111d2acbeff7afd3f2792f87b394 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 16 Jul 2018 22:47:20 -0400 Subject: Adding fail and assert functions Signed-off-by: Dave Henderson --- funcs/aws_test.go | 2 +- funcs/test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 funcs/test.go (limited to 'funcs') diff --git a/funcs/aws_test.go b/funcs/aws_test.go index 7ae55da1..6002a976 100644 --- a/funcs/aws_test.go +++ b/funcs/aws_test.go @@ -12,7 +12,7 @@ func TestNSIsIdempotent(t *testing.T) { right := AWSNS() assert.True(t, left == right) } -func TestFuncs(t *testing.T) { +func TestAWSFuncs(t *testing.T) { m := aws.NewDummyEc2Meta() i := aws.NewDummyEc2Info(m) af := &Funcs{meta: m, info: i} diff --git a/funcs/test.go b/funcs/test.go new file mode 100644 index 00000000..b9d6376b --- /dev/null +++ b/funcs/test.go @@ -0,0 +1,61 @@ +package funcs + +import ( + "sync" + + "github.com/hairyhenderson/gomplate/conv" + "github.com/pkg/errors" + + "github.com/hairyhenderson/gomplate/test" +) + +var ( + testNS *TestFuncs + testNSInit sync.Once +) + +// TestNS - +func TestNS() *TestFuncs { + testNSInit.Do(func() { testNS = &TestFuncs{} }) + return testNS +} + +// AddTestFuncs - +func AddTestFuncs(f map[string]interface{}) { + f["test"] = TestNS + + f["assert"] = TestNS().Assert + f["fail"] = TestNS().Fail +} + +// TestFuncs - +type TestFuncs struct{} + +// Assert - +func (f *TestFuncs) Assert(args ...interface{}) (string, error) { + input := conv.ToBool(args[len(args)-1]) + switch len(args) { + case 1: + return test.Assert(input, "") + case 2: + message, ok := args[0].(string) + if !ok { + return "", errors.Errorf("at <1>: expected string; found %T", args[0]) + } + return test.Assert(input, message) + default: + return "", errors.Errorf("wrong number of args: want 1 or 2, got %d", len(args)) + } +} + +// Fail - +func (f *TestFuncs) Fail(args ...interface{}) (string, error) { + switch len(args) { + case 0: + return "", test.Fail("") + case 1: + return "", test.Fail(conv.ToString(args[0])) + default: + return "", errors.Errorf("wrong number of args: want 0 or 1, got %d", len(args)) + } +} -- cgit v1.2.3