Skip to content

Commit

Permalink
Migrated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kralizek committed Sep 5, 2020
1 parent 2b0520b commit 4d3b1b4
Show file tree
Hide file tree
Showing 7 changed files with 958 additions and 0 deletions.
153 changes: 153 additions & 0 deletions Src/IdiomsUnitTest/EqualityComparerAssertionTest.cs
@@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using AutoFixture.Idioms;
using TestTypeFoundation;
using Xunit;

namespace AutoFixture.IdiomsUnitTest
{
public class EqualityComparerAssertionTest
{
[Fact]
public void SutIsIdiomaticAssertion()
{
// Arrange
var dummyComposer = new Fixture();
// Act
var sut = new EqualityComparerAssertion(dummyComposer);
// Assert
Assert.IsAssignableFrom<IIdiomaticAssertion>(sut);
}

[Fact]
public void ConstructWithNullComposerThrows()
{
// Arrange
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
new EqualityComparerAssertion(null));
}

[Fact]
public void VerifyNullMethodThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
sut.Verify((MethodInfo)null));
}

[Fact]
public void VerifyNonEqualityComparerDoesNothing()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Null(Record.Exception(() =>
sut.Verify(typeof(NonEqualityComparer))));
}

[Fact]
public void VerifyWellBehavedEqualityComparerDoesNotThrow()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Null(Record.Exception(() =>
sut.Verify(typeof(WellBehavedEqualityComparer))));
}

[Fact]
public void VerifyIllBehavedNullEqualityComparerThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Throws<EqualityComparerImplementationException>(() =>
sut.Verify(typeof(IllBehavedNullEqualityComparer)));
}

[Fact]
public void VerifyIllBehavedSelfEqualityComparerThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Throws<EqualityComparerImplementationException>(() =>
sut.Verify(typeof(IllBehavedSelfEqualityComparer)));
}
#pragma warning disable 659
private class WellBehavedEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (ReferenceEquals(x, y))
return true;

if (ReferenceEquals(x, null))
return false;

if (ReferenceEquals(y, null))
return false;

if (x.GetType() != y.GetType())
return false;

return x.Property == y.Property;
}

public int GetHashCode(PropertyHolder<int> obj)
{
return obj.Property;
}
}

private class IllBehavedNullEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (x == null && y == null)
return false;

if (x == null ^ y == null)
return true;

return false;
}

public int GetHashCode(PropertyHolder<int> obj)
{
throw new Exception();
}
}

private class IllBehavedSelfEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (ReferenceEquals(x, y))
return false;

return true;
}

public int GetHashCode(PropertyHolder<int> obj)
{
throw new Exception();
}
}

#pragma warning restore 659

private class NonEqualityComparer
{
}
}
}
136 changes: 136 additions & 0 deletions Src/IdiomsUnitTest/EqualityComparerEqualsNullAssertionTest.cs
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using AutoFixture.Idioms;
using AutoFixture.Kernel;
using TestTypeFoundation;
using Xunit;

namespace AutoFixture.IdiomsUnitTest
{
public class EqualityComparerEqualsNullAssertionTest
{
[Fact]
public void SutIsIdiomaticAssertion()
{
// Arrange
var dummyComposer = new Fixture();
// Act
var sut = new EqualityComparerEqualsNullAssertion(dummyComposer);
// Assert
Assert.IsAssignableFrom<IdiomaticAssertion>(sut);
}

[Fact]
public void ComposerIsCorrect()
{
// Arrange
var expectedComposer = new Fixture();
var sut = new EqualityComparerEqualsNullAssertion(expectedComposer);
// Act
ISpecimenBuilder result = sut.Builder;
// Assert
Assert.Equal(expectedComposer, result);
}

[Fact]
public void ConstructWithNullComposerThrows()
{
// Arrange
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
new EqualityComparerEqualsNullAssertion(null));
}

[Fact]
public void VerifyNullMethodThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerEqualsNullAssertion(dummyComposer);
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
sut.Verify((MethodInfo)null));
}

[Fact]
public void VerifyNonEqualityComparerDoesNothing()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerEqualsNullAssertion(dummyComposer);
// Act & Assert
Assert.Null(Record.Exception(() =>
sut.Verify(typeof(NonEqualityComparer))));
}

[Fact]
public void VerifyWellBehavedEqualityComparerDoesNotThrow()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerEqualsNullAssertion(dummyComposer);
// Act & Assert
Assert.Null(Record.Exception(() =>
sut.Verify(typeof(WellBehavedEqualityComparer))));
}

[Fact]
public void VerifyIllBehavedEqualityComparerThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerEqualsNullAssertion(dummyComposer);
// Act & Assert
Assert.Throws<EqualityComparerImplementationException>(() =>
sut.Verify(typeof(IllBehavedEqualityComparer)));
}

#pragma warning disable 659
private class WellBehavedEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (ReferenceEquals(x, y))
return true;

if (ReferenceEquals(x, null))
return false;

if (ReferenceEquals(y, null))
return false;

if (x.GetType() != y.GetType())
return false;

return x.Property == y.Property;
}

public int GetHashCode(PropertyHolder<int> obj)
{
return obj.Property;
}
}

private class IllBehavedEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (x == null ^ y == null)
return true;

return false;
}

public int GetHashCode(PropertyHolder<int> obj)
{
throw new Exception();
}
}
#pragma warning restore 659

private class NonEqualityComparer
{
}
}
}

0 comments on commit 4d3b1b4

Please sign in to comment.