-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_builder_test.go
48 lines (41 loc) · 1.06 KB
/
rule_builder_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
package golymorph
import (
"github.com/SoulKa/golymorph/objectpath"
"reflect"
"testing"
)
func rulesEqual(a Rule, b Rule) bool {
return a.ValuePath.IsEqualTo(&b.ValuePath) &&
a.NewType == b.NewType
}
func TestRuleBuilder(t *testing.T) {
// Arrange
valuePathString := "foo/bar"
comparatorValue := "test"
newType := reflect.TypeOf(int64(0))
err, valuePath := objectpath.NewObjectPathFromString(valuePathString)
if err != nil {
t.Fatalf("error parsing input path [%s]: %s", valuePathString, err)
}
expectedRule := Rule{
ValuePath: *valuePath,
ComparatorFunction: func(v any) bool { return v == comparatorValue },
NewType: newType,
}
// Act
errors, rule := NewRuleBuilder().
WhenValueAt(valuePathString).
IsEqualTo(comparatorValue).
ThenAssignType(newType).
Build()
// Assert
if len(errors) > 0 {
for _, err := range errors {
t.Log(err)
}
t.Fatalf("expected no errors, but got %d errors", len(errors))
}
if !rulesEqual(rule, expectedRule) {
t.Fatalf("expected rule to be %+v, but got %+v", expectedRule, rule)
}
}