Skip to content

Commit

Permalink
Added and tested IsString condition
Browse files Browse the repository at this point in the history
Fixed IsNumeric condition
  • Loading branch information
redjack96 committed Sep 3, 2024
1 parent 83a95dd commit 68784b8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
36 changes: 22 additions & 14 deletions internal/fc/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/grussorusso/serverledge/internal/function"
"github.com/grussorusso/serverledge/utils"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -118,6 +117,8 @@ func (c Condition) String() string {
return fmt.Sprintf("IsPresent(%v)", c.Op[0])
case IsNumeric:
return fmt.Sprintf("IsNumeric(%v)", c.Op[0])
case IsString:
return fmt.Sprintf("IsString(%v)", c.Op[0])
case StringMatches:
return fmt.Sprintf("StringMatches(%s,%s)", c.Op[0], c.Op[1])
default:
Expand Down Expand Up @@ -317,24 +318,23 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
op1 := ops[0]
return !(op1 == "null" || op1 == nil), nil
case IsNumeric:
isNumeric := func(s interface{}) bool {
_, err := function.Int{}.Convert(s)
if err == nil {
return true
}
numericStringMaybe, ok := s.(string)
if !ok {
return false
}
_, err = strconv.ParseFloat(numericStringMaybe, 64)
return err == nil
ops, err := c.findInputs(input)
if err != nil {
return false, err
}

switch ops[0].(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
return true, nil
default:
return false, nil
}
case IsString:
ops, err := c.findInputs(input)
if err != nil {
return false, err
}
return isNumeric(ops[0]), nil
_, ok := ops[0].(string)
return ok, nil
case StringMatches:
ops, err := c.findInputs(input)
inputString, okString := ops[0].(string)
Expand Down Expand Up @@ -661,6 +661,14 @@ func NewIsNumericParamCondition(param1 *ParamOrValue) Condition {
}
}

func NewIsStringParamCondition(param1 *ParamOrValue) Condition {
return Condition{
Type: IsString,
Find: []bool{param1.IsParam},
Op: []interface{}{param1.GetOperand()},
}
}

func NewStringMatchesParamCondition(param1 *ParamOrValue, param2 *ParamOrValue) Condition {
return Condition{
Type: StringMatches,
Expand Down
67 changes: 45 additions & 22 deletions internal/test/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,31 @@ func TestBuilder(t *testing.T) {
}

func TestIsNumeric(t *testing.T) {
isNumeric := fc.NewIsNumericParamCondition(fc.NewValue("123"))
ok, err := isNumeric.Test(map[string]interface{}{})
utils.AssertNil(t, err)
utils.AssertTrue(t, ok)

isNumeric = fc.NewIsNumericParamCondition(fc.NewParam("foo"))
tests := []struct {
parameter string
shouldBeNumeric bool
}{
{"name", false},
{"age", true},
{"height", true},
{"phone", false},
{"isStudent", false},
{"nonExistent", false},
}
testMap := make(map[string]interface{})
testMap["foo"] = "123"
ok, err = isNumeric.Test(testMap)
utils.AssertNil(t, err)
utils.AssertTrue(t, ok)
testMap["name"] = "John"
testMap["age"] = 33
testMap["height"] = 173.3
testMap["phone"] = "3348176718"
testMap["isStudent"] = false

isNumeric = fc.NewIsNumericParamCondition(fc.NewParam("foo"))
testMap = make(map[string]interface{})
testMap["foo"] = "bar"
ok, err = isNumeric.Test(testMap)
utils.AssertNil(t, err)
utils.AssertFalse(t, ok)

isNumeric = fc.NewIsNumericParamCondition(fc.NewParam("foo"))
testMap = make(map[string]interface{})
ok, err = isNumeric.Test(testMap)
// foo is not specified, so ok should be false. No errors should be expected
utils.AssertNil(t, err)
utils.AssertFalse(t, ok)
for i, test := range tests {
cond := fc.NewIsNumericParamCondition(fc.NewParam(test.parameter))
ok, err := cond.Test(testMap)
utils.AssertNil(t, err)
utils.AssertEqualsMsg(t, test.shouldBeNumeric, ok, fmt.Sprintf("test %d: expected IsString(%v) to be %v", i+1, test.parameter, test.shouldBeNumeric))
}
}

func TestStringGreaterAndSmaller(t *testing.T) {
Expand Down Expand Up @@ -250,3 +250,26 @@ func TestIsNullIsPresent(t *testing.T) {
utils.AssertNil(t, err2)
utils.AssertFalse(t, ok2)
}

func TestIsString(t *testing.T) {
tests := []struct {
parameter string
shouldBeString bool
}{
{"name", true},
{"age", false},
{"isStudent", false},
{"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))
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))
}
}

0 comments on commit 68784b8

Please sign in to comment.