Skip to content

Commit

Permalink
diamond: sync tests (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Jan 22, 2025
1 parent 03f35fd commit 20a6f4e
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 79 deletions.
5 changes: 3 additions & 2 deletions exercises/practice/diamond/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns diamond)
(ns diamond
(:require [clojure.string :as str]))

(def alphabet (map char (range (int \A) (inc (int \Z)))))

Expand All @@ -18,5 +19,5 @@
(defn diamond [c]
(let [num-letters (- (int c) (dec (int \A)))
top-half (map one-row alphabet (row-paddings num-letters))]
(concat top-half (rest (reverse top-half)))))
(str/join "\n" (concat top-half (rest (reverse top-half))))))

4 changes: 4 additions & 0 deletions exercises/practice/diamond/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns diamond-generator)

(defn update-test-case [test-case]
(update-in test-case [:input :letter] first))
12 changes: 12 additions & 0 deletions exercises/practice/diamond/.meta/generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns diamond-test
(:require [clojure.test :refer [deftest testing is]]
[clojure.string :as str]
diamond))

{{#test_cases.rows}}
(deftest diamond_test_{{idx}}
(testing {{description}}
(is (= (str/join "\n" [{{#expected}}
{{.}}{{/expected}}])
(diamond/diamond {{input.letter}})))))
{{/test_cases.rows}}
8 changes: 5 additions & 3 deletions exercises/practice/diamond/src/diamond.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns diamond)

(defn diamond [] ;; <- arglist goes here
;; your code goes here
)
(defn diamond
"Returns the diamond shape for a letter."
[letter]
;; function body
)
160 changes: 86 additions & 74 deletions exercises/practice/diamond/test/diamond_test.clj
Original file line number Diff line number Diff line change
@@ -1,79 +1,91 @@
(ns diamond-test
(:require [clojure.test :refer [deftest is are]]
[diamond :refer [diamond]]))
(:require [clojure.test :refer [deftest testing is]]
[clojure.string :as str]
diamond))

(deftest single-a-row
(is (= (diamond \A) ["A"])))
(deftest diamond_test_1
(testing "Degenerate case with a single 'A' row"
(is (= (str/join "\n" ["A"])
(diamond/diamond \A)))))

(deftest b-diamond
(is (= (diamond \B) [" A "
"B B"
" A "])))
(deftest c-diamond
(is (= (diamond \C) [" A "
" B B "
"C C"
" B B "
" A "])))
(deftest diamond_test_2
(testing "Degenerate case with no row containing 3 distinct groups of spaces"
(is (= (str/join "\n" [" A "
"B B"
" A "])
(diamond/diamond \B)))))

(deftest d-diamond
(is (= (diamond \D) [" A "
" B B "
" C C "
"D D"
" C C "
" B B "
" A "])))
(deftest diamond_test_3
(testing "Smallest non-degenerate case with odd diamond side length"
(is (= (str/join "\n" [" A "
" B B "
"C C"
" B B "
" A "])
(diamond/diamond \C)))))

(deftest full-z-diamond
(is (= (diamond \Z) [" A "
" B B "
" C C "
" D D "
" E E "
" F F "
" G G "
" H H "
" I I "
" J J "
" K K "
" L L "
" M M "
" N N "
" O O "
" P P "
" Q Q "
" R R "
" S S "
" T T "
" U U "
" V V "
" W W "
" X X "
" Y Y "
"Z Z"
" Y Y "
" X X "
" W W "
" V V "
" U U "
" T T "
" S S "
" R R "
" Q Q "
" P P "
" O O "
" N N "
" M M "
" L L "
" K K "
" J J "
" I I "
" H H "
" G G "
" F F "
" E E "
" D D "
" C C "
" B B "
" A "])))
(deftest diamond_test_4
(testing "Smallest non-degenerate case with even diamond side length"
(is (= (str/join "\n" [" A "
" B B "
" C C "
"D D"
" C C "
" B B "
" A "])
(diamond/diamond \D)))))

(deftest diamond_test_5
(testing "Largest possible diamond"
(is (= (str/join "\n" [" A "
" B B "
" C C "
" D D "
" E E "
" F F "
" G G "
" H H "
" I I "
" J J "
" K K "
" L L "
" M M "
" N N "
" O O "
" P P "
" Q Q "
" R R "
" S S "
" T T "
" U U "
" V V "
" W W "
" X X "
" Y Y "
"Z Z"
" Y Y "
" X X "
" W W "
" V V "
" U U "
" T T "
" S S "
" R R "
" Q Q "
" P P "
" O O "
" N N "
" M M "
" L L "
" K K "
" J J "
" I I "
" H H "
" G G "
" F F "
" E E "
" D D "
" C C "
" B B "
" A "])
(diamond/diamond \Z)))))
3 changes: 3 additions & 0 deletions generators/src/formatting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
(defn format-code [code]
(reformat-string code))

(defn format-char [c _next]
(str "\\" c))

(defn format-string [s _next]
(str "\"" (str/escape s char-escape-string) "\""))

Expand Down
1 change: 1 addition & 0 deletions generators/src/templates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
(. with (formatting/formatter set? formatting/format-set))
(. with (formatting/formatter list? formatting/format-list))
(. with (formatting/formatter string? formatting/format-string))
(. with (formatting/formatter char? formatting/format-char))
(. with EscapingStrategy/NOOP)))

(defhelper ifzero [ctx options]
Expand Down

0 comments on commit 20a6f4e

Please sign in to comment.