Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public override void Format(TimeSpan value, StringBuilder stringBuilder,
FormattingOptions options)
{
string formatString;
if (value.Days > 0)
if (value.Days != 0)
{
formatString = @"d\.hh\:mm\:ss";
}
else if (value.Hours > 0)
else if (value.Hours != 0)
{
formatString = @"h\:mm\:ss";
}
Expand All @@ -28,6 +28,11 @@ public override void Format(TimeSpan value, StringBuilder stringBuilder,
formatString += @"\.fff";
}

if (value < TimeSpan.Zero)
{
stringBuilder.Append('-');
}

stringBuilder.Append(value.ToString(formatString));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Formatting;
using Testably.Expectations.Options;
using Testably.Expectations.Results;

namespace Testably.Expectations;

public static partial class ThatNullableTimeSpanShould
{
/// <summary>
/// Verifies that the subject is equal to the <paramref name="expected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> Be(
this IThat<TimeSpan?> source,
TimeSpan? expected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
expected,
$"be {Formatter.Format(expected)}",
(a, e, t) => IsWithinTolerance(t, a - e),
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}

/// <summary>
/// Verifies that the subject is not equal to the <paramref name="unexpected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> NotBe(
this IThat<TimeSpan?> source,
TimeSpan? unexpected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
unexpected,
$"not be {Formatter.Format(unexpected)}",
(a, e, t) => !IsWithinTolerance(t, a - e),
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Formatting;
using Testably.Expectations.Options;
using Testably.Expectations.Results;

namespace Testably.Expectations;

public static partial class ThatNullableTimeSpanShould
{
/// <summary>
/// Verifies that the subject is greater than the <paramref name="expected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> BeGreaterThan(
this IThat<TimeSpan?> source,
TimeSpan? expected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
expected,
$"be greater than {Formatter.Format(expected)}",
(a, e, t) => a + t > e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}

/// <summary>
/// Verifies that the subject is not greater than the <paramref name="unexpected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> NotBeGreaterThan(
this IThat<TimeSpan?> source,
TimeSpan? unexpected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
unexpected,
$"not be greater than {Formatter.Format(unexpected)}",
(a, e, t) => a - t <= e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Formatting;
using Testably.Expectations.Options;
using Testably.Expectations.Results;

namespace Testably.Expectations;

public static partial class ThatNullableTimeSpanShould
{
/// <summary>
/// Verifies that the subject is greater than or equal to the <paramref name="expected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> BeGreaterThanOrEqualTo(
this IThat<TimeSpan?> source,
TimeSpan? expected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
expected,
$"be greater than or equal to {Formatter.Format(expected)}",
(a, e, t) => a + t >= e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}

/// <summary>
/// Verifies that the subject is not greater than or equal to the <paramref name="unexpected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> NotBeGreaterThanOrEqualTo(
this IThat<TimeSpan?> source,
TimeSpan? unexpected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
unexpected,
$"not be greater than or equal to {Formatter.Format(unexpected)}",
(a, e, t) => a - t < e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Formatting;
using Testably.Expectations.Options;
using Testably.Expectations.Results;

namespace Testably.Expectations;

public static partial class ThatNullableTimeSpanShould
{
/// <summary>
/// Verifies that the subject is less than the <paramref name="expected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> BeLessThan(
this IThat<TimeSpan?> source,
TimeSpan? expected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
expected,
$"be less than {Formatter.Format(expected)}",
(a, e, t) => a - t < e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}

/// <summary>
/// Verifies that the subject is not less than the <paramref name="unexpected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> NotBeLessThan(
this IThat<TimeSpan?> source,
TimeSpan? unexpected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
unexpected,
$"not be less than {Formatter.Format(unexpected)}",
(a, e, t) => a + t >= e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Formatting;
using Testably.Expectations.Options;
using Testably.Expectations.Results;

namespace Testably.Expectations;

public static partial class ThatNullableTimeSpanShould
{
/// <summary>
/// Verifies that the subject is less than or equal to the <paramref name="expected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> BeLessThanOrEqualTo(
this IThat<TimeSpan?> source,
TimeSpan? expected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
expected,
$"be less than or equal to {Formatter.Format(expected)}",
(a, e, t) => a - t <= e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}

/// <summary>
/// Verifies that the subject is not less than or equal to the <paramref name="unexpected" /> value.
/// </summary>
public static TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>> NotBeLessThanOrEqualTo(
this IThat<TimeSpan?> source,
TimeSpan? unexpected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan?, IThat<TimeSpan?>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
unexpected,
$"not be less than or equal to {Formatter.Format(unexpected)}",
(a, e, t) => a + t > e,
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Core.Constraints;
using Testably.Expectations.Options;

namespace Testably.Expectations;

/// <summary>
/// Expectations on <see cref="TimeSpan" /> values.
/// </summary>
public static partial class ThatNullableTimeSpanShould
{
/// <summary>
/// Start expectations for current <see cref="TimeSpan" /> <paramref name="subject" />.
/// </summary>
public static IThat<TimeSpan?> Should(this IExpectSubject<TimeSpan?> subject)
=> subject.Should(_ => { });

private static bool IsWithinTolerance(TimeSpan? tolerance, TimeSpan? difference)
{
if (tolerance == null)
{
return difference == TimeSpan.Zero;
}

return difference <= tolerance.Value &&
difference >= tolerance.Value.Negate();
}

private readonly struct ConditionConstraint(
TimeSpan? expected,
string expectation,
Func<TimeSpan?, TimeSpan?, TimeSpan, bool> condition,
Func<TimeSpan?, TimeSpan?, string> failureMessageFactory,
TimeTolerance tolerance) : IValueConstraint<TimeSpan?>
{
public ConstraintResult IsMetBy(TimeSpan? actual)
{
if (condition(actual, expected, tolerance.Tolerance ?? TimeSpan.Zero))
{
return new ConstraintResult.Success<TimeSpan?>(actual, ToString());
}

return new ConstraintResult.Failure(ToString(),
failureMessageFactory(actual, expected));
}

public override string ToString()
=> expectation + tolerance;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Testably.Expectations.Core;
using Testably.Expectations.Formatting;
using Testably.Expectations.Options;
using Testably.Expectations.Results;

namespace Testably.Expectations;
Expand All @@ -10,25 +11,40 @@ public static partial class ThatTimeSpanShould
/// <summary>
/// Verifies that the subject is equal to the <paramref name="expected" /> value.
/// </summary>
public static AndOrResult<TimeSpan, IThat<TimeSpan>> Be(this IThat<TimeSpan> source,
TimeSpan expected)
=> new(source.ExpectationBuilder
public static TimeToleranceResult<TimeSpan, IThat<TimeSpan>> Be(
this IThat<TimeSpan> source,
TimeSpan? expected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan, IThat<TimeSpan>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
expected,
(a, e) => a.Equals(e),
$"be {Formatter.Format(expected)}")),
source);
$"be {Formatter.Format(expected)}",
(a, e, t) => IsWithinTolerance(t, a - e),
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}

/// <summary>
/// Verifies that the subject is not equal to the <paramref name="unexpected" /> value.
/// </summary>
public static AndOrResult<TimeSpan, IThat<TimeSpan>> NotBe(
public static TimeToleranceResult<TimeSpan, IThat<TimeSpan>> NotBe(
this IThat<TimeSpan> source,
TimeSpan unexpected)
=> new(source.ExpectationBuilder
TimeSpan? unexpected)
{
TimeTolerance tolerance = new();
return new TimeToleranceResult<TimeSpan, IThat<TimeSpan>>(
source.ExpectationBuilder
.AddConstraint(new ConditionConstraint(
unexpected,
(a, e) => !a.Equals(e),
$"not be {Formatter.Format(unexpected)}")),
source);
$"not be {Formatter.Format(unexpected)}",
(a, e, t) => !IsWithinTolerance(t, a - e),
(a, _) => $"found {Formatter.Format(a)}",
tolerance)),
source,
tolerance);
}
}
Loading
Loading