Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 assertions accept two optional arguments:
- `~printer` defines a printer (stringifier) for compared values.
- `~msg` is 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://ocaml.org/p/ounit2/2.2.3/doc/index.html#error-reporting)
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