Skip to content

Commit add27f1

Browse files
committed
feat(tests): add tests for generic method handling in expressive properties
1 parent 456018d commit add27f1

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ExpressivePropertyTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,68 @@ public class Item;
453453
result.GeneratedTrees.Select(t => t.ToString())));
454454
}
455455

456+
[TestMethod]
457+
public void GenericMethodWithClosedInnerTypeArg_PinsByExactEquality()
458+
{
459+
// Inner type arg is concrete (List<int>), exercising the else branch in EnsureMethodInfo.
460+
var compilation = CreateCompilation(
461+
"""
462+
#nullable enable
463+
using ExpressiveSharp.Mapping;
464+
465+
namespace Foo {
466+
public static class ListExt {
467+
public static T Pick<T>(this IEnumerable<T> src, List<int> filter) => default!;
468+
}
469+
public partial class ClosedInnerArg {
470+
public List<int> Filter { get; init; } = null!;
471+
472+
[ExpressiveProperty("Head")]
473+
private string HeadExpr => new[] { "x" }.Pick(Filter);
474+
}
475+
}
476+
""");
477+
var result = RunExpressiveGenerator(compilation);
478+
479+
Assert.AreEqual(0, result.Diagnostics.Length,
480+
"Unexpected diagnostics: " + string.Join(", ", result.Diagnostics.Select(d => d.Id + ": " + d.GetMessage())));
481+
482+
var generated = string.Join("\n", result.GeneratedTrees.Select(t => t.ToString()));
483+
StringAssert.Contains(generated, "GetGenericArguments()[0] == typeof(int)",
484+
"Closed inner type arg must be pinned by exact equality.");
485+
}
486+
487+
[TestMethod]
488+
public void GenericMethodWithArrayOfTypeParam_GeneratesValidCode()
489+
{
490+
// Inner type arg is T[] (IArrayTypeSymbol), exercising the array recursion in ContainsTypeParameter.
491+
var compilation = CreateCompilation(
492+
"""
493+
#nullable enable
494+
using ExpressiveSharp.Mapping;
495+
496+
namespace Foo {
497+
public static class ArrExt {
498+
public static int Total<T>(this IEnumerable<T[]> source) => 0;
499+
}
500+
public partial class ArrayOfTypeParam {
501+
public IEnumerable<int[]> Source { get; init; } = null!;
502+
503+
[ExpressiveProperty("Sum")]
504+
private int SumExpr => Source.Total();
505+
}
506+
}
507+
""");
508+
var result = RunExpressiveGenerator(compilation);
509+
510+
Assert.AreEqual(0, result.Diagnostics.Length,
511+
"Unexpected diagnostics: " + string.Join(", ", result.Diagnostics.Select(d => d.Id + ": " + d.GetMessage())));
512+
513+
var generated = string.Join("\n", result.GeneratedTrees.Select(t => t.ToString()));
514+
Assert.IsFalse(generated.Contains("typeof(T)") || generated.Contains("typeof(T["),
515+
"Array-of-type-parameter slot must not leak the open type parameter into typeof().");
516+
}
517+
456518
[TestMethod]
457519
public Task BodyUsingQueryableWhereOrderBy_EmitsValidExpressionTree()
458520
{

0 commit comments

Comments
 (0)