-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestDescription.cs
More file actions
371 lines (297 loc) · 14.9 KB
/
TestDescription.cs
File metadata and controls
371 lines (297 loc) · 14.9 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections.Generic;
using com.IvanMurzak.ReflectorNet.Tests.Model;
using com.IvanMurzak.ReflectorNet.Model;
using com.IvanMurzak.ReflectorNet.Utils;
using Xunit.Abstractions;
using com.IvanMurzak.ReflectorNet.Json;
namespace com.IvanMurzak.ReflectorNet.Tests.SchemaTests
{
public partial class TestDescription : BaseTest
{
public TestDescription(ITestOutputHelper output) : base(output) { }
[Fact]
public void PropertyDescriptionOfCustomType()
{
var reflector = new Reflector();
TestClassMembersDescription(typeof(GameObjectRef), reflector);
TestClassMembersDescription(typeof(GameObjectRefList), reflector);
TestClassMembersDescription(typeof(List<GameObjectRef>), reflector);
TestClassMembersDescription(typeof(GameObjectRef[]), reflector);
}
[Fact]
public void PropertyDescriptionOfTestClassWithDescriptions()
{
var reflector = new Reflector();
// Test a class with various types of members and descriptions
TestClassMembersDescription(typeof(TestClassWithDescriptions), reflector);
}
[Fact]
public void PropertyDescriptionOfCollectionWithDescriptions()
{
var reflector = new Reflector();
// Test collection classes with descriptions - but only for complex types
TestClassMembersDescription(typeof(TestCollectionWithDescription), reflector);
// For List<TestClassWithDescriptions>, just test schema generation
var listComplexSchema = reflector.GetSchema(typeof(List<TestClassWithDescriptions>));
Assert.NotNull(listComplexSchema);
_output.WriteLine($"List<TestClassWithDescriptions> schema: {listComplexSchema}");
}
[Fact]
public void PropertyDescriptionOfReflectorNetModels()
{
var reflector = new Reflector();
reflector.JsonSerializer.AddConverter(new MethodDataConverter());
// Test MethodPointerRef and MethodDataRef which have Description attributes
TestClassMembersDescription(typeof(MethodRef), reflector);
TestClassMembersDescription(typeof(MethodData), reflector);
// SerializedMember and SerializedMemberList use custom converters with descriptions
// Let's test their schema generation separately
var serializedMemberSchema = reflector.GetSchema(typeof(SerializedMember));
Assert.NotNull(serializedMemberSchema);
_output.WriteLine($"SerializedMember schema: {serializedMemberSchema}");
var serializedMemberListSchema = reflector.GetSchema(typeof(SerializedMemberList));
Assert.NotNull(serializedMemberListSchema);
_output.WriteLine($"SerializedMemberList schema: {serializedMemberListSchema}");
}
[Fact]
public void PropertyDescriptionOfArrayTypes()
{
var reflector = new Reflector();
// Test complex array types that have members to inspect
TestClassMembersDescription(typeof(GameObjectRef[]), reflector);
TestClassMembersDescription(typeof(TestClassWithDescriptions[]), reflector);
// For primitive arrays, just test schema generation
var stringArraySchema = reflector.GetSchema(typeof(string[]));
Assert.NotNull(stringArraySchema);
_output.WriteLine($"string[] schema: {stringArraySchema}");
var intArraySchema = reflector.GetSchema(typeof(int[]));
Assert.NotNull(intArraySchema);
_output.WriteLine($"int[] schema: {intArraySchema}");
}
[Fact]
public void PropertyDescriptionOfGenericCollections()
{
var reflector = new Reflector();
// Test generic collections - but only ones that contain complex types
TestClassMembersDescription(typeof(List<GameObjectRef>), reflector);
TestClassMembersDescription(typeof(List<TestClassWithDescriptions>), reflector);
// For primitive collections, just test schema generation
var listStringSchema = reflector.GetSchema(typeof(List<string>));
Assert.NotNull(listStringSchema);
_output.WriteLine($"List<string> schema: {listStringSchema}");
var listIntSchema = reflector.GetSchema(typeof(List<int>));
Assert.NotNull(listIntSchema);
_output.WriteLine($"List<int> schema: {listIntSchema}");
var dictionarySchema = reflector.GetSchema(typeof(Dictionary<string, int>));
Assert.NotNull(dictionarySchema);
_output.WriteLine($"Dictionary<string, int> schema: {dictionarySchema}");
}
[Fact]
public void PropertyDescriptionOfPrimitiveTypes()
{
var reflector = new Reflector();
// Primitive types don't have members to test, but we can test schema generation
var stringSchema = reflector.GetSchema(typeof(string));
Assert.NotNull(stringSchema);
_output.WriteLine($"String schema: {stringSchema}");
var intSchema = reflector.GetSchema(typeof(int));
Assert.NotNull(intSchema);
_output.WriteLine($"Int schema: {intSchema}");
var boolSchema = reflector.GetSchema(typeof(bool));
Assert.NotNull(boolSchema);
_output.WriteLine($"Bool schema: {boolSchema}");
var doubleSchema = reflector.GetSchema(typeof(double));
Assert.NotNull(doubleSchema);
_output.WriteLine($"Double schema: {doubleSchema}");
}
[Fact]
public void PropertyDescriptionOfNullableTypes()
{
var reflector = new Reflector();
// Nullable types also don't have members to test, but we can test schema generation
var nullableIntSchema = reflector.GetSchema(typeof(int?));
Assert.NotNull(nullableIntSchema);
_output.WriteLine($"Nullable int schema: {nullableIntSchema}");
var nullableBoolSchema = reflector.GetSchema(typeof(bool?));
Assert.NotNull(nullableBoolSchema);
_output.WriteLine($"Nullable bool schema: {nullableBoolSchema}");
var nullableDateTimeSchema = reflector.GetSchema(typeof(DateTime?));
Assert.NotNull(nullableDateTimeSchema);
_output.WriteLine($"Nullable DateTime schema: {nullableDateTimeSchema}");
}
[Fact]
public void ClassLevelDescription()
{
var reflector = new Reflector();
// Test that class-level descriptions are properly captured
var schema = reflector.GetSchema(typeof(GameObjectRef));
Assert.NotNull(schema);
var description = schema[JsonSchema.Description]?.ToString();
Assert.NotNull(description);
Assert.Contains("Find GameObject", description);
_output.WriteLine($"GameObjectRef class description: {description}");
}
[Fact]
public void ClassLevelDescriptionForTestClass()
{
var reflector = new Reflector();
// Test class-level description for our test class
var schema = reflector.GetSchema(typeof(TestClassWithDescriptions));
Assert.NotNull(schema);
var description = schema[JsonSchema.Description]?.ToString();
Assert.NotNull(description);
Assert.Contains("Test class with various member types", description);
_output.WriteLine($"TestClassWithDescriptions class description: {description}");
}
[Fact]
public void MissingDescriptionsHandledGracefully()
{
var reflector = new Reflector();
// Create a simple class without descriptions to test graceful handling
var schema = reflector.GetSchema(typeof(string));
Assert.NotNull(schema);
// Primitive types might not have descriptions, that's OK
_output.WriteLine($"String schema: {schema}");
}
[Fact]
public void PropertyDescriptionOfStructTypes()
{
var reflector = new Reflector();
// Test struct types with descriptions
TestClassMembersDescription(typeof(TestStructWithDescriptions), reflector);
TestClassMembersDescription(typeof(TestStructWithDescriptions[]), reflector);
TestClassMembersDescription(typeof(List<TestStructWithDescriptions>), reflector);
}
[Fact]
public void PropertyDescriptionOfEnumTypes()
{
var reflector = new Reflector();
// Test enum types with descriptions
TestClassMembersDescription(typeof(TestEnumWithDescriptions), reflector);
TestClassMembersDescription(typeof(TestEnumWithDescriptions[]), reflector);
}
[Fact]
public void StructLevelDescription()
{
var reflector = new Reflector();
// Test that struct-level descriptions are properly captured
var schema = reflector.GetSchema(typeof(TestStructWithDescriptions));
Assert.NotNull(schema);
var description = schema[JsonSchema.Description]?.ToString();
Assert.NotNull(description);
Assert.Contains("test struct", description);
_output.WriteLine($"TestStructWithDescriptions description: {description}");
}
[Fact]
public void EnumLevelDescription()
{
var reflector = new Reflector();
// Test that enum-level descriptions are properly captured
var schema = reflector.GetSchema(typeof(TestEnumWithDescriptions));
Assert.NotNull(schema);
// For enums, the schema might be different - let's just verify it generates
_output.WriteLine($"TestEnumWithDescriptions schema: {schema}");
}
[Fact]
public void NestedTypesWithDescriptions()
{
var reflector = new Reflector();
// Test nested complex types
TestClassMembersDescription(typeof(List<List<GameObjectRef>>), reflector);
TestClassMembersDescription(typeof(Dictionary<string, GameObjectRef>), reflector);
TestClassMembersDescription(typeof(Dictionary<string, List<TestClassWithDescriptions>>), reflector);
}
[Fact]
public void PropertyDescriptionWithBindingFlags()
{
var reflector = new Reflector();
// Test with different binding flags to include non-public members
TestClassMembersDescription(
type: typeof(TestClassWithDescriptions),
reflector: reflector,
bindingFlags: System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
}
[Fact]
public void ValidateSpecificDescriptionContent()
{
var reflector = new Reflector();
// Test that specific descriptions are correctly applied to schema properties
var schema = reflector.GetSchema(typeof(GameObjectRef));
Assert.NotNull(schema);
var properties = schema[JsonSchema.Properties]?.AsObject();
Assert.NotNull(properties);
// Check instanceID property description
var instanceIdProperty = properties["instanceID"];
Assert.NotNull(instanceIdProperty);
var instanceIdDescription = instanceIdProperty[JsonSchema.Description]?.ToString();
Assert.NotNull(instanceIdDescription);
Assert.Contains("instanceID", instanceIdDescription);
Assert.Contains("Priority: 1", instanceIdDescription);
// Check path property description
var pathProperty = properties["path"];
Assert.NotNull(pathProperty);
var pathDescription = pathProperty[JsonSchema.Description]?.ToString();
Assert.NotNull(pathDescription);
Assert.Contains("path", pathDescription);
Assert.Contains("Priority: 2", pathDescription);
_output.WriteLine($"instanceID description: {instanceIdDescription}");
_output.WriteLine($"path description: {pathDescription}");
}
[Fact]
public void DescriptionInheritanceFromBaseType()
{
var reflector = new Reflector();
// Test that descriptions work with inheritance
TestClassMembersDescription(typeof(MethodData), reflector); // inherits from MethodPointerRef
}
[Fact]
public void CustomConverterDescriptions()
{
var reflector = new Reflector();
reflector.JsonSerializer.ClearConverters();
reflector.JsonSerializer.AddConverter(new SerializedMemberCustomDescriptionConverter(reflector));
// Test that custom JSON converters provide proper descriptions in schema
var serializedMemberSchema = reflector.GetSchema(typeof(SerializedMember));
Assert.NotNull(serializedMemberSchema);
var properties = serializedMemberSchema[JsonSchema.Properties]?.AsObject();
Assert.NotNull(properties);
foreach (var property in properties)
{
_output.WriteLine($"Property: {property.Key}, Description: {property.Value![JsonSchema.Description]}");
Assert.Equal(SerializedMemberCustomDescriptionConverter.CustomDescription, property.Value[JsonSchema.Description]?.ToString());
}
}
void NoEmptyOrNullDescriptions(Reflector reflector, IEnumerable<Type> types)
{
foreach (var type in types)
{
var schema = reflector.GetSchema(type);
var descriptions = JsonSchema.FindAllProperties(schema, JsonSchema.Description);
if (descriptions.Count == 0)
continue;
_output.WriteLine($"Check type: {type.GetTypeShortName()}");
_output.WriteLine($"Found {descriptions.Count} description(s) in the schema:");
foreach (var property in descriptions)
{
_output.WriteLine($"- Path: {property?.GetPath()}\n {property}\n");
Assert.NotNull(property);
Assert.NotNull(property.ToString());
Assert.NotEmpty(property.ToString());
}
}
}
[Fact]
public void NoEmptyOrNullDescriptions_AllNonStaticReflectorModelTypes()
{
var reflector = new Reflector();
NoEmptyOrNullDescriptions(reflector, TestUtils.Types.AllNonStaticReflectorModelTypes);
}
[Fact]
public void NoEmptyOrNullDescriptions_AllBaseNonStaticTypes()
{
var reflector = new Reflector();
NoEmptyOrNullDescriptions(reflector, TestUtils.Types.AllBaseNonStaticTypes);
}
}
}