Skip to content

Commit 6702cf0

Browse files
authored
Merge pull request #20 from fredrikhr/extended-nullable
Skip.If nullable-reference type post-condition annotations
2 parents bea1452 + 0bd7969 commit 6702cf0

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

Directory.Build.props

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PackageReference Include="Nerdbank.GitVersioning" Version="3.1.74" PrivateAssets="all" />
3030
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.5.0" PrivateAssets="all" />
3131
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164" PrivateAssets="all" />
32+
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
3233
</ItemGroup>
3334
<ItemGroup>
3435
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" />

src/Xunit.SkippableFact/Skip.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Xunit
55
{
6+
using System.Diagnostics.CodeAnalysis;
7+
68
/// <summary>
79
/// Static methods for dynamically skipping tests identified with
810
/// the <see cref="SkippableFactAttribute"/>.
@@ -14,7 +16,9 @@ public static class Skip
1416
/// </summary>
1517
/// <param name="condition">The condition that must evaluate to <c>true</c> for the test to be skipped.</param>
1618
/// <param name="reason">The explanation for why the test is skipped.</param>
17-
public static void If(bool condition, string? reason = null)
19+
public static void If(
20+
[DoesNotReturnIf(true)] bool condition,
21+
string? reason = null)
1822
{
1923
if (condition)
2024
{
@@ -27,7 +31,9 @@ public static void If(bool condition, string? reason = null)
2731
/// </summary>
2832
/// <param name="condition">The condition that must evaluate to <c>false</c> for the test to be skipped.</param>
2933
/// <param name="reason">The explanation for why the test is skipped.</param>
30-
public static void IfNot(bool condition, string? reason = null)
34+
public static void IfNot(
35+
[DoesNotReturnIf(false)] bool condition,
36+
string? reason = null)
3137
{
3238
Skip.If(!condition, reason);
3339
}

src/Xunit.SkippableFact/Xunit.SkippableFact.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<None Update="xunit.runner.json">
1212
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1313
</None>
14-
</ItemGroup>
14+
</ItemGroup>
1515
<ItemGroup>
1616
<PackageReference Include="Validation" Version="2.4.18" PrivateAssets="compile;contentfiles;analyzers;build" />
1717
<PackageReference Include="xunit.extensibility.execution" Version="2.1.0" Condition=" '$(TargetFramework)' == 'net45' " />

test/Xunit.SkippableFact.Tests/SkipTests.cs

+32
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,37 @@ public void IfNot_WithReason()
4848
Assert.Equal(reason, ex.Message);
4949
}
5050
}
51+
52+
[Fact]
53+
public void If_SupportsNullableReferenceTypesPostCondition()
54+
{
55+
// Provoke a possibly null value that is not detectable through
56+
// static analysis
57+
string? value =
58+
int.Parse("42", System.Globalization.CultureInfo.InvariantCulture) == 42
59+
? "Not null"
60+
: null;
61+
62+
Skip.If(value is null);
63+
64+
// Does not trigger a nullable reference type warning
65+
_ = value.Substring(0);
66+
}
67+
68+
[Fact]
69+
public void IfNot_SupportsNullableReferenceTypesPostCondition()
70+
{
71+
// Provoke a possibly null value that is not detectable through
72+
// static analysis
73+
string? value =
74+
int.Parse("42", System.Globalization.CultureInfo.InvariantCulture) == 42
75+
? "Not null"
76+
: null;
77+
78+
Skip.IfNot(value is object);
79+
80+
// Does not trigger a nullable reference type warning
81+
_ = value.Substring(0);
82+
}
5183
}
5284
}

0 commit comments

Comments
 (0)