Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Elisp version of the kata #572

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions elisp/gilded-rose-test.el
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this test similar to other sample tests, e.g. https://github.com/emilybache/GildedRose-Refactoring-Kata/blob/main/clojure/test/gilded/core_test.clj
i.e. check that the name of "foo" is not "fixme".

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(require 'ert)
(require 'gilded-rose)

(ert-deftest check-name-of-item ()
"Test that item is called sweets. This test should fail!"
(let ((sweets (make-item "sweets" 5 5)))
(should (string= (item-name sweets) "You should change this"))))

(ert-run-tests-interactively t)
38 changes: 38 additions & 0 deletions elisp/gilded-rose.el
MGadhvi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(defun make-item (name sell-in quality)
"Create an item with NAME, SELL-IN, and QUALITY."
(list :name name :sell-in sell-in :quality quality))

(defun item-name (item)
"Return the name of ITEM."
(plist-get item :name))

(defun item-sell-in (item)
"Return the sell-in value of ITEM."
(plist-get item :sell-in))

(defun item-quality (item)
"Return the quality of ITEM."
(plist-get item :quality))

(defun update-quality (item)
"Update the quality of the ITEM according to the Gilded Rose rules."
(let ((quality (item-quality item))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the lets are already a simplification/improvement of the original code. Is it possible to access the value from the struct each time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. This was the simplest way I could represent the problem with the elisp I know. I'll have a look at getting the value from the struct each time

(sell-in (item-sell-in item)))
(cond
;; Aged Brie
((string= (item-name item) "Aged Brie")
(setf (nth 2 item) (min 50 (1+ quality))))

;; Backstage passes
((string= (item-name item) "Backstage passes")
(cond
((> sell-in 10) (setf (nth 2 item) quality))
((and (<= sell-in 10) (> sell-in 5)) (setf (nth 2 item) (min 50 (+ quality 2))))
((and (<= sell-in 5) (> sell-in 0)) (setf (nth 2 item) (min 50 (+ quality 3))))
(t (setf (nth 2 item) 0))))

;; Sulfuras
((string= (item-name item) "Sulfuras")
(setf (nth 2 item) quality))

(provide 'gilded-rose)
10 changes: 10 additions & 0 deletions elisp/readme.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* Gilded Rose
Gilded Rose kata in Elisp!

* Structure
~gilded-rose.el~ contains source code. ~gilded-rose-test.el~ uses [[https://www.gnu.org/software/emacs/manual/html_mono/ert.html][ert]] to run tests

* Running tests
- Use the command ~eval-buffer~ on ~gilded-rose.el~
- Use the command ~eval-buffer~ on ~gilded-rose-test.el~
- Test can be run interactively using the command ~ert~ or with ~(ert-run-tests-interactively t)~