forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasher_test.go
93 lines (89 loc) · 2.99 KB
/
hasher_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package expr
import "testing"
func TestPrimitiveHash(t *testing.T) {
cases := map[string]Primitive{
"boolean": Boolean,
"int": Int,
"int32": Int32,
"int64": Int64,
"uint": UInt,
"uint32": UInt32,
"uint64": UInt64,
"float32": Float32,
"float64": Float64,
"string": String,
"bytes": Bytes,
"any": Any,
}
for k, p := range cases {
if actual := Hash(p, true, false, false); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, true, false, true); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, true, true, false); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, true, true, true); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, false, false, false); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, false, false, true); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, false, true, false); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
if actual := Hash(p, false, true, true); k != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, k)
}
}
}
func TestObjectHash(t *testing.T) {
var (
attributeInt = &AttributeExpr{Type: Int}
attributeString = &AttributeExpr{Type: String}
attributeArray = &AttributeExpr{Type: &Array{ElemType: attributeString}}
attributeMap = &AttributeExpr{Type: &Map{KeyType: attributeInt, ElemType: attributeString}}
userType = &UserTypeExpr{AttributeExpr: attributeString, TypeName: "ut"}
namedAttributePrimitive = &NamedAttributeExpr{Name: "foo", Attribute: attributeInt}
namedAttributeArray = &NamedAttributeExpr{Name: "bar", Attribute: attributeArray}
namedAttributeMap = &NamedAttributeExpr{Name: "baz", Attribute: attributeMap}
namedAttributeUserType = &NamedAttributeExpr{Name: "quux", Attribute: &AttributeExpr{Type: userType}}
attributeUnion = &AttributeExpr{Type: &Union{TypeName: "quuuux", Values: []*NamedAttributeExpr{namedAttributePrimitive}}}
namedAttributeUnion = &NamedAttributeExpr{Name: "qux", Attribute: attributeUnion}
)
cases := map[string]struct {
object Object
expected string
}{
"nil": {
object: nil,
expected: "_o_",
},
"single attribute": {
object: Object{namedAttributePrimitive},
expected: "_o_-foo/int",
},
"multiple attributes": {
object: Object{
namedAttributePrimitive,
namedAttributeArray,
namedAttributeMap,
namedAttributeUnion,
namedAttributeUserType,
},
expected: "_o_-bar/_a_string-baz/_m_int:string-foo/int-quux/_t_ut-qux/_u_quuuux_*_foo_|_int",
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
if actual := Hash(&tc.object, true, false, true); actual != tc.expected {
t.Errorf("got %#v, expected %#v", actual, tc.expected)
}
})
}
}