From 8fca65b51803f321da560e491b160e2689a797f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Thu, 9 Jul 2020 12:36:59 +0200 Subject: [PATCH 1/4] Add post-condition annotations --- src/Xunit.SkippableFact/Skip.cs | 18 ++++++++++++++++-- .../Xunit.SkippableFact.csproj | 2 +- .../Xunit.SkippableFact.Tests.csproj | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Xunit.SkippableFact/Skip.cs b/src/Xunit.SkippableFact/Skip.cs index 345e5e4..d74ab1a 100644 --- a/src/Xunit.SkippableFact/Skip.cs +++ b/src/Xunit.SkippableFact/Skip.cs @@ -3,6 +3,10 @@ namespace Xunit { +#if NETSTANDARD2_1 + using System.Diagnostics.CodeAnalysis; +#endif + /// /// Static methods for dynamically skipping tests identified with /// the . @@ -14,7 +18,12 @@ public static class Skip /// /// The condition that must evaluate to true for the test to be skipped. /// The explanation for why the test is skipped. - public static void If(bool condition, string? reason = null) + public static void If( +#if NETSTANDARD2_1 + [DoesNotReturnIf(true)] +#endif + bool condition, + string? reason = null) { if (condition) { @@ -27,7 +36,12 @@ public static void If(bool condition, string? reason = null) /// /// The condition that must evaluate to false for the test to be skipped. /// The explanation for why the test is skipped. - public static void IfNot(bool condition, string? reason = null) + public static void IfNot( +#if NETSTANDARD2_1 + [DoesNotReturnIf(false)] +#endif + bool condition, + string? reason = null) { Skip.If(!condition, reason); } diff --git a/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj b/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj index 0c0e4bb..4ff32fd 100644 --- a/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj +++ b/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj @@ -1,6 +1,6 @@  - netstandard2.0;net45;net452 + netstandard2.0;netstandard2.1;net45;net452 Xunit Dynamic test skipping for Xunit diff --git a/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj b/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj index 4193404..f7c1072 100644 --- a/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj +++ b/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj @@ -1,6 +1,6 @@  - net45;net461;netcoreapp2.1 + net45;net461;netcoreapp2.1;netcoreapp3.1 false From c5c5471a273889726208d5ae3c2add7449bf3dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Thu, 9 Jul 2020 12:50:22 +0200 Subject: [PATCH 2/4] Add NRT Post-Condition tests --- test/Xunit.SkippableFact.Tests/SkipTests.cs | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/Xunit.SkippableFact.Tests/SkipTests.cs b/test/Xunit.SkippableFact.Tests/SkipTests.cs index 7378263..c8b530d 100644 --- a/test/Xunit.SkippableFact.Tests/SkipTests.cs +++ b/test/Xunit.SkippableFact.Tests/SkipTests.cs @@ -48,5 +48,39 @@ public void IfNot_WithReason() Assert.Equal(reason, ex.Message); } } + +#if NETCOREAPP3_1 + [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 null)); + + // Does not trigger a nullable reference type warning + _ = value.Substring(0); + } +#endif } } From b4f4ce22fb7f7abae5f730b515b5f5cf498c6778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Thu, 9 Jul 2020 15:11:06 +0200 Subject: [PATCH 3/4] Use Nullable package for backwards compatability of Nullable Reference Type annotations Adopted from https://github.com/thnetii/repository-default/blob/master/DotNet-Solution-Directory/Directory.Build.targets --- Directory.Build.targets | 27 +++++++++++++++++++++++++++ src/Xunit.SkippableFact/Skip.cs | 6 ------ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 Directory.Build.targets diff --git a/Directory.Build.targets b/Directory.Build.targets new file mode 100644 index 0000000..9a8d6c7 --- /dev/null +++ b/Directory.Build.targets @@ -0,0 +1,27 @@ + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + \ No newline at end of file diff --git a/src/Xunit.SkippableFact/Skip.cs b/src/Xunit.SkippableFact/Skip.cs index d74ab1a..6a32793 100644 --- a/src/Xunit.SkippableFact/Skip.cs +++ b/src/Xunit.SkippableFact/Skip.cs @@ -3,9 +3,7 @@ namespace Xunit { -#if NETSTANDARD2_1 using System.Diagnostics.CodeAnalysis; -#endif /// /// Static methods for dynamically skipping tests identified with @@ -19,9 +17,7 @@ public static class Skip /// The condition that must evaluate to true for the test to be skipped. /// The explanation for why the test is skipped. public static void If( -#if NETSTANDARD2_1 [DoesNotReturnIf(true)] -#endif bool condition, string? reason = null) { @@ -37,9 +33,7 @@ public static void If( /// The condition that must evaluate to false for the test to be skipped. /// The explanation for why the test is skipped. public static void IfNot( -#if NETSTANDARD2_1 [DoesNotReturnIf(false)] -#endif bool condition, string? reason = null) { From 0bd79690a9e796303d9072595cfa77d638fd8ab6 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Thu, 9 Jul 2020 07:26:21 -0600 Subject: [PATCH 4/4] Simplify build authoring required for nullable annotations --- Directory.Build.props | 1 + Directory.Build.targets | 27 ------------------- src/Xunit.SkippableFact/Skip.cs | 6 ++--- .../Xunit.SkippableFact.csproj | 4 +-- test/Xunit.SkippableFact.Tests/SkipTests.cs | 4 +-- .../Xunit.SkippableFact.Tests.csproj | 2 +- 6 files changed, 7 insertions(+), 37 deletions(-) delete mode 100644 Directory.Build.targets diff --git a/Directory.Build.props b/Directory.Build.props index 87fa852..bb79b93 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -29,6 +29,7 @@ + diff --git a/Directory.Build.targets b/Directory.Build.targets deleted file mode 100644 index 9a8d6c7..0000000 --- a/Directory.Build.targets +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - all - runtime; build; native; contentfiles; analyzers - - - \ No newline at end of file diff --git a/src/Xunit.SkippableFact/Skip.cs b/src/Xunit.SkippableFact/Skip.cs index 6a32793..8c5ec7a 100644 --- a/src/Xunit.SkippableFact/Skip.cs +++ b/src/Xunit.SkippableFact/Skip.cs @@ -17,8 +17,7 @@ public static class Skip /// The condition that must evaluate to true for the test to be skipped. /// The explanation for why the test is skipped. public static void If( - [DoesNotReturnIf(true)] - bool condition, + [DoesNotReturnIf(true)] bool condition, string? reason = null) { if (condition) @@ -33,8 +32,7 @@ public static void If( /// The condition that must evaluate to false for the test to be skipped. /// The explanation for why the test is skipped. public static void IfNot( - [DoesNotReturnIf(false)] - bool condition, + [DoesNotReturnIf(false)] bool condition, string? reason = null) { Skip.If(!condition, reason); diff --git a/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj b/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj index 4ff32fd..b16fd6b 100644 --- a/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj +++ b/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj @@ -1,6 +1,6 @@  - netstandard2.0;netstandard2.1;net45;net452 + netstandard2.0;net45;net452 Xunit Dynamic test skipping for Xunit @@ -11,7 +11,7 @@ PreserveNewest - + diff --git a/test/Xunit.SkippableFact.Tests/SkipTests.cs b/test/Xunit.SkippableFact.Tests/SkipTests.cs index c8b530d..0ea1ef6 100644 --- a/test/Xunit.SkippableFact.Tests/SkipTests.cs +++ b/test/Xunit.SkippableFact.Tests/SkipTests.cs @@ -49,7 +49,6 @@ public void IfNot_WithReason() } } -#if NETCOREAPP3_1 [Fact] public void If_SupportsNullableReferenceTypesPostCondition() { @@ -76,11 +75,10 @@ public void IfNot_SupportsNullableReferenceTypesPostCondition() ? "Not null" : null; - Skip.IfNot(!(value is null)); + Skip.IfNot(value is object); // Does not trigger a nullable reference type warning _ = value.Substring(0); } -#endif } } diff --git a/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj b/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj index f7c1072..4193404 100644 --- a/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj +++ b/test/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj @@ -1,6 +1,6 @@  - net45;net461;netcoreapp2.1;netcoreapp3.1 + net45;net461;netcoreapp2.1 false