| 46 | } |
| 47 | |
| 48 | func TestObjectHash(t *testing.T) { |
| 49 | var ( |
| 50 | attributeInt = &AttributeExpr{Type: Int} |
| 51 | attributeString = &AttributeExpr{Type: String} |
| 52 | attributeArray = &AttributeExpr{Type: &Array{ElemType: attributeString}} |
| 53 | attributeMap = &AttributeExpr{Type: &Map{KeyType: attributeInt, ElemType: attributeString}} |
| 54 | userType = &UserTypeExpr{AttributeExpr: attributeString, TypeName: "ut"} |
| 55 | namedAttributePrimitive = &NamedAttributeExpr{Name: "foo", Attribute: attributeInt} |
| 56 | namedAttributeArray = &NamedAttributeExpr{Name: "bar", Attribute: attributeArray} |
| 57 | namedAttributeMap = &NamedAttributeExpr{Name: "baz", Attribute: attributeMap} |
| 58 | namedAttributeUserType = &NamedAttributeExpr{Name: "quux", Attribute: &AttributeExpr{Type: userType}} |
| 59 | attributeUnion = &AttributeExpr{Type: &Union{TypeName: "quuuux", Values: []*NamedAttributeExpr{namedAttributePrimitive}}} |
| 60 | namedAttributeUnion = &NamedAttributeExpr{Name: "qux", Attribute: attributeUnion} |
| 61 | ) |
| 62 | cases := map[string]struct { |
| 63 | object Object |
| 64 | expected string |
| 65 | }{ |
| 66 | "nil": { |
| 67 | object: nil, |
| 68 | expected: "_o_", |
| 69 | }, |
| 70 | "single attribute": { |
| 71 | object: Object{namedAttributePrimitive}, |
| 72 | expected: "_o_-foo/int", |
| 73 | }, |
| 74 | "multiple attributes": { |
| 75 | object: Object{ |
| 76 | namedAttributePrimitive, |
| 77 | namedAttributeArray, |
| 78 | namedAttributeMap, |
| 79 | namedAttributeUnion, |
| 80 | namedAttributeUserType, |
| 81 | }, |
| 82 | expected: "_o_-bar/_a_string-baz/_m_int:string-foo/int-quux/_t_ut-qux/_u_quuuux_*_foo_|_int", |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | for k, tc := range cases { |
| 87 | t.Run(k, func(t *testing.T) { |
| 88 | if actual := Hash(&tc.object, true, false, true); actual != tc.expected { |
| 89 | t.Errorf("got %#v, expected %#v", actual, tc.expected) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |