-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathdsl_test.go
49 lines (38 loc) · 1.52 KB
/
dsl_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package dsl
import (
"fmt"
"testing"
"github.com/Knetic/govaluate"
"github.com/stretchr/testify/require"
)
func TestDslExpressions(t *testing.T) {
dslExpressions := map[string]interface{}{
`resolve("scanme.sh")`: "128.199.158.128",
`resolve("scanme.sh","a")`: "128.199.158.128",
`resolve("scanme.sh","6")`: "2400:6180:0:d0::91:1001",
`resolve("scanme.sh","aaaa")`: "2400:6180:0:d0::91:1001",
`resolve("scanme.sh","soa")`: "ns69.domaincontrol.com",
}
testDslExpressionScenarios(t, dslExpressions)
}
func evaluateExpression(t *testing.T, dslExpression string) interface{} {
compiledExpression, err := govaluate.NewEvaluableExpressionWithFunctions(dslExpression, HelperFunctions)
require.NoError(t, err, "Error while compiling the %q expression", dslExpression)
actualResult, err := compiledExpression.Evaluate(make(map[string]interface{}))
require.NoError(t, err, "Error while evaluating the compiled %q expression", dslExpression)
for _, negativeTestWord := range []string{"panic", "invalid", "error"} {
require.NotContains(t, fmt.Sprintf("%v", actualResult), negativeTestWord)
}
return actualResult
}
func testDslExpressionScenarios(t *testing.T, dslExpressions map[string]interface{}) {
for dslExpression, expectedResult := range dslExpressions {
t.Run(dslExpression, func(t *testing.T) {
actualResult := evaluateExpression(t, dslExpression)
if expectedResult != nil {
require.Equal(t, expectedResult, actualResult)
}
fmt.Printf("%s: \t %v\n", dslExpression, actualResult)
})
}
}