-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphism_builder_test.go
72 lines (61 loc) · 1.46 KB
/
polymorphism_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package golymorph
import (
"reflect"
"testing"
)
func TestPolymorphismBuilder_UsingRule(t *testing.T) {
// Arrange
errors, rule1 := NewRuleBuilder().
WhenValueAt("foo/bar").
IsEqualTo("test").
ThenAssignType(reflect.TypeOf(int64(0))).
Build()
if HasErrors(t, errors) {
t.Fatalf("expected no errors, but got %d errors", len(errors))
}
// Arrange
errors, rule2 := NewRuleBuilder().
WhenValueAt("foo/bar").
IsEqualTo("test").
ThenAssignType(reflect.TypeOf(int64(0))).
Build()
if HasErrors(t, errors) {
t.Fatalf("expected no errors, but got %d errors", len(errors))
}
// Act
err, polymorphism := NewPolymorphismBuilder().
DefineTypeAt("foo/bar").
UsingRule(rule1).
UsingRule(rule2).
Build()
// Assert
if err != nil {
t.Fatalf("expected no errors, but got %s", err)
} else if polymorphism == nil {
t.Fatalf("expected polymorphism to not be nil")
}
}
func TestPolymorphismBuilder_UsingTypeMap(t *testing.T) {
// Arrange
typeMap := TypeMap{
"test": reflect.TypeOf(int64(0)),
}
// Act
err, polymorphism := NewPolymorphismBuilder().
DefineTypeAt("foo/bar").
UsingTypeMap(typeMap).
WithDiscriminatorAt("foo/bar/discriminator").
Build()
// Assert
if err != nil {
t.Fatalf("expected no errors, but got %s", err)
} else if polymorphism == nil {
t.Fatalf("expected polymorphism to not be nil")
}
}
func HasErrors(t *testing.T, errors []error) bool {
for _, err := range errors {
t.Error(err)
}
return len(errors) > 0
}