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

Statically typed equality test #21

Closed
tpetricek opened this issue Oct 3, 2013 · 3 comments
Closed

Statically typed equality test #21

tpetricek opened this issue Oct 3, 2013 · 3 comments

Comments

@tpetricek
Copy link
Member

In an earlier version, there used to be shouldEqual primitive that beahaved like y |> should equal x but statically required x and y to have the same type.

I find that extremely useful - I was using some untyped equal constraints and it quite regularly caused trouble where test would compile, but fail - when I changed some API.

Can we have this function (or some alternative) back?

@fsoikin
Copy link

fsoikin commented Oct 6, 2014

+1 on this.

Here is a simple repro:

let someFn x = 
   match x with 
      | 42 -> []
      | _ -> [1;x]

[<Test>]
let ``someFn should return empty list when given 42``() =
   someFn 42
   |> should equal []

The test fails and error message cryptically states that Expected: [], was: []

@dtchepak
Copy link
Member

dtchepak commented Dec 3, 2014

+1. It would be useful to support statically typed matchers in other cases too. I am currently trying the following (not tested yet, please point out errors):

let inline equalWithin t x =
    let isInRange y = y <= x+t && y>= x-t
    NHamcrest.CustomMatcher<_>(sprintf "%s with a tolerance of %s" (string x) (string t), fun v -> isInRange v)

let inline should' m expected actual = Xunit.Assert.That(actual, m expected)

This has the advantage of equalWithin working on any comparable type that supports -, + (including DateTime with TimeSpan tolerance). The current equalWithin only works with objects whose string representations parse as doubles.

@sergey-tihon
Copy link
Member

Released in v2.2.0-beta1

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

No branches or pull requests

4 participants