Skip to content

Commit

Permalink
Merge pull request #19 from CaptnCodr/feature/assert-that-is
Browse files Browse the repository at this point in the history
Assert.That and Is()
  • Loading branch information
CaptnCodr authored Jun 8, 2023
2 parents fcf5f36 + 7d3df84 commit 7882e6e
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
os: [windows-latest, macOS-latest, ubuntu-latest]
dotnet: [7.0.100]
dotnet: [7.0.302]
runs-on: ${{ matrix.os }}

steps:
Expand Down
4 changes: 2 additions & 2 deletions src/NHamcrest.NUnit.Examples/NHamcrest.NUnit.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NHamcrest.NUnit\NHamcrest.NUnit.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions src/NHamcrest.NUnit/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void That<T>(T actual, IMatcher<T> matcher, string message, params
{
if (matcher.Matches(actual))
return;

var writer = new TextMessageWriter(message, args);

WriteExpected(matcher, writer);
Expand All @@ -67,7 +67,7 @@ private static void WriteExpected(ISelfDescribing matcher, TextWriter writer)

private static void WriteActual<T>(T actual, IMatcher<T> matcher, TextWriter writer)
{
writer.Write(" But ");
writer.Write(TextMessageWriter.Pfx_Actual);
var mismatchDescription = new StringDescription();
matcher.DescribeMismatch(actual, mismatchDescription);
writer.Write(mismatchDescription.ToString());
Expand Down
14 changes: 6 additions & 8 deletions src/NHamcrest.NUnit/NHamcrest.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
<TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Copyright>Copyright © 2022</Copyright>
<Version Condition="'$(VersionSuffix)' == ''">3.1.0</Version>
<Version Condition="'$(VersionSuffix)' != ''">3.1.0-$(VersionSuffix)</Version>
<Copyright>Copyright © 2023</Copyright>
<Version Condition="'$(VersionSuffix)' == ''">3.2.0</Version>
<Version Condition="'$(VersionSuffix)' != ''">3.2.0-$(VersionSuffix)</Version>
<Description>Adapter for NUnit for using NHamcrest library</Description>
<Authors>Graham Hay, Algirdas Lašas</Authors>
<Company />
<Product />
<Authors>Graham Hay, Algirdas Lašas, Constantin Tews</Authors>
<PackageLicenseUrl>https://github.com/nhamcrest/NHamcrest/blob/master/LICENCE.txt</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/nhamcrest/NHamcrest</PackageProjectUrl>
<PackageTags>unittesting hamcrest matcher test</PackageTags>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
<AssemblyVersion>3.2.0.0</AssemblyVersion>
<FileVersion>3.2.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
Expand Down
3 changes: 1 addition & 2 deletions src/NHamcrest.Tests/Core/IsEqualTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using NHamcrest.Core;
using Xunit;
using NHAssert = NHamcrest.Tests.Internal.Assert;
Expand Down Expand Up @@ -43,7 +42,7 @@ public void Append_description()

isEqual.DescribeTo(description);

Assert.Equal("\"" + test + "\"", description.ToString());
Assert.Equal($"\"{test}\"", description.ToString());
}
}
}
33 changes: 33 additions & 0 deletions src/NHamcrest.Tests/Core/IsExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Xunit;
using NHAssert = NHamcrest.Tests.Internal.Assert;
using static NHamcrest.Extensions.IsExtensions;

namespace NHamcrest.Tests.Core
{
public class IsExtensionsTests
{
[Fact]
public void Match_if_bool_is_equal_with_IsExtension()
{
NHAssert.That(true, Is(true));
}

[Fact]
public void Match_if_string_is_equal_with_IsExtension()
{
NHAssert.That("test", Is("test"));
}

[Fact]
public void Match_if_int_is_equal_with_IsExtension()
{
NHAssert.That(1, Is(1));
}

[Fact]
public void Match_if_decimal_is_equal_with_IsExtension()
{
NHAssert.That(1m, Is(1m));
}
}
}
8 changes: 4 additions & 4 deletions src/NHamcrest.Tests/NHamcrest.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>3.1.0</Version>
<Version>3.2.0</Version>
<Description />
</PropertyGroup>
<ItemGroup>
Expand All @@ -14,9 +14,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Castle.Core" Version="5.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="Microsoft.NET.TEST.Sdk" Version="17.4.0" />
<PackageReference Include="Castle.Core" Version="5.1.1" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Microsoft.NET.TEST.Sdk" Version="17.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NHamcrest.XUnit\NHamcrest.XUnit.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>
Expand Down
34 changes: 32 additions & 2 deletions src/NHamcrest.XUnit/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ public class Assert : Xunit.Assert
/// <param name="matcher"></param>
/// <exception cref="MatchException"></exception>
public static void That<T>(T actual, IMatcher<T> matcher)
{
That(actual, matcher, null);
}

