diff --git a/dsl_test.go b/dsl_test.go index a5268b7..c5ce01a 100644 --- a/dsl_test.go +++ b/dsl_test.go @@ -11,6 +11,15 @@ import ( "github.com/stretchr/testify/require" ) +func TestIndex(t *testing.T) { + index, err := govaluate.NewEvaluableExpressionWithFunctions("index(split(url, '.', -1), 1) == 'example'", DefaultHelperFunctions) + require.Nil(t, err, "could not compile index") + + result, err := index.Evaluate(map[string]interface{}{"url": "https://www.example.com"}) + require.Nil(t, err, "could not evaluate index") + require.Equal(t, true, result, "could not get index data") +} + func TestDSLURLEncodeDecode(t *testing.T) { encoded, err := DefaultHelperFunctions["url_encode"]("&test\"") require.Nil(t, err, "could not url encode") @@ -227,6 +236,7 @@ func TestDslExpressions(t *testing.T) { `hex_decode("6161")`: "aa", `len("Hello")`: float64(5), `len(1234)`: float64(4), + `len(split("1.2.3.4",'.',-1))`: float64(4), `contains("Hello", "lo")`: true, `starts_with("Hello", "He")`: true, `ends_with("Hello", "lo")`: true, diff --git a/test/slice_test.go b/test/slice_test.go deleted file mode 100644 index b629de0..0000000 --- a/test/slice_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package testing - -import ( - "github.com/Knetic/govaluate" - "github.com/projectdiscovery/dsl" - "testing" -) - -var data = map[string]interface{}{ - "username": "johndoe", - "email": "johndoe@example.com", - "password": "12345", - "ip": "127.0.0.1", - "url": "https://www.example.com", - "date": "2022-05-01", -} - -func Helper(expressions map[string]string, t *testing.T) { - for matcherName, expression := range expressions { - compiledExpression, err := govaluate.NewEvaluableExpressionWithFunctions(expression, dsl.DefaultHelperFunctions) - if err != nil { - t.Logf("Failed to compile expresion: %v\n", expression) - } - - result, err := compiledExpression.Evaluate(data) - if err != nil { - t.Logf("Failed to evaluate expresion: %v\n", expression) - } - - if result == true { - t.Logf("[%v] matches data\n", matcherName) - } else { - t.Logf("[%v] not matches data\n", matcherName) - } - } -} - -func TestSliceOps(t *testing.T) { - // Define the expressions to evaluate - expressions := map[string]string{ - "split-01": "index(split(url, '.', -1), 1) == 'example'", - // "sprintf-01": "(split(url,'.',-1))[1] == 'example' ", - "split-02": "len(split(url, '.', -1)) == 3", - "sprintf-02": "print_debug(split(\"https://www.example.com\", \".\", -1))", - "split-04": "len(url) == 23", - "test": "index(url, 1) == 't'", - } - Helper(expressions, t) -}