Skip to content

Commit

Permalink
Merge pull request #383 from dennisdoomen/Fix382
Browse files Browse the repository at this point in the history
Comparing two empty collections resulted in a weird failure.
  • Loading branch information
dennisdoomen committed Apr 1, 2016
2 parents 0db7a6a + baaace8 commit 5c66394
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Src/Core/Collections/CollectionAssertions.cs
Expand Up @@ -1039,7 +1039,7 @@ public AndConstraint<TAssertions> StartWith(object element, string because = "",
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to start with {0}{reason}, ", expected)
.Given(() => actualItems)
.AssertCollectionIsNotNullOrEmpty()
.AssertCollectionIsNotNullOrEmpty(expected.Length)
.Then
.AssertCollectionHasEnoughItems(expected.Length)
.Then
Expand Down Expand Up @@ -1072,7 +1072,7 @@ public AndConstraint<TAssertions> EndWith(object element, string because = "", p
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to end with {0}{reason}, ", expected)
.Given(() => actual)
.AssertCollectionIsNotNullOrEmpty()
.AssertCollectionIsNotNullOrEmpty(expected.Length)
.Then
.AssertCollectionHasEnoughItems(expected.Length)
.Then
Expand Down
60 changes: 32 additions & 28 deletions Src/Core/Execution/GivenSelectorExtensions.cs
@@ -1,35 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions.Common;

namespace FluentAssertions.Execution
{
internal static class GivenSelectorExtensions
{
public static ContinuationOfGiven<IEnumerable<T>> AssertCollectionIsNotNull<T>(this GivenSelector<IEnumerable<T>> givenSelector)
public static ContinuationOfGiven<IEnumerable<T>> AssertCollectionIsNotNullOrEmpty<T>(
this GivenSelector<IEnumerable<T>> givenSelector, int length)
{
return givenSelector
.AssertCollectionIsNotNull()
.Then
.AssertEitherCollectionIsNotEmpty(length);
}

public static ContinuationOfGiven<IEnumerable<T>> AssertCollectionIsNotNull<T>(
this GivenSelector<IEnumerable<T>> givenSelector)
{
return givenSelector
.ForCondition(items => !ReferenceEquals(items, null))
.FailWith("but found collection is <null>.");
}

public static ContinuationOfGiven<IEnumerable<T>> AssertCollectionIsNotEmpty<T>(this GivenSelector<IEnumerable<T>> givenSelector)
public static ContinuationOfGiven<IEnumerable<T>> AssertEitherCollectionIsNotEmpty<T>(
this GivenSelector<IEnumerable<T>> givenSelector, int length)
{
return givenSelector
.ForCondition(items => items.Any())
.FailWith("but found empty collection.");
.ForCondition(items => !EitherIsEmpty(length, items.Count()))
.FailWith("but found empty collection.");
}

public static ContinuationOfGiven<IEnumerable<T>> AssertCollectionIsNotNullOrEmpty<T>(this GivenSelector<IEnumerable<T>> givenSelector)
private static bool EitherIsEmpty(int length1, int length2)
{
return givenSelector
.AssertCollectionIsNotNull()
.Then
.AssertCollectionIsNotEmpty();
return ((length1 == 0) && (length2 > 0)) || ((length1 > 0) && (length2 == 0));
}

public static ContinuationOfGiven<T[]> AssertCollectionHasEnoughItems<T>(this GivenSelector<IEnumerable<T>> givenSelector, int length)
public static ContinuationOfGiven<T[]> AssertCollectionHasEnoughItems<T>(this GivenSelector<IEnumerable<T>> givenSelector,
int length)
{
return givenSelector
.Given(items => items.ToArray())
Expand All @@ -44,37 +52,33 @@ public static ContinuationOfGiven<T[]> AssertCollectionHasEnoughItems<T>(this Gi
.FailWith("but {0} contains {1} item(s) less.", items => items, items => length - items.Length);
}

public static ContinuationOfGiven<T[]> AssertCollectionHasNotTooManyItems<T>(this GivenSelector<IEnumerable<T>> givenSelector, int length)
{
return givenSelector
.Given(items => items.ToArray())
.AssertCollectionHasNotTooManyItems(length);
}

public static ContinuationOfGiven<T[]> AssertCollectionHasNotTooManyItems<T>(this GivenSelector<T[]> givenSelector, int length)
public static ContinuationOfGiven<T[]> AssertCollectionHasNotTooManyItems<T>(this GivenSelector<T[]> givenSelector,
int length)
{
return givenSelector
.Given(items => items.ToArray())
.ForCondition(items => items.Length <= length)
.FailWith("but {0} contains {1} item(s) too many.", items => items, items => items.Length - length);
}

public static ContinuationOfGiven<T[]> AssertCollectionsHaveSameCount<T>(this GivenSelector<IEnumerable<T>> givenSelector, int length)
public static ContinuationOfGiven<T[]> AssertCollectionsHaveSameCount<T>(this GivenSelector<IEnumerable<T>> givenSelector,
int length)
{
return givenSelector
.AssertCollectionIsNotEmpty()
.Then
.AssertCollectionHasEnoughItems(length)
.Then
.AssertCollectionHasNotTooManyItems(length);
.AssertEitherCollectionIsNotEmpty(length)
.Then
.AssertCollectionHasEnoughItems(length)
.Then
.AssertCollectionHasNotTooManyItems(length);
}

public static void AssertCollectionsHaveSameItems<TActual, TExpected>(this GivenSelector<TActual[]> givenSelector, TExpected[] expected, Func<TActual[], TExpected[], int> findIndex)
public static void AssertCollectionsHaveSameItems<TActual, TExpected>(this GivenSelector<TActual[]> givenSelector,
TExpected[] expected, Func<TActual[], TExpected[], int> findIndex)
{
givenSelector
.Given(actual => new { Items = actual, Index = findIndex(actual, expected) })
.Given(actual => new {Items = actual, Index = findIndex(actual, expected)})
.ForCondition(diff => diff.Index == -1)
.FailWith("but {0} differs at index {1}.", diff => diff.Items, diff => diff.Index);
}
}
}
}
22 changes: 21 additions & 1 deletion Tests/FluentAssertions.Shared.Specs/CollectionAssertionSpecs.cs
Expand Up @@ -918,7 +918,27 @@ public void When_any_item_does_not_match_according_to_a_predicate_it_should_thro
action
.ShouldThrow<AssertFailedException>()
.WithMessage("*Expected*equal to*, but*differs at index 3.*");
}
}

[TestMethod]
public void When_both_collections_are_empty_it_should_them_as_equal()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var actual = new List<string>();
var expected = new List<string>();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => actual.Should().Equal(expected);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.ShouldNotThrow();
}

#endregion

Expand Down
18 changes: 9 additions & 9 deletions Tests/TestFrameworks/XUnit2.Specs/XUnit2.Specs.csproj
Expand Up @@ -12,7 +12,8 @@
<AssemblyName>XUnit2.Specs</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>62131439</NuGetPackageImportStamp>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -41,13 +42,18 @@
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions">
<HintPath>..\..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert">
<HintPath>..\..\..\packages\xunit.assert.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll</HintPath>
<HintPath>..\..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core">
<HintPath>..\..\..\packages\xunit.extensibility.core.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll</HintPath>
<HintPath>..\..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop">
<HintPath>..\..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
Expand All @@ -72,12 +78,6 @@
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
9 changes: 5 additions & 4 deletions Tests/TestFrameworks/XUnit2.Specs/packages.config
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="2.0.0" targetFramework="net45" />
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.0.0" targetFramework="net45" />
<package id="xunit.core" version="2.0.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
<package id="xunit.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
</packages>

0 comments on commit 5c66394

Please sign in to comment.