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/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 +``` diff --git a/ocaml/dune-project b/ocaml/dune-project new file mode 100644 index 00000000..6eb4f1cd --- /dev/null +++ b/ocaml/dune-project @@ -0,0 +1,21 @@ +(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 + ppx_deriving + (alcotest (>= 1.7.0)))) + diff --git a/ocaml/gilded_rose.opam b/ocaml/gilded_rose.opam new file mode 100644 index 00000000..2400917f --- /dev/null +++ b/ocaml/gilded_rose.opam @@ -0,0 +1,33 @@ +# 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"} + "ppx_deriving" + "alcotest" {>= "1.7.0"} + "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..f3ac778b --- /dev/null +++ b/ocaml/lib/dune @@ -0,0 +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 diff --git a/ocaml/test/dune b/ocaml/test/dune new file mode 100644 index 00000000..93d58f02 --- /dev/null +++ b/ocaml/test/dune @@ -0,0 +1,3 @@ +(test + (name gilded_rose) + (libraries gilded_rose alcotest)) diff --git a/ocaml/test/gilded_rose.ml b/ocaml/test/gilded_rose.ml new file mode 100644 index 00000000..7be406ff --- /dev/null +++ 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 ]) ]