This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
AttributeTests.cs
235 lines (195 loc) · 9.5 KB
/
AttributeTests.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
namespace System.Tests
{
public static partial class AttributeTests
{
[Fact]
public static void DefaultEquality()
{
var a1 = new ParentAttribute { Prop = 1 };
var a2 = new ParentAttribute { Prop = 42 };
var a3 = new ParentAttribute { Prop = 1 };
var d1 = new ChildAttribute { Prop = 1 };
var d2 = new ChildAttribute { Prop = 42 };
var d3 = new ChildAttribute { Prop = 1 };
var s1 = new GrandchildAttribute { Prop = 1 };
var s2 = new GrandchildAttribute { Prop = 42 };
var s3 = new GrandchildAttribute { Prop = 1 };
var f1 = new ChildAttributeWithField { Prop = 1 };
var f2 = new ChildAttributeWithField { Prop = 42 };
var f3 = new ChildAttributeWithField { Prop = 1 };
Assert.NotEqual(a1, a2);
Assert.NotEqual(a2, a3);
Assert.Equal(a1, a3);
// The implementation of Attribute.Equals uses reflection to
// enumerate fields. On .NET core, we add `BindingFlags.DeclaredOnly`
// to fix a bug where an instance of a subclass of an attribute can
// be equal to an instance of the parent class.
// See https://github.com/dotnet/coreclr/pull/6240
Assert.False(d1.Equals(d2));
Assert.False(d2.Equals(d3));
Assert.Equal(d1, d3);
Assert.False(s1.Equals(s2));
Assert.False(s2.Equals(s3));
Assert.Equal(s1, s3);
Assert.False(f1.Equals(f2));
Assert.False(f2.Equals(f3));
Assert.Equal(f1, f3);
Assert.NotEqual(d1, a1);
Assert.NotEqual(d2, a2);
Assert.NotEqual(d3, a3);
Assert.NotEqual(d1, a3);
Assert.NotEqual(d3, a1);
Assert.NotEqual(d1, s1);
Assert.NotEqual(d2, s2);
Assert.NotEqual(d3, s3);
Assert.NotEqual(d1, s3);
Assert.NotEqual(d3, s1);
Assert.NotEqual(f1, a1);
Assert.NotEqual(f2, a2);
Assert.NotEqual(f3, a3);
Assert.NotEqual(f1, a3);
Assert.NotEqual(f3, a1);
}
[Fact]
public static void DefaultHashCode()
{
var a1 = new ParentAttribute { Prop = 1 };
var a2 = new ParentAttribute { Prop = 42 };
var a3 = new ParentAttribute { Prop = 1 };
var d1 = new ChildAttribute { Prop = 1 };
var d2 = new ChildAttribute { Prop = 42 };
var d3 = new ChildAttribute { Prop = 1 };
var s1 = new GrandchildAttribute { Prop = 1 };
var s2 = new GrandchildAttribute { Prop = 42 };
var s3 = new GrandchildAttribute { Prop = 1 };
var f1 = new ChildAttributeWithField { Prop = 1 };
var f2 = new ChildAttributeWithField { Prop = 42 };
var f3 = new ChildAttributeWithField { Prop = 1 };
Assert.NotEqual(0, a1.GetHashCode());
Assert.NotEqual(0, a2.GetHashCode());
Assert.NotEqual(0, a3.GetHashCode());
Assert.NotEqual(0, d1.GetHashCode());
Assert.NotEqual(0, d2.GetHashCode());
Assert.NotEqual(0, d3.GetHashCode());
Assert.NotEqual(0, s1.GetHashCode());
Assert.NotEqual(0, s2.GetHashCode());
Assert.NotEqual(0, s3.GetHashCode());
Assert.Equal(0, f1.GetHashCode());
Assert.Equal(0, f2.GetHashCode());
Assert.Equal(0, f3.GetHashCode());
Assert.NotEqual(a1.GetHashCode(), a2.GetHashCode());
Assert.NotEqual(a2.GetHashCode(), a3.GetHashCode());
Assert.Equal(a1.GetHashCode(), a3.GetHashCode());
// The implementation of Attribute.GetHashCode uses reflection to
// enumerate fields. On .NET core, we add `BindingFlags.DeclaredOnly`
// to fix a bug where the hash code of a subclass of an attribute can
// be equal to an instance of the parent class.
// See https://github.com/dotnet/coreclr/pull/6240
Assert.False(s1.GetHashCode().Equals(s2.GetHashCode()));
Assert.False(s2.GetHashCode().Equals(s3.GetHashCode()));
Assert.Equal(s1.GetHashCode(), s3.GetHashCode());
Assert.False(d1.GetHashCode().Equals(d2.GetHashCode()));
Assert.False(d2.GetHashCode().Equals(d3.GetHashCode()));
Assert.Equal(d1.GetHashCode(), d3.GetHashCode());
Assert.Equal(f1.GetHashCode(), f2.GetHashCode());
Assert.Equal(f2.GetHashCode(), f3.GetHashCode());
Assert.Equal(f1.GetHashCode(), f3.GetHashCode());
Assert.True(d1.GetHashCode().Equals(a1.GetHashCode()));
Assert.True(d2.GetHashCode().Equals(a2.GetHashCode()));
Assert.True(d3.GetHashCode().Equals(a3.GetHashCode()));
Assert.True(d1.GetHashCode().Equals(a3.GetHashCode()));
Assert.True(d3.GetHashCode().Equals(a1.GetHashCode()));
Assert.True(d1.GetHashCode().Equals(s1.GetHashCode()));
Assert.True(d2.GetHashCode().Equals(s2.GetHashCode()));
Assert.True(d3.GetHashCode().Equals(s3.GetHashCode()));
Assert.True(d1.GetHashCode().Equals(s3.GetHashCode()));
Assert.True(d3.GetHashCode().Equals(s1.GetHashCode()));
Assert.NotEqual(f1.GetHashCode(), a1.GetHashCode());
Assert.NotEqual(f2.GetHashCode(), a2.GetHashCode());
Assert.NotEqual(f3.GetHashCode(), a3.GetHashCode());
Assert.NotEqual(f1.GetHashCode(), a3.GetHashCode());
Assert.NotEqual(f3.GetHashCode(), a1.GetHashCode());
}
class ParentAttribute : Attribute
{
public int Prop {get;set;}
}
class ChildAttribute : ParentAttribute { }
class GrandchildAttribute : ChildAttribute { }
class ChildAttributeWithField : ParentAttribute
{
public int Field = 0;
}
[Fact]
[StringValue("\uDFFF")]
public static void StringArgument_InvalidCodeUnits_FallbackUsed()
{
MethodInfo thisMethod = typeof(AttributeTests).GetTypeInfo().GetDeclaredMethod("StringArgument_InvalidCodeUnits_FallbackUsed");
Assert.NotNull(thisMethod);
CustomAttributeData cad = thisMethod.CustomAttributes.Where(ca => ca.AttributeType == typeof(StringValueAttribute)).FirstOrDefault();
Assert.NotNull(cad);
string stringArg = cad.ConstructorArguments[0].Value as string;
Assert.NotNull(stringArg);
Assert.Equal("\uFFFD\uFFFD", stringArg);
}
public static IEnumerable<object[]> Equals_TestData()
{
yield return new object[] { new StringValueAttribute("hello"), new StringValueAttribute("hello"), true, true };
yield return new object[] { new StringValueAttribute("hello"), new StringValueAttribute("foo"), false, false };
yield return new object[] { new StringValueIntValueAttribute("hello", 1), new StringValueIntValueAttribute("hello", 1), true, true };
yield return new object[] { new StringValueIntValueAttribute("hello", 1), new StringValueIntValueAttribute("hello", 2), false, true }; // GetHashCode() ignores the int value
yield return new object[] { new EmptyAttribute(), new EmptyAttribute(), true, true };
yield return new object[] { new StringValueAttribute("hello"), new StringValueIntValueAttribute("hello", 1), false, true }; // GetHashCode() ignores the int value
yield return new object[] { new StringValueAttribute("hello"), "hello", false, false };
yield return new object[] { new StringValueAttribute("hello"), null, false, false };
}
[Theory]
[MemberData(nameof(Equals_TestData))]
public static void Equals(Attribute attr1, object obj, bool expected, bool hashEqualityExpected)
{
Assert.Equal(expected, attr1.Equals(obj));
Attribute attr2 = obj as Attribute;
if (attr2 != null)
{
Assert.Equal(hashEqualityExpected, attr1.GetHashCode() == attr2.GetHashCode());
}
}
[AttributeUsage(AttributeTargets.Method)]
private sealed class StringValueAttribute : Attribute
{
public string StringValue;
public StringValueAttribute(string stringValue)
{
StringValue = stringValue;
}
}
private sealed class StringValueIntValueAttribute : Attribute
{
public string StringValue;
private int IntValue;
public StringValueIntValueAttribute(string stringValue, int intValue)
{
StringValue = stringValue;
IntValue = intValue;
}
}
[AttributeUsage(AttributeTargets.Method)]
private sealed class EmptyAttribute : Attribute { }
[Fact]
public static void ValidateDefaults()
{
StringValueAttribute sav = new StringValueAttribute("test");
Assert.False(sav.IsDefaultAttribute());
Assert.Equal(sav.GetType(), sav.TypeId);
Assert.True(sav.Match(sav));
}
}
}