Skip to content

Commit a387023

Browse files
Compile SourceGenerator-tests to ensure that the sample itself is valid (#8484)
Co-authored-by: Michael Staib <[email protected]>
1 parent 7951b9d commit a387023

13 files changed

+1016
-23
lines changed

src/HotChocolate/Core/src/Types.Analyzers/Models/DataLoaderInfo.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,23 @@ private static bool IsQueryContext(IParameterSymbol parameter)
290290

291291
public static bool IsKeyValuePair(ITypeSymbol returnTypeSymbol, ITypeSymbol keyType, ITypeSymbol valueType)
292292
{
293-
if (returnTypeSymbol is INamedTypeSymbol namedTypeSymbol
294-
&& namedTypeSymbol.IsGenericType
295-
&& namedTypeSymbol.OriginalDefinition.SpecialType == SpecialType.None
296-
&& namedTypeSymbol.ConstructedFrom.ToDisplayString().StartsWith(WellKnownTypes.KeyValuePair)
297-
&& keyType.Equals(namedTypeSymbol.TypeArguments[0], SymbolEqualityComparer.Default)
298-
&& valueType.Equals(namedTypeSymbol.TypeArguments[1], SymbolEqualityComparer.Default))
293+
if (returnTypeSymbol is INamedTypeSymbol namedTypeSymbol)
299294
{
300-
return true;
295+
// Handle nullable types and extract the underlying type
296+
var actualTypeSymbol = namedTypeSymbol;
297+
if (namedTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
298+
{
299+
actualTypeSymbol = (INamedTypeSymbol)namedTypeSymbol.TypeArguments[0];
300+
}
301+
302+
if (actualTypeSymbol.IsGenericType
303+
&& actualTypeSymbol.OriginalDefinition.SpecialType == SpecialType.None
304+
&& actualTypeSymbol.ConstructedFrom.ToDisplayString().StartsWith(WellKnownTypes.KeyValuePair)
305+
&& keyType.Equals(actualTypeSymbol.TypeArguments[0], SymbolEqualityComparer.Default)
306+
&& valueType.Equals(actualTypeSymbol.TypeArguments[1], SymbolEqualityComparer.Default))
307+
{
308+
return true;
309+
}
301310
}
302311

303312
return false;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.Analyzers", "HotChocolate.Types.Analyzers.csproj", "{BB54B7B2-D13C-F2C3-BA62-E873A000FED6}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{BB54B7B2-D13C-F2C3-BA62-E873A000FED6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{BB54B7B2-D13C-F2C3-BA62-E873A000FED6}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{BB54B7B2-D13C-F2C3-BA62-E873A000FED6}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{BB54B7B2-D13C-F2C3-BA62-E873A000FED6}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {D6EA0B42-0306-484C-A978-F6563BDD6395}
23+
EndGlobalSection
24+
EndGlobal

src/HotChocolate/Core/test/Types.Analyzers.Tests/DataLoaderTests.cs

Lines changed: 204 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ public class Entity
7979
""").MatchMarkdownAsync();
8080
}
8181

82+
[Fact]
83+
public async Task GenerateSource_BatchDataLoader_Nullable_Object_MatchesSnapshot()
84+
{
85+
await TestHelper.GetGeneratedSourceSnapshot(
86+
"""
87+
using System.Collections.Generic;
88+
using System.Threading;
89+
using System.Threading.Tasks;
90+
using HotChocolate;
91+
using GreenDonut;
92+
93+
namespace TestNamespace;
94+
95+
internal static class TestClass
96+
{
97+
[DataLoader]
98+
public static Task<IReadOnlyDictionary<int, Entity?>> GetEntityByIdAsync(
99+
IReadOnlyList<int> entityIds,
100+
CancellationToken cancellationToken)
101+
=> default!;
102+
}
103+
104+
public class Entity
105+
{
106+
public int Id { get; set; }
107+
}
108+
""").MatchMarkdownAsync();
109+
}
110+
82111
[Fact]
83112
public async Task GenerateSource_BatchDataLoader_With_Group_MatchesSnapshot()
84113
{
@@ -200,6 +229,96 @@ public class Entity
200229
""").MatchMarkdownAsync();
201230
}
202231

232+
[Fact]
233+
public async Task GenerateSource_GroupedDataLoader_Nullable_Object_MatchesSnapshot()
234+
{
235+
await TestHelper.GetGeneratedSourceSnapshot(
236+
"""
237+
using System.Collections.Generic;
238+
using System.Linq;
239+
using System.Threading;
240+
using System.Threading.Tasks;
241+
using HotChocolate;
242+
using GreenDonut;
243+
244+
namespace TestNamespace;
245+
246+
internal static class TestClass
247+
{
248+
[DataLoader]
249+
public static Task<ILookup<int, Entity?>> GetEntitiesByIdAsync(
250+
IReadOnlyList<int> entityIds,
251+
CancellationToken cancellationToken)
252+
=> default!;
253+
}
254+
255+
public class Entity
256+
{
257+
public int Id { get; set; }
258+
}
259+
""").MatchMarkdownAsync();
260+
}
261+
262+
[Fact]
263+
public async Task GenerateSource_GroupedDataLoader_ValueType_MatchesSnapshot()
264+
{
265+
await TestHelper.GetGeneratedSourceSnapshot(
266+
"""
267+
using System.Collections.Generic;
268+
using System.Linq;
269+
using System.Threading;
270+
using System.Threading.Tasks;
271+
using HotChocolate;
272+
using GreenDonut;
273+
274+
namespace TestNamespace;
275+
276+
internal static class TestClass
277+
{
278+
[DataLoader]
279+
public static Task<ILookup<int, long>> GetEntitiesByIdAsync(
280+
IReadOnlyList<int> entityIds,
281+
CancellationToken cancellationToken)
282+
=> default!;
283+
}
284+
285+
public class Entity
286+
{
287+
public int Id { get; set; }
288+
}
289+
""").MatchMarkdownAsync();
290+
}
291+
292+
[Fact]
293+
public async Task GenerateSource_GroupedDataLoader_Nullable_ValueType_MatchesSnapshot()
294+
{
295+
await TestHelper.GetGeneratedSourceSnapshot(
296+
"""
297+
using System.Collections.Generic;
298+
using System.Linq;
299+
using System.Threading;
300+
using System.Threading.Tasks;
301+
using HotChocolate;
302+
using GreenDonut;
303+
304+
namespace TestNamespace;
305+
306+
internal static class TestClass
307+
{
308+
[DataLoader]
309+
public static Task<ILookup<int, long?>> GetEntitiesByIdAsync(
310+
IReadOnlyList<int> entityIds,
311+
CancellationToken cancellationToken)
312+
=> default!;
313+
}
314+
315+
public class Entity
316+
{
317+
public int Id { get; set; }
318+
}
319+
""").MatchMarkdownAsync();
320+
}
321+
203322
[Fact]
204323
public async Task GenerateSource_CacheDataLoader_MatchesSnapshot()
205324
{
@@ -228,6 +347,90 @@ public class Entity
228347
""").MatchMarkdownAsync();
229348
}
230349

350+
[Fact]
351+
public async Task GenerateSource_CacheDataLoader_Nullable_Object_MatchesSnapshot()
352+
{
353+
await TestHelper.GetGeneratedSourceSnapshot(
354+
"""
355+
using System.Threading;
356+
using System.Threading.Tasks;
357+
using HotChocolate;
358+
using GreenDonut;
359+
360+
namespace TestNamespace;
361+
362+
internal static class TestClass
363+
{
364+
[DataLoader]
365+
public static Task<Entity?> GetEntityByIdAsync(
366+
int entityId,
367+
CancellationToken cancellationToken)
368+
=> default!;
369+
}
370+
371+
public class Entity
372+
{
373+
public int Id { get; set; }
374+
}
375+
""").MatchMarkdownAsync();
376+
}
377+
378+
[Fact]
379+
public async Task GenerateSource_CacheDataLoader_ValueType_MatchesSnapshot()
380+
{
381+
await TestHelper.GetGeneratedSourceSnapshot(
382+
"""
383+
using System.Threading;
384+
using System.Threading.Tasks;
385+
using HotChocolate;
386+
using GreenDonut;
387+
388+
namespace TestNamespace;
389+
390+
internal static class TestClass
391+
{
392+
[DataLoader]
393+
public static Task<long> GetEntityByIdAsync(
394+
int entityId,
395+
CancellationToken cancellationToken)
396+
=> default!;
397+
}
398+
399+
public class Entity
400+
{
401+
public int Id { get; set; }
402+
}
403+
""").MatchMarkdownAsync();
404+
}
405+
406+
[Fact]
407+
public async Task GenerateSource_CacheDataLoader_Nullable_ValueType_MatchesSnapshot()
408+
{
409+
await TestHelper.GetGeneratedSourceSnapshot(
410+
"""
411+
using System.Threading;
412+
using System.Threading.Tasks;
413+
using HotChocolate;
414+
using GreenDonut;
415+
416+
namespace TestNamespace;
417+
418+
internal static class TestClass
419+
{
420+
[DataLoader]
421+
public static Task<long?> GetEntityByIdAsync(
422+
int entityId,
423+
CancellationToken cancellationToken)
424+
=> default!;
425+
}
426+
427+
public class Entity
428+
{
429+
public int Id { get; set; }
430+
}
431+
""").MatchMarkdownAsync();
432+
}
433+
231434
[Fact]
232435
public async Task GenerateSource_GenericBatchDataLoader_MatchesSnapshot()
233436
{
@@ -324,7 +527,7 @@ public static Task<IDictionary<int, Entity2>> GetEntityByIdAsync(
324527
CancellationToken cancellationToken)
325528
=> default!;
326529
327-
public static KeyValuePair<int, Entity2> CreateLookupKey(Entity1 key)
530+
public static KeyValuePair<int, Entity2>? CreateLookupKey(Entity1 key)
328531
=> default!;
329532
}
330533

src/HotChocolate/Core/test/Types.Analyzers.Tests/RequestMiddlewareTests.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ public class RequestMiddlewareTests
77
[Fact]
88
public async Task GenerateSource_RequestMiddleware_MatchesSnapshot()
99
{
10-
var currentUiCulture = CultureInfo.CurrentUICulture;
11-
try
12-
{
13-
// Snapshot contains localized strings -> adjust culture
14-
CultureInfo.CurrentUICulture = new CultureInfo("en-US");
15-
await TestHelper.GetGeneratedSourceSnapshot(
10+
await TestHelper.GetGeneratedSourceSnapshot(
11+
[
1612
"""
1713
#nullable enable
1814
using System.Threading.Tasks;
@@ -52,11 +48,8 @@ public async ValueTask InvokeAsync(
5248
5349
public class Service1;
5450
public class Service2;
55-
""").MatchMarkdownAsync();
56-
}
57-
finally
58-
{
59-
CultureInfo.CurrentUICulture = currentUiCulture;
60-
}
51+
"""
52+
],
53+
enableInterceptors: true).MatchMarkdownAsync();
6154
}
6255
}

0 commit comments

Comments
 (0)