Skip to content

Commit

Permalink
Added single item iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pogromca-SCP committed Mar 29, 2024
1 parent dd4e9f1 commit 7d2c26c
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 19 deletions.
39 changes: 39 additions & 0 deletions SLCommandScript.Core.UnitTests/Iterables/EmptyIterableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using FluentAssertions;
using NUnit.Framework;
using SLCommandScript.Core.Iterables;

namespace SLCommandScript.Core.UnitTests.Iterables;

[TestFixture]
public class EmptyIterableTests
{
#region Constructor Tests
[Test]
public void EmptyIterable_ShouldProperlyInitialize()
{
// Act
var iterable = new EmptyIterable();

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

#region LoadNext Tests
[Test]
public void LoadNext_ShouldReturnFalse()
{
// Arrange
var iterable = new EmptyIterable();

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

// Assert
result.Should().BeFalse();
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(0);
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using NUnit.Framework;
using SLCommandScript.Core.Iterables;
using System.Collections.Generic;
using System.Linq;

namespace SLCommandScript.Core.UnitTests.Iterables;

Expand All @@ -14,24 +13,11 @@ 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"]];

private static readonly int[] _sizes = [-1, 0, 1, 2, 3];

private static readonly float[] _percentages = [-1.0f, 0.0f, 0.25f, 0.1f, 0.5f, 2.5f];

private static IEnumerable<object[]> StringsXSizes => JoinArrays(_strings, _sizes);

private static IEnumerable<object[]> StringsXPercentages => JoinArrays(_strings, _percentages);

private static IEnumerable<object[]> JoinArrays<TFirst, TSecond>(TFirst[] first, TSecond[] second) =>
first.SelectMany(f => second.Select(s => new object[] { f, s }));
#endregion

#region Constructor Tests
[Test]
public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()
public void PredefinedIterable_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()
{
// Act
var iterable = new PredefinedIterable<string>(null, null);
Expand All @@ -43,7 +29,7 @@ public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()


[TestCaseSource(nameof(_strings))]
public void IterableList_ShouldProperlyInitialize_WhenProvidedDataSourceIsNotNull(string[] strings)
public void PredefinedIterable_ShouldProperlyInitialize_WhenProvidedDataSourceIsNotNull(string[] strings)
{
// Act
var iterable = new PredefinedIterable<string>(strings, null);
Expand Down
268 changes: 268 additions & 0 deletions SLCommandScript.Core.UnitTests/Iterables/SingleItemIterableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
using FluentAssertions;
using NUnit.Framework;
using SLCommandScript.Core.Iterables;
using System;
using System.Collections.Generic;

namespace SLCommandScript.Core.UnitTests.Iterables;

[TestFixture]
public class SingleItemIterableTests
{
private const string TestString = "test";

private static void Inject(IDictionary<string, string> target, string item)
{
target[item] = item;
}

#region Constructor Tests
[Test]
public void SingleItemIterable_ShouldProperlyInitialize_WhenProvidedDataSourceIsNull()
{
// Act
var iterable = new SingleItemIterable<string>((Func<string>) null, null);

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

[Test]
public void SingleItemIterable_ShouldProperlyInitialize_WhenProvidedDataSourceIsNotNull()
{
// Act
var iterable = new SingleItemIterable<string>(() => TestString, null);

// Assert
iterable.IsAtEnd.Should().BeFalse();
iterable.Count.Should().Be(1);
}

[Test]
public void SingleItemIterable_ShouldProperlyInitialize_WhenProvidedItemDirectly()
{
// Act
var iterable = new SingleItemIterable<string>(TestString, null);

// Assert
iterable.IsAtEnd.Should().BeFalse();
iterable.Count.Should().Be(1);
}
#endregion

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

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

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

[Test]
public void LoadNext_ShouldProperlyIterate_WhenProvidedDictionaryIsNull()
{
// Arrange
var iterable = new SingleItemIterable<string>(() => TestString, null);
var count = 0;

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

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

[Test]
public void LoadNext_ShouldProperlyIterate_WhenProvidedMapperIsNull()
{
// Arrange
var iterable = new SingleItemIterable<string>(() => TestString, null);
var variables = new TestVariablesCollector();
var count = 0;

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

// Assert
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(1);
count.Should().Be(1);
variables.GetArray().Should().BeEmpty();
}

[Test]
public void LoadNext_ShouldProperlySetVariables_WhenProvidedDictionaryIsNotNull()
{
// Arrange
var iterable = new SingleItemIterable<string>(() => TestString, Inject);
var variables = new TestVariablesCollector();
var count = 0;

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

// Assert
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(1);
count.Should().Be(1);
variables.GetArray().Should().HaveCount(1);
variables.GetArray().Should().Contain(TestString);
}

[Test]
public void LoadNext_ShouldProperlyIterate_WhenProvidedItemAndDictionaryIsNull()
{
// Arrange
var iterable = new SingleItemIterable<string>(TestString, null);
var count = 0;

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

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

[Test]
public void LoadNext_ShouldProperlyIterate_WhenProvidedItemAndMapperIsNull()
{
// Arrange
var iterable = new SingleItemIterable<string>(TestString, null);
var variables = new TestVariablesCollector();
var count = 0;

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

// Assert
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(1);
count.Should().Be(1);
variables.GetArray().Should().BeEmpty();
}

[Test]
public void LoadNext_ShouldProperlySetVariables_WhenProvidedItemAndDictionaryIsNotNull()
{
// Arrange
var iterable = new SingleItemIterable<string>(TestString, Inject);
var variables = new TestVariablesCollector();
var count = 0;

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

// Assert
iterable.IsAtEnd.Should().BeTrue();
iterable.Count.Should().Be(1);
count.Should().Be(1);
variables.GetArray().Should().HaveCount(1);
variables.GetArray().Should().Contain(TestString);
}
#endregion

#region Reset Tests
[Test]
public void Reset_ShouldProperlyResetIterable_WhenSourceIsNull()
{
// Arrange
var iterable = new SingleItemIterable<string>((Func<string>) null, Inject);

// Act
iterable.Reset();

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

[Test]
public void Reset_ShouldProperlyResetIterable_BeforeRunning()
{
// Arrange
var iterable = new SingleItemIterable<string>(() => TestString, Inject);

// Act
iterable.Reset();

// Assert
iterable.IsAtEnd.Should().BeFalse();
iterable.Count.Should().Be(1);
}

[Test]
public void Reset_ShouldProperlyResetIterable_AfterRunning()
{
// Arrange
var iterable = new SingleItemIterable<string>(() => TestString, Inject);

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

// Assert
iterable.IsAtEnd.Should().BeFalse();
iterable.Count.Should().Be(1);
}

[Test]
public void Reset_ShouldProperlyResetIterable_BeforeRunningOnItem()
{
// Arrange
var iterable = new SingleItemIterable<string>(TestString, Inject);

// Act
iterable.Reset();

// Assert
iterable.IsAtEnd.Should().BeFalse();
iterable.Count.Should().Be(1);
}

[Test]
public void Reset_ShouldProperlyResetIterable_AfterRunningOnItem()
{
// Arrange
var iterable = new SingleItemIterable<string>(TestString, Inject);

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

// Assert
iterable.IsAtEnd.Should().BeFalse();
iterable.Count.Should().Be(1);
}
#endregion
}
Loading

0 comments on commit 7d2c26c

Please sign in to comment.