diff --git a/janet/.gitignore b/janet/.gitignore new file mode 100644 index 00000000..6743d20f --- /dev/null +++ b/janet/.gitignore @@ -0,0 +1,10 @@ +* + +!.gitignore +!project.janet +!README.md + +!src/ +!src/main.janet +!test/ +!test/*.janet diff --git a/janet/README.md b/janet/README.md new file mode 100644 index 00000000..7cdffd9b --- /dev/null +++ b/janet/README.md @@ -0,0 +1,39 @@ +# Gilded Rose + +This is the Gilded Rose kata in [janet](https://janet-lang.org). + +## Getting started + +We'll need to install [janet and jpm](https://janet-lang.org/docs/index.html). + +## Running texttest + +To produce output suitable for texttest, we can run: + +```sh +janet test/texttest.janet +``` + +We can specify the number of days as an argument: + +```sh +janet test/texttest.janet 30 +``` + +## Running tests + +First, we install the test library dependency using `jpm -l deps`. Then we can run all the tests using: + +```sh +jpm -l test +``` + +The testing library being used is [judge](https://github.com/ianthehenry/judge), that enables the writing of inline snapshot tests. +Specifically, Judge has the ability to capture a snapshot of stdout, which gives us a similar experience to that offered by texttest, but natively in the janet tests. + +Having run `jpm -l deps`, we now have a (repository-local) installed executable of judge, which we can run directly using: + +```sh +./jpm_tree/bin/judge +``` +(pass `-h` for usage). diff --git a/janet/project.janet b/janet/project.janet new file mode 100644 index 00000000..789199d4 --- /dev/null +++ b/janet/project.janet @@ -0,0 +1,6 @@ +(declare-project + :name "gilded-rose" + :description "A program for updating inventory at the Gilded Rose" + :dependencies ["https://github.com/ianthehenry/judge.git"]) + +(task "test" [] (shell "jpm_tree/bin/judge")) diff --git a/janet/src/main.janet b/janet/src/main.janet new file mode 100644 index 00000000..1ec8400e --- /dev/null +++ b/janet/src/main.janet @@ -0,0 +1,46 @@ +(defn item [name quality sell-in] @{:name name :quality quality :sell-in sell-in}) + +(defn update-quality [items] + (for i 0 (length items) + (if (and + (not (= "Aged Brie" ((get items i) :name))) + (not (= "Backstage passes to a TAFKAL80ETC concert" ((get items i) :name)))) + (if (< 0 ((get items i) :quality)) + (if (not (= "Sulfuras, Hand of Ragnaros" ((get items i) :name))) + (put (get items i) :quality (- ((get items i) :quality) 1)))) + (if (> 50 ((get items i) :quality)) + (do + (put (get items i) :quality (+ ((get items i) :quality) 1)) + (if (= "Backstage passes to a TAFKAL80ETC concert" ((get items i) :name)) + (do + (if (> 11 ((get items i) :sell-in)) + (if (> 50 ((get items i) :quality)) + (put (get items i) :quality (+ ((get items i) :quality) 1)))) + (if (> 6 ((get items i) :sell-in)) + (if (> 50 ((get items i) :quality)) + (put (get items i) :quality (+ ((get items i) :quality) 1))))))))) + (if (not (= "Sulfuras, Hand of Ragnaros" ((get items i) :name))) + (put (get items i) :sell-in (- ((get items i) :sell-in) 1))) + (if (> 0 ((get items i) :sell-in)) + (if (not (= "Aged Brie" ((get items i) :name))) + (if (not (= "Backstage passes to a TAFKAL80ETC concert" ((get items i) :name))) + (if (< 0 ((get items i) :quality)) + (if (not (= "Sulfuras, Hand of Ragnaros" ((get items i) :name))) + (put (get items i) :quality (- ((get items i) :quality) 1)))) + (put (get items i) :quality (- ((get items i) :quality) ((get items i) :quality)))) + (if (> 50 ((get items i) :quality)) + (put (get items i) :quality (+ ((get items i) :quality) 1))))))) + +(defn- item-to-str [x] + (string (get x :name) ", " (get x :sell-in) ", " (get x :quality))) + +(defn run [days items] + (print "OMGHAI!") + (for i 0 (+ 1 days) + (do + (print (string "-------- day " i " --------")) + (print "name, sellIn, quality") + (for j 0 (length items) + (print (item-to-str (get items j)))) + (print "") + (update-quality items)))) diff --git a/janet/test/gilded-rose.janet b/janet/test/gilded-rose.janet new file mode 100644 index 00000000..789f3f50 --- /dev/null +++ b/janet/test/gilded-rose.janet @@ -0,0 +1,7 @@ +(import ../src/main :as shop) +(use judge) + +(do + (def items [(shop/item "foo" 0 0)]) + (shop/update-quality items) + (test ((first items) :name) "fixme")) diff --git a/janet/test/texttest.janet b/janet/test/texttest.janet new file mode 100644 index 00000000..a8e3be2e --- /dev/null +++ b/janet/test/texttest.janet @@ -0,0 +1,22 @@ +(import ../src/main :as shop) + +(defn get-items [] + [ (shop/item "+5 Dexterity Vest" 20 10) + (shop/item "Aged Brie" 0 2) + (shop/item "Elixir of the Mongoose" 7 5) + (shop/item "Sulfuras, Hand of Ragnaros" 80 0) + (shop/item "Sulfuras, Hand of Ragnaros" 80 -1) + (shop/item "Backstage passes to a TAFKAL80ETC concert" 20 15) + (shop/item "Backstage passes to a TAFKAL80ETC concert" 49 10) + (shop/item "Backstage passes to a TAFKAL80ETC concert" 49 5) + (shop/item "Conjured Mana Cake" 6 3)]) + +# judge allows for snapshot testing of stdout, much like the texttest tool. +# So something like the following would create a janet-native test of the output of shop/run: +# +# (test-stdout (shop/run 30 (get-items))) + +(defn main [& args] + (def num-days (scan-number (or (get args 1) "-1"))) + (def items (get-items)) + (shop/run num-days items))