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

Recipe: OUnit assertions #472

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions content/languages/ocaml/recipes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: OCaml recipes for Codewars
tags: [authoring, recipe, ocaml]
---


## Informative assertion messages

Default assertion messages of OCaml OUnit are usually very bad. OUnit assertons accept two optional, named arguments:
hobovsky marked this conversation as resolved.
Show resolved Hide resolved
hobovsky marked this conversation as resolved.
Show resolved Hide resolved
- `~printer`, used to stringify values presented by assertion messages.
Copy link
Contributor

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.

Copy link
Contributor Author

@hobovsky hobovsky Feb 19, 2023

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.

Copy link
Contributor

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)?

Copy link
Contributor Author

@hobovsky hobovsky Feb 19, 2023

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!

- `~msg`, used to provide additional information about failure, if necessary.

With both arguments used, test output becomes more explicit:

```ocaml
"(square 3) should return 9" >:: (fun _ ->
let actual = square 3 in
assert_equal 9 actual ~printer: string_of_int ~msg: "Incorrect answer for n=3"
)
```

```text
Tests for square
(square 3) should return 9
Incorrect answer for n=3
expected: 9 but got: 10
Completed in 0.22ms
Completed in 0.24ms
```

### References

- [Improving OUnit Output](https://cs3110.github.io/textbook/chapters/data/ounit.html#improving-ounit-output)
- [OUnit: xUnit testing framework for OCaml](https://gildor478.github.io/ounit/ounit2/index.html#error-reporting)
hobovsky marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ module.exports = {
type: "doc",
id: "languages/ocaml/index",
},
items: ["languages/ocaml/ounit"],
items: [
"languages/ocaml/ounit",
"languages/ocaml/recipes"
],
},
{
type: "category",
Expand Down