Skip to content

Commit

Permalink
Updated iterables settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pogromca-SCP committed May 26, 2024
1 parent 0ab0297 commit d8c9178
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 271 deletions.
208 changes: 206 additions & 2 deletions SLCommandScript.Core.UnitTests/Iterables/ListIterableTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using NUnit.Framework;
using SLCommandScript.Core.Iterables;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -34,7 +35,7 @@ private static IEnumerable<object[]> JoinArrays<TFirst, TSecond>(TFirst[] first,
public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()
{
// Act
var iterable = new ListIterable<string>(null, null);
var iterable = new ListIterable<string>((Func<IEnumerable<string>>) null, null);

// Assert
iterable.IsAtEnd.Should().BeTrue();
Expand All @@ -53,14 +54,38 @@ public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNotNul
iterable.IsAtEnd.Should().Be(len < 1);
iterable.Count.Should().Be(len);
}

[Test]
public void IterableList_ShouldProperlyInitialize_WhenProvidedItemsAreNull()
{
// Act
var iterable = new ListIterable<string>((IEnumerable<string>) null, null);

// Assert
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(0);
}


[TestCaseSource(nameof(_strings))]
public void IterableList_ShouldProperlyInitialize_WhenProvidedItemsAreNotNull(string[] strings)
{
// Act
var iterable = new ListIterable<string>(strings, null);

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().Be(len < 1);
iterable.Count.Should().Be(len);
}
#endregion

#region LoadNext Tests
[Test]
public void LoadNext_ShouldProperlyIterate_WhenDataSourceIsNull()
{
// Arrange
var iterable = new ListIterable<string>(null, null);
var iterable = new ListIterable<string>((Func<IEnumerable<string>>) null, null);

// Act
var result = iterable.LoadNext(null);
Expand Down Expand Up @@ -134,6 +159,85 @@ public void LoadNext_ShouldProperlySetVariables_WhenProvidedDictionaryIsNotNull(
count.Should().Be(len);
variables.GetArray().Should().Equal(strings ?? []);
}

[Test]
public void LoadNext_ShouldProperlyIterate_WhenItemsAreNull()
{
// Arrange
var iterable = new ListIterable<string>((IEnumerable<string>) null, null);

// Act
var result = iterable.LoadNext(null);

// Assert
result.Should().BeFalse();
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(0);
}

[TestCaseSource(nameof(_strings))]
public void LoadNext_ShouldProperlyIterate_WhenPredefinedAndProvidedDictionaryIsNull(string[] strings)
{
// Arrange
var iterable = new ListIterable<string>(strings, null);
var count = 0;

// Act
while (iterable.LoadNext(null))
{
++count;
}

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(len);
count.Should().Be(len);
}

[TestCaseSource(nameof(_strings))]
public void LoadNext_ShouldProperlyIterate_WhenPredefinedAndProvidedMapperIsNull(string[] strings)
{
// Arrange
var iterable = new ListIterable<string>(strings, null);
var variables = new TestVariablesCollector();
var count = 0;

// Act
while (iterable.LoadNext(variables))
{
++count;
}

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(len);
count.Should().Be(len);
variables.GetArray().Should().BeEmpty();
}

[TestCaseSource(nameof(_strings))]
public void LoadNext_ShouldProperlySetVariables_WhenPredefinedAndProvidedDictionaryIsNotNull(string[] strings)
{
// Arrange
var iterable = new ListIterable<string>(strings, Inject);
var variables = new TestVariablesCollector();
var count = 0;

// Act
while (iterable.LoadNext(variables))
{
++count;
}

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(len);
count.Should().Be(len);
variables.GetArray().Should().Equal(strings ?? []);
}
#endregion

#region Randomize Tests
Expand Down Expand Up @@ -205,6 +309,75 @@ public void Randomize_ShouldProperlyRandomizeElementsByPercentage(string[] strin
iterable.Count.Should().Be(len > randAmount && percentage > 0.0f ? randAmount : len);
count.Should().Be(len > randAmount && percentage > 0.0f ? randAmount : len);
}

[TestCaseSource(nameof(_strings))]
public void Randomize_ShouldProperlyRandomizePredefinedElements(string[] strings)
{
// Arrange
var iterable = new ListIterable<string>(strings, Inject);
var count = 0;
var variables = new TestVariablesCollector();

// Act
iterable.Randomize();

while (iterable.LoadNext(variables))
{
++count;
}

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(len);
count.Should().Be(len);
variables.GetArray().Should().BeEquivalentTo(strings ?? []);
}

[TestCaseSource(nameof(StringsXSizes))]
public void Randomize_ShouldProperlyRandomizePredefinedElements(string[] strings, int randAmount)
{
// Arrange
var iterable = new ListIterable<string>(strings, Inject);
var count = 0;

// Act
iterable.Randomize(randAmount);

while (iterable.LoadNext(null))
{
++count;
}

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(len > randAmount && randAmount > 0 ? randAmount : len);
count.Should().Be(len > randAmount && randAmount > 0 ? randAmount : len);
}

[TestCaseSource(nameof(StringsXPercentages))]
public void Randomize_ShouldProperlyRandomizePredefinedElementsByPercentage(string[] strings, float percentage)
{
// Arrange
var iterable = new ListIterable<string>(strings, Inject);
var count = 0;
var len = strings?.Length ?? 0;
var randAmount = (int)(len * percentage);

// Act
iterable.Randomize(percentage);

while (iterable.LoadNext(null))
{
++count;
}

// Assert
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(len > randAmount && percentage > 0.0f ? randAmount : len);
count.Should().Be(len > randAmount && percentage > 0.0f ? randAmount : len);
}
#endregion

#region Reset Tests
Expand Down Expand Up @@ -238,5 +411,36 @@ public void Reset_ShouldProperlyResetIterable_AfterRunning(string[] strings)
iterable.IsAtEnd.Should().Be(len < 1);
iterable.Count.Should().Be(len);
}

[TestCaseSource(nameof(_strings))]
public void Reset_ShouldProperlyResetPredefinedIterable_BeforeRunning(string[] strings)
{
// Arrange
var iterable = new ListIterable<string>(strings, Inject);

// Act
iterable.Reset();

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().Be(len < 1);
iterable.Count.Should().Be(len);
}

[TestCaseSource(nameof(_strings))]
public void Reset_ShouldProperlyResetPredefinedIterable_AfterRunning(string[] strings)
{
// Arrange
var iterable = new ListIterable<string>(strings, Inject);

// Act
while (iterable.LoadNext(null)) { }
iterable.Reset();

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().Be(len < 1);
iterable.Count.Should().Be(len);
}
#endregion
}
Loading

0 comments on commit d8c9178

Please sign in to comment.