Skip to content

Commit f2ba304

Browse files
Add test
1 parent 055b816 commit f2ba304

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Testing;
3+
4+
namespace Sundstrom.CheckedExceptions.Tests;
5+
6+
using Verifier = CSharpAnalyzerVerifier<CheckedExceptionsAnalyzer, DefaultVerifier>;
7+
8+
partial class CheckedExceptionsAnalyzerTests
9+
{
10+
[Fact]
11+
public async Task ThrowingConstructor_WithThrowsAttribute_ShouldReportDiagnostics_WhenCallerDoesNotHandleOrDeclare()
12+
{
13+
var test = /* lang=c#-test */ """
14+
using System;
15+
16+
public class ThrowingObject
17+
{
18+
[Throws(typeof(InvalidOperationException))]
19+
public ThrowingObject()
20+
{
21+
throw new InvalidOperationException();
22+
}
23+
}
24+
25+
public class TestClass
26+
{
27+
public void ExplicitNew()
28+
{
29+
ThrowingObject obj = new ThrowingObject(); // ❗ Expect diagnostic
30+
}
31+
32+
public void TargetTypedNew()
33+
{
34+
ThrowingObject obj = new(); // ❗ Expect diagnostic
35+
}
36+
}
37+
""";
38+
39+
var expected1 = Verifier.MightBeThrown("InvalidOperationException")
40+
.WithSpan(16, 30, 16, 50); // new ThrowingObject()
41+
42+
var expected2 = Verifier.MightBeThrown("InvalidOperationException")
43+
.WithSpan(21, 30, 21, 35); // new()
44+
45+
await Verifier.VerifyAnalyzerAsync(test, expected1, expected2);
46+
}
47+
}

0 commit comments

Comments
 (0)