Skip to content

Instantly share code, notes, and snippets.

@MichalBrylka
Created November 30, 2020 13:30
Show Gist options
  • Save MichalBrylka/da9028fc4f2a472a1f9de263b0f63dbb to your computer and use it in GitHub Desktop.
Save MichalBrylka/da9028fc4f2a472a1f9de263b0f63dbb to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using NUnit.Framework;
namespace NUnit_CountBug
{
[TestFixture]
public class CountBug
{
[Test]
public void CountConstraint_ShouldWorkForInterface()
{
Assert.That(new[] { 1, 2, 3 }, Has.Length.EqualTo(3), "Length"); //ok
/*this obviously will not work. It's a shame though that you do not provide non-breaking
"Cardinality" constraint that encompasses both Length and Count*/
//Assert.That(new[]{1,2,3}, Has.Count.EqualTo(3), "Count");
//this works
var list = new List<int> {1, 2, 3};
Assert.That(list, Has.Count.EqualTo(3), "List");
//this too...
var readOnlyCollection = new List<int> { 1, 2, 3 }.AsReadOnly();
Assert.That(readOnlyCollection, Has.Count.EqualTo(3), "ReadOnlyCollection");
IReadOnlyList<int> arrayCast = new []{ 1, 2, 3 };
//so far so good
Assert.That(arrayCast.Count, Is.EqualTo(3), "ArrayCast without constraint");
//crashes here even though Count exists
Assert.That(arrayCast, Has.Count.EqualTo(3), "ArrayCast");
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment