Skip to content

Commit

Permalink
Merge pull request #20 from fredrikhr/extended-nullable
Browse files Browse the repository at this point in the history
Skip.If nullable-reference type post-condition annotations
  • Loading branch information
AArnott authored Jul 9, 2020
2 parents bea1452 + 0bd7969 commit 6702cf0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PackageReference Include="Nerdbank.GitVersioning" Version="3.1.74" PrivateAssets="all" />
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.5.0" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164" PrivateAssets="all" />
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" />
Expand Down
10 changes: 8 additions & 2 deletions src/Xunit.SkippableFact/Skip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Xunit
{
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Static methods for dynamically skipping tests identified with
/// the <see cref="SkippableFactAttribute"/>.
Expand All @@ -14,7 +16,9 @@ public static class Skip
/// </summary>
/// <param name="condition">The condition that must evaluate to <c>true</c> for the test to be skipped.</param>
/// <param name="reason">The explanation for why the test is skipped.</param>
public static void If(bool condition, string? reason = null)
public static void If(
[DoesNotReturnIf(true)] bool condition,
string? reason = null)
{
if (condition)
{
Expand All @@ -27,7 +31,9 @@ public static void If(bool condition, string? reason = null)
/// </summary>
/// <param name="condition">The condition that must evaluate to <c>false</c> for the test to be skipped.</param>
/// <param name="reason">The explanation for why the test is skipped.</param>
public static void IfNot(bool condition, string? reason = null)
public static void IfNot(
[DoesNotReturnIf(false)] bool condition,
string? reason = null)
{
Skip.If(!condition, reason);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Xunit.SkippableFact/Xunit.SkippableFact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Validation" Version="2.4.18" PrivateAssets="compile;contentfiles;analyzers;build" />
<PackageReference Include="xunit.extensibility.execution" Version="2.1.0" Condition=" '$(TargetFramework)' == 'net45' " />
Expand Down
32 changes: 32 additions & 0 deletions test/Xunit.SkippableFact.Tests/SkipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,37 @@ public void IfNot_WithReason()
Assert.Equal(reason, ex.Message);
}
}

[Fact]
public void If_SupportsNullableReferenceTypesPostCondition()
{
// Provoke a possibly null value that is not detectable through
// static analysis
string? value =
int.Parse("42", System.Globalization.CultureInfo.InvariantCulture) == 42
? "Not null"
: null;

Skip.If(value is null);

// Does not trigger a nullable reference type warning
_ = value.Substring(0);
}

[Fact]
public void IfNot_SupportsNullableReferenceTypesPostCondition()
{
// Provoke a possibly null value that is not detectable through
// static analysis
string? value =
int.Parse("42", System.Globalization.CultureInfo.InvariantCulture) == 42
? "Not null"
: null;

Skip.IfNot(value is object);

// Does not trigger a nullable reference type warning
_ = value.Substring(0);
}
}
}

0 comments on commit 6702cf0

Please sign in to comment.