Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/GalaxyCheck.Xunit.Tests/GalaxyCheck.Xunit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<IsPackable>false</IsPackable>
<RootNamespace>Tests</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="IntegrationSample\**" />
<EmbeddedResource Remove="IntegrationSample\**" />
<None Remove="IntegrationSample\**" />
<Compile Remove="samples\**" />
<EmbeddedResource Remove="samples\**" />
<None Remove="samples\**" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using FluentAssertions;
using GalaxyCheck;
using GalaxyCheck.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Tests.GenSnapshotInitializerTests
{
public class AboutAssertSnapshotMatches
{
#pragma warning disable xUnit1000 // Test classes must be public
private class GenSnapshots
#pragma warning restore xUnit1000 // Test classes must be public
{
[GenSnapshot]
public object GenSnapshot() => new { };
}

[Fact]
public void ItPassesThroughAssertSnapshotMatches()
{
Func<ISnapshot, Task> assertSnapshotMatches = (_) => Task.CompletedTask;

var result = TestProxy.Initialize(
typeof(GenSnapshots),
nameof(GenSnapshots.GenSnapshot),
configure: config => config.AssertSnapshotMatches = assertSnapshotMatches);

result.AssertSnapshotMatches.Should().Be(assertSnapshotMatches);
}

[Fact]
public void ItThrowsIfAssertSnapshotMatchesIsNull()
{
var action = () => TestProxy.Initialize(
typeof(GenSnapshots),
nameof(GenSnapshots.GenSnapshot),
configure: config => config.AssertSnapshotMatches = null);

action.Should().Throw<Exception>().WithMessage("Configuration value \"AssertSnapshotMatches\" needs to be set in order to use gen snapshots");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
using GalaxyCheck;
using GalaxyCheck.Gens;
using GalaxyCheck.Internal;
using Moq;
using System;
using System.Collections.Generic;
using System.Reflection;
using Xunit;

namespace Tests.GenSnapshotInitializerTests
{
public class AboutFactory
{
[Theory]
[InlineData(nameof(GenSnapshotsWithoutFactoryConfig.GenSnapshotWithoutFactoryConfig))]
public void WhenClassNorMethodHasFactoryConfig_ItPassesThroughNull(string testMethodName)
{
var mockParametersGenFactory = MockParametersGenFactory();

TestProxy.Initialize(
typeof(GenSnapshotsWithoutFactoryConfig),
testMethodName,
parametersGenFactory: mockParametersGenFactory.Object);

VerifyFactoryNotPassedThrough(mockParametersGenFactory);
}

[Theory]
[InlineData(nameof(GenSnapshotsWithoutFactoryConfig.GenSnapshotWithFactoryConfig))]
public void ItPassesThroughTheFactoryFromTheMethod(string testMethodName)
{
var mockParametersGenFactory = MockParametersGenFactory();

TestProxy.Initialize(
typeof(GenSnapshotsWithoutFactoryConfig),
testMethodName,
parametersGenFactory: mockParametersGenFactory.Object);

VerifyFactoryPassedThrough(mockParametersGenFactory, typeof(GenFactory1));
}

[Theory]
[InlineData(nameof(GenSnapshotsWithFactoryConfig.GenSnapshotWithoutFactoryConfig))]
public void ItPassesThroughTheFactoryFromTheClass(string testMethodName)
{
var mockParametersGenFactory = MockParametersGenFactory();

TestProxy.Initialize(
typeof(GenSnapshotsWithFactoryConfig),
testMethodName,
parametersGenFactory: mockParametersGenFactory.Object);

VerifyFactoryPassedThrough(mockParametersGenFactory, typeof(GenFactory2));
}

[Theory]
[InlineData(nameof(GenSnapshotsWithFactoryConfig.GenSnapshotWithFactoryConfig))]
public void AMethodLevelFactoryIsPreferredOverAClassLevelFactory(string testMethodName)
{
var mockParametersGenFactory = MockParametersGenFactory();

TestProxy.Initialize(
typeof(GenSnapshotsWithFactoryConfig),
testMethodName,
parametersGenFactory: mockParametersGenFactory.Object);

VerifyFactoryPassedThrough(mockParametersGenFactory, typeof(GenFactory1));
}

private static Mock<IParametersGenFactory> MockParametersGenFactory()
{
var mock = new Mock<IParametersGenFactory>();

return mock;
}

private static void VerifyFactoryPassedThrough(Mock<IParametersGenFactory> mockParametersGenFactory, Type expectedFactoryType)
{
mockParametersGenFactory.Verify(
x => x.CreateParametersGen(
It.IsAny<MethodInfo>(),
It.Is<IGenFactory?>(factory => factory != null && factory.GetType() == expectedFactoryType),
It.IsAny<IReadOnlyDictionary<int, IGen>>()),
Times.Once);
}

private static void VerifyFactoryNotPassedThrough(Mock<IParametersGenFactory> mockParametersGenFactory)
{
mockParametersGenFactory.Verify(
x => x.CreateParametersGen(
It.IsAny<MethodInfo>(),
It.Is<IGenFactory?>(x => x == null),
It.IsAny<IReadOnlyDictionary<int, IGen>>()),
Times.Once);
}

private class GenFactory1 : IGenFactory
{
public IReflectedGen<T> Create<T>(NullabilityInfo? nullabilityInfo = null) where T : notnull
{
throw new NotImplementedException();
}

public IGenFactory RegisterType(Type type, IGen gen)
{
throw new NotImplementedException();
}

public IGenFactory RegisterType<T>(IGen<T> gen)
{
throw new NotImplementedException();
}

public IGenFactory RegisterType<T>(Func<IGenFactory, IGen<T>> genFunc)
{
throw new NotImplementedException();
}

public IGenFactory RegisterType(Type type, Func<IGenFactory, IGen> genFunc)
{
throw new NotImplementedException();
}
}

private class GenFactory1Attribute : GenFactoryAttribute
{
public override IGenFactory Value => new GenFactory1();
}

private class GenFactory2 : IGenFactory
{
public IReflectedGen<T> Create<T>(NullabilityInfo? nullabilityInfo = null) where T : notnull
{
throw new NotImplementedException();
}

public IGenFactory RegisterType(Type type, IGen gen)
{
throw new NotImplementedException();
}

public IGenFactory RegisterType<T>(IGen<T> gen)
{
throw new NotImplementedException();
}

public IGenFactory RegisterType<T>(Func<IGenFactory, IGen<T>> genFunc)
{
throw new NotImplementedException();
}

public IGenFactory RegisterType(Type type, Func<IGenFactory, IGen> genFunc)
{
throw new NotImplementedException();
}
}

private class GenFactory2Attribute : GenFactoryAttribute
{
public override IGenFactory Value => new GenFactory2();
}

#pragma warning disable xUnit1000 // Test classes must be public
private class GenSnapshotsWithoutFactoryConfig
#pragma warning restore xUnit1000 // Test classes must be public
{
[GenSnapshot]
[GenFactory1]
public object GenSnapshotWithFactoryConfig() => new { };

[GenSnapshot]
public object GenSnapshotWithoutFactoryConfig() => new { };
}

[GenFactory2]
#pragma warning disable xUnit1000 // Test classes must be public
private class GenSnapshotsWithFactoryConfig
#pragma warning restore xUnit1000 // Test classes must be public
{
[GenSnapshot]
[GenFactory1]
public object GenSnapshotWithFactoryConfig() => new { };

[GenSnapshot]
public object GenSnapshotWithoutFactoryConfig() => new { };
}


private static MethodInfo GetMethod(Type type, string name)
{
var methodInfo = type.GetMethod(name, BindingFlags.Public | BindingFlags.Instance);

if (methodInfo == null)
{
throw new Exception("Unable to locate method");
}

return methodInfo;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using FluentAssertions;
using GalaxyCheck;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Tests.GenSnapshotInitializerTests
{
public class AboutInvalidReturnTypes
{
#pragma warning disable xUnit1000 // Test classes must be public
private class GenSnapshots
#pragma warning restore xUnit1000 // Test classes must be public
{
[GenSnapshot]
public void GenSnapshotVoid()
{
}

[GenSnapshot]
public Task GenSnapshotTaskVoid()
{
return Task.CompletedTask;
}

[GenSnapshot]
public ValueTask GenSnapshotValueTaskVoid()
{
return ValueTask.CompletedTask;
}
}

[Theory]
[InlineData(nameof(GenSnapshots.GenSnapshotVoid))]
[InlineData(nameof(GenSnapshots.GenSnapshotTaskVoid))]
[InlineData(nameof(GenSnapshots.GenSnapshotValueTaskVoid))]
public void ItPassesThroughSeedsForTheIterations(string testMethodName)
{
var action = () => TestProxy.Initialize(typeof(GenSnapshots), testMethodName);

action.Should().Throw<Exception>().WithMessage("Test method must return a value for snapshotting, or a task containing a value");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentAssertions;
using GalaxyCheck;
using Xunit;

namespace Tests.GenSnapshotInitializerTests
{
public class AboutIterations
{
#pragma warning disable xUnit1000 // Test classes must be public
private class GenSnapshots
#pragma warning restore xUnit1000 // Test classes must be public
{
[GenSnapshot]
public object GenSnapshotWithDefaultIterations() => new { };

[GenSnapshot(Iterations = 5)]
public object GenSnapshotWith5Iterations() => new { };

[GenSnapshot(Iterations = 10)]
public object GenSnapshotWith10Iterations() => new { };
}

[Theory]
[InlineData(nameof(GenSnapshots.GenSnapshotWith5Iterations), new int[] { 0, 1, 2, 3, 4 })]
[InlineData(nameof(GenSnapshots.GenSnapshotWith10Iterations), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })]
public void ItPassesThroughSeedsForTheIterations(string testMethodName, int[] expectedSeeds)
{
var result = TestProxy.Initialize(typeof(GenSnapshots), testMethodName, configure: config => config.DefaultIterations = 999);

result.Seeds.Should().BeEquivalentTo(expectedSeeds);
}

[Theory]
[InlineData(nameof(GenSnapshots.GenSnapshotWithDefaultIterations))]
public void ItPassesThroughSeedsFromTheDefaultIterationsIfNotExplicitlySet(string testMethodName)
{
var result = TestProxy.Initialize(typeof(GenSnapshots), testMethodName, configure: config => config.DefaultIterations = 5);

result.Seeds.Should().BeEquivalentTo(new [] { 0, 1, 2, 3, 4 });
}
}
}
Loading