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 --- docs/content/functions/test.md | 81 ++++++++++++++++++++++++++++++++++++++++++ funcs.go | 1 + funcs/aws_test.go | 2 +- funcs/test.go | 61 +++++++++++++++++++++++++++++++ test/test.go | 24 +++++++++++++ test/test_test.go | 24 +++++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 docs/content/functions/test.md create mode 100644 funcs/test.go create mode 100644 test/test.go create mode 100644 test/test_test.go diff --git a/docs/content/functions/test.md b/docs/content/functions/test.md new file mode 100644 index 00000000..49026b92 --- /dev/null +++ b/docs/content/functions/test.md @@ -0,0 +1,81 @@ +--- +title: test functions +menu: + main: + parent: functions +--- + +The `test` namespace contains some simple functions to help validate +assumptions and can cause template generation to fail in specific cases. + +## `test.Assert` + + +**Alias:** `assert` + + +Asserts that the given expression or value is `true`. If it is not, causes +template generation to fail immediately with an optional message. + + +### Usage +```go +test.Assert [message] value +``` + +```go +value | test.Assert [message] +``` + + +### Arguments + +| name | description | +|------|-------------| +| `message` | _(optional)_ The optional message to provide in the case of failure | +| `value` | _(required)_ The value to test | + + +### Examples + +```console +$ gomplate -i '{{ assert (eq "foo" "bar") }}' +template: :1:3: executing "" at : error calling assert: assertion failed +$ gomplate -i '{{ assert "something horrible happened" false }}' +template: :1:3: executing "" at : error calling assert: assertion failed: something horrible happened +``` + +## `test.Fail` + + +**Alias:** `fail` + + +Cause template generation to fail immediately, with an optional message. + + +### Usage +```go +test.Fail [message] +``` + +```go +message | test.Fail +``` + + +### Arguments + +| name | description | +|------|-------------| +| `message` | _(optional)_ The optional message to provide | + + +### Examples + +```console +$ gomplate -i '{{ fail }}' +template: :1:3: executing "" at : error calling fail: template generation failed +$ gomplate -i '{{ test.Fail "something is wrong!" }}' +template: :1:7: executing "" at : error calling Fail: template generation failed: something is wrong! +``` diff --git a/funcs.go b/funcs.go index 1a0527f0..7d8fd3a2 100644 --- a/funcs.go +++ b/funcs.go @@ -25,5 +25,6 @@ func initFuncs(d *data.Data) template.FuncMap { funcs.AddFilePathFuncs(f) funcs.AddPathFuncs(f) funcs.AddSockaddrFuncs(f) + funcs.AddTestFuncs(f) return f } 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)) + } +} diff --git a/test/test.go b/test/test.go new file mode 100644 index 00000000..c3c2a62c --- /dev/null +++ b/test/test.go @@ -0,0 +1,24 @@ +package test + +import ( + "github.com/pkg/errors" +) + +// Assert - +func Assert(value bool, message string) (string, error) { + if !value { + if message != "" { + return "", errors.Errorf("assertion failed: %s", message) + } + return "", errors.New("assertion failed") + } + return "", nil +} + +// Fail - +func Fail(message string) error { + if message != "" { + return errors.Errorf("template generation failed: %s", message) + } + return errors.New("template generation failed") +} diff --git a/test/test_test.go b/test/test_test.go new file mode 100644 index 00000000..cba36440 --- /dev/null +++ b/test/test_test.go @@ -0,0 +1,24 @@ +package test + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAssert(t *testing.T) { + _, err := Assert(false, "") + assert.Error(t, err) + _, err = Assert(false, "a message") + assert.EqualError(t, err, "assertion failed: a message") + + _, err = Assert(true, "") + assert.NoError(t, err) +} + +func TestFail(t *testing.T) { + err := Fail("") + assert.Error(t, err) + err = Fail("msg") + assert.EqualError(t, err, "template generation failed: msg") +} -- cgit v1.2.3