/// <summary>
/// Checks if actual matches in IMatcher.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actual"></param>
/// <param name="matcher"></param>
/// <param name="message"></param>
public static void That<T>(T actual, IMatcher<T> matcher, string message)
{
That(actual, matcher, message, null);
}

/// <summary>
/// Checks if actual matches in IMatcher.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actual"></param>
/// <param name="matcher"></param>
/// <param name="message"></param>
/// <param name="args"></param>
/// <exception cref="MatchException"></exception>
public static void That<T>(T actual, IMatcher<T> matcher, string message, params object[] args)
{
if (matcher.Matches(actual))
return;
Expand All @@ -25,7 +51,11 @@ public static void That<T>(T actual, IMatcher<T> matcher)
var mismatchDescription = new StringDescription();
matcher.DescribeMismatch(actual, mismatchDescription);

throw new MatchException(description.ToString(), mismatchDescription.ToString(), null);
string userMessage = args != null && args.Length > 0
? string.Format(message, args)
: message;

throw new MatchException(description.ToString(), mismatchDescription.ToString(), userMessage);
}
}
}
}
12 changes: 6 additions & 6 deletions src/NHamcrest.XUnit/NHamcrest.XUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Copyright>Copyright © 2022</Copyright>
<Version Condition="'$(VersionSuffix)' == ''">3.1.0</Version>
<Version Condition="'$(VersionSuffix)' != ''">3.1.0-$(VersionSuffix)</Version>
<Copyright>Copyright © 2023</Copyright>
<Version Condition="'$(VersionSuffix)' == ''">3.2.0</Version>
<Version Condition="'$(VersionSuffix)' != ''">3.2.0-$(VersionSuffix)</Version>
<Description>Adapter for xunit for using NHamcrest library</Description>
<Authors>Graham Hay, Algirdas Lašas</Authors>
<Authors>Graham Hay, Algirdas Lašas, Constantin Tews</Authors>
<PackageLicenseUrl>https://github.com/nhamcrest/NHamcrest/blob/master/LICENCE.txt</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/nhamcrest/NHamcrest</PackageProjectUrl>
<PackageTags>unittesting hamcrest matcher test</PackageTags>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
<AssemblyVersion>3.2.0.0</AssemblyVersion>
<FileVersion>3.2.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit.assert" Version="2.4.2" />
Expand Down
1 change: 1 addition & 0 deletions src/NHamcrest/Core/StructuralComparisonMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHamcrest.Extensions;

namespace NHamcrest.Core
{
Expand Down
10 changes: 10 additions & 0 deletions src/NHamcrest/Extensions/IsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NHamcrest.Extensions
{
public static class IsExtensions
{
public static IMatcher<T> Is<T>(T value)
{
return NHamcrest.Is.EqualTo(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
using System.Linq;
using System.Reflection;

namespace NHamcrest.Core
namespace NHamcrest.Extensions
{
internal static class TypeExtensions
{
public static bool IsSimpleType(
this Type type)
public static bool IsSimpleType(this Type type)
{
return
type.GetTypeInfo().IsValueType ||
type.GetTypeInfo().IsPrimitive ||
new[] {
typeof(String),
typeof(Decimal),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Guid)
typeof(string),
typeof(decimal),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Guid)
}.Contains(type) ||
Convert.GetTypeCode(type) != TypeCode.Object;
}
}
}
}
14 changes: 6 additions & 8 deletions src/NHamcrest/NHamcrest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
<TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Copyright>Copyright © 2022</Copyright>
<Version Condition="'$(VersionSuffix)' == ''">3.1.0</Version>
<Version Condition="'$(VersionSuffix)' != ''">3.1.0-$(VersionSuffix)</Version>
<Copyright>Copyright © 2023</Copyright>
<Version Condition="'$(VersionSuffix)' == ''">3.2.0</Version>
<Version Condition="'$(VersionSuffix)' != ''">3.2.0-$(VersionSuffix)</Version>
<Description>.NET port of Hamcrest, a matcher library with some extra matchers</Description>
<Authors>Graham Hay, Algirdas Lašas</Authors>
<Company />
<Product />
<Authors>Graham Hay, Algirdas Lašas, Constantin Tews</Authors>
<PackageLicenseUrl>https://github.com/nhamcrest/NHamcrest/blob/master/LICENCE.txt</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/nhamcrest/NHamcrest</PackageProjectUrl>
<PackageTags>unittesting hamcrest matcher test</PackageTags>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
<AssemblyVersion>3.2.0.0</AssemblyVersion>
<FileVersion>3.2.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1705;1591</NoWarn>
Expand Down
3 changes: 0 additions & 3 deletions src/NHamcrest/app.config

This file was deleted.

0 comments on commit 7882e6e

Please sign in to comment.