From 36278e06c5fa11b081779d56598f05679618c7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Tue, 27 Aug 2024 14:33:46 +1200 Subject: [PATCH] Add error handling to the collatz-conjecture exercise (#33) --- .../collatz-conjecture/.meta/Example.roc | 11 +++++----- .../collatz-conjecture/.meta/template.j2 | 6 +++++- .../collatz-conjecture/.meta/tests.toml | 2 -- .../collatz-conjecture-test.roc | 20 +++++++++++++------ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/exercises/practice/collatz-conjecture/.meta/Example.roc b/exercises/practice/collatz-conjecture/.meta/Example.roc index 107f2ac..57a3f3c 100644 --- a/exercises/practice/collatz-conjecture/.meta/Example.roc +++ b/exercises/practice/collatz-conjecture/.meta/Example.roc @@ -1,10 +1,11 @@ module [steps] steps = \n -> - if n == 1 then - 0 + if n <= 0 then + Err "Only positive integers are allowed" + else if n == 1 then + Ok 0 else if Num.isEven n then - (steps (n // 2)) + 1 + Ok ((steps? (n // 2)) + 1) else - (steps (3 * n + 1)) + 1 - + Ok ((steps? (3 * n + 1)) + 1) diff --git a/exercises/practice/collatz-conjecture/.meta/template.j2 b/exercises/practice/collatz-conjecture/.meta/template.j2 index 6b7828b..7b290f7 100644 --- a/exercises/practice/collatz-conjecture/.meta/template.j2 +++ b/exercises/practice/collatz-conjecture/.meta/template.j2 @@ -6,6 +6,10 @@ import {{ exercise | to_pascal }} exposing [steps] {% for case in cases -%} # {{ case["description"] }} -expect {{ case["property"] | to_camel }} {{ case["input"]["number"] }} == {{ case["expected"] }} +{% if case["expected"]["error"] -%} +expect {{ case["property"] | to_camel }} {{ case["input"]["number"] }} == Err {{ case["expected"]["error"] | to_roc }} +{%- else -%} +expect {{ case["property"] | to_camel }} {{ case["input"]["number"] }} == Ok {{ case["expected"] }} +{%- endif %} {% endfor %} diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml index acf36f8..cc34e16 100644 --- a/exercises/practice/collatz-conjecture/.meta/tests.toml +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -28,7 +28,6 @@ include = false [2187673d-77d6-4543-975e-66df6c50e2da] description = "zero is an error" reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3" -include = false [c6c795bf-a288-45e9-86a1-841359ad426d] description = "negative value is an error" @@ -37,4 +36,3 @@ include = false [ec11f479-56bc-47fd-a434-bcd7a31a7a2e] description = "negative value is an error" reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d" -include = false diff --git a/exercises/practice/collatz-conjecture/collatz-conjecture-test.roc b/exercises/practice/collatz-conjecture/collatz-conjecture-test.roc index b5143fd..0e9f832 100644 --- a/exercises/practice/collatz-conjecture/collatz-conjecture-test.roc +++ b/exercises/practice/collatz-conjecture/collatz-conjecture-test.roc @@ -1,7 +1,9 @@ # These tests are auto-generated with test data from: # https://github.com/exercism/problem-specifications/tree/main/exercises/collatz-conjecture/canonical-data.json -# File last updated on 2024-08-24 -app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br" } +# File last updated on 2024-08-27 +app [main] { + pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br", +} import pf.Task exposing [Task] @@ -11,14 +13,20 @@ main = import CollatzConjecture exposing [steps] # zero steps for one -expect steps 1 == 0 +expect steps 1 == Ok 0 # divide if even -expect steps 16 == 4 +expect steps 16 == Ok 4 # even and odd steps -expect steps 12 == 9 +expect steps 12 == Ok 9 # large number of even and odd steps -expect steps 1000000 == 152 +expect steps 1000000 == Ok 152 + +# zero is an error +expect steps 0 == Err "Only positive integers are allowed" + +# negative value is an error +expect steps -15 == Err "Only positive integers are allowed"