Skip to content

Commit

Permalink
Reviving validations lost during v2.0 rewriting.
Browse files Browse the repository at this point in the history
  • Loading branch information
DyegoMaas committed Jun 26, 2021
1 parent 7d90646 commit 88ec91b
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
namespace ForeverFactory.Core.Transforms.Guards.Specifications
using System;

namespace ForeverFactory.Core.Transforms.Guards.Specifications
{
internal class ApplyTransformToFirstInstancesSpecification : CanApplyTransformSpecification
{
private readonly int _countToApply;

public ApplyTransformToFirstInstancesSpecification(int countToApply)
public ApplyTransformToFirstInstancesSpecification(int countToApply, int targetCount)
{
if (countToApply < 0)
throw new ArgumentException($"Not possible to apply to {countToApply}. Only positive values are accepted");

if (countToApply > targetCount)
throw new ArgumentException($"Not possible to apply to {countToApply}. Max size is {targetCount}.");

_countToApply = countToApply;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ForeverFactory.Core.Transforms.Guards.Specifications
using System;

namespace ForeverFactory.Core.Transforms.Guards.Specifications
{
internal class ApplyTransformToLastInstancesSpecification : CanApplyTransformSpecification
{
Expand All @@ -7,6 +9,12 @@ internal class ApplyTransformToLastInstancesSpecification : CanApplyTransformSpe

public ApplyTransformToLastInstancesSpecification(int countToApply, int targetCount)
{
if (countToApply < 0)
throw new ArgumentException($"Not possible to apply to {countToApply}. Only positive values are accepted");

if (countToApply > targetCount)
throw new ArgumentException($"Not possible to apply to {countToApply}. Max size is {targetCount}.");

_countToApply = countToApply;
_targetCount = targetCount;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ForeverFactory.Tests/Core/GeneratorNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public void It_should_apply_transforms_with_guards()
);
generatorNode.AddTransform(
new FuncTransform<Person, string>(x => x.FirstName = "Martha"),
new ApplyTransformToFirstInstancesSpecification(2)
new ApplyTransformToFirstInstancesSpecification(countToApply: 2, targetCount: 5)
);
generatorNode.AddTransform(
new FuncTransform<Person, string>(x => x.FirstName = "Mirage"),
new ApplyTransformToLastInstancesSpecification(2, 5)
new ApplyTransformToLastInstancesSpecification(countToApply: 2, targetCount: 5)
);

var persons = generatorNode.ProduceInstances().ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentAssertions;
using ForeverFactory.Core.Transforms.Guards.Specifications;
using Xunit;

namespace ForeverFactory.Tests.Core.Transforms.Guards.Specifications
{
public class AlwaysApplyTransformSpecificationTests
{
[Fact]
public void It_should_apply_if_no_conditions_were_set()
{
var guardSpecification = new AlwaysApplyTransformSpecification();

var canApply = guardSpecification.CanApply(0);

canApply.Should().BeTrue();
}




}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using FluentAssertions;
using ForeverFactory.Core.Transforms.Guards.Specifications;
using Xunit;

namespace ForeverFactory.Tests.Core.Transforms.Guards.Specifications
{
public class ApplyTransformToFirstInstancesSpecificationTests
{
[Theory]
[InlineData(0, 2, true)]
[InlineData(1, 2, true)]
[InlineData(2, 2, false)]
public void It_should_apply_only_to_the_first_n_instances(int currentIndex, int countToApply, bool expected)
{
var guardSpecification = new ApplyTransformToFirstInstancesSpecification(countToApply, targetCount: 2);

var canApply = guardSpecification.CanApply(currentIndex);

canApply.Should().Be(expected);
}

[Theory]
[InlineData(2, 1)]
[InlineData(3, 2)]
public void It_should_throw_if_count_to_apply_to_first_n_instances_is_greater_than_target_count(int countToApply, int targetCount)
{
Action invalidConfigurationBuild =
() => new ApplyTransformToFirstInstancesSpecification(countToApply, targetCount);

invalidConfigurationBuild.Should()
.Throw<ArgumentException>("it is not possible to apply transformations beyond set size");
}

[Theory]
[InlineData(-10)]
[InlineData(-1)]
public void It_should_throw_if_count_to_apply_to_first_n_instances_is_negative(int countToApply)
{
Action invalidConfigurationBuild =
() => new ApplyTransformToFirstInstancesSpecification(countToApply, targetCount: 2);

invalidConfigurationBuild.Should()
.Throw<ArgumentException>("it is not possible to apply transformations to negative counts");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using FluentAssertions;
using ForeverFactory.Core.Transforms.Guards.Specifications;
using Xunit;

namespace ForeverFactory.Tests.Core.Transforms.Guards.Specifications
{
public class ApplyTransformToLastInstancesSpecificationTests
{
[Theory]
[InlineData(0, 2, 4, false)]
[InlineData(1, 2, 4, false)]
[InlineData(2, 2, 4, true)]
[InlineData(3, 2, 4, true)]
public void It_should_apply_only_to_the_last_n_instances(int currentIndex, int countToApply, int targetCount,
bool expected)
{
var guardSpecification = new ApplyTransformToLastInstancesSpecification(countToApply, targetCount);

var canApply = guardSpecification.CanApply(currentIndex);

canApply.Should().Be(expected);
}

[Theory]
[InlineData(2, 1)]
[InlineData(3, 2)]
public void It_should_throw_if_count_to_apply_to_last_n_instances_is_greater_than_target_count(int countToApply, int targetCount)
{
Action invalidConfigurationBuild =
() => new ApplyTransformToLastInstancesSpecification(countToApply, targetCount);

invalidConfigurationBuild.Should()
.Throw<ArgumentException>("it is not possible to apply transformations beyond set size");
}

[Theory]
[InlineData(-10)]
[InlineData(-1)]
public void It_should_throw_if_count_to_apply_to_last_n_instances_is_negative(int countToApply)
{
Action invalidConfigurationBuild =
() => new ApplyTransformToLastInstancesSpecification(countToApply, targetCount: 2);

invalidConfigurationBuild.Should()
.Throw<ArgumentException>("it is not possible to apply transformations to negative counts");
}
}
}

This file was deleted.

126 changes: 126 additions & 0 deletions tests/ForeverFactory.Tests/MultipleInstanceBuildingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using ForeverFactory.Tests.CustomizedFactories.ExampleFactories;
using Xunit;

namespace ForeverFactory.Tests
{
public class MultipleInstanceBuildingTests
{
[Fact]
public void Should_throw_an_argument_exception_for_first_count_bigger_than_total_size()
{
Action invalidConfigurationBuild = () => new ProductFactory()
.Many(10)
.WithFirst(count: 11, x => x.Description = "xxx")
.Build();

invalidConfigurationBuild.Should()
.Throw<ArgumentException>("it is not possible to apply transformations beyond set size");
}

[Fact]
public void Should_throw_an_argument_exception_for_last_count_bigger_than_total_size()
{
Action invalidConfigurationBuild = () => new ProductFactory()
.Many(10)
.WithLast(count: 11, x => x.Description = "xxx")
.Build();

invalidConfigurationBuild.Should()
.Throw<ArgumentException>("it is not possible to apply transformations beyond set size");
}

[Fact]
public void WithFirst_applies_transformation_only_over_the_first_n_instances_produced()
{
var songs = new ProductFactory()
.Many(10)
.With(x => x.Description = "Dies Irae")
.WithFirst(count: 2, x => x.Description = "Requiem II: Dies Irae")
.Build()
.ToList();

var firstTwo = songs.Take(2);
foreach (var song in firstTwo)
{
song.Description.Should().Be("Requiem II: Dies Irae");
}

var lastEight = songs.Skip(2);
foreach (var song in lastEight)
{
song.Description.Should().Be("Dies Irae");
}
}

[Fact]
public void WithFirst_transforms_are_applied_in_order_overriding_previous_ones()
{
var products = new ProductFactory()
.Many(10)
.With(x => x.Description = "Default description")
.WithFirst(5, x => x.Description = "Original first five")
.WithFirst(2, x => x.Description = "New first two")
.Build()
.ToArray();

var firstTwo = products.Take(2).Select(x => x.Description);
firstTwo.Should().OnlyContain(x => x == "New first two");

var nextThree = products.Skip(2).Take(3).Select(x => x.Description);
nextThree.Should().OnlyContain(x => x == "Original first five");

var lastFive = products.Skip(5).Select(x => x.Description);
lastFive.Should().OnlyContain(x => x == "Default description");
}

[Fact]
public void WithLast_transforms_are_applied_in_order_overriding_previous_ones()
{
var products = new ProductFactory()
.Many(10)
.With(x => x.Description = "Default description")
.WithLast(5, x => x.Description = "Original last five")
.WithLast(2, x => x.Description = "New last two")
.Build()
.ToArray();

var firstFive = products.Take(5).Select(x => x.Description);
firstFive.Should().OnlyContain(x => x == "Default description");

var nextThree = products.Skip(5).Take(3).Select(x => x.Description);
nextThree.Should().OnlyContain(x => x == "Original last five");

var lastTwo = products.Skip(8).Select(x => x.Description);
lastTwo.Should().OnlyContain(x => x == "New last two");
}

[Fact]
public void WithLast_applies_transformation_only_over_the_last_x_instances_produced()
{
var songs = new ProductFactory()
.Many(10)
.With(x => x.Description = "Dies Irae")
.WithLast(count: 2, x => x.Description = "Requiem II: Dies Irae")
.Build()
.ToList();

songs.Should().HaveCount(10);

var firstEight = songs.Take(8);
foreach (var song in firstEight)
{
song.Description.Should().Be("Dies Irae");
}

var lastTwo = songs.Skip(8);
foreach (var song in lastTwo)
{
song.Description.Should().Be("Requiem II: Dies Irae");
}
}
}
}

0 comments on commit 88ec91b

Please sign in to comment.