Skip to content

Allow an example to specify that it is expected to throw an exception #648

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
Oct 26, 2022
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
1 change: 1 addition & 0 deletions standard/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Because of array covariance, assignments to elements of reference type arrays in

> *Example*:
>
> <!-- Example: {template:"standalone-console", name:"CovarianceException", expectedException:"ArrayTypeMismatchException"} -->
> ```csharp
> class Test
> {
Expand Down
1 change: 1 addition & 0 deletions tools/ExampleExtractor/ExampleMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ExampleMetadata
public List<string> ExpectedErrors { get; set; }
public List<string> ExpectedWarnings { get; set; }
public List<string> ExpectedOutput { get; set; }
public string ExpectedException { get; set; }

// Information provided by the example extractor
public string MarkdownFile { get; set; }
Expand Down
36 changes: 34 additions & 2 deletions tools/ExampleTester/GeneratedExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,19 @@ bool ValidateOutput()

var oldOut = Console.Out;
List<string> actualLines;
Exception? actualException = null;
try
{
var builder = new StringBuilder();
Console.SetOut(new StringWriter(builder));
method.Invoke(null, arguments);
try
{
method.Invoke(null, arguments);
}
catch (TargetInvocationException outer)
{
actualException = outer.InnerException ?? throw new InvalidOperationException("TargetInvocationException had no nested exception");
}
// Skip blank lines, to avoid unnecessary trailing empties.
actualLines = builder.ToString().Replace("\r\n", "\n").Split('\n').Where(line => line != "").ToList();
}
Expand All @@ -108,7 +116,31 @@ bool ValidateOutput()
Console.SetOut(oldOut);
}
var expectedLines = Metadata.ExpectedOutput ?? new List<string>();
return ValidateExpectedAgainstActual("output", expectedLines, actualLines);
return ValidateException(actualException, Metadata.ExpectedException) &&
ValidateExpectedAgainstActual("output", expectedLines, actualLines);
}

bool ValidateException(Exception? actualException, string? expectedExceptionName)
{
return (actualException, expectedExceptionName) switch
{
(null, null) => true,
(Exception ex, string name) =>
MaybeReportError(ex.GetType().Name == name, $" Mismatched exception type: Expected {name}; Was {ex.GetType().Name}"),
(null, string name) =>
MaybeReportError(false, $" Expected exception type {name}; no exception was thrown"),
(Exception ex, null) =>
MaybeReportError(false, $" Exception type {ex.GetType().Name} was thrown unexpectedly; Message: {ex.Message}")
};

bool MaybeReportError(bool result, string message)
{
if (!result)
{
Console.WriteLine(message);
}
return result;
}
}

bool ValidateExpectedAgainstActual(string type, List<string> expected, List<string> actual)
Expand Down