Skip to content

Commit

Permalink
Merge pull request #488 from maiste/port-for-ocaml
Browse files Browse the repository at this point in the history
Gilded Rose port for OCaml
  • Loading branch information
emilybache authored Oct 22, 2023
2 parents e621073 + 70eba3e commit 50163ec
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions ocaml/.ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile = default
30 changes: 30 additions & 0 deletions ocaml/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions ocaml/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(lang dune 3.11)
(generate_opam_files true)

(name gilded_rose)
(source
(github emilybache/GildedRose-Refactoring-Kata))
(authors "Maiste <[email protected]>")
(maintainers "Maiste <[email protected]>")
(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 changes: 33 additions & 0 deletions ocaml/gilded_rose.opam
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
authors: ["Maiste <[email protected]>"]
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 changes: 3 additions & 0 deletions ocaml/lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(preprocess (pps ppx_deriving.show))
(name gilded_rose))
54 changes: 54 additions & 0 deletions ocaml/lib/gilded_rose.ml
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions ocaml/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(test
(name gilded_rose)
(libraries gilded_rose alcotest))
11 changes: 11 additions & 0 deletions ocaml/test/gilded_rose.ml
Original file line number Diff line number Diff line change
@@ -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 ]) ]

0 comments on commit 50163ec

Please sign in to comment.