From 7dcb1cb19ccf79aae702c72f679775541a6a3446 Mon Sep 17 00:00:00 2001 From: giacomo Date: Tue, 3 Sep 2024 15:56:55 +0200 Subject: [PATCH] Added and tested IsBoolean Rewritten TestIsString --- internal/fc/conditions.go | 17 ++++++++++++++ internal/test/condition_test.go | 41 +++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/internal/fc/conditions.go b/internal/fc/conditions.go index 23354a32..4db31c47 100644 --- a/internal/fc/conditions.go +++ b/internal/fc/conditions.go @@ -119,6 +119,8 @@ func (c Condition) String() string { return fmt.Sprintf("IsNumeric(%v)", c.Op[0]) case IsString: return fmt.Sprintf("IsString(%v)", c.Op[0]) + case IsBoolean: + return fmt.Sprintf("IsBoolean(%v)", c.Op[0]) case StringMatches: return fmt.Sprintf("StringMatches(%s,%s)", c.Op[0], c.Op[1]) default: @@ -335,6 +337,13 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) { } _, ok := ops[0].(string) return ok, nil + case IsBoolean: + ops, err := c.findInputs(input) + if err != nil { + return false, err + } + _, ok := ops[0].(bool) + return ok, nil case StringMatches: ops, err := c.findInputs(input) inputString, okString := ops[0].(string) @@ -669,6 +678,14 @@ func NewIsStringParamCondition(param1 *ParamOrValue) Condition { } } +func NewIsBooleanParamCondition(param1 *ParamOrValue) Condition { + return Condition{ + Type: IsBoolean, + Find: []bool{param1.IsParam}, + Op: []interface{}{param1.GetOperand()}, + } +} + func NewStringMatchesParamCondition(param1 *ParamOrValue, param2 *ParamOrValue) Condition { return Condition{ Type: StringMatches, diff --git a/internal/test/condition_test.go b/internal/test/condition_test.go index ca281d29..40a2ac44 100644 --- a/internal/test/condition_test.go +++ b/internal/test/condition_test.go @@ -232,13 +232,15 @@ func TestIsNullIsPresent(t *testing.T) { func TestIsString(t *testing.T) { tests := []struct { - parameter string + paramOrValue *fc.ParamOrValue shouldBeString bool }{ - {"name", true}, - {"age", false}, - {"isStudent", false}, - {"nonExistent", false}, + {fc.NewValue("name"), true}, + {fc.NewValue(false), false}, + {fc.NewParam("name"), true}, + {fc.NewParam("age"), false}, + {fc.NewParam("isStudent"), false}, + {fc.NewParam("nonExistent"), false}, } testMap := make(map[string]interface{}) testMap["name"] = "John" @@ -246,9 +248,34 @@ func TestIsString(t *testing.T) { testMap["isStudent"] = false for i, test := range tests { - cond := fc.NewIsStringParamCondition(fc.NewParam(test.parameter)) + cond := fc.NewIsStringParamCondition(test.paramOrValue) + ok, err := cond.Test(testMap) + utils.AssertNil(t, err) + utils.AssertEqualsMsg(t, ok, test.shouldBeString, fmt.Sprintf("test %d: expected IsString(%v) to be %v", i+1, test.paramOrValue, test.shouldBeString)) + } +} + +func TestIsBoolean(t *testing.T) { + tests := []struct { + paramOrValue *fc.ParamOrValue + shouldBeBool bool + }{ + {fc.NewValue(true), true}, + {fc.NewValue(false), true}, + {fc.NewValue("true"), false}, + {fc.NewValue("false"), false}, + {fc.NewValue(nil), false}, + {fc.NewParam("isStudent"), true}, + {fc.NewParam("notBoolean"), false}, + } + testMap := make(map[string]interface{}) + testMap["notBoolean"] = "true" + testMap["isStudent"] = false + + for i, test := range tests { + cond := fc.NewIsBooleanParamCondition(test.paramOrValue) ok, err := cond.Test(testMap) utils.AssertNil(t, err) - utils.AssertEqualsMsg(t, ok, test.shouldBeString, fmt.Sprintf("test %d: expected IsString(%v) to be %v", i+1, test.parameter, test.shouldBeString)) + utils.AssertEqualsMsg(t, ok, test.shouldBeBool, fmt.Sprintf("test %d: expected IsBool(%v) to be %v", i+1, test.paramOrValue, test.shouldBeBool)) } }