Skip to content

Commit

Permalink
Added and tested IsBoolean
Browse files Browse the repository at this point in the history
Rewritten TestIsString
  • Loading branch information
redjack96 committed Sep 3, 2024
1 parent 91b8f4e commit 7dcb1cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
17 changes: 17 additions & 0 deletions internal/fc/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
41 changes: 34 additions & 7 deletions internal/test/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,50 @@ 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"
testMap["age"] = 33
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))
}
}

0 comments on commit 7dcb1cb

Please sign in to comment.