Skip to content

Commit ec5bd43

Browse files
committed
Test GetEnumerator/Dispose/Deconstruct
1 parent 6803108 commit ec5bd43

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

src/Compilers/CSharp/Test/Semantic/Semantics/InterceptorsTests.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,6 +3425,99 @@ class C : I
34253425
verifier.VerifyDiagnostics();
34263426
}
34273427

3428+
[Fact]
3429+
public void InterceptGetEnumerator()
3430+
{
3431+
var source = """
3432+
using System.Collections;
3433+
using System.Runtime.CompilerServices;
3434+
3435+
var myEnumerable = new MyEnumerable();
3436+
foreach (var item in myEnumerable)
3437+
{
3438+
}
3439+
3440+
class MyEnumerable : IEnumerable
3441+
{
3442+
public IEnumerator GetEnumerator() => throw null!;
3443+
}
3444+
3445+
static class MyEnumerableExt
3446+
{
3447+
[InterceptsLocation("Program.cs", 5, 22)] // 1
3448+
public static IEnumerator GetEnumerator1(this MyEnumerable en) => throw null!;
3449+
}
3450+
""";
3451+
3452+
var comp = CreateCompilation(new[] { (source, "Program.cs"), s_attributesSource }, parseOptions: RegularWithInterceptors);
3453+
comp.VerifyEmitDiagnostics(
3454+
// Program.cs(16,6): error CS27014: Possible method name 'myEnumerable' cannot be intercepted because it is not being invoked.
3455+
// [InterceptsLocation("Program.cs", 5, 22)] // 1
3456+
Diagnostic(ErrorCode.ERR_InterceptorNameNotInvoked, @"InterceptsLocation(""Program.cs"", 5, 22)").WithArguments("myEnumerable").WithLocation(16, 6));
3457+
}
3458+
3459+
[Fact]
3460+
public void InterceptDispose()
3461+
{
3462+
var source = """
3463+
using System;
3464+
using System.Runtime.CompilerServices;
3465+
3466+
var myDisposable = new MyDisposable();
3467+
using (myDisposable)
3468+
{
3469+
}
3470+
3471+
class MyDisposable : IDisposable
3472+
{
3473+
public void Dispose() => throw null!;
3474+
}
3475+
3476+
static class MyDisposeExt
3477+
{
3478+
[InterceptsLocation("Program.cs", 5, 8)] // 1
3479+
public static void Dispose1(this MyDisposable md) => throw null!;
3480+
}
3481+
""";
3482+
3483+
var comp = CreateCompilation(new[] { (source, "Program.cs"), s_attributesSource }, parseOptions: RegularWithInterceptors);
3484+
comp.VerifyEmitDiagnostics(
3485+
// Program.cs(16,6): error CS27014: Possible method name 'myDisposable' cannot be intercepted because it is not being invoked.
3486+
// [InterceptsLocation("Program.cs", 5, 8)] // 1
3487+
Diagnostic(ErrorCode.ERR_InterceptorNameNotInvoked, @"InterceptsLocation(""Program.cs"", 5, 8)").WithArguments("myDisposable").WithLocation(16, 6)
3488+
);
3489+
}
3490+
3491+
[Fact]
3492+
public void InterceptDeconstruct()
3493+
{
3494+
var source = """
3495+
using System;
3496+
using System.Runtime.CompilerServices;
3497+
3498+
var myDeconstructable = new MyDeconstructable();
3499+
var (x, y) = myDeconstructable;
3500+
3501+
class MyDeconstructable
3502+
{
3503+
public void Deconstruct(out int x, out int y) => throw null!;
3504+
}
3505+
3506+
static class MyDeconstructableExt
3507+
{
3508+
[InterceptsLocation("Program.cs", 5, 14)] // 1
3509+
public static void Deconstruct1(this MyDeconstructable md, out int x, out int y) => throw null!;
3510+
}
3511+
""";
3512+
3513+
var comp = CreateCompilation(new[] { (source, "Program.cs"), s_attributesSource }, parseOptions: RegularWithInterceptors);
3514+
comp.VerifyEmitDiagnostics(
3515+
// Program.cs(14,6): error CS27014: Possible method name 'myDeconstructable' cannot be intercepted because it is not being invoked.
3516+
// [InterceptsLocation("Program.cs", 5, 14)] // 1
3517+
Diagnostic(ErrorCode.ERR_InterceptorNameNotInvoked, @"InterceptsLocation(""Program.cs"", 5, 14)").WithArguments("myDeconstructable").WithLocation(14, 6)
3518+
);
3519+
}
3520+
34283521
[Fact]
34293522
public void PathMapping_01()
34303523
{

0 commit comments

Comments
 (0)