Skip to content

Fix ResultsOfTGenerated tests #57465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/Http/Http.Results/test/ResultsOfTTests.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Results<ChecksumResult1, ChecksumResult2> MyApi(int checksum)
}

[Fact]
public void ResultsOfTResult1TResult2_Throws_ArgumentNullException_WhenHttpContextIsNull()
public async Task ResultsOfTResult1TResult2_Throws_ArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
Expand All @@ -72,7 +72,7 @@ Results<ChecksumResult1, NoContent> MyApi()
// Act & Assert
var result = MyApi();

Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of surprised there isn't an analyzer for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is now, which is why we noticed this.
See #57303

{
await result.ExecuteAsync(httpContext);
});
Expand All @@ -84,7 +84,7 @@ public void ResultsOfTResult1TResult2_Throws_InvalidOperationException_WhenResul
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
{
return new ChecksumResult1(1);
return (ChecksumResult1)null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I reading correctly that the test was looking for an argument null exception but the argument wasn't null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, although it throws an InvalidOperationException because the null value here is not an argument but the return type of a method we've indicated cannot be null.

}
var httpContext = GetHttpContext();

Expand Down Expand Up @@ -275,7 +275,7 @@ Results<ChecksumResult1, ChecksumResult2, ChecksumResult3> MyApi(int checksum)
}

[Fact]
public void ResultsOfTResult1TResult2TResult3_Throws_ArgumentNullException_WhenHttpContextIsNull()
public async Task ResultsOfTResult1TResult2TResult3_Throws_ArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
Expand All @@ -287,7 +287,7 @@ Results<ChecksumResult1, NoContent> MyApi()
// Act & Assert
var result = MyApi();

Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await result.ExecuteAsync(httpContext);
});
Expand All @@ -299,7 +299,7 @@ public void ResultsOfTResult1TResult2TResult3_Throws_InvalidOperationException_W
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
{
return new ChecksumResult1(1);
return (ChecksumResult1)null;
}
var httpContext = GetHttpContext();

Expand Down Expand Up @@ -559,7 +559,7 @@ Results<ChecksumResult1, ChecksumResult2, ChecksumResult3, ChecksumResult4> MyAp
}

[Fact]
public void ResultsOfTResult1TResult2TResult3TResult4_Throws_ArgumentNullException_WhenHttpContextIsNull()
public async Task ResultsOfTResult1TResult2TResult3TResult4_Throws_ArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
Expand All @@ -571,7 +571,7 @@ Results<ChecksumResult1, NoContent> MyApi()
// Act & Assert
var result = MyApi();

Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await result.ExecuteAsync(httpContext);
});
Expand All @@ -583,7 +583,7 @@ public void ResultsOfTResult1TResult2TResult3TResult4_Throws_InvalidOperationExc
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
{
return new ChecksumResult1(1);
return (ChecksumResult1)null;
}
var httpContext = GetHttpContext();

Expand Down Expand Up @@ -920,7 +920,7 @@ Results<ChecksumResult1, ChecksumResult2, ChecksumResult3, ChecksumResult4, Chec
}

[Fact]
public void ResultsOfTResult1TResult2TResult3TResult4TResult5_Throws_ArgumentNullException_WhenHttpContextIsNull()
public async Task ResultsOfTResult1TResult2TResult3TResult4TResult5_Throws_ArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
Expand All @@ -932,7 +932,7 @@ Results<ChecksumResult1, NoContent> MyApi()
// Act & Assert
var result = MyApi();

Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await result.ExecuteAsync(httpContext);
});
Expand All @@ -944,7 +944,7 @@ public void ResultsOfTResult1TResult2TResult3TResult4TResult5_Throws_InvalidOper
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
{
return new ChecksumResult1(1);
return (ChecksumResult1)null;
}
var httpContext = GetHttpContext();

Expand Down Expand Up @@ -1366,7 +1366,7 @@ Results<ChecksumResult1, ChecksumResult2, ChecksumResult3, ChecksumResult4, Chec
}

