summaryrefslogtreecommitdiff
path: root/funcs
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-11-17 12:29:26 -0500
committerGitHub <noreply@github.com>2018-11-17 12:29:26 -0500
commit0cf7734ef776cc7701dfbb08b080cecf5066f0eb (patch)
tree5a04e4f0204d29c312e45cce0321eeb00ff10f7b /funcs
parent3dad8fefa1dccc15c9d0bcba3219305c3cbab69c (diff)
parent45b527a6237e758faa022b9e5f9b02a806ea9b2e (diff)
Merge pull request #434 from hairyhenderson/ternary-func-428
New test.Ternary function
Diffstat (limited to 'funcs')
-rw-r--r--funcs/test.go9
-rw-r--r--funcs/test_test.go15
2 files changed, 24 insertions, 0 deletions
diff --git a/funcs/test.go b/funcs/test.go
index 4def26f1..ed80e6dc 100644
--- a/funcs/test.go
+++ b/funcs/test.go
@@ -27,6 +27,7 @@ func AddTestFuncs(f map[string]interface{}) {
f["assert"] = TestNS().Assert
f["fail"] = TestNS().Fail
f["required"] = TestNS().Required
+ f["ternary"] = TestNS().Ternary
}
// TestFuncs -
@@ -76,3 +77,11 @@ func (f *TestFuncs) Required(args ...interface{}) (interface{}, error) {
return nil, errors.Errorf("wrong number of args: want 1 or 2, got %d", len(args))
}
}
+
+// Ternary -
+func (f *TestFuncs) Ternary(tval, fval, b interface{}) interface{} {
+ if conv.ToBool(b) {
+ return tval
+ }
+ return fval
+}
diff --git a/funcs/test_test.go b/funcs/test_test.go
index 7b7adfca..1a6868d3 100644
--- a/funcs/test_test.go
+++ b/funcs/test_test.go
@@ -63,3 +63,18 @@ func TestRequired(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, v, "foo")
}
+
+func TestTernary(t *testing.T) {
+ f := TestNS()
+ testdata := []struct {
+ tval, fval, b interface{}
+ expected interface{}
+ }{
+ {"foo", 42, false, 42},
+ {"foo", 42, "yes", "foo"},
+ {false, true, true, false},
+ }
+ for _, d := range testdata {
+ assert.Equal(t, d.expected, f.Ternary(d.tval, d.fval, d.b))
+ }
+}