elm-explorations / elm-explorations/test

Better expect API

Open
#216 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Elm
Stars
244
Forks
40
Avg merge
1d 23h
Merged PRs (30d)
2

Description

Currently the `Expect` api has a number of equality assertions specialised for different datastructures.

The problem with these is that they don't compose. This is most notable with the float equality operator (Expect.within). You even get an error message when you try to `Expect.equal` two `Float` values, but `Expect.equal { foo = 3.14 } { foo = pi }` is fine...

This is also the case with `listsEqual` `setsEqual` and `dictsEqual`, albeit it's a bit less of a problem for those.

Finally, I should mention (and perhaps this is worth highlighting in a separate issue) that the floating equality doesn't deal with `NaN` values...

### API Proposal

So I would propose the following:

```elm
equal : a -> a -> Expectation

type NaNBehavior = NaNsAlwaysEqual | NaNsNeverEqual

equalWithNumbers : NaNBehavior -> FloatingPointTolerance -> a -> a -> Expectation

not : (a -> b -> Expectation) -> a -> b -> Expectation
```

and remove:

- `notEqual` (achieved with `subject |> Expect.not Expect.equal expectation`)
- `within` (achieved with `float |> Expect.equalWithNumbers NaNsAlwaysEqual tolerance expectation`)
- `notWithin` (achieved with `float |> Expect.not (Expect.equalWithFloats NaNsAlwaysEqual tolerance) expectation`)
- `equalLists` (just `equal` will do)
- `equalSets` (just `equal` will do)
- `equalDicts` (just `equal` will do)

### Implementation concerns

Of course to implement the new APIs I think we would need to reach into native code. Essentially we would be copying https://github.com/elm/core/blob/65cea00afa0de03d7dda0487d964a305fc3d58e3/src/Elm/Kernel/Utils.js#L15, but rather than returning booleans, we would use runtime type information to specialise behavior.

Numbers are relatively easy to detect, and we can use the arguments to provide correct matching behavior. I'm less sure about the feasability of correctly detecting lists, sets and dicts to provide nice diff formatting...

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.