[Fact]
public void ResultsOfTResult1TResult2TResult3TResult4TResult5TResult6_Throws_ArgumentNullException_WhenHttpContextIsNull()
public async Task ResultsOfTResult1TResult2TResult3TResult4TResult5TResult6_Throws_ArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
Expand All @@ -1378,7 +1378,7 @@ Results<ChecksumResult1, NoContent> MyApi()
// Act & Assert
var result = MyApi();

Assert.ThrowsAsync<ArgumentNullException>(async () =>
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await result.ExecuteAsync(httpContext);
});
Expand All @@ -1390,7 +1390,7 @@ public void ResultsOfTResult1TResult2TResult3TResult4TResult5TResult6_Throws_Inv
// Arrange
Results<ChecksumResult1, NoContent> MyApi()
{
return new ChecksumResult1(1);
return (ChecksumResult1)null;
}
var httpContext = GetHttpContext();

Expand Down
10 changes: 5 additions & 5 deletions src/Http/Http.Results/tools/ResultsOfTGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ static void GenerateTest_ExecuteResult_ExecutesAssignedResult(StreamWriter write
static void GenerateTest_Throws_ArgumentNullException_WhenHttpContextIsNull(StreamWriter writer, int typeArgNumber)
{
//[Fact]
//public void ResultsOfTResult1TResult2_Throws_ArgumentNullException_WhenHttpContextIsNull()
//public async Task ResultsOfTResult1TResult2_Throws_ArgumentNullException_WhenHttpContextIsNull()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this sample test need the (ChecksumResult1)null change too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily. This one is testing the exception that is thrown when no HTTP context is provided (ref) which happens before we asset on the IResult type being null.

//{
// // Arrange
// Results<ChecksumResult1, NoContent> MyApi()
Expand All @@ -494,7 +494,7 @@ static void GenerateTest_Throws_ArgumentNullException_WhenHttpContextIsNull(Stre
// // Act & Assert
// var result = MyApi();

// Assert.ThrowsAsync<ArgumentNullException>(async () =>
// await Assert.ThrowsAsync<ArgumentNullException>(async () =>
// {
// await result.ExecuteAsync(httpContext);
// });
Expand All @@ -504,7 +504,7 @@ static void GenerateTest_Throws_ArgumentNullException_WhenHttpContextIsNull(Stre
writer.WriteIndentedLine("[Fact]");

// Start method
writer.WriteIndent(1, "public void ResultsOf");
writer.WriteIndent(1, "public async Task ResultsOf");
for (int j = 1; j <= typeArgNumber; j++)
{
writer.Write($"TResult{j}");
Expand All @@ -526,7 +526,7 @@ static void GenerateTest_Throws_ArgumentNullException_WhenHttpContextIsNull(Stre
writer.WriteIndentedLine(2, "var result = MyApi();");
writer.WriteLine();

writer.WriteIndentedLine(2, "Assert.ThrowsAsync<ArgumentNullException>(async () =>");
writer.WriteIndentedLine(2, "await Assert.ThrowsAsync<ArgumentNullException>(async () =>");
writer.WriteIndentedLine(2, "{");
writer.WriteIndentedLine(3, "await result.ExecuteAsync(httpContext);");
writer.WriteIndentedLine(2, "});");
Expand Down Expand Up @@ -573,7 +573,7 @@ static void GenerateTest_Throws_InvalidOperationException_WhenResultIsNull(Strea
writer.WriteIndentedLine(2, "// Arrange");
writer.WriteIndentedLine(2, "Results<ChecksumResult1, NoContent> MyApi()");
writer.WriteIndentedLine(2, "{");
writer.WriteIndentedLine(3, "return new ChecksumResult1(1);");
writer.WriteIndentedLine(3, "return (ChecksumResult1)null;");
writer.WriteIndentedLine(2, "}");
writer.WriteIndentedLine(2, "var httpContext = GetHttpContext();");
writer.WriteLine();
Expand Down
Loading