-
Notifications
You must be signed in to change notification settings - Fork 14
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
Use custom functions to evaluate equality #39
Comments
I'd add that floating-point equality is an important case but it would also be great to support fully custom expectations on custom types. For example, in point =
Point2d.fromCoordinates ( 1, 0 )
Point2d.rotateAround Point2d.origin (degrees 45) point
--> Point2d.fromCoordinates ( 0.707, 0.707 ) and a corresponding test might use import OpenSolid.Geometry.Expect as Expect
Point2d.fromCoordinates ( 1, 0 )
|> Point2d.rotateAround Point2d.origin (degrees 45)
|> Expect.point2dWithin 0.001
(Point2d.fromCoordinates ( 0.707, 0.707 ) Could this potentially be implemented by adding some more options to |
This is the reason, why I'm against adding special syntax for more complex expectations. Adding special syntax (
I think you could just do: cos (2*pi * 90/360)
|> truncate
--> 0 |
I think e.g. this example is perfectly within the scope of the library. Still it isn't possible to verify it without introducing some additional function to truncate the numbers, like in your example. Doing so would introduce the same amount of clutter than the syntax I proposed does. Another possibilty would be to define which comparison function should be used in a seperate non-documentation comment above the example, like that:
What do you think about that? |
Agreed, but I think it's easier for people new to a library to grok an example if it doesn't hide the code for verifying a result. You can use intermediate definitions to simplify an example and still have a function to verify it. This way it's all explicit and you can separate the function from the example using a line-comment. Would that work for your use-case? Separately, I think it's better to keep the verified example as close to actual elm code as possible. The current syntax is totally valid elm and doesn't introduce any new syntax feature. |
I think there are 2 separate issues going on here:
example input >= 3 --> True or some other way to make a custom predicate, and it will be easier for users to understand what you are saying. As such, I think we can close this issue. If there is some other angle we feel hasn't been covered, we can of course re-open this discussion. |
I'd like to propose to add a syntax to allow
Expectation
creating functions other thanExpect.equal
. The case I'd need such a syntax for is to test examples containing floating point numbers, e.g.This would fail because the result is actually something really small but different from 0.
I need to check for equality with a certain tolerance. The test should look like this:
instead of:
I think this could be achieved by modifying the syntax to include another special character which marks a line that contains the function to be used, e.g. like this:
What do you think? Would this obfuscate the examples too much? Do you maybe have a better idea on how to test examples like that? Is this doable?
The text was updated successfully, but these errors were encountered: