Skip to content

Commit 49a0d01

Browse files
Update
1 parent c54cefc commit 49a0d01

3 files changed

Lines changed: 65 additions & 24 deletions

File tree

IntelliTect.TestTools.Console.Tests/ConsoleAssertTests.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void ConsoleTester_HelloWorld_DontNormalizeCRLF()
145145
{
146146
const string view = "Hello World\r\n";
147147

148-
Assert.ThrowsExactly<Exception>(() =>
148+
Assert.ThrowsExactly<ConsoleAssertException>(() =>
149149
{
150150
ConsoleAssert.Expect(view, () =>
151151
{
@@ -160,7 +160,7 @@ public void ConsoleTester_HelloWorld_DontNormalizeCRLF()
160160
[DataRow("+hello+world+")]
161161
public void ConsoleTester_OutputIncludesPluses_PlusesAreNotStripped(string consoleInput)
162162
{
163-
Exception exception = Assert.ThrowsExactly<Exception>(() =>
163+
ConsoleAssertException exception = Assert.ThrowsExactly<ConsoleAssertException>(() =>
164164
{
165165
ConsoleAssert.Expect(consoleInput, () =>
166166
{
@@ -275,7 +275,7 @@ public void ExpectThrows_WhenNoExceptionIsThrown_ThrowsException()
275275
{
276276
const string view = @"Hello World";
277277

278-
Exception exception = Assert.ThrowsExactly<Exception>(() =>
278+
ConsoleAssertException exception = Assert.ThrowsExactly<ConsoleAssertException>(() =>
279279
{
280280
ConsoleAssert.ExpectThrows<FormatException>(view, () =>
281281
{
@@ -292,7 +292,7 @@ public async Task ExpectThrowsAsync_WhenNoExceptionIsThrown_ThrowsException()
292292
{
293293
const string view = @"Hello World";
294294

295-
Exception exception = await Assert.ThrowsExactlyAsync<Exception>(async () =>
295+
ConsoleAssertException exception = await Assert.ThrowsExactlyAsync<ConsoleAssertException>(async () =>
296296
{
297297
await ConsoleAssert.ExpectThrowsAsync<FormatException>(view, async () =>
298298
{
@@ -338,4 +338,21 @@ public void ExpectThrows_WithNormalizeOptions_AppliesNormalization()
338338
Assert.IsNotNull(exception);
339339
StringAssert.Contains(exception.Message, "Test exception");
340340
}
341-
}
341+
342+
[TestMethod]
343+
public void ExpectThrows_WithWrongExpectedOutput_ThrowsConsoleAssertException()
344+
{
345+
const string view = "Wrong output <<invalid\n>>Error: Invalid input";
346+
347+
Assert.ThrowsExactly<ConsoleAssertException>(() =>
348+
{
349+
ConsoleAssert.ExpectThrows<FormatException>(view, () =>
350+
{
351+
System.Console.Write("Enter a number: ");
352+
string input = System.Console.ReadLine();
353+
System.Console.Write("Error: Invalid input");
354+
int.Parse(input);
355+
});
356+
});
357+
}
358+
}

IntelliTect.TestTools.Console/ConsoleAssert.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static void Expect<T>(string expected, Func<string[], T> func, T expected
166166

167167
if (!expectedReturn.Equals(@return))
168168
{
169-
throw new Exception($"The value returned from {nameof(func)} ({@return}) was not the {nameof(expectedReturn)}({expectedReturn}) value.");
169+
throw new ConsoleAssertException($"The value returned from {nameof(func)} ({@return}) was not the {nameof(expectedReturn)}({expectedReturn}) value.");
170170
}
171171
}
172172

@@ -468,7 +468,7 @@ private static void AssertExpectation(string expectedOutput, string output, Func
468468
bool failTest = !areEquivalentOperator(expectedOutput, output);
469469
if (failTest)
470470
{
471-
throw new Exception(GetMessageText(expectedOutput, output, equivalentOperatorErrorMessage));
471+
throw new ConsoleAssertException(GetMessageText(expectedOutput, output, equivalentOperatorErrorMessage));
472472
}
473473
}
474474

@@ -739,16 +739,21 @@ public static TException ExpectThrows<TException>(string expected,
739739
NormalizeOptions normalizeOptions = NormalizeOptions.Default)
740740
where TException : Exception
741741
{
742-
try
743-
{
744-
Expect(expected, action, normalizeOptions);
745-
}
746-
catch (TException ex)
742+
(string input, string expectedOutput) = Parse(expected);
743+
TException caughtException = null;
744+
745+
string output = Execute(input, () =>
747746
{
748-
return ex;
749-
}
747+
try { action(); }
748+
catch (TException ex) { caughtException = ex; }
749+
});
750+
751+
if (caughtException is null)
752+
throw new ConsoleAssertException($"Expected exception of type {typeof(TException).Name} was not thrown.");
753+
754+
CompareOutput(output, expectedOutput, normalizeOptions, (l, r) => l == r, "Values are not equal");
750755

751-
throw new Exception($"Expected exception of type {typeof(TException).Name} was not thrown.");
756+
return caughtException;
752757
}
753758

754759
/// <summary>
@@ -768,15 +773,20 @@ public static async Task<TException> ExpectThrowsAsync<TException>(string expect
768773
NormalizeOptions normalizeOptions = NormalizeOptions.Default)
769774
where TException : Exception
770775
{
771-
try
772-
{
773-
await ExpectAsync(expected, action, normalizeOptions);
774-
}
775-
catch (TException ex)
776+
(string input, string expectedOutput) = Parse(expected);
777+
TException caughtException = null;
778+
779+
string output = await ExecuteAsync(input, async () =>
776780
{
777-
return ex;
778-
}
781+
try { await action(); }
782+
catch (TException ex) { caughtException = ex; }
783+
});
784+
785+
if (caughtException is null)
786+
throw new ConsoleAssertException($"Expected exception of type {typeof(TException).Name} was not thrown.");
787+
788+
CompareOutput(output, expectedOutput, normalizeOptions, (l, r) => l == r, "Values are not equal");
779789

780-
throw new Exception($"Expected exception of type {typeof(TException).Name} was not thrown.");
790+
return caughtException;
781791
}
782-
}
792+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace IntelliTect.TestTools.Console;
2+
3+
/// <summary>
4+
/// Exception thrown when a <see cref="ConsoleAssert"/> assertion fails.
5+
/// </summary>
6+
public class ConsoleAssertException : Exception
7+
{
8+
/// <inheritdoc />
9+
public ConsoleAssertException(string message) : base(message) { }
10+
11+
/// <inheritdoc />
12+
public ConsoleAssertException(string message, Exception innerException)
13+
: base(message, innerException) { }
14+
}

0 commit comments

Comments
 (0)