-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathApiParityTests.cs
More file actions
136 lines (119 loc) · 4.82 KB
/
ApiParityTests.cs
File metadata and controls
136 lines (119 loc) · 4.82 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
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using aweXpect;
using static aweXpect.Expect;
using NUnit.Framework;
using static System.Reflection.BindingFlags;
namespace System.IO.Abstractions.Tests
{
[TestFixture]
public class ApiParityTests
{
[Test]
public async Task File() =>
await AssertParity(
typeof(File),
typeof(FileBase)
);
[Test]
public async Task FileInfo() =>
await AssertParity(
typeof(FileInfo),
typeof(FileInfoBase)
);
[Test]
public async Task FileVersionInfo() =>
await AssertParity(
typeof(Diagnostics.FileVersionInfo),
typeof(FileVersionInfoBase)
);
[Test]
public async Task Directory() =>
await AssertParity(
typeof(Directory),
typeof(DirectoryBase)
);
[Test]
public async Task DirectoryInfo() =>
await AssertParity(
typeof(DirectoryInfo),
typeof(DirectoryInfoBase)
);
[Test]
public async Task DriveInfo() =>
await AssertParity(
typeof(DriveInfo),
typeof(DriveInfoBase)
);
[Test]
public async Task Path() =>
await AssertParity(
typeof(Path),
typeof(PathBase)
);
[Test]
public async Task FileSystemWatcher() =>
await AssertParity(
typeof(FileSystemWatcher),
typeof(FileSystemWatcherBase)
);
private async Task AssertParity(Type referenceType, Type abstractionType)
{
static IEnumerable<string> GetMembers(Type type) => type
.GetMembers(bindingAttr: Instance | Static | Public | FlattenHierarchy)
.Select(x => x.ToString())
.OrderBy(x => x, StringComparer.Ordinal);
var referenceMembers = GetMembers(referenceType)
.Select(x => x.Replace("System.IO.FileStream", "System.IO.Abstractions.FileSystemStream"))
.Select(x => x.Replace("System.IO.Abstractions.FileSystemStreamOptions", "System.IO.FileStreamOptions"))
.Select(x => x.Replace("System.IO.FileSystemInfo", "System.IO.Abstractions.IFileSystemInfo"))
.Select(x => x.Replace("System.IO.FileInfo", "System.IO.Abstractions.IFileInfo"))
.Select(x => x.Replace("System.IO.DirectoryInfo", "System.IO.Abstractions.IDirectoryInfo"))
.Select(x => x.Replace("System.IO.DriveInfo", "System.IO.Abstractions.IDriveInfo"))
.Select(x => x.Replace("System.IO.WaitForChangedResult", "System.IO.Abstractions.IWaitForChangedResult"))
.Where(x => x != "System.Diagnostics.FileVersionInfo GetVersionInfo(System.String)");
var abstractionMembers = GetMembers(abstractionType)
.Where(x => !x.Contains("op_Implicit"))
.Where(x => x != "System.IO.Abstractions.IFileSystem get_FileSystem()")
.Where(x => x != "System.IO.Abstractions.IFileSystem FileSystem");
var diff = new ApiDiff(
extraMembers: abstractionMembers.Except(referenceMembers),
missingMembers: referenceMembers.Except(abstractionMembers)
);
var serializedDiff = JsonSerializer.Serialize(diff, SerializerOptions);
var snapshotPath = IO.Path.GetFullPath("../../../__snapshots__/");
var fileName = $"ApiParityTests.{referenceType.Name}_{snapshotSuffix}.snap";
var fileContent = IO.File.ReadAllText(IO.Path.Combine(snapshotPath, fileName));
await That(fileContent).IsEqualTo(serializedDiff)
.IgnoringNewlineStyle()
.IgnoringTrailingWhiteSpace();
}
private static JsonSerializerOptions SerializerOptions = new()
{
WriteIndented = true
};
private readonly struct ApiDiff
{
public ApiDiff(IEnumerable<string> extraMembers, IEnumerable<string> missingMembers)
{
ExtraMembers = extraMembers.ToArray();
MissingMembers = missingMembers.ToArray();
}
public string[] ExtraMembers { get; }
public string[] MissingMembers { get; }
}
#if NET472
private const string snapshotSuffix = ".NET Framework 4.7.2";
#elif NET6_0
private const string snapshotSuffix = ".NET 6.0";
#elif NET8_0
private const string snapshotSuffix = ".NET 8.0";
#elif NET9_0
private const string snapshotSuffix = ".NET 9.0";
#else
#error Unknown target framework.
#endif
}
}