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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using GalaxyCheck;
using System;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using Xunit;
using static Tests.V2.DomainGenAttributes;
Expand Down Expand Up @@ -58,36 +57,5 @@ public void IfThePreconditionPasses_WhenThePropertyIsSampled_AllTheValuesPassThe

result.Should().OnlyContain(x => pred(x));
}

private GalaxyCheck.Property NestedPrecondition(int x)
{
Func<int, bool> pred = x => x % 2 == 0;

GalaxyCheck.Property.Precondition(pred(x));

return GalaxyCheck.Gen.Int32().ForAll(_ => pred(x).Should().BeTrue());
}

[Fact]
public void ForNestedProperties_IfThePreconditionPasses_ThenTheEquivalentAssertPasses()
{
var property = GalaxyCheck.Property.Reflect(GetMethod(nameof(NestedPrecondition)), this);

var result = property.Check(seed: 0);

result.Counterexample.Should().BeNull();
}

private static MethodInfo GetMethod(string name)
{
var methodInfo = typeof(AboutPreconditions).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);

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

return methodInfo;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@

namespace Tests.V2.PropertyTests.ReflectedPropertyTests
{
/// <summary>
/// TODO:
/// - Use different gen types as parameters when supported as parameters. Otherwise we are vulnerable to parameter
/// ordering bugs.
/// </summary>
public class AboutMethodInfoInput
{
[Fact]
public void AVoidMethodInfoReceivesInputLikeForAll()
{
var gen0 = Gen.Int32();
var gen1 = Gen.Int32();
var gen1 = Gen.String();

var forAllProperty = Property.ForAll(gen0, gen1, (x, y) => { });
var forAllPropertyInput = forAllProperty.Select(x => new object[] { x.Input.Item1, x.Input.Item2 });

Action<int, int> f = (x, y) => { };
Action<int, string> f = (x, y) => { };
var methodInfoProperty = Property.Reflect(f.Method, null);
var methodInfoPropertyInput = methodInfoProperty.Select(x => x.Input);

Expand All @@ -33,48 +28,16 @@ public void AVoidMethodInfoReceivesInputLikeForAll()
public void ABooleanReturningMethodInfoReceivesInputLikeForAll()
{
var gen0 = Gen.Int32();
var gen1 = Gen.Int32();
var gen1 = Gen.String();

var forAllProperty = Property.ForAll(gen0, gen1, (x, y) => true);
var forAllPropertyInput = forAllProperty.Select(x => new object[] { x.Input.Item1, x.Input.Item2 });

Func<int, int, bool> f = (x, y) => true;
Func<int, string, bool> f = (x, y) => true;
var methodInfoProperty = Property.Reflect(f.Method, null);
var methodInfoPropertyInput = methodInfoProperty.Select(x => x.Input);

GenAssert.Equal(forAllPropertyInput, methodInfoPropertyInput, 0, 10);
}

private Property ARecursiveProperty(int x)
{
return Gen.Int32().ForAll(y => true);
}

[Fact]
public void APropertyReturningMethodInfoReceivesAConcatenationOfInput()
{
var gen0 = Gen.Int32();
var gen1 = Gen.Int32();

var forAllProperty = Property.ForAll(gen0, gen1, (x, y) => true);
var forAllPropertyInput = forAllProperty.Select(x => new object[] { x.Input.Item1, x.Input.Item2 });

var methodInfoProperty = Property.Reflect(GetMethod(nameof(ARecursiveProperty)), this);
var methodInfoPropertyInput = methodInfoProperty.Select(x => x.PresentedInput);

GenAssert.Equal(forAllPropertyInput, methodInfoPropertyInput, 0, 1);
}

private static MethodInfo GetMethod(string name)
{
var methodInfo = typeof(AboutMethodInfoInput).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);

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

return methodInfo;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public void ABooleanMethodInfoCanBeFalsified()
checkResult.Falsified.Should().BeTrue();
}

private Property AnInfallibleNestedPropertyFunction(int x) => Gen.Constant<object?>(null).ForAll(_ => true);
private Property AnInfallibleNestedPropertyFunctionWithVaryingTypes(int x) => Gen.Constant(true).ForAll(_ => true);
private Property AnInfalliblePropertyFunction() => Gen.Constant<object?>(null).ForAll(_ => true);
private Property AnInfalliblePropertyFunctionWithVaryingTypes() => Gen.Constant(true).ForAll(_ => true);

[Theory]
[InlineData(nameof(AnInfallibleNestedPropertyFunction))]
[InlineData(nameof(AnInfallibleNestedPropertyFunctionWithVaryingTypes))]
public void ANestedPropertyMethodInfoCanBeUnfalsifiable(string methodName)
[InlineData(nameof(AnInfalliblePropertyFunction))]
[InlineData(nameof(AnInfalliblePropertyFunctionWithVaryingTypes))]
public void APropertyMethodInfoCanBeUnfalsifiable(string methodName)
{
var property = Property.Reflect(GetMethod(methodName), this);

Expand All @@ -75,12 +75,12 @@ public void ANestedPropertyMethodInfoCanBeUnfalsifiable(string methodName)
checkResult.Falsified.Should().BeFalse();
}

private Property AFallibleNestedPropertyFunction(int x) => Gen.Int32().ForAll(x => x < 100);
private Property AFalliblePropertyFunction() => Gen.Int32().ForAll(x => x < 100);

[Fact]
public void ANestedPropertyMethodInfoCanBeFalsified()
public void APropertyMethodInfoCanBeFalsified()
{
var property = Property.Reflect(GetMethod(nameof(AFallibleNestedPropertyFunction)), this);
var property = Property.Reflect(GetMethod(nameof(AFalliblePropertyFunction)), this);

var checkResult = property.Check();

Expand Down
148 changes: 77 additions & 71 deletions src/GalaxyCheck.Tests/RunnerTests/PrintTests/Snapshots.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,108 +9,114 @@ namespace Tests.V2.RunnerTests.PrintTests
{
public class Snapshots
{
[Fact]
public void PrintInt32Gen()
public class PrintGen
{
var gen = Gen.Int32();
[Fact]
public void PrintInt32Gen()
{
var gen = Gen.Int32();

var print = PrintAndCollect(gen.Cast<object>());
var print = PrintAndCollect(gen.Cast<object>());

Snapshot.Match(print);
}
Snapshot.Match(print);
}

[Fact]
public void PrintInt32GenFiltered()
{
var gen = Gen.Int32().Where(x => x % 2 == 0);
[Fact]
public void PrintInt32GenFiltered()
{
var gen = Gen.Int32().Where(x => x % 2 == 0);

var print = PrintAndCollect(gen.Cast<object>());
var print = PrintAndCollect(gen.Cast<object>());

Snapshot.Match(print);
}
Snapshot.Match(print);
}

[Fact]
public void PrintListOfInt32Gen()
{
var gen = Gen.Int32().ListOf();
[Fact]
public void PrintListOfInt32Gen()
{
var gen = Gen.Int32().ListOf();

var print = PrintAndCollect(gen);
var print = PrintAndCollect(gen);

Snapshot.Match(print);
}
Snapshot.Match(print);
}

[Fact]
public void PrintNullaryProperty()
{
var property = Property.Nullary(() => false);
private static string PrintAndCollect<T>(IGen<T> gen)
{
var log = new List<string>();

var print = PrintAndCollect(property);
gen.Advanced.Print(seed: 0, stdout: log.Add);

Snapshot.Match(print);
return string.Join(Environment.NewLine, log);
}
}

[Fact]
public void PrintUnaryProperty()
public class PrintProperty
{
var property = Gen.Int32().ForAll((_) => false);
[Fact]
public void PrintNullaryProperty()
{
var property = Property.Nullary(() => false);

var print = PrintAndCollect(property);
var print = PrintAndCollect(property);

Snapshot.Match(print);
}
Snapshot.Match(print);
}

[Fact]
public void PrintBinaryProperty()
{
var property =
from x in Gen.Int32().LessThan(int.MaxValue)
from y in Gen.Int32().GreaterThan(x)
select Property.ForThese(() => false);
[Fact]
public void PrintUnaryProperty()
{
var property = Gen.Int32().ForAll((_) => false);

var print = PrintAndCollect(property);
var print = PrintAndCollect(property);

Snapshot.Match(print);
}
Snapshot.Match(print);
}

[Fact]
public void PrintUnaryListProperty()
{
var property = Gen.Int32().ListOf().ForAll((_) => false);
[Fact]
public void PrintBinaryProperty()
{
var property = new Property(
from x in Gen.Int32().LessThan(int.MaxValue)
from y in Gen.Int32().GreaterThan(x)
select Property.ForThese(() => false));

var print = PrintAndCollect(property);
var print = PrintAndCollect(property);

Snapshot.Match(print);
}
Snapshot.Match(print);
}

[Fact]
public void PrintBinaryListProperty()
{
var property =
from xs in Gen.Int32().ListOf()
from ys in Gen.Int32().ListOf().WithCountGreaterThan(xs.Count)
select Property.ForThese(() => false);
[Fact]
public void PrintUnaryListProperty()
{
var property = Gen.Int32().ListOf().ForAll((_) => false);

var print = PrintAndCollect(property);
var print = PrintAndCollect(property);

Snapshot.Match(print);
}
Snapshot.Match(print);
}

private static string PrintAndCollect<T>(IGen<T> gen)
{
var log = new List<string>();
[Fact]
public void PrintBinaryListProperty()
{
var property = new Property(
from xs in Gen.Int32().ListOf()
from ys in Gen.Int32().ListOf().WithCountGreaterThan(xs.Count)
select Property.ForThese(() => false));

gen.Advanced.Print(seed: 0, stdout: log.Add);
var print = PrintAndCollect(property);

return string.Join(Environment.NewLine, log);
}
Snapshot.Match(print);
}

private static string PrintAndCollect<T>(IGen<Test<T>> gen)
{
var log = new List<string>();
private static string PrintAndCollect(Property property)
{
var log = new List<string>();

gen.Advanced.Print(seed: 0, stdout: log.Add);
property.Print(seed: 0, stdout: log.Add);

return string.Join(Environment.NewLine, log);
}
return string.Join(Environment.NewLine, log);
}
}
}
}
11 changes: 8 additions & 3 deletions src/GalaxyCheck.Xunit.Tests/IntegrationSample/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public void FallibleBooleanProperty([Between(0, 100)] int x)
}

[Property]
public Property InfallibleNestedProperty([Between(0, 100)] int x)
public Property InfallibleNestedProperty()
{
AnnounceTestInvocation(nameof(InfallibleNestedProperty));
return Gen.Int32().Between(0, 100).ForAll(y => { });
}

[Property]
public Property FallibleNestedProperty([Between(0, 100)] int x)
public Property FallibleNestedProperty()
{
AnnounceTestInvocation(nameof(FallibleNestedProperty));
return Gen.Int32().Between(0, 100).ForAll(y => { throw new Exception("Failed!"); });
Expand Down Expand Up @@ -97,12 +97,17 @@ public void PropertyWithGenFromMemberGen([MemberGen(nameof(EvenInt32))] int x)
}

[Sample]
public IGen<Test> Sample() =>
public IGen<Test> SamplePureProperty() =>
from a in Gen.Int32().Between(0, 100)
select Property.ForThese(() =>
{
});

[Sample]
public void SampleVoidProperty([Between(0, 100)] int x)
{
}

private void AnnounceTestInvocation(string testName, params object[] injectedValues)
{
_testOutputHelper.WriteLine(JsonConvert.SerializeObject(new
Expand Down
Loading