From a2046014299e2a0ec7655c42655d7d8465c5fe08 Mon Sep 17 00:00:00 2001 From: Maiste Date: Wed, 18 Oct 2023 19:25:21 +0200 Subject: [PATCH 1/4] Init OCaml repository --- ocaml/.ocamlformat | 1 + ocaml/dune-project | 19 +++++++++++++++++++ ocaml/gilded_rose.opam | 31 +++++++++++++++++++++++++++++++ ocaml/lib/dune | 2 ++ ocaml/test/dune | 2 ++ ocaml/test/gilded_rose.ml | 0 6 files changed, 55 insertions(+) create mode 100644 ocaml/.ocamlformat create mode 100644 ocaml/dune-project create mode 100644 ocaml/gilded_rose.opam create mode 100644 ocaml/lib/dune create mode 100644 ocaml/test/dune create mode 100644 ocaml/test/gilded_rose.ml diff --git a/ocaml/.ocamlformat b/ocaml/.ocamlformat new file mode 100644 index 00000000..37525ae3 --- /dev/null +++ b/ocaml/.ocamlformat @@ -0,0 +1 @@ +profile = default diff --git a/ocaml/dune-project b/ocaml/dune-project new file mode 100644 index 00000000..09667c2b --- /dev/null +++ b/ocaml/dune-project @@ -0,0 +1,19 @@ +(lang dune 3.11) +(generate_opam_files true) + +(name gilded_rose) +(source + (github emilybache/GildedRose-Refactoring-Kata)) +(authors "Maiste ") +(maintainers "Maiste ") +(license MIT) +(documentation https://github.com/emilybache/GildedRose-Refactoring-Kata) + +(package + (name gilded_rose) + (synopsis "Gilded Rose Refactoring Kata") + (description "The Gilded Rose Refactoring Kata in OCaml") + (depends + (ocaml (>= 4.08)) + dune)) + diff --git a/ocaml/gilded_rose.opam b/ocaml/gilded_rose.opam new file mode 100644 index 00000000..57ea38b4 --- /dev/null +++ b/ocaml/gilded_rose.opam @@ -0,0 +1,31 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "Gilded Rose Refactoring Kata" +description: "The Gilded Rose Refactoring Kata in OCaml" +maintainer: ["Maiste "] +authors: ["Maiste "] +license: "MIT" +homepage: "https://github.com/emilybache/GildedRose-Refactoring-Kata" +doc: "https://github.com/emilybache/GildedRose-Refactoring-Kata" +bug-reports: + "https://github.com/emilybache/GildedRose-Refactoring-Kata/issues" +depends: [ + "ocaml" {>= "4.08"} + "dune" {>= "3.11"} + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/emilybache/GildedRose-Refactoring-Kata.git" diff --git a/ocaml/lib/dune b/ocaml/lib/dune new file mode 100644 index 00000000..2ef54c3f --- /dev/null +++ b/ocaml/lib/dune @@ -0,0 +1,2 @@ +(library + (name gilded_rose)) diff --git a/ocaml/test/dune b/ocaml/test/dune new file mode 100644 index 00000000..1d1b4a80 --- /dev/null +++ b/ocaml/test/dune @@ -0,0 +1,2 @@ +(test + (name gilded_rose)) diff --git a/ocaml/test/gilded_rose.ml b/ocaml/test/gilded_rose.ml new file mode 100644 index 00000000..e69de29b From c7bdcb974c6a738891dc4811feff1c6d321c2a9b Mon Sep 17 00:00:00 2001 From: Maiste Date: Wed, 18 Oct 2023 21:10:26 +0200 Subject: [PATCH 2/4] Add Gilded Rose exercise --- ocaml/lib/dune | 1 + ocaml/lib/gilded_rose.ml | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 ocaml/lib/gilded_rose.ml diff --git a/ocaml/lib/dune b/ocaml/lib/dune index 2ef54c3f..f3ac778b 100644 --- a/ocaml/lib/dune +++ b/ocaml/lib/dune @@ -1,2 +1,3 @@ (library + (preprocess (pps ppx_deriving.show)) (name gilded_rose)) diff --git a/ocaml/lib/gilded_rose.ml b/ocaml/lib/gilded_rose.ml new file mode 100644 index 00000000..6a2d0946 --- /dev/null +++ b/ocaml/lib/gilded_rose.ml @@ -0,0 +1,54 @@ +module Item = struct + type t = { name : string; sell_in : int; quality : int } [@@deriving show] + + let v name sell_in quality = { name; sell_in; quality } +end + +module Items = struct + type items = Item.t list [@@deriving show] + + let v ?(items = []) () = items + let show items : string = show_items items + + let update_quality items = + let update_quality_items ({ name; sell_in; quality } as item : Item.t) = + let quality' = + if + name <> "Aged Brie" + && name <> "Backstage passes to a TAFKAL80ETC concert" + then + if quality > 0 then + if name <> "Sulfuras, Hand of Ragnaros" then quality - 1 + else quality + else quality + else if quality < 50 then + quality + 1 + + + if name = "Backstage passes to a TAFKAL80ETC concert" then + if sell_in < 11 then + if quality < 49 then + 1 + if sell_in < 6 then if quality < 48 then 1 else 0 else 0 + else 0 + else 0 + else 0 + else quality + in + let sell_in' = + if name <> "Sulfuras, Hand of Ragnaros" then sell_in - 1 else sell_in + in + if sell_in' < 0 then + if name <> "Aged Brie" then + if name <> "Backstage passes to a TAFKAL80ETC concert" then + if quality' > 0 then + if name <> "Sulfuras, Hand of Ragnaros" then + { item with sell_in = sell_in'; quality = quality' - 1 } + else { item with sell_in = sell_in'; quality = quality' } + else { item with sell_in = sell_in'; quality = quality' } + else { item with sell_in = sell_in'; quality = quality' - quality' } + else if quality' < 50 then + { item with sell_in = sell_in'; quality = quality' + 1 } + else { item with sell_in = sell_in'; quality = quality' } + else { item with sell_in = sell_in'; quality = quality' } + in + List.map update_quality_items items +end From 5f97fd6e341ad166a41d9b29837af9aa7ce5f1ba Mon Sep 17 00:00:00 2001 From: Maiste Date: Thu, 19 Oct 2023 14:03:12 +0200 Subject: [PATCH 3/4] Add initial test --- ocaml/dune-project | 4 +++- ocaml/gilded_rose.opam | 2 ++ ocaml/test/dune | 3 ++- ocaml/test/gilded_rose.ml | 11 +++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ocaml/dune-project b/ocaml/dune-project index 09667c2b..6eb4f1cd 100644 --- a/ocaml/dune-project +++ b/ocaml/dune-project @@ -15,5 +15,7 @@ (description "The Gilded Rose Refactoring Kata in OCaml") (depends (ocaml (>= 4.08)) - dune)) + dune + ppx_deriving + (alcotest (>= 1.7.0)))) diff --git a/ocaml/gilded_rose.opam b/ocaml/gilded_rose.opam index 57ea38b4..2400917f 100644 --- a/ocaml/gilded_rose.opam +++ b/ocaml/gilded_rose.opam @@ -12,6 +12,8 @@ bug-reports: depends: [ "ocaml" {>= "4.08"} "dune" {>= "3.11"} + "ppx_deriving" + "alcotest" {>= "1.7.0"} "odoc" {with-doc} ] build: [ diff --git a/ocaml/test/dune b/ocaml/test/dune index 1d1b4a80..93d58f02 100644 --- a/ocaml/test/dune +++ b/ocaml/test/dune @@ -1,2 +1,3 @@ (test - (name gilded_rose)) + (name gilded_rose) + (libraries gilded_rose alcotest)) diff --git a/ocaml/test/gilded_rose.ml b/ocaml/test/gilded_rose.ml index e69de29b..7be406ff 100644 --- a/ocaml/test/gilded_rose.ml +++ b/ocaml/test/gilded_rose.ml @@ -0,0 +1,11 @@ +open Gilded_rose + +let test () = + let items = Items.v ~items:[ Item.v "Foo" 10 20 ] () in + Alcotest.(check string) + "Broken test" "Fixme" + (List.hd items |> fun item -> item.name) + +let () = + let open Alcotest in + run "Gilded Rose" [ ("Our first test", [ test_case "fixme" `Quick test ]) ] From 70eba3e2b04415a4b93aad75c857be3ee9d30f01 Mon Sep 17 00:00:00 2001 From: Maiste Date: Fri, 20 Oct 2023 09:24:25 +0200 Subject: [PATCH 4/4] Document installation and running for OCaml --- ocaml/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ocaml/README.md diff --git a/ocaml/README.md b/ocaml/README.md new file mode 100644 index 00000000..97ef4bfc --- /dev/null +++ b/ocaml/README.md @@ -0,0 +1,30 @@ +## Gilded Rose Kata for OCaml + +### Requirements + +To run the project, the following package must be available on your computer: +- `opam` `>= 2.0` + +### Installation + +At the root of the _ocaml_ directory, execute: +```sh +opam switch create . --deps-only +eval $(opam env) +``` + +It will install all the required dependencies for the project to run. + +### Running + +This project relies on `dune`. To build it, run this command in your terminal: +```sh +dune exec gilded_rose +``` + +### Testing + +The test suite is built with `Alcostest`. To launch the tests, just type: +```sh +dune runtest +```