Merge pull request #455 from add1ed/janet

add janet version
This commit is contained in:
Emily Bache 2023-07-14 06:57:56 +02:00 committed by GitHub
commit c03808272e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 130 additions and 0 deletions

10
janet/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
*
!.gitignore
!project.janet
!README.md
!src/
!src/main.janet
!test/
!test/*.janet

39
janet/README.md Normal file
View File

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

6
janet/project.janet Normal file
View File

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

46
janet/src/main.janet Normal file
View File

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

View File

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

22
janet/test/texttest.janet Normal file
View File

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