summaryrefslogtreecommitdiff
path: root/funcs
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-07-16 22:47:20 -0400
committerDave Henderson <dhenderson@gmail.com>2018-07-16 23:31:03 -0400
commitd7b399111b60111d2acbeff7afd3f2792f87b394 (patch)
tree21a744835e2bb52748c8eb81ac12a31c17aaf844 /funcs
parenta6de67d2cc283042569501854fbfaa438032f52f (diff)
Adding fail and assert functions
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'funcs')
-rw-r--r--funcs/aws_test.go2
-rw-r--r--funcs/test.go61
2 files changed, 62 insertions, 1 deletions
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))
+ }
+}