Last active
October 11, 2019 00:57
-
-
Save heypoom/75fd6eebf8e489c27b2dd77a26f7e1a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def ranks [2 3 4 5 6 7 8 9 0 "J" "Q" "K" "A"]) | |
(def suits ["C" "D" "H" "S"]) | |
(defn card-at | |
"Retrieves the deck of card at the nth position" | |
[n] | |
(if (< n 52) | |
(str | |
(ranks (mod n 13)) | |
(suits | |
(int (/ n 13)))) | |
(throw | |
(ex-info "Deck of cards should not be more than 51." | |
{:type :validation-failed})))) | |
;; Unit Tests for the cardAt method. | |
(def test-cases [ | |
[0 "2C"] | |
[1 "3C"] | |
[34 "0H"] | |
[35 "JH"] | |
[51 "AS"] | |
]) | |
(defn test-card-at | |
"Test the card-at method with the given input array" | |
[case] | |
(= | |
(card-at (first case)) | |
(last case))) | |
;; Iterate over the test cases and see if they pass. | |
"Does every test case pass?" | |
(if | |
(every? | |
true? | |
(map test-card-at test-cases)) | |
"Yes!" "No.")) | |
;; Invoke card-at directly in the REPL to see if the result is expected. | |
(card-at 0) | |
(card-at 1) | |
(card-at 34) | |
(card-at 35) | |
(card-at 51) | |
;; Ideally, we should test for exceptions too in the case that | |
;; the card exceeds 51, but that would | |
;; clutter the REPL due to the way repl.it works: | |
;; | |
;; (is (thrown-with-msg? | |
;; clojure.lang.ExceptionInfo | |
;; #"Deck of cards should not be more than" | |
;; (card-at 52))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment