From 8d32bb422144959776bc00d41329382b0b7bcdcd Mon Sep 17 00:00:00 2001 From: hobovsky Date: Sat, 18 Feb 2023 19:45:30 +0100 Subject: [PATCH 1/3] Recipe: OUnit assertions --- content/languages/ocaml/recipes.md | 36 ++++++++++++++++++++++++++++++ sidebars.js | 5 ++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 content/languages/ocaml/recipes.md diff --git a/content/languages/ocaml/recipes.md b/content/languages/ocaml/recipes.md new file mode 100644 index 00000000..72db9383 --- /dev/null +++ b/content/languages/ocaml/recipes.md @@ -0,0 +1,36 @@ +--- +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: +- `~printer`, used to stringify values presented by assertion messages. +- `~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 +Time: 1083ms Passed: 0Failed: 1Exit Code: 1 + Test Results: + Tests for square + (square 3) should return 9 + Incorrect answer for n=3 + expected: 9 but got: 10 + Completed in 0.24ms + Completed in 0.26ms +``` + +### 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) \ No newline at end of file diff --git a/sidebars.js b/sidebars.js index 20e6bde8..bfd399b7 100644 --- a/sidebars.js +++ b/sidebars.js @@ -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", From 73535a89a477d135db44759d2501ed4e34a6c2a5 Mon Sep 17 00:00:00 2001 From: hobovsky Date: Sat, 18 Feb 2023 22:28:19 +0100 Subject: [PATCH 2/3] Remove unnecessary lines from example output --- content/languages/ocaml/recipes.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/content/languages/ocaml/recipes.md b/content/languages/ocaml/recipes.md index 72db9383..64d4f13c 100644 --- a/content/languages/ocaml/recipes.md +++ b/content/languages/ocaml/recipes.md @@ -20,14 +20,12 @@ With both arguments used, test output becomes more explicit: ``` ```text -Time: 1083ms Passed: 0Failed: 1Exit Code: 1 - Test Results: - Tests for square - (square 3) should return 9 +Tests for square + (square 3) should return 9 Incorrect answer for n=3 expected: 9 but got: 10 - Completed in 0.24ms - Completed in 0.26ms + Completed in 0.22ms +Completed in 0.24ms ``` ### References From 67f7fbfb816de91b7c17a9a9ef64e3020986d001 Mon Sep 17 00:00:00 2001 From: hobovsky Date: Sun, 19 Feb 2023 01:38:57 +0100 Subject: [PATCH 3/3] Remarks from review --- content/languages/ocaml/recipes.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/languages/ocaml/recipes.md b/content/languages/ocaml/recipes.md index 64d4f13c..2136a139 100644 --- a/content/languages/ocaml/recipes.md +++ b/content/languages/ocaml/recipes.md @@ -6,9 +6,9 @@ tags: [authoring, recipe, ocaml] ## 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. -- `~msg`, used to provide additional information about failure, if necessary. +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: @@ -31,4 +31,4 @@ 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) \ No newline at end of file +- [OUnit: xUnit testing framework for OCaml](https://ocaml.org/p/ounit2/2.2.3/doc/index.html#error-reporting)