Skip to content

Commit

Permalink
refactor: simplify and modularize SourceGenerator (#178)
Browse files Browse the repository at this point in the history
The project "Testably.Abstractions.Tests.SourceGenerator" was a
proof-of-concept and should be refactored so that it is easier to
understand.

Also some test classes are named inconsistently with the classes under
test and the location is not always as expected compared with the
overall structure of the test classes.
  • Loading branch information
vbreuss authored Oct 31, 2022
1 parent 036587b commit 5516a14
Show file tree
Hide file tree
Showing 20 changed files with 594 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using Testably.Abstractions.Testing.FileSystemInitializer;

namespace Testably.Abstractions.Testing.Tests.FileSystemInitializer;
namespace Testably.Abstractions.Testing.Tests;

public class FileSystemInitializerTests
public class FileSystemInitializerExtensionsTests
{
[Fact]
public void Initialize_WithAFile_ShouldCreateFile()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Testably.Abstractions.Testing.Helpers;

namespace Testably.Abstractions.Testing.Tests.Internal;
namespace Testably.Abstractions.Testing.Tests.Helpers;

public class EncryptionHelperTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Tests.TestHelpers;

namespace Testably.Abstractions.Testing.Tests.Internal;
namespace Testably.Abstractions.Testing.Tests.Helpers;

public class FilePlatformIndependenceExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Testably.Abstractions.Testing.Helpers;

namespace Testably.Abstractions.Testing.Tests.Internal;
namespace Testably.Abstractions.Testing.Tests.Helpers;

public class FileSystemExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Tests.TestHelpers;

namespace Testably.Abstractions.Testing.Tests.Internal;
namespace Testably.Abstractions.Testing.Tests.Helpers;

public class PathHelperTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using Testably.Abstractions.Testing.Helpers;

namespace Testably.Abstractions.Testing.Tests.Internal;
namespace Testably.Abstractions.Testing.Tests.Helpers;

public class RandomSystemExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
using Moq;
using System.Collections.Generic;
using System.Linq;

namespace Testably.Abstractions.Testing.Tests;

public partial class RandomProviderTests
{
public class GeneratorTests
{
[Fact]
public void Dispose_ShouldThrowObjectDisposedExceptionOnGetNext()
{
int maxRange = 100;
IEnumerable<int> enumerable = Enumerable.Range(0, maxRange);
RandomProvider.Generator<int> sut =
RandomProvider.Generator<int>.FromEnumerable(enumerable);
sut.GetNext();
sut.GetNext();

sut.Dispose();
Exception? exception = Record.Exception(() =>
{
sut.GetNext();
});

exception.Should().BeOfType<ObjectDisposedException>();
}

[Theory]
[AutoData]
public void FromArray_Overflow_ShouldStartAgain(Guid[] values)
{
RandomProvider.Generator<Guid> sut =
RandomProvider.Generator<Guid>.FromArray(values);

Guid[] results = new Guid[values.Length * 2];
for (int i = 0; i < values.Length * 2; i++)
{
results[i] = sut.GetNext();
}

results.Should().BeEquivalentTo(values.Concat(values));
}

[Theory]
[AutoData]
public void FromArray_ShouldIterateThroughArrayValue(Guid[] values)
{
RandomProvider.Generator<Guid> sut =
RandomProvider.Generator<Guid>.FromArray(values);

Guid[] results = new Guid[values.Length];
for (int i = 0; i < values.Length; i++)
{
results[i] = sut.GetNext();
}

results.Should().BeEquivalentTo(values);
}

[Fact]
public void FromCallback_ShouldExecuteCallback()
{
int iterations = 30;
int startValue = 10;
int executionCount = 0;
RandomProvider.Generator<int> sut =
RandomProvider.Generator<int>.FromCallback(
() => startValue + executionCount++);

int[] results = new int[iterations];
for (int i = 0; i < iterations; i++)
{
results[i] = sut.GetNext();
}

executionCount.Should().Be(iterations);
results.Should().BeEquivalentTo(Enumerable.Range(startValue, iterations));
}

[Fact]
public void FromEnumerable_Dispose_ShouldDisposeEnumerator()
{
Mock<IEnumerable<int>> mock = new();
Mock<IEnumerator<int>> enumeratorMock = new();
mock.Setup(m => m.GetEnumerator()).Returns(enumeratorMock.Object);
RandomProvider.Generator<int> sut =
RandomProvider.Generator<int>.FromEnumerable(mock.Object);

sut.Dispose();

enumeratorMock.Verify(m => m.Dispose());
}

[Fact]
public void FromEnumerable_Overflow_List_ShouldResetEnumerator()
{
int maxRange = 100;
// A list as enumerable does support `Reset`
List<int> values = Enumerable.Range(0, maxRange).ToList();
IEnumerable<int> enumerable = values;
RandomProvider.Generator<int> sut =
RandomProvider.Generator<int>.FromEnumerable(enumerable);

int[] results = new int[maxRange * 2];
for (int i = 0; i < maxRange * 2; i++)
{
results[i] = sut.GetNext();
}

results.Should().BeEquivalentTo(values.Concat(values));
}

[Fact]
public void FromEnumerable_Overflow_ShouldResetEnumerator()
{
int maxRange = 100;
IEnumerable<int> enumerable = Enumerable.Range(0, maxRange);
RandomProvider.Generator<int> sut =
RandomProvider.Generator<int>.FromEnumerable(enumerable);

for (int i = 0; i < maxRange; i++)
{
_ = sut.GetNext();
}

Exception? exception = Record.Exception(() =>
{
sut.GetNext();
});

// Enumerable.Range does not support `Reset`
exception.Should().BeOfType<NotSupportedException>();
}

[Fact]
public void FromEnumerable_ShouldReturnEnumerableValues()
{
int maxRange = 100;
IEnumerable<int> enumerable = Enumerable.Range(0, maxRange);
RandomProvider.Generator<int> sut =
RandomProvider.Generator<int>.FromEnumerable(enumerable);

int[] results = new int[maxRange];
for (int i = 0; i < maxRange; i++)
{
results[i] = sut.GetNext();
}

results.Should().BeEquivalentTo(Enumerable.Range(0, maxRange));
}

[Theory]
[AutoData]
public void FromValue_ShouldReturnFixedValue(Guid value)
{
int maxRange = 100;
RandomProvider.Generator<Guid> sut =
RandomProvider.Generator<Guid>.FromValue(value);

Guid[] results = new Guid[maxRange];
for (int i = 0; i < maxRange; i++)
{
results[i] = sut.GetNext();
}

results.Should().AllBeEquivalentTo(value);
}

[Theory]
[AutoData]
public void Operator_FromArray(Guid[] values)
{
RandomProvider.Generator<Guid> sut = values;

Guid[] results = new Guid[values.Length];
for (int i = 0; i < values.Length; i++)
{
results[i] = sut.GetNext();
}

results.Should().BeEquivalentTo(values);
}

[Theory]
[AutoData]
public void Operator_FromCallback(Guid value)
{
int maxRange = 100;
// ReSharper disable once ConvertToLocalFunction
Func<Guid> castFrom = () => value;
RandomProvider.Generator<Guid> sut = castFrom;

Guid[] results = new Guid[maxRange];
for (int i = 0; i < maxRange; i++)
{
results[i] = sut.GetNext();
}

results.Should().AllBeEquivalentTo(value);
}

[Theory]
[AutoData]
public void Operator_FromValue(Guid value)
{
int maxRange = 100;
RandomProvider.Generator<Guid> sut = value;

Guid[] results = new Guid[maxRange];
for (int i = 0; i < maxRange; i++)
{
results[i] = sut.GetNext();
}

results.Should().AllBeEquivalentTo(value);
}
}
}
Loading

0 comments on commit 5516a14

Please sign in to comment.