Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add statistic assertions #104

Merged
merged 8 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add WithThirdParameter
  • Loading branch information
vbreuss committed Apr 1, 2024
commit 7eda87d51ebf78d1af41fcc5d75c260528802724
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Testably.Abstractions.FluentAssertions;

public class StatisticMethodAssertions<TType, TAssertions>

Check failure on line 8 in Source/Testably.Abstractions.FluentAssertions/StatisticMethodAssertions.cs

View workflow job for this annotation

GitHub Actions / Test (Windows)

Missing XML comment for publicly visible type or member 'StatisticMethodAssertions<TType, TAssertions>'

Check failure on line 8 in Source/Testably.Abstractions.FluentAssertions/StatisticMethodAssertions.cs

View workflow job for this annotation

GitHub Actions / Test (Ubuntu)

Missing XML comment for publicly visible type or member 'StatisticMethodAssertions<TType, TAssertions>'
: StatisticAssertionsCount<TType, TAssertions>
where TAssertions : StatisticAssertions<TType>
{
Expand All @@ -25,7 +25,7 @@

private readonly IEnumerable<MethodStatistic> _methods;

public StatisticMethodAssertions(TAssertions assertions, string methodName)

Check failure on line 28 in Source/Testably.Abstractions.FluentAssertions/StatisticMethodAssertions.cs

View workflow job for this annotation

GitHub Actions / Test (Windows)

Missing XML comment for publicly visible type or member 'StatisticMethodAssertions<TType, TAssertions>.StatisticMethodAssertions(TAssertions, string)'

Check failure on line 28 in Source/Testably.Abstractions.FluentAssertions/StatisticMethodAssertions.cs

View workflow job for this annotation

GitHub Actions / Test (Ubuntu)

Missing XML comment for publicly visible type or member 'StatisticMethodAssertions<TType, TAssertions>.StatisticMethodAssertions(TAssertions, string)'
: base(assertions)
{
_assertions = assertions;
Expand All @@ -34,7 +34,7 @@
_methods = Array.Empty<MethodStatistic>();
}

public StatisticMethodAssertions(TAssertions assertions, string methodName,

Check failure on line 37 in Source/Testably.Abstractions.FluentAssertions/StatisticMethodAssertions.cs

View workflow job for this annotation

GitHub Actions / Test (Windows)

Missing XML comment for publicly visible type or member 'StatisticMethodAssertions<TType, TAssertions>.StatisticMethodAssertions(TAssertions, string, IEnumerable<MethodStatistic>)'

Check failure on line 37 in Source/Testably.Abstractions.FluentAssertions/StatisticMethodAssertions.cs

View workflow job for this annotation

GitHub Actions / Test (Ubuntu)

Missing XML comment for publicly visible type or member 'StatisticMethodAssertions<TType, TAssertions>.StatisticMethodAssertions(TAssertions, string, IEnumerable<MethodStatistic>)'
IEnumerable<MethodStatistic> methods)
: base(assertions)
{
Expand Down Expand Up @@ -92,6 +92,21 @@
Func<TParameter, bool> predicate)
=> WithParameterAt(1, predicate);

/// <summary>
/// Filters for methods whose third parameter equals <paramref name="parameterValue" />.
/// </summary>
public StatisticMethodAssertions<TType, TAssertions> WithThirdParameter<TParameter>(
TParameter parameterValue)
=> WithParameterAt<TParameter>(2,
p => p == null ? parameterValue == null : p.Equals(parameterValue));

/// <summary>
/// Filters for methods whose third parameter matches the <paramref name="predicate" />.
/// </summary>
public StatisticMethodAssertions<TType, TAssertions> WithThirdParameter<TParameter>(
Func<TParameter, bool> predicate)
=> WithParameterAt(2, predicate);

/// <inheritdoc />
protected override int GetCount()
=> _methods.Count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FluentAssertions;
using System;
using System.IO.Abstractions;
using System.Text;
using Testably.Abstractions.Testing;
using Testably.Abstractions.Testing.Statistics;
using Xunit;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void HaveCalled_Never_WhenNotCalled_ShouldNotThrow()

[Theory]
[AutoData]
public void WithParameterAt_WithPredicate_Never_WhenCalled_ShouldThrow(
public void WithFirstParameter_WithPredicate_Never_WhenCalled_ShouldThrow(
string because)
{
MockFileSystem fileSystem = new();
Expand All @@ -51,7 +52,7 @@ public void WithParameterAt_WithPredicate_Never_WhenCalled_ShouldThrow(
Exception? exception = Record.Exception(() =>
{
sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithParameterAt<string>(0, p => p == "foo")
.WithFirstParameter<string>(p => p == "foo")
.Never(because);
});

Expand All @@ -62,20 +63,20 @@ public void WithParameterAt_WithPredicate_Never_WhenCalled_ShouldThrow(
}

[Fact]
public void WithParameterAt_WithPredicate_Never_WhenNotCalled_ShouldNotThrow()
public void WithFirstParameter_WithPredicate_Never_WhenNotCalled_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar");
IStatistics<IFile> sut = fileSystem.Statistics.File;

sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithParameterAt<string>(0, p => p == "bar")
.WithFirstParameter<string>(p => p == "bar")
.Never();
}

[Theory]
[AutoData]
public void WithParameterAt_WithValue_Never_WhenCalled_ShouldThrow(
public void WithFirstParameter_WithValue_Never_WhenCalled_ShouldThrow(
string because)
{
MockFileSystem fileSystem = new();
Expand All @@ -85,7 +86,7 @@ public void WithParameterAt_WithValue_Never_WhenCalled_ShouldThrow(
Exception? exception = Record.Exception(() =>
{
sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithParameterAt(0, "foo")
.WithFirstParameter("foo")
.Never(because);
});

Expand All @@ -96,20 +97,20 @@ public void WithParameterAt_WithValue_Never_WhenCalled_ShouldThrow(
}

[Fact]
public void WithParameterAt_WithValue_Never_WhenNotCalled_ShouldNotThrow()
public void WithFirstParameter_WithValue_Never_WhenNotCalled_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar");
IStatistics<IFile> sut = fileSystem.Statistics.File;

sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithParameterAt(0, "bar")
.WithFirstParameter("bar")
.Never();
}

[Theory]
[AutoData]
public void WithFirstParameter_WithPredicate_Never_WhenCalled_ShouldThrow(
public void WithParameterAt_WithPredicate_Never_WhenCalled_ShouldThrow(
string because)
{
MockFileSystem fileSystem = new();
Expand All @@ -119,7 +120,7 @@ public void WithFirstParameter_WithPredicate_Never_WhenCalled_ShouldThrow(
Exception? exception = Record.Exception(() =>
{
sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithFirstParameter<string>(p => p == "foo")
.WithParameterAt<string>(0, p => p == "foo")
.Never(because);
});

Expand All @@ -130,20 +131,20 @@ public void WithFirstParameter_WithPredicate_Never_WhenCalled_ShouldThrow(
}

[Fact]
public void WithFirstParameter_WithPredicate_Never_WhenNotCalled_ShouldNotThrow()
public void WithParameterAt_WithPredicate_Never_WhenNotCalled_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar");
IStatistics<IFile> sut = fileSystem.Statistics.File;

sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithFirstParameter<string>(p => p == "bar")
.WithParameterAt<string>(0, p => p == "bar")
.Never();
}

[Theory]
[AutoData]
public void WithFirstParameter_WithValue_Never_WhenCalled_ShouldThrow(
public void WithParameterAt_WithValue_Never_WhenCalled_ShouldThrow(
string because)
{
MockFileSystem fileSystem = new();
Expand All @@ -153,7 +154,7 @@ public void WithFirstParameter_WithValue_Never_WhenCalled_ShouldThrow(
Exception? exception = Record.Exception(() =>
{
sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithFirstParameter("foo")
.WithParameterAt(0, "foo")
.Never(because);
});

Expand All @@ -164,14 +165,14 @@ public void WithFirstParameter_WithValue_Never_WhenCalled_ShouldThrow(
}

[Fact]
public void WithFirstParameter_WithValue_Never_WhenNotCalled_ShouldNotThrow()
public void WithParameterAt_WithValue_Never_WhenNotCalled_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar");
IStatistics<IFile> sut = fileSystem.Statistics.File;

sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithFirstParameter("bar")
.WithParameterAt(0, "bar")
.Never();
}

Expand Down Expand Up @@ -242,4 +243,38 @@ public void WithSecondParameter_WithValue_Never_WhenNotCalled_ShouldNotThrow()
.WithSecondParameter("foo")
.Never();
}

[Theory]
[AutoData]
public void WithThirdParameter_WithValue_Never_WhenCalled_ShouldThrow(
string because)
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar", Encoding.UTF8);
IStatistics<IFile> sut = fileSystem.Statistics.File;

Exception? exception = Record.Exception(() =>
{
sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithThirdParameter(Encoding.UTF8)
.Never(because);
});

exception.Should().NotBeNull();
exception!.Message.Should()
.Be(
$"Expected method `WriteAllText` to never be called {because}, but it was once.");
}

[Fact]
public void WithThirdParameter_WithValue_Never_WhenNotCalled_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
fileSystem.File.WriteAllText("foo", "bar", Encoding.UTF8);
IStatistics<IFile> sut = fileSystem.Statistics.File;

sut.Should().HaveCalled(nameof(IFile.WriteAllText))
.WithThirdParameter(Encoding.ASCII)
.Never();
}
}
Loading