Skip to content

Commit

Permalink
Added predefined iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
Pogromca-SCP committed Mar 28, 2024
1 parent 9fe42f1 commit dd4e9f1
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 152 deletions.
115 changes: 64 additions & 51 deletions SLCommandScript.Core.UnitTests/Iterables/IterableListTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FluentAssertions;
using NUnit.Framework;
using SLCommandScript.Core.Iterables;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -10,6 +9,11 @@ namespace SLCommandScript.Core.UnitTests.Iterables;
[TestFixture]
public class IterableListTests
{
private static void Inject(IDictionary<string, string> target, string item)
{
target[item] = item;
}

#region Test Case Sources
private static readonly string[][] _strings = [null, [], [null, null, null, null], ["example", null, "", "test"], [" \t ", "Test", "test", "TEST"]];

Expand All @@ -30,7 +34,7 @@ private static IEnumerable<object[]> JoinArrays<TFirst, TSecond>(TFirst[] first,
public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()
{
// Act
var iterable = new TestIterable(null);
var iterable = new IterableList<string>(null, null);

// Assert
iterable.IsAtEnd.Should().BeTrue();
Expand All @@ -41,15 +45,13 @@ public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()
[TestCaseSource(nameof(_strings))]
public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNotNull(string[] strings)
{
// Arrange
var filtered = strings?.Where(s => s is not null);

// Act
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, null);

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

Expand All @@ -58,7 +60,7 @@ public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNotNul
public void LoadNext_ShouldProperlyIterate_WhenDataSourceIsNull()
{
// Arrange
var iterable = new TestIterable(null);
var iterable = new IterableList<string>(null, null);

// Act
var result = iterable.LoadNext(null);
Expand All @@ -73,8 +75,7 @@ public void LoadNext_ShouldProperlyIterate_WhenDataSourceIsNull()
public void LoadNext_ShouldProperlyIterate_WhenProvidedDictionaryIsNull(string[] strings)
{
// Arrange
var filteredCount = strings?.Where(s => s is not null).Count() ?? 0;
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, null);
var count = 0;

// Act
Expand All @@ -84,17 +85,39 @@ public void LoadNext_ShouldProperlyIterate_WhenProvidedDictionaryIsNull(string[]
}

// 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_WhenProvidedMapperIsNull(string[] strings)
{
// Arrange
var iterable = new IterableList<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(filteredCount);
count.Should().Be(filteredCount);
iterable.Count.Should().Be(len);
count.Should().Be(len);
variables.GetArray().Should().BeEmpty();
}

[TestCaseSource(nameof(_strings))]
public void LoadNext_ShouldProperlySetVariables_WhenProvidedDictionaryIsNotNull(string[] strings)
{
// Arrange
var iterable = new TestIterable(() => strings);
var filteredStrings = strings?.Where(s => s is not null) ?? Array.Empty<string>();
var iterable = new IterableList<string>(() => strings, Inject);
var variables = new TestVariablesCollector();
var count = 0;

Expand All @@ -105,10 +128,11 @@ public void LoadNext_ShouldProperlySetVariables_WhenProvidedDictionaryIsNotNull(
}

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

Expand All @@ -117,10 +141,8 @@ public void LoadNext_ShouldProperlySetVariables_WhenProvidedDictionaryIsNotNull(
public void Randomize_ShouldProperlyRandomizeElements(string[] strings)
{
// Arrange
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, Inject);
var count = 0;
var filteredStrings = strings?.Where(s => s is not null) ?? Array.Empty<string>();
var filteredCount = filteredStrings.Count();
var variables = new TestVariablesCollector();

// Act
Expand All @@ -132,20 +154,19 @@ public void Randomize_ShouldProperlyRandomizeElements(string[] strings)
}

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

[TestCaseSource(nameof(StringsXSizes))]
public void Randomize_ShouldProperlyRandomizeElements(string[] strings, int randAmount)
{
// Arrange
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, Inject);
var count = 0;
var filteredStrings = strings?.Where(s => s is not null) ?? Array.Empty<string>();
var filteredCount = filteredStrings.Count();

// Act
iterable.Randomize(randAmount);
Expand All @@ -156,20 +177,20 @@ public void Randomize_ShouldProperlyRandomizeElements(string[] strings, int rand
}

// Assert
var len = strings?.Length ?? 0;
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(filteredCount > randAmount && randAmount > 0 ? randAmount : filteredCount);
count.Should().Be(filteredCount > randAmount && randAmount > 0 ? randAmount : filteredCount);
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_ShouldProperlyRandomizeElementsByPercentage(string[] strings, float percentage)
{
// Arrange
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, Inject);
var count = 0;
var filteredStrings = strings?.Where(s => s is not null) ?? Array.Empty<string>();
var filteredCount = filteredStrings.Count();
var randAmount = (int) (filteredCount * percentage);
var len = strings?.Length ?? 0;
var randAmount = (int) (len * percentage);

// Act
iterable.Randomize(percentage);
Expand All @@ -181,8 +202,8 @@ public void Randomize_ShouldProperlyRandomizeElementsByPercentage(string[] strin

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

Expand All @@ -191,39 +212,31 @@ public void Randomize_ShouldProperlyRandomizeElementsByPercentage(string[] strin
public void Reset_ShouldProperlyResetIterable_BeforeRunning(string[] strings)
{
// Arrange
var filtered = strings?.Where(s => s is not null) ?? Array.Empty<string>();
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, Inject);

// Act
iterable.Reset();

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

[TestCaseSource(nameof(_strings))]
public void Reset_ShouldProperlyResetIterable_AfterRunning(string[] strings)
{
// Arrange
var filtered = strings?.Where(s => s is not null) ?? Array.Empty<string>();
var iterable = new TestIterable(() => strings);
var iterable = new IterableList<string>(() => strings, Inject);

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

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

public class TestIterable(Func<IEnumerable<string>> strings) : IterableListBase<string>(strings)
{
protected override void LoadVariables(IDictionary<string, string> targetVars, string str)
{
targetVars[str] = str;
}
}
Loading

0 comments on commit dd4e9f1

Please sign in to comment.