From 45b527a6237e758faa022b9e5f9b02a806ea9b2e Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 17 Nov 2018 12:22:15 -0500 Subject: New test.Ternary function Signed-off-by: Dave Henderson --- funcs/test.go | 9 +++++++++ funcs/test_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+) (limited to 'funcs') 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)) + } +} -- cgit v1.2.3