Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
29 changes: 29 additions & 0 deletions src/MrKWatkins.Assertions.Tests/EnumerableAssertionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,33 @@ public async Task NotSequenceEqual_Chain()
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task Contain_Null()
{
IEnumerable<int> nullEnumerable = null!;

await Assert.That(() => nullEnumerable.Should().Contain(1)).Throws<AssertionException>()
.WithMessage("Value should not be null.");
}

[Test]
public async Task Contain()
{
var value = new List<int> { 1, 2, 3 };

await Assert.That(() => value.Should().Contain(2)).ThrowsNothing();
await Assert.That(() => value.Should().Contain(5)).Throws<AssertionException>()
.WithMessage("Value should contain 5 but did not.");
}

[Test]
public async Task Contain_Chain()
{
var value = new List<int> { 1, 2, 3 };

var chain = value.Should().Contain(2);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}
}
123 changes: 123 additions & 0 deletions src/MrKWatkins.Assertions.Tests/FloatingPointAssertionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
namespace MrKWatkins.Assertions.Tests;

public sealed class FloatingPointAssertionsTests
{
[Test]
public async Task BeApproximately()
{
const double value = 17.5;

await Assert.That(() => value.Should().BeApproximately(17.5, 0.1)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.45, 0.1)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.55, 0.1)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.6, 0.1)).Throws<AssertionException>()
.WithMessage("Value should be approximately 17.6 (±0.1) but was 17.5.");
await Assert.That(() => value.Should().BeApproximately(17.4, 0.1)).Throws<AssertionException>()
.WithMessage("Value should be approximately 17.4 (±0.1) but was 17.5.");
}

[Test]
public async Task BeApproximately_Float()
{
const float value = 17.5f;

await Assert.That(() => value.Should().BeApproximately(17.5f, 0.1f)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.6f, 0.05f)).Throws<AssertionException>();
}

[Test]
public async Task BeApproximately_Chain()
{
const double value = 17.5;

var chain = value.Should().BeApproximately(17.5, 0.1);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeLessThan()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeLessThan(10.0)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThan(5.0)).Throws<AssertionException>()
.WithMessage("Value should be less than 5 but was 5.");
await Assert.That(() => value.Should().BeLessThan(1.0)).Throws<AssertionException>()
.WithMessage("Value should be less than 1 but was 5.");
}

[Test]
public async Task BeLessThan_Chain()
{
const double value = 5.0;

var chain = value.Should().BeLessThan(10.0);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeLessThanOrEqualTo()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeLessThanOrEqualTo(10.0)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(5.0)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(1.0)).Throws<AssertionException>()
.WithMessage("Value should be less than or equal to 1 but was 5.");
}

[Test]
public async Task BeLessThanOrEqualTo_Chain()
{
const double value = 5.0;

var chain = value.Should().BeLessThanOrEqualTo(5.0);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThan()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeGreaterThan(1.0)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThan(5.0)).Throws<AssertionException>()
.WithMessage("Value should be greater than 5 but was 5.");
await Assert.That(() => value.Should().BeGreaterThan(10.0)).Throws<AssertionException>()
.WithMessage("Value should be greater than 10 but was 5.");
}

[Test]
public async Task BeGreaterThan_Chain()
{
const double value = 5.0;

var chain = value.Should().BeGreaterThan(1.0);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThanOrEqualTo()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(1.0)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(5.0)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(10.0)).Throws<AssertionException>()
.WithMessage("Value should be greater than or equal to 10 but was 5.");
}

[Test]
public async Task BeGreaterThanOrEqualTo_Chain()
{
const double value = 5.0;

var chain = value.Should().BeGreaterThanOrEqualTo(5.0);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}
}
80 changes: 80 additions & 0 deletions src/MrKWatkins.Assertions.Tests/IntegerAssertionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,84 @@ public async Task ChainWithInheritedMethods()
await Assert.That(() => value.Should().BePositive().And.NotEqual(0)).ThrowsNothing();
await Assert.That(() => value.Should().NotBeZero().And.Equal(42)).ThrowsNothing();
}

[Test]
public async Task BeLessThan()
{
const int value = 5;

await Assert.That(() => value.Should().BeLessThan(10)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThan(5)).Throws<AssertionException>().WithMessage("Value should be less than 5 but was 5.");
await Assert.That(() => value.Should().BeLessThan(1)).Throws<AssertionException>().WithMessage("Value should be less than 1 but was 5.");
}

