Skip to content

Commit

Permalink
1.3.0: add .NET 7 support, fix false-positive for decimals in .NET 7,…
Browse files Browse the repository at this point in the history
… fast lane for numeric types, remove codeql
  • Loading branch information
kasthack committed Jan 7, 2023
1 parent 0000000 commit 0000000
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 83 deletions.
71 changes: 0 additions & 71 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[![GitHub release](https://img.shields.io/github/release/kasthack-labs/kasthack.NotEmpty.svg)](https://github.com/kasthack-labs/kasthack.NotEmpty/releases/latest)
[![license](https://img.shields.io/github/license/kasthack-labs/kasthack.NotEmpty.svg)](LICENSE)
[![.NET Status](https://github.com/kasthack-labs/kasthack.NotEmpty/workflows/.NET/badge.svg)](https://github.com/kasthack-labs/kasthack.NotEmpty/actions?query=workflow%3A.NET)
[![CodeQL](https://github.com/kasthack-labs/kasthack.NotEmpty/workflows/CodeQL/badge.svg)](https://github.com/kasthack-labs/kasthack.NotEmpty/actions?query=workflow%3ACodeQL)
[![Patreon pledges](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dkasthack%26type%3Dpledges&style=flat)](https://patreon.com/kasthack)
[![Patreon patrons](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dkasthack%26type%3Dpatrons&style=flat)](https://patreon.com/kasthack)

Expand Down
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<NoWarn>SA1633;SA1600;SA1601;CS1591</NoWarn>
<NoWarn>SA1633;SA1600;SA1601;CS1591;SA1300</NoWarn>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<StyleCopTreatErrorsAsWarnings>True</StyleCopTreatErrorsAsWarnings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PublishReferencesDocumentationFiles>true</PublishReferencesDocumentationFiles>
<PublishDocumentationFile>true</PublishDocumentationFile>
<PublishDocumentationFiles>true</PublishDocumentationFiles>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>1.2.0</PackageVersion>
<PackageVersion>1.3.0</PackageVersion>
<PackageDescription>.NotEmpty&lt;T&gt;() test extension</PackageDescription>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>kasthack nunit xunit mstest test empty null notempty emptinness nullability</PackageTags>
Expand Down
40 changes: 34 additions & 6 deletions src/kasthack.NotEmpty.Core/NotEmptyExtensionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@

public abstract class NotEmptyExtensionsBase
{
private static readonly ISet<Type> KnownNumericTypes = new HashSet<Type>()
{
typeof(byte),
typeof(sbyte),
typeof(short),
typeof(ushort),
typeof(char),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(decimal),
typeof(BigInteger),
typeof(Complex),
#if NET5_0_OR_GREATER
typeof(Half),
typeof(nint),
typeof(nuint),
#endif
};

[Obsolete($"Use {nameof(NotEmptyExtensionsBase.NotEmpty)}<T>(T, {nameof(AssertOptions)}) instead.")]
public void NotEmpty<T>(T? value) => this.NotEmpty(value, null);

Expand All @@ -31,16 +54,16 @@ internal void NotEmptyInternal<T>(T? value, AssertContext context)
var path = context.Path;
string message = GetEmptyMessage(path);
this.Assert(value is not null, message, path); // fast lane

var valueType = typeof(T);
var isKnownNumericType = KnownNumericTypes.Contains(valueType);

var skipDueToBeingNumberInArrayWhenAllowedByOptions = context.ElementKind == ElementKind.ArrayElement
&& context.Options.AllowZerosInNumberArrays
&& value is byte or sbyte or short or ushort or char or int or uint or long or ulong or float or double or decimal or BigInteger or Complex
#if NET5_0_OR_GREATER
or Half or nint or nuint
#endif
;
&& isKnownNumericType;
var skipDueToBeingBooleanPropertyWhenAllowedByOptions = context.Options.AllowFalseBooleanProperties && context.ElementKind == ElementKind.Property && value is bool;

var skipDueToBeingEnumPropertyWhenAllowedByOptions = context.Options.AllowDefinedDefaultEnumValues && typeof(T).IsEnum && Enum.IsDefined(typeof(T), value!);
var skipDueToBeingEnumPropertyWhenAllowedByOptions = context.Options.AllowDefinedDefaultEnumValues && valueType.IsEnum && Enum.IsDefined(valueType, value!);

if (!(
skipDueToBeingBooleanPropertyWhenAllowedByOptions
Expand All @@ -52,6 +75,11 @@ or Half or nint or nuint
this.Assert(!EqualityComparer<T>.Default.Equals(default!, value!), message, path);
}

if (isKnownNumericType)
{
return;
}

switch (value)
{
// ignore properties on builtin time structs as it's a reasonable thing to do
Expand Down
8 changes: 7 additions & 1 deletion src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public abstract class NotEmptyTestBase
#region Numbers

[Fact]
public void NotDefaultPrimitiveWorks() => this.Action(new { Value = 1 });
public void NotDefaultNullableDecimalWorks() => this.Action(new { Value = new Nullable<decimal>(1m) });

[Fact]
public void NotDefaultDecimalWorks() => this.Action(new { Value = 1m });

[Fact]
public void NotDefaultIntWorks() => this.Action(new { Value = 1 });

[Fact]
public void BoxedNotDefaultPrimitiveWorks() => this.Action(new { Value = (object)1 });
Expand Down
1 change: 0 additions & 1 deletion src/kasthack.NotEmpty.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "kasthack.NotEmpty.Raw", "ka
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CI", "CI", "{FA63E98E-9B05-480A-B16D-8D253629254A}"
ProjectSection(SolutionItems) = preProject
..\.github\workflows\codeql-analysis.yml = ..\.github\workflows\codeql-analysis.yml
..\.github\workflows\dotnet.yml = ..\.github\workflows\dotnet.yml
..\.github\workflows\push.yml = ..\.github\workflows\push.yml
EndProjectSection
Expand Down

0 comments on commit 0000000

Please sign in to comment.