Merge pull request #488 from maiste/port-for-ocaml

Gilded Rose port for OCaml
This commit is contained in:
Emily Bache 2023-10-22 10:43:17 +02:00 committed by GitHub
commit 50163ec350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 0 deletions

1
ocaml/.ocamlformat Normal file
View File

@ -0,0 +1 @@
profile = default

30
ocaml/README.md Normal file
View File

@ -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
```

21
ocaml/dune-project Normal file
View File

@ -0,0 +1,21 @@
(lang dune 3.11)
(generate_opam_files true)
(name gilded_rose)
(source
(github emilybache/GildedRose-Refactoring-Kata))
(authors "Maiste <dev@maiste.fr>")
(maintainers "Maiste <dev@maiste.fr>")
(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))))

33
ocaml/gilded_rose.opam Normal file
View File

@ -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 <dev@maiste.fr>"]
authors: ["Maiste <dev@maiste.fr>"]
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"

3
ocaml/lib/dune Normal file
View File

@ -0,0 +1,3 @@
(library
(preprocess (pps ppx_deriving.show))
(name gilded_rose))

54
ocaml/lib/gilded_rose.ml Normal file
View File

@ -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

3
ocaml/test/dune Normal file
View File

@ -0,0 +1,3 @@
(test
(name gilded_rose)
(libraries gilded_rose alcotest))

11
ocaml/test/gilded_rose.ml Normal file
View File

@ -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 ]) ]