[Test]
public async Task BeLessThan_Chain()
{
const int value = 5;

var chain = value.Should().BeLessThan(10);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeLessThanOrEqualTo()
{
const int value = 5;

await Assert.That(() => value.Should().BeLessThanOrEqualTo(10)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(5)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(1)).Throws<AssertionException>().WithMessage("Value should be less than or equal to 1 but was 5.");
}

[Test]
public async Task BeLessThanOrEqualTo_Chain()
{
const int value = 5;

var chain = value.Should().BeLessThanOrEqualTo(5);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThan()
{
const int value = 5;

await Assert.That(() => value.Should().BeGreaterThan(1)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThan(5)).Throws<AssertionException>().WithMessage("Value should be greater than 5 but was 5.");
await Assert.That(() => value.Should().BeGreaterThan(10)).Throws<AssertionException>().WithMessage("Value should be greater than 10 but was 5.");
}

[Test]
public async Task BeGreaterThan_Chain()
{
const int value = 5;

var chain = value.Should().BeGreaterThan(1);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThanOrEqualTo()
{
const int value = 5;

await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(1)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(5)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(10)).Throws<AssertionException>().WithMessage("Value should be greater than or equal to 10 but was 5.");
}

[Test]
public async Task BeGreaterThanOrEqualTo_Chain()
{
const int value = 5;

var chain = value.Should().BeGreaterThanOrEqualTo(5);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}
}
14 changes: 14 additions & 0 deletions src/MrKWatkins.Assertions/EnumerableAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,18 @@ public EnumerableAssertionsChain<TEnumerable, T> NotSequenceEqual(params IEnumer

return new EnumerableAssertionsChain<TEnumerable, T>(this);
}

/// <summary>
/// Asserts that the enumerable contains the specified item.
/// </summary>
/// <param name="expected">The item that should be present in the enumerable.</param>
/// <returns>An <see cref="EnumerableAssertionsChain{TEnumerable, T}" /> for chaining further assertions.</returns>
public EnumerableAssertionsChain<TEnumerable, T> Contain(T expected)
{
NotBeNull();

Verify.That(Value.Contains(expected), $"Value should contain {expected} but did not.");

return new EnumerableAssertionsChain<TEnumerable, T>(this);
}
}
73 changes: 73 additions & 0 deletions src/MrKWatkins.Assertions/FloatingPointAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Numerics;

namespace MrKWatkins.Assertions;

/// <summary>
/// Provides assertions for floating-point values.
/// </summary>
/// <typeparam name="T">The floating-point type of the value being asserted on.</typeparam>
/// <param name="value">The value to assert on.</param>
public sealed class FloatingPointAssertions<T>(T value) : ObjectAssertions<T>(value)
where T : struct, IFloatingPoint<T>
{
/// <summary>
/// Asserts that the floating-point value is approximately equal to the expected value within the specified precision.
/// </summary>
/// <param name="expected">The expected value.</param>
/// <param name="precision">The maximum allowed difference between the value and the expected value.</param>
/// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns>
public FloatingPointAssertionsChain<T> BeApproximately(T expected, T precision)
{
Verify.That(T.Abs(Value - expected) <= precision, $"Value should be approximately {expected} (±{precision}) but was {Value}.");

return new FloatingPointAssertionsChain<T>(this);
}

/// <summary>
/// Asserts that the floating-point value is less than the expected value.
/// </summary>
/// <param name="expected">The value the floating-point number should be less than.</param>
/// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns>
public FloatingPointAssertionsChain<T> BeLessThan(T expected)
{
Verify.That(Value < expected, $"Value should be less than {expected} but was {Value}.");

return new FloatingPointAssertionsChain<T>(this);
}

/// <summary>
/// Asserts that the floating-point value is less than or equal to the expected value.
/// </summary>
/// <param name="expected">The value the floating-point number should be less than or equal to.</param>
/// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns>
public FloatingPointAssertionsChain<T> BeLessThanOrEqualTo(T expected)
{
Verify.That(Value <= expected, $"Value should be less than or equal to {expected} but was {Value}.");

return new FloatingPointAssertionsChain<T>(this);
}

/// <summary>
/// Asserts that the floating-point value is greater than the expected value.
/// </summary>
/// <param name="expected">The value the floating-point number should be greater than.</param>
/// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns>
public FloatingPointAssertionsChain<T> BeGreaterThan(T expected)
{
Verify.That(Value > expected, $"Value should be greater than {expected} but was {Value}.");

return new FloatingPointAssertionsChain<T>(this);
}

/// <summary>
/// Asserts that the floating-point value is greater than or equal to the expected value.
/// </summary>
/// <param name="expected">The value the floating-point number should be greater than or equal to.</param>
/// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns>
public FloatingPointAssertionsChain<T> BeGreaterThanOrEqualTo(T expected)
{
Verify.That(Value >= expected, $"Value should be greater than or equal to {expected} but was {Value}.");

return new FloatingPointAssertionsChain<T>(this);
}
}
22 changes: 22 additions & 0 deletions src/MrKWatkins.Assertions/FloatingPointAssertionsChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Numerics;

namespace MrKWatkins.Assertions;

/// <summary>
/// Enables chaining of assertions on a floating-point value after a successful assertion.
/// </summary>
/// <typeparam name="T">The floating-point type of the value being asserted on.</typeparam>
/// <param name="floatingPointAssertions">The assertions object to chain from.</param>
public readonly struct FloatingPointAssertionsChain<T>(FloatingPointAssertions<T> floatingPointAssertions)
where T : struct, IFloatingPoint<T>
{
/// <summary>
/// Gets the assertions object for chaining further assertions.
/// </summary>
public FloatingPointAssertions<T> And { get; } = floatingPointAssertions;

/// <summary>
/// Gets the floating-point value being asserted on.
/// </summary>
public T Value => And.Value;
}
Loading
Loading