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

Add DateTime/TimeSpan support for inequality tolerance #4600

Closed
RenderMichael opened this issue Jan 16, 2024 · 7 comments · Fixed by #4618
Closed

Add DateTime/TimeSpan support for inequality tolerance #4600

RenderMichael opened this issue Jan 16, 2024 · 7 comments · Fixed by #4618

Comments

@RenderMichael
Copy link
Contributor

It would be nice if the following code worked:

public static DateTime LastDateAccessed { get; set; } // simulate a remote value

[Test]
public void TestMe()
{
    DateTime beforeUpdate = DateTime.UtcNow;

    // Do operation
    LastDateAccessed = DateTime.UtcNow.AddMilliseconds(-50); // some kind of timing imperfection on the server

    // This fails because of the timing issue
    Assert.That(LastDateAccessed, Is.GreaterThan(beforeUpdate));

    // It would be nice to do this
    Assert.That(LastDateAccessed, Is.GreaterThan(beforeUpdate).Within(TimeSpan.FromSeconds(1)));
}

For consistency, this should probably be done on all constraints that extend ComparisonConstraint.

I am willing to take up the task. I just want to make sure this is kind of change would be accepted.

@RenderMichael
Copy link
Contributor Author

My workaround, using the above example:

Assert.That(LastDateAccessed, Is.InRange(beforeUpdate - TimeSpan.FromSeconds(1), beforeUpdate + TimeSpan.FromSeconds(1)));

But it's less elegant or intuitive.

@manfred-brands
Copy link
Member

@RenderMichael The ComparisonConstraint does seem to have support for Within, it might just be that it doesn't work on DateTime. The special time suffixes are only on the EqualsConstraint. Maybe those need to be move up to its base class. It also looks like the Tolerance.LinearRange doesn't handle this.

Feel free to work on this to make it more consistent with IsEqualTo.

@RenderMichael
Copy link
Contributor Author

Opened PR #4618

@OsirisTerje OsirisTerje added this to the 4.1 milestone Feb 15, 2024
@OsirisTerje
Copy link
Member

@RenderMichael
Copy link
Contributor Author

Works flawlessly 👍
image

@OsirisTerje
Copy link
Member

OsirisTerje commented Feb 21, 2024

@RenderMichael Seems this one needs documentation too, for the constraints that now have got them.

Can you add that?

@RenderMichael
Copy link
Contributor Author

@OsirisTerje opened nunit/docs#907, let me know if there's something else you had in mind as I've never contributed docs before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants