-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExternalAssemblyErrorTypeTests.cs
More file actions
181 lines (146 loc) · 6.5 KB
/
ExternalAssemblyErrorTypeTests.cs
File metadata and controls
181 lines (146 loc) · 6.5 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
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace CSharpFunctionalExtensions.HttpResults.Generators.Tests;
/// <summary>
/// Tests that verify the source generator correctly handles error types defined in external assemblies.
/// This addresses the issue where short type names were used instead of fully qualified names,
/// causing compilation errors when error types came from referenced assemblies.
/// </summary>
public class ExternalAssemblyErrorTypeTests
{
[Fact]
public void GeneratesFullyQualifiedTypeNamesForExternalErrorType_WithToOkHttpResult()
{
// Create a fake external assembly with an error type
var externalErrorTypeCode = """
namespace MyApp.Infrastructure.Errors;
public sealed record NotFoundError(string Message);
""";
var externalAssemblySyntaxTree = CSharpSyntaxTree.ParseText(externalErrorTypeCode);
var externalCompilation = CSharpCompilation.Create(
"ExternalAssembly",
[externalAssemblySyntaxTree],
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location)],
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
// Emit the external assembly to get a reference
using var ms = new MemoryStream();
var emitResult = externalCompilation.Emit(ms);
emitResult.Success.Should().BeTrue("External assembly should compile");
ms.Position = 0;
var externalAssemblyReference = MetadataReference.CreateFromStream(ms);
// Create mapper in the main assembly that references the external error type
var mapperCode = """
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using CSharpFunctionalExtensions.HttpResults;
using MyApp.Infrastructure.Errors;
namespace MyApp.Api.ErrorMappers;
public class NotFoundErrorMapper : IResultErrorMapper<NotFoundError, ProblemHttpResult>
{
public ProblemHttpResult Map(NotFoundError error) =>
TypedResults.Problem(
statusCode: StatusCodes.Status404NotFound,
title: "Not Found",
detail: error.Message
);
}
""";
var (_, generatedSource) = GeneratorTestHelper.RunGenerator(mapperCode, [externalAssemblyReference]);
generatedSource.Should().Contain("this Result<T,global::MyApp.Infrastructure.Errors.NotFoundError> result");
}
[Fact]
public void GeneratesFullyQualifiedTypeNamesForExternalErrorType_MultipleMappers()
{
// Create fake external assemblies with error types
var externalErrorTypesCode = """
namespace MyApp.Infrastructure.Errors;
public sealed record NotFoundError(string Message);
public sealed record ValidationError(string Message);
""";
var externalAssemblySyntaxTree = CSharpSyntaxTree.ParseText(externalErrorTypesCode);
var externalCompilation = CSharpCompilation.Create(
"ExternalAssembly",
[externalAssemblySyntaxTree],
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location)],
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
using var ms = new MemoryStream();
var emitResult = externalCompilation.Emit(ms);
emitResult.Success.Should().BeTrue();
ms.Position = 0;
var externalAssemblyReference = MetadataReference.CreateFromStream(ms);
// Create multiple mappers
var mappersCode = """
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using CSharpFunctionalExtensions.HttpResults;
using MyApp.Infrastructure.Errors;
namespace MyApp.Api.ErrorMappers;
public class NotFoundErrorMapper : IResultErrorMapper<NotFoundError, ProblemHttpResult>
{
public ProblemHttpResult Map(NotFoundError error) =>
TypedResults.Problem(
statusCode: StatusCodes.Status404NotFound,
title: "Not Found",
detail: error.Message
);
}
public class ValidationErrorMapper : IResultErrorMapper<ValidationError, BadRequest<ProblemHttpResult>>
{
public BadRequest<ProblemHttpResult> Map(ValidationError error) =>
TypedResults.BadRequest(
TypedResults.Problem(
statusCode: StatusCodes.Status400BadRequest,
title: "Validation Error",
detail: error.Message
)
);
}
""";
var (_, generatedSource) = GeneratorTestHelper.RunGenerator(mappersCode, [externalAssemblyReference]);
generatedSource.Should().Contain("this Result<T,global::MyApp.Infrastructure.Errors.ValidationError");
generatedSource.Should().Contain("this Result<T,global::MyApp.Infrastructure.Errors.NotFoundError");
}
[Fact]
public void NoCompilationErrorsWithExternalErrorType()
{
// Create external assembly
var externalErrorTypeCode = """
namespace MyApp.Infrastructure.Errors;
public sealed record NotFoundError(string Message);
""";
var externalAssemblySyntaxTree = CSharpSyntaxTree.ParseText(externalErrorTypeCode);
var externalCompilation = CSharpCompilation.Create(
"ExternalAssembly",
[externalAssemblySyntaxTree],
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location)],
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
using var ms = new MemoryStream();
var emitResult = externalCompilation.Emit(ms);
emitResult.Success.Should().BeTrue();
ms.Position = 0;
var externalAssemblyReference = MetadataReference.CreateFromStream(ms);
var mapperCode = """
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using CSharpFunctionalExtensions.HttpResults;
using MyApp.Infrastructure.Errors;
namespace MyApp.Api.ErrorMappers;
public class NotFoundErrorMapper : IResultErrorMapper<NotFoundError, ProblemHttpResult>
{
public ProblemHttpResult Map(NotFoundError error) =>
TypedResults.Problem(
statusCode: StatusCodes.Status404NotFound,
title: "Not Found",
detail: error.Message
);
}
""";
var (diagnostics, generatedSource) = GeneratorTestHelper.RunGenerator(mapperCode, [externalAssemblyReference]);
diagnostics.Should().BeEmpty("Should not report diagnostics about NotFoundError being unresolved");
generatedSource.Should().NotBeNullOrEmpty("Should generate extension methods");
}
}