* Amendments

- Initial test modified to be similar to Clojure version
- update-quality modified to read from make-item directly. Helper methods removed
This commit is contained in:
mgd 2024-10-07 21:22:57 +01:00 committed by Peter Kofler
parent 3615cfb8ab
commit 91ebc85a70
3 changed files with 15 additions and 26 deletions

View File

@ -1,9 +1,10 @@
(require 'ert)
(require 'gilded-rose)
(defconst foo (make-item "foo" 20 10))
(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"))))
(should (string= "fixme" (plist-get foo :name))))
(ert-run-tests-interactively t)

View File

@ -2,37 +2,22 @@
"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))
(sell-in (item-sell-in item)))
(let* ((quality (plist-get item :quality))
(sell-in (plist-get item :sell-in))
(name (plist-get item :name)))
(cond
;; Aged Brie
((string= (item-name item) "Aged Brie")
((string= name "Aged Brie")
(setf (nth 2 item) (min 50 (1+ quality))))
;; Backstage passes
((string= (item-name item) "Backstage passes")
((string= name "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")
((string= name "Sulfuras")
(setf (nth 2 item) quality))
(t
(setf (nth 2 item) (max 0 (1- quality)))))))
(provide 'gilded-rose)

View File

@ -8,3 +8,6 @@ Gilded Rose kata in Elisp!
- 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)~
* Todo
- Add TestText fixture