|
1 | | -using System.Reflection; |
2 | | -using System.Text; |
| 1 | +using PublicApiGenerator; |
| 2 | +using VerifyXunit; |
3 | 3 |
|
4 | 4 | namespace ServiceComposer.AspNetCore.Testing.Tests; |
5 | 5 |
|
6 | 6 | public class PublicApiApprovalTests |
7 | 7 | { |
8 | 8 | [Fact] |
9 | | - public void Public_api_is_approved() |
| 9 | + public Task Public_api_is_approved() |
10 | 10 | { |
11 | | - var actual = GeneratePublicApi(); |
12 | | - var approvedPath = Path.Combine(AppContext.BaseDirectory, "PublicApi.approved.txt"); |
13 | | - var approved = File.ReadAllText(approvedPath); |
14 | | - |
15 | | - Assert.Equal(Normalize(approved), Normalize(actual)); |
16 | | - } |
17 | | - |
18 | | - static string GeneratePublicApi() |
19 | | - { |
20 | | - var assembly = typeof(WebApplicationFactoryWithWebHost<>).Assembly; |
21 | | - var lines = new List<string>(); |
22 | | - |
23 | | - foreach (var type in assembly.GetExportedTypes().OrderBy(t => t.FullName, StringComparer.Ordinal)) |
24 | | - { |
25 | | - lines.Add($"type {FormatTypeName(type)}"); |
26 | | - |
27 | | - foreach (var constructor in type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).OrderBy(c => c.ToString(), StringComparer.Ordinal)) |
28 | | - { |
29 | | - lines.Add($" ctor {FormatConstructor(constructor)}"); |
30 | | - } |
31 | | - |
32 | | - foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly).OrderBy(p => p.Name, StringComparer.Ordinal)) |
33 | | - { |
34 | | - lines.Add($" property {FormatTypeName(property.PropertyType)} {property.Name}"); |
35 | | - } |
36 | | - |
37 | | - foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) |
38 | | - .Where(m => !m.IsSpecialName) |
39 | | - .OrderBy(m => m.ToString(), StringComparer.Ordinal)) |
40 | | - { |
41 | | - lines.Add($" method {FormatTypeName(method.ReturnType)} {method.Name}({string.Join(", ", method.GetParameters().Select(FormatParameter))})"); |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - return string.Join(Environment.NewLine, lines); |
46 | | - } |
47 | | - |
48 | | - static string FormatConstructor(ConstructorInfo constructor) => $"{constructor.DeclaringType!.Name}({string.Join(", ", constructor.GetParameters().Select(FormatParameter))})"; |
49 | | - |
50 | | - static string FormatParameter(ParameterInfo parameter) => $"{FormatTypeName(parameter.ParameterType)} {parameter.Name}"; |
51 | | - |
52 | | - static string FormatTypeName(Type type) |
53 | | - { |
54 | | - if (type.IsGenericType) |
55 | | - { |
56 | | - var genericTypeName = type.GetGenericTypeDefinition().FullName!; |
57 | | - var tickIndex = genericTypeName.IndexOf('`'); |
58 | | - var typeName = tickIndex >= 0 ? genericTypeName[..tickIndex] : genericTypeName; |
59 | | - return $"{typeName}<{string.Join(", ", type.GetGenericArguments().Select(FormatTypeName))}>"; |
60 | | - } |
61 | | - |
62 | | - return type.FullName ?? type.Name; |
63 | | - } |
64 | | - |
65 | | - static string Normalize(string value) |
66 | | - { |
67 | | - var builder = new StringBuilder(value.Length); |
68 | | - using var reader = new StringReader(value); |
69 | | - string? line; |
70 | | - var first = true; |
71 | | - while ((line = reader.ReadLine()) != null) |
72 | | - { |
73 | | - if (!first) |
74 | | - { |
75 | | - builder.Append('\n'); |
76 | | - } |
77 | | - |
78 | | - builder.Append(line.TrimEnd()); |
79 | | - first = false; |
80 | | - } |
81 | | - |
82 | | - return builder.ToString(); |
| 11 | + var publicApi = ApiGenerator.GeneratePublicApi(typeof(WebApplicationFactoryWithWebHost<>).Assembly); |
| 12 | + return Verifier.Verify(publicApi); |
83 | 13 | } |
84 | 14 | } |
0 commit comments