-
Notifications
You must be signed in to change notification settings - Fork 203
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
Recipe: OUnit assertions #472
base: master
Are you sure you want to change the base?
Conversation
content/languages/ocaml/recipes.md
Outdated
## Informative assertion messages | ||
|
||
Default assertion messages of OCaml OUnit are usually very bad. OUnit assertons accept two optional, named arguments: | ||
- `~printer`, used to stringify values presented by assertion messages. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend the following definition:
~printer
defines a printer (stringifier) for compared values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, your wording sounds more like "stringified values are compared" rather than "stringified values are presented in a message". I understand it as something like Javascript's Test.assertSimilar
.
But I trust your experience more than my feelings, so I go with your proposal.
content/languages/ocaml/recipes.md
Outdated
## Informative assertion messages | ||
|
||
Default assertion messages of OCaml OUnit are usually very bad. OUnit assertons accept two optional, named arguments: | ||
- `~printer`, used to stringify values presented by assertion messages. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful to provide some common printer
values: for printing int
, float
, 'a list
, 'a array
, etc. Printers can be defined with standard OCaml functions or with Batteries (or Core) functions.
Since not all translators use Batteries, it may be preferable to use standard OCaml functions in this recipe. On the other hand, Batteries include many useful printing functions. Here is a function for printing int list
written by trashy_incel:
let string_of_int_list (lst : int list) : string =
"[" ^ String.concat "; " (List.map string_of_int lst) ^ "]"
And here is the same function using Batteries:
let string_of_int_list = IO.to_string (List.print Int.print)
It is very short and can be inlined.
What do you think? Should we include examples of common printers here? If yes, then is it better to use standard OCaml functions or functions from Batteries (I don't know Core so I cannot say if it is better or not)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I planned to present an example of a custom printer, but decided not to (for now).
I like the idea of listing some commonly used printers, but I have one problem with this: I struggle with finding some balance between helping authors, and repeating "obvious" things which can be found in obvious places by anyone who would be bothered to search.
Nothing of this short recipe is a thing which, in my opinion, should be presented this explicitly. The recipe presents almost nothing specific to Codewars authoring, and everything here is practical aspects of the ecosystem. Heck, even if someone is totally not familiar with OCaml, or OUnit, all it takes is one run of failing tests, a tiny piece of reflection that "heck, this is bad, there has to be some way to make this look better", and a search in official docs . I know why I have written this recipe, but why it has to be written is beyond me.
If authors struggle to find out that ~printer
even exists, then I think it's not reasonable to expect them to know how to create custom printers. I'd give them commonly used printers too. If you have an idea what printers, and ideas for implementation, just drop a list here, or on Discord, or in a ticket. I for example did not know what printer to use to print strings, or I would not know what to use to print a generic 'a list
. Whether to use Core or Batteries, I don't know because I am totally not familiar with OCaml ecosystem and I do not know what are common practices. For me, both are fine. I will rely on judgment of you and other users.
Thanks for reviewing!
Since authors often do not bother to check output of failing tests and are not aware of how bad the default assertion messages are with OUnit, I created this recipe to post it as links in problematic translations.
Preview