Skip to content

Commit 316b47f

Browse files
authored
leverage DoesNotReturnIfAttribute for better assertion (#3168)
1 parent 20f3fa4 commit 316b47f

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

test/UnitTests/MSTestAdapter.UnitTests/Execution/TestMethodInfoTests.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ public void TestMethodInfoInvokeShouldSetStackTraceInformationIfTestClassConstru
299299
var exception = _testMethodInfo.Invoke(null).TestFailureException as TestFailedException;
300300

301301
Verify(exception is not null);
302+
Verify(exception.StackTraceInformation is not null);
302303
Verify(
303-
(bool)exception?.StackTraceInformation.ErrorStackTrace.StartsWith(
304-
" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.<TestMethodInfoInvokeShouldSetStackTraceInformationIfTestClassConstructorThrows>b__", StringComparison.Ordinal));
304+
exception.StackTraceInformation.ErrorStackTrace.StartsWith(
305+
" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.<TestMethodInfoInvokeShouldSetStackTraceInformationIfTestClassConstructorThrows>b__", StringComparison.Ordinal));
305306
}
306307

307308
public void TestMethodInfoInvokeShouldSetStackTraceInformationIfTestClassConstructorThrowsWithoutInnerException()
@@ -313,8 +314,9 @@ public void TestMethodInfoInvokeShouldSetStackTraceInformationIfTestClassConstru
313314
var exception = method.Invoke(null).TestFailureException as TestFailedException;
314315

315316
Verify(exception is not null);
317+
Verify(exception.StackTraceInformation is not null);
316318
Verify(
317-
(bool)exception?.StackTraceInformation.ErrorStackTrace.StartsWith(
319+
exception.StackTraceInformation.ErrorStackTrace.StartsWith(
318320
" at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)", StringComparison.Ordinal));
319321
}
320322

@@ -403,9 +405,10 @@ public void TestMethodInfoInvokeShouldSetStackTraceInformationIfSetTestContextTh
403405
var exception = _testMethodInfo.Invoke(null).TestFailureException as TestFailedException;
404406

405407
Verify(exception is not null);
408+
Verify(exception.StackTraceInformation is not null);
406409
Verify(
407-
(bool)exception?.StackTraceInformation.ErrorStackTrace.StartsWith(
408-
" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.<TestMethodInfoInvokeShouldSetStackTraceInformationIfSetTestContextThrows>b__", StringComparison.Ordinal));
410+
exception.StackTraceInformation.ErrorStackTrace.StartsWith(
411+
" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.<TestMethodInfoInvokeShouldSetStackTraceInformationIfSetTestContextThrows>b__", StringComparison.Ordinal));
409412
}
410413

411414
#endregion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#if NETSTANDARD2_0 || NETFRAMEWORK
5+
6+
#pragma warning disable SA1623 // Remove ridiculous stylecop documentation warning
7+
namespace System.Diagnostics.CodeAnalysis;
8+
9+
/// <summary>
10+
/// Specifies that the method will not return if the associated <see cref="bool"/>
11+
/// parameter is passed the specified value.
12+
/// </summary>
13+
[ExcludeFromCodeCoverage]
14+
[DebuggerNonUserCode]
15+
[AttributeUsage(AttributeTargets.Parameter)]
16+
public sealed class DoesNotReturnIfAttribute :
17+
Attribute
18+
{
19+
/// <summary>
20+
/// Gets the condition parameter value.
21+
/// Code after the method is considered unreachable by diagnostics if the argument
22+
/// to the associated parameter matches this value.
23+
/// </summary>
24+
public bool ParameterValue { get; }
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="DoesNotReturnIfAttribute"/>
28+
/// class with the specified parameter value.
29+
/// </summary>
30+
/// <param name="parameterValue">
31+
/// The condition parameter value.
32+
/// Code after the method is considered unreachable by diagnostics if the argument
33+
/// to the associated parameter matches this value.
34+
/// </param>
35+
public DoesNotReturnIfAttribute(bool parameterValue) =>
36+
ParameterValue = parameterValue;
37+
}
38+
39+
#endif

test/Utilities/TestFramework.ForTestingMSTest/TestContainer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Dispose()
5151
}
5252

5353
public static void Verify(
54-
bool condition,
54+
[DoesNotReturnIf(false)] bool condition,
5555
[CallerArgumentExpression(nameof(condition))] string? expression = default,
5656
[CallerMemberName] string? caller = default,
5757
[CallerFilePath] string? filePath = default,

0 commit comments

Comments
 (0)