Repository navigation
Expand file tree
/
Copy pathAssemblyMethodAnalyzerTests.cs
More file actions
655 lines (576 loc) · 33.8 KB
/
Copy pathAssemblyMethodAnalyzerTests.cs
File metadata and controls
655 lines (576 loc) · 33.8 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using FolderDiffIL4DotNet.Services;
using Xunit;
using static FolderDiffIL4DotNet.Services.AssemblyMethodAnalyzer;
namespace FolderDiffIL4DotNet.Tests.Services
{
public sealed class AssemblyMethodAnalyzerTests : IDisposable
{
private readonly string _tempDir;
public AssemblyMethodAnalyzerTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), $"AsmAnalyzerTests_{Guid.NewGuid():N}");
Directory.CreateDirectory(_tempDir);
}
public void Dispose()
{
try { Directory.Delete(_tempDir, recursive: true); } catch { }
}
[Fact]
public void Analyze_SameAssembly_NoChanges()
{
// Compare a real assembly to itself — should report no changes
// 実アセンブリを自分自身と比較 — 変更なしが期待される
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(assemblyPath, assemblyPath);
Assert.NotNull(result);
Assert.False(result.HasChanges);
Assert.Empty(result.Entries);
Assert.Equal(0, result.AddedCount);
Assert.Equal(0, result.RemovedCount);
Assert.Equal(0, result.ModifiedCount);
}
[Fact]
public void Analyze_NonExistentFile_ReturnsNull()
{
// Attempting to analyse a missing file should gracefully return null
// 存在しないファイルの解析は null を返すべき
var result = AssemblyMethodAnalyzer.Analyze("/nonexistent/old.dll", "/nonexistent/new.dll");
Assert.Null(result);
}
[Fact]
[Trait("Category", "Unit")]
public void Analyze_NonExistentFile_InvokesOnErrorCallbackWithException()
{
// When analysis fails and onError is provided, it should be invoked with the exception.
// 解析失敗時に onError が提供されている場合、例外を渡して呼び出されるべき。
Exception? captured = null;
var result = AssemblyMethodAnalyzer.Analyze(
"/nonexistent/old.dll", "/nonexistent/new.dll",
onError: ex => captured = ex);
Assert.Null(result);
Assert.NotNull(captured);
Assert.IsAssignableFrom<Exception>(captured);
}
[Fact]
[Trait("Category", "Unit")]
public void Analyze_SameAssembly_DoesNotInvokeOnErrorCallback()
{
// Successful analysis should not invoke the onError callback.
// 正常な解析では onError コールバックが呼ばれないこと。
bool errorInvoked = false;
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(assemblyPath, assemblyPath, onError: _ => errorInvoked = true);
Assert.NotNull(result);
Assert.False(errorInvoked);
}
[Fact]
public void Analyze_InvalidFile_ReturnsNull()
{
// Attempting to analyse a non-PE file should gracefully return null
// PE でないファイルの解析は null を返すべき
var textFile = typeof(AssemblyMethodAnalyzerTests).Assembly.Location + ".runtimeconfig.json";
if (!System.IO.File.Exists(textFile)) return; // skip if runtime config not available
var result = AssemblyMethodAnalyzer.Analyze(textFile, textFile);
Assert.Null(result);
}
[Fact]
public void Analyze_DifferentAssemblies_DetectsChanges()
{
// Compare test assembly to main assembly — should detect differences
// テストアセンブリとメインアセンブリを比較 — 差異が検出されるべき
var testAssembly = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var mainAssembly = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(testAssembly, mainAssembly);
Assert.NotNull(result);
Assert.True(result.HasChanges);
Assert.True(result.Entries.Count > 0);
}
[Fact]
public void Analyze_DifferentAssemblies_EntriesHaveStructuredData()
{
// Entries should contain structured MemberChangeEntry data
// エントリには構造化された MemberChangeEntry データが含まれるべき
var testAssembly = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var mainAssembly = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(testAssembly, mainAssembly);
Assert.NotNull(result);
var firstEntry = result.Entries.First();
Assert.False(string.IsNullOrEmpty(firstEntry.Change));
Assert.False(string.IsNullOrEmpty(firstEntry.TypeName));
Assert.False(string.IsNullOrEmpty(firstEntry.MemberKind));
Assert.Contains(firstEntry.Change, new[] { "Added", "Removed", "Modified" });
Assert.Contains(firstEntry.MemberKind, new[] { "Class", "Record", "Struct", "Interface", "Enum", "Constructor", "StaticConstructor", "Method", "Property", "Field" });
}
[Fact]
public void Analyze_DifferentAssemblies_ModifiedEntriesIfPresentHaveValidChangeKind()
{
// When comparing different assemblies, if any Modified entries exist,
// they should have Change="Modified" and a valid MemberKind.
// 異なるアセンブリ比較時、Modified エントリが存在する場合、
// Change="Modified" と有効な MemberKind を持つべき。
var testAssembly = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var mainAssembly = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(testAssembly, mainAssembly);
Assert.NotNull(result);
var modifiedEntries = result.Entries.Where(e => e.Change == "Modified").ToList();
// Modified entries may or may not exist between unrelated assemblies,
// but if they do, they must have valid structure.
foreach (var entry in modifiedEntries)
{
Assert.Equal("Modified", entry.Change);
Assert.False(string.IsNullOrEmpty(entry.TypeName));
Assert.Contains(entry.MemberKind, new[] { "Constructor", "StaticConstructor", "Method", "Property", "Field" });
}
}
[Fact]
public void Analyze_DifferentAssemblies_AllEntriesHavePopulatedAccessField()
{
// All entries (Added/Removed/Modified) should have the Access field populated
// for methods, properties, and fields.
// すべてのエントリ(Added/Removed/Modified)で、メソッド・プロパティ・フィールドの
// Access フィールドが設定されているべき。
var testAssembly = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var mainAssembly = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(testAssembly, mainAssembly);
Assert.NotNull(result);
var memberEntries = result.Entries
.Where(e => e.MemberKind is "Method" or "Property" or "Field"
or "Constructor" or "StaticConstructor")
.ToList();
Assert.True(memberEntries.Count > 0, "Expected at least one member entry between two different assemblies");
// Every member-level entry should have a non-empty Access value
// (or "old → new" for Modified entries with access changes)
Assert.True(memberEntries.All(m => !string.IsNullOrEmpty(m.Access)),
"All member entries should have a non-empty Access field");
}
[Fact]
public void Analyze_TruncatedPEFile_ReturnsNull()
{
// A file with a valid MZ header but truncated PE data should trigger the
// catch-all fallback and return null instead of throwing.
// 有効な MZ ヘッダーを持つが PE データが切り詰められたファイルは
// catch-all フォールバックで null を返すべき。
var truncatedPath = Path.Combine(_tempDir, "truncated.dll");
// MZ header (first two bytes) followed by garbage — enough to pass initial
// File.Open but fail during metadata parsing.
var bytes = new byte[64];
bytes[0] = 0x4D; // 'M'
bytes[1] = 0x5A; // 'Z'
File.WriteAllBytes(truncatedPath, bytes);
var result = AssemblyMethodAnalyzer.Analyze(truncatedPath, truncatedPath);
Assert.Null(result);
}
[Fact]
public void Analyze_EmptyFile_ReturnsNull()
{
// A zero-byte file should trigger the catch-all and return null.
// 0 バイトファイルは catch-all でnull を返すべき。
var emptyPath = Path.Combine(_tempDir, "empty.dll");
File.WriteAllBytes(emptyPath, Array.Empty<byte>());
var result = AssemblyMethodAnalyzer.Analyze(emptyPath, emptyPath);
Assert.Null(result);
}
[Fact]
public void Analyze_CorruptPEWithValidHeader_ReturnsNull()
{
// A file with a plausible PE header but corrupted metadata tables should
// trigger the catch-all fallback path in AssemblyMethodAnalyzer.Analyze.
// もっともらしい PE ヘッダーを持つが破損したメタデータテーブルのファイルは
// AssemblyMethodAnalyzer.Analyze の catch-all フォールバックを発火させるべき。
var corruptPath = Path.Combine(_tempDir, "corrupt.dll");
// Build a minimal DOS header → PE signature → COFF header → optional header
// but with invalid metadata RVA so System.Reflection.Metadata will fail.
// Copy a real assembly then corrupt the metadata section.
var realAssembly = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var assemblyBytes = File.ReadAllBytes(realAssembly);
// Corrupt bytes in the middle of the assembly (metadata tables region)
// to trigger an exception during ReadAssemblySnapshot.
var random = new Random(42);
int corruptStart = Math.Min(256, assemblyBytes.Length / 2);
int corruptEnd = Math.Min(corruptStart + 512, assemblyBytes.Length);
for (int i = corruptStart; i < corruptEnd; i++)
{
assemblyBytes[i] = (byte)random.Next(256);
}
File.WriteAllBytes(corruptPath, assemblyBytes);
var result = AssemblyMethodAnalyzer.Analyze(corruptPath, corruptPath);
// Should return null (catch-all) rather than throwing
Assert.Null(result);
}
[Fact]
public void Analyze_OneValidOneCorrupt_ReturnsNull()
{
// When one assembly is valid but the other is corrupt, the catch-all
// should still gracefully return null.
// 一方が有効で他方が破損している場合でも catch-all で null を返すべき。
var validPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var corruptPath = Path.Combine(_tempDir, "one-corrupt.dll");
File.WriteAllBytes(corruptPath, new byte[] { 0x4D, 0x5A, 0x00, 0x00 });
var result = AssemblyMethodAnalyzer.Analyze(validPath, corruptPath);
Assert.Null(result);
}
// ── SimpleSignatureTypeProvider improvement tests ────────────────────
[Fact]
[Trait("Category", "Unit")]
public void GenericContext_FromType_ResolvesTypeParameterNames()
{
// Verify that GenericContext reads type-level generic parameter names
// from a real assembly containing generic types.
// 実アセンブリのジェネリック型から型レベルジェネリックパラメータ名を読み取ることを検証。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
// Find a generic type in the runtime assembly (e.g. from System.Private.CoreLib via references)
// We'll look in the test assembly's referenced types instead — use Dictionary<string,string>
// which should be used somewhere. Alternatively, check the main assembly which has
// Dictionary<,> fields.
// 代わりにメインアセンブリのジェネリック型を検証
var mainAssemblyPath = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
using var mainStream = new FileStream(mainAssemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var mainPeReader = new PEReader(mainStream);
var mainReader = mainPeReader.GetMetadataReader();
bool foundGenericType = false;
foreach (var typeHandle in mainReader.TypeDefinitions)
{
var typeDef = mainReader.GetTypeDefinition(typeHandle);
var genericParams = typeDef.GetGenericParameters();
if (genericParams.Count > 0)
{
var context = GenericContext.FromType(mainReader, typeDef);
Assert.Equal(genericParams.Count, context.TypeParameters.Length);
Assert.True(context.MethodParameters.IsEmpty);
// All parameter names should be non-empty / すべてのパラメータ名が空でないこと
foreach (var paramName in context.TypeParameters)
Assert.False(string.IsNullOrEmpty(paramName), "Generic type parameter name should not be empty");
foundGenericType = true;
break;
}
}
Assert.True(foundGenericType, "Expected at least one generic type definition in the main assembly");
}
[Fact]
[Trait("Category", "Unit")]
public void GenericContext_FromMethod_ResolvesMethodParameterNames()
{
// Verify that GenericContext reads method-level generic parameter names.
// メソッドレベルのジェネリックパラメータ名を読み取ることを検証。
var assemblyPath = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
bool foundGenericMethod = false;
foreach (var typeHandle in reader.TypeDefinitions)
{
var typeDef = reader.GetTypeDefinition(typeHandle);
foreach (var methodHandle in typeDef.GetMethods())
{
var methodDef = reader.GetMethodDefinition(methodHandle);
var genericParams = methodDef.GetGenericParameters();
if (genericParams.Count > 0)
{
var context = GenericContext.FromMethod(reader, typeDef, methodDef);
Assert.Equal(genericParams.Count, context.MethodParameters.Length);
foreach (var paramName in context.MethodParameters)
Assert.False(string.IsNullOrEmpty(paramName), "Generic method parameter name should not be empty");
foundGenericMethod = true;
break;
}
}
if (foundGenericMethod) break;
}
// Note: if no generic methods exist in the main assembly, this test documents it.
// Even without generic methods, the FromMethod path is exercised and shouldn't crash.
// ジェネリックメソッドが存在しなくてもクラッシュしないことを確認。
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetGenericTypeParameter_ResolvesWithContext()
{
// When a GenericContext is provided, GetGenericTypeParameter should return
// the declared name instead of the index-based fallback.
// GenericContext が提供された場合、インデックスベースのフォールバックではなく
// 宣言名を返すべき。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
var context = new GenericContext(
ImmutableArray.Create("TKey", "TValue"),
ImmutableArray.Create("TResult"));
// Type parameters / 型パラメータ
Assert.Equal("TKey", provider.GetGenericTypeParameter(context, 0));
Assert.Equal("TValue", provider.GetGenericTypeParameter(context, 1));
Assert.Equal("!2", provider.GetGenericTypeParameter(context, 2)); // out of range fallback
// Method parameters / メソッドパラメータ
Assert.Equal("TResult", provider.GetGenericMethodParameter(context, 0));
Assert.Equal("!!1", provider.GetGenericMethodParameter(context, 1)); // out of range fallback
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetGenericTypeParameter_FallsBackWithoutContext()
{
// When context is null, fall back to the index-based representation.
// コンテキストが null の場合、インデックスベース表現にフォールバック。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
Assert.Equal("!0", provider.GetGenericTypeParameter(null, 0));
Assert.Equal("!!0", provider.GetGenericMethodParameter(null, 0));
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetFunctionPointerType_ExpandsSignature()
{
// GetFunctionPointerType should expand the full signature rather than
// returning a fixed "delegate*" string.
// 固定文字列 "delegate*" ではなく完全なシグネチャを展開すべき。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
// No parameters / パラメータなし
var noParamSig = new MethodSignature<string>(
default(SignatureHeader),
"System.Void",
0,
0,
ImmutableArray<string>.Empty);
Assert.Equal("delegate*<System.Void>", provider.GetFunctionPointerType(noParamSig));
// With parameters / パラメータあり
var withParamsSig = new MethodSignature<string>(
default(SignatureHeader),
"System.Int32",
0,
2,
ImmutableArray.Create("System.String", "System.Boolean"));
Assert.Equal("delegate*<System.String, System.Boolean, System.Int32>", provider.GetFunctionPointerType(withParamsSig));
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetModifiedType_PreservesModifiers()
{
// GetModifiedType should preserve modreq/modopt annotations.
// modreq/modopt 注釈を保持すべき。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
Assert.Equal(
"System.Int32 modreq(System.Runtime.CompilerServices.IsVolatile)",
provider.GetModifiedType("System.Runtime.CompilerServices.IsVolatile", "System.Int32", isRequired: true));
Assert.Equal(
"System.IntPtr modopt(System.Runtime.CompilerServices.IsConst)",
provider.GetModifiedType("System.Runtime.CompilerServices.IsConst", "System.IntPtr", isRequired: false));
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetPinnedType_PreservesPinnedAnnotation()
{
// GetPinnedType should add a "pinned" prefix.
// "pinned" プレフィックスを付加すべき。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
Assert.Equal("pinned System.Byte", provider.GetPinnedType("System.Byte"));
}
[Fact]
[Trait("Category", "Unit")]
public void Analyze_SameAssembly_GenericSignaturesDoNotCauseChanges()
{
// Self-comparison after the generic context improvement should still detect
// zero changes — ensures the new name-resolved signatures are deterministic.
// ジェネリックコンテキスト改善後も自己比較で変更なしが維持されることを確認 —
// 名前解決後のシグネチャが決定的であることを保証。
var mainAssembly = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(mainAssembly, mainAssembly);
Assert.NotNull(result);
Assert.False(result.HasChanges);
Assert.Empty(result.Entries);
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetGenericInstantiation_StripsAritySuffix()
{
// GetGenericInstantiation should strip the backtick-arity suffix from the generic type name
// since the type arguments make the arity explicit.
// 型引数によりアリティは明示されるため、バッククォートアリティ接尾辞を除去すべき。
var assemblyPath = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
// Single type argument / 単一型引数
var result1 = provider.GetGenericInstantiation(
"System.Collections.Generic.List`1",
ImmutableArray.Create("System.Int32"));
Assert.Equal("System.Collections.Generic.List<System.Int32>", result1);
// Multiple type arguments / 複数型引数
var result2 = provider.GetGenericInstantiation(
"System.Collections.Generic.Dictionary`2",
ImmutableArray.Create("System.String", "System.Int32"));
Assert.Equal("System.Collections.Generic.Dictionary<System.String, System.Int32>", result2);
// Nested generics: inner result already resolved / ネストしたジェネリクス: 内側は解決済み
var innerGeneric = provider.GetGenericInstantiation(
"System.Collections.Generic.List`1",
ImmutableArray.Create("System.Int32"));
var result3 = provider.GetGenericInstantiation(
"System.Collections.Generic.Dictionary`2",
ImmutableArray.Create("System.String", innerGeneric));
Assert.Equal(
"System.Collections.Generic.Dictionary<System.String, System.Collections.Generic.List<System.Int32>>",
result3);
// Deeply nested generics: Func<string, Task<IEnumerable<int>>>
// 深くネストしたジェネリクス: Func<string, Task<IEnumerable<int>>>
var innermost = provider.GetGenericInstantiation(
"System.Collections.Generic.IEnumerable`1",
ImmutableArray.Create("System.Int32"));
var middle = provider.GetGenericInstantiation(
"System.Threading.Tasks.Task`1",
ImmutableArray.Create(innermost));
var outer = provider.GetGenericInstantiation(
"System.Func`2",
ImmutableArray.Create("System.String", middle));
Assert.Equal(
"System.Func<System.String, System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Int32>>>",
outer);
// No arity suffix: should pass through unchanged / アリティ接尾辞なし: そのまま通過
var result4 = provider.GetGenericInstantiation(
"MyNamespace.MyType",
ImmutableArray.Create("System.String"));
Assert.Equal("MyNamespace.MyType<System.String>", result4);
}
[Fact]
[Trait("Category", "Unit")]
public void SimpleSignatureTypeProvider_GetTypeFromReference_ResolvesNestedTypes()
{
// GetTypeFromReference should follow ResolutionScope for nested type references
// so that nested types are fully qualified (e.g. "Outer/Inner" not just "Inner").
// ネストされた型参照の ResolutionScope をたどり完全修飾名を返すことを検証。
var assemblyPath = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
using var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var reader = peReader.GetMetadataReader();
var provider = new SimpleSignatureTypeProvider(reader);
// Find a nested type reference in the assembly / アセンブリ内のネスト型参照を探す
bool foundNestedRef = false;
foreach (var handle in reader.TypeReferences)
{
var typeRef = reader.GetTypeReference(handle);
if (typeRef.ResolutionScope.Kind == HandleKind.TypeReference)
{
string result = provider.GetTypeFromReference(reader, handle, 0);
// Should contain "/" separator for nested types / ネスト型は "/" 区切りを含むべき
Assert.Contains("/", result);
// Should not start with "/" / "/" で始まらないこと
Assert.False(result.StartsWith("/"), $"Nested type name should not start with '/': {result}");
foundNestedRef = true;
break;
}
}
// If no nested type references found, this is a documentation test —
// the assembly may not reference nested types from other assemblies.
// ネスト型参照が見つからない場合はドキュメントテスト(参照が存在しない可能性あり)。
if (!foundNestedRef)
{
// At minimum, verify non-nested references still work / 非ネスト参照が正常に動作することを確認
foreach (var handle in reader.TypeReferences)
{
var typeRef = reader.GetTypeReference(handle);
if (typeRef.ResolutionScope.Kind != HandleKind.TypeReference)
{
string result = provider.GetTypeFromReference(reader, handle, 0);
Assert.False(string.IsNullOrEmpty(result));
break;
}
}
}
}
[Theory]
[Trait("Category", "Unit")]
// Documented System.Reflection.Metadata failure modes — must be recoverable so
// the signature-decoder fallbacks keep returning the "(#hex)" / empty-string
// fallbacks instead of failing the whole analysis.
// System.Reflection.Metadata が出す可能性のある失敗モード — シグネチャデコーダ
// のフォールバックが丸ごと失敗しないよう、これらは回復可能と扱う必要がある。
[InlineData(typeof(BadImageFormatException))]
[InlineData(typeof(InvalidOperationException))]
[InlineData(typeof(ArgumentException))]
[InlineData(typeof(ArgumentOutOfRangeException))] // subclass of ArgumentException / ArgumentException のサブクラス
[InlineData(typeof(NotSupportedException))]
[InlineData(typeof(OverflowException))]
[InlineData(typeof(IndexOutOfRangeException))]
public void IsMetadataDecodeRecoverable_ReturnsTrue_ForKnownMetadataDecodeFailureTypes(Type exceptionType)
{
bool matched = InvokeIsMetadataDecodeRecoverable((Exception)Activator.CreateInstance(exceptionType)!);
Assert.True(matched, $"Expected IsMetadataDecodeRecoverable to accept {exceptionType.Name}.");
}
[Theory]
[Trait("Category", "Unit")]
// Programmer errors and unrecoverable runtime failures must NOT be swallowed by the
// narrow filter; they have to propagate so the outer Analyze catch-all can invoke
// onError with the real exception rather than hiding it as a hex-string fallback.
// プログラマエラーや回復不能系は、narrow フィルタで握り潰してはならない。外側の
// Analyze の catch-all で onError を介してそのまま surface させる。
[InlineData(typeof(NullReferenceException))]
[InlineData(typeof(StackOverflowException))]
[InlineData(typeof(OutOfMemoryException))]
[InlineData(typeof(AccessViolationException))]
[InlineData(typeof(OperationCanceledException))]
public void IsMetadataDecodeRecoverable_ReturnsFalse_ForNonMetadataFailureTypes(Type exceptionType)
{
bool matched = InvokeIsMetadataDecodeRecoverable((Exception)Activator.CreateInstance(exceptionType)!);
Assert.False(matched, $"Expected IsMetadataDecodeRecoverable to reject {exceptionType.Name}.");
}
private static bool InvokeIsMetadataDecodeRecoverable(Exception exception)
{
var method = typeof(AssemblyMethodAnalyzer).GetMethod(
"IsMetadataDecodeRecoverable",
BindingFlags.Static | BindingFlags.NonPublic);
Assert.NotNull(method);
var result = method.Invoke(null, [exception]);
Assert.IsType<bool>(result);
return (bool)result!;
}
[Fact]
[Trait("Category", "Unit")]
public void Analyze_RuntimeAssembly_GenericSignaturesDoNotContainAritySuffix()
{
// Verify that after the fix, analysed assemblies produce signatures
// without backtick-arity suffixes in generic instantiations.
// 修正後、解析されたアセンブリのジェネリクスインスタンス化にバッククォートアリティ接尾辞が含まれないことを検証。
var testAssembly = typeof(AssemblyMethodAnalyzerTests).Assembly.Location;
var mainAssembly = typeof(FolderDiffIL4DotNet.Models.ConfigSettings).Assembly.Location;
var result = AssemblyMethodAnalyzer.Analyze(testAssembly, mainAssembly);
Assert.NotNull(result);
// Check that no entry has backtick-arity in generic type arguments
// (signatures like "Dictionary`2<String, Int32>" should now be "Dictionary<String, Int32>")
// ジェネリック型引数にバッククォートアリティが含まれないことを確認
foreach (var entry in result.Entries)
{
// Only check entries that contain angle brackets (generic signatures)
// 山括弧を含むエントリ(ジェネリクスシグネチャ)のみ検査
if (entry.Parameters.Contains('<'))
Assert.DoesNotMatch(@"`\d+<", entry.Parameters);
if (entry.ReturnType.Contains('<'))
Assert.DoesNotMatch(@"`\d+<", entry.ReturnType);
if (entry.BaseType.Contains('<'))
Assert.DoesNotMatch(@"`\d+<", entry.BaseType);
}
}
}
}