diff --git a/janet/.gitignore b/janet/.gitignore index ee1dbd89..6743d20f 100644 --- a/janet/.gitignore +++ b/janet/.gitignore @@ -2,7 +2,9 @@ !.gitignore !project.janet -!gilded-rose/ -!gilded-rose/main.janet +!README.md + +!src/ +!src/main.janet !test/ -!test/*.janet \ No newline at end of file +!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 index d7201034..789199d4 100644 --- a/janet/project.janet +++ b/janet/project.janet @@ -3,10 +3,4 @@ :description "A program for updating inventory at the Gilded Rose" :dependencies ["https://github.com/ianthehenry/judge.git"]) - -(declare-executable - :name "gilded-rose" - :entry "gilded-rose/main.janet" - :install true) - -(task "test" [] (shell "jpm_tree/bin/judge")) \ No newline at end of file +(task "test" [] (shell "jpm_tree/bin/judge")) diff --git a/janet/gilded-rose/main.janet b/janet/src/main.janet similarity index 74% rename from janet/gilded-rose/main.janet rename to janet/src/main.janet index 0952991c..1ec8400e 100644 --- a/janet/gilded-rose/main.janet +++ b/janet/src/main.janet @@ -1,3 +1,5 @@ +(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 @@ -29,23 +31,12 @@ (if (> 50 ((get items i) :quality)) (put (get items i) :quality (+ ((get items i) :quality) 1))))))) -(defn item-to-str [x] +(defn- item-to-str [x] (string (get x :name) ", " (get x :sell-in) ", " (get x :quality))) -(defn main [& args] - (def num-days (scan-number (or (get args 1) "2"))) +(defn run [days items] (print "OMGHAI!") - (def items - [@{ :name "+5 Dexterity Vest" :quality 20 :sell-in 10} - @{ :name "Aged Brie" :quality 0 :sell-in 2} - @{ :name "Elixir of the Mongoose" :quality 7 :sell-in 5} - @{ :name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0} - @{ :name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in -1} - @{ :name "Backstage passes to a TAFKAL80ETC concert" :quality 20 :sell-in 15} - @{ :name "Backstage passes to a TAFKAL80ETC concert" :quality 49 :sell-in 10} - @{ :name "Backstage passes to a TAFKAL80ETC concert" :quality 49 :sell-in 5} - @{ :name "Conjured Mana Cake" :quality 6 :sell-in 3}]) - (for i 0 num-days + (for i 0 (+ 1 days) (do (print (string "-------- day " i " --------")) (print "name, sellIn, quality") diff --git a/janet/test/gilded-rose.janet b/janet/test/gilded-rose.janet index fdb04cab..789f3f50 100644 --- a/janet/test/gilded-rose.janet +++ b/janet/test/gilded-rose.janet @@ -1,10 +1,7 @@ -(import ../gilded-rose/main) +(import ../src/main :as shop) (use judge) (do - (def items - [@{ :name "foo" :quality 3 :sell-in 5} - @{ :name "bar" :quality 6 :sell-in 6}]) - (main/update-quality items) - (def found (find |(= ($ :quality) 2) items)) - (test (found :name) "fixme")) \ No newline at end of file + (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))