Skip to content

Commit

Permalink
Change library for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Jan 10, 2025
1 parent 750a785 commit 66cb526
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
3 changes: 2 additions & 1 deletion exercises/practice/isogram/.meta/example.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
(:require [clojure.string :as str]))

(defn isogram? [word]
(apply distinct? (filter #(Character/isLetter %) (str/lower-case word))))
(let [letters (filter #(Character/isLetter %) (str/lower-case word))]
(or (empty? letters) (apply distinct? letters))))
17 changes: 12 additions & 5 deletions exercises/practice/isogram/.meta/generator.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
(ns isogram-test
(:require [clojure.test :refer [deftest testing is]]
isogram))
{% for test_case in test_cases %}
(deftest isogram_test_{{forloop.counter}}
(testing "{{test_case.path|join:" - "}}"
{% if test_case.expected %}(is (isogram/isogram? "{{test_case.input.phrase}}")))) {% else %}(is (not (isogram/isogram? "{{test_case.input.phrase}}")))){% endif %}
{% endfor %}

{{#test_cases}}
(deftest isogram_test_{{idx}}
(testing "{{description}}"
{{#expected}}
(is (isogram/isogram? "{{input.phrase}}"))))
{{/expected}}
{{^expected}}
(is (not (isogram/isogram? "{{input.phrase}}")))))
{{/expected}}

{{/test_cases}}
28 changes: 14 additions & 14 deletions exercises/practice/isogram/test/isogram_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,57 @@

(deftest isogram_test_1
(testing "empty string"
(is (isogram/isogram? ""))))
(is (isogram/isogram? ""))))

(deftest isogram_test_2
(testing "isogram with only lower case characters"
(is (isogram/isogram? "isogram"))))
(is (isogram/isogram? "isogram"))))

(deftest isogram_test_3
(testing "word with one duplicated character"
(is (not (isogram/isogram? "eleven"))))
(is (not (isogram/isogram? "eleven")))))

(deftest isogram_test_4
(testing "word with one duplicated character from the end of the alphabet"
(is (not (isogram/isogram? "zzyzx"))))
(is (not (isogram/isogram? "zzyzx")))))

(deftest isogram_test_5
(testing "longest reported english isogram"
(is (isogram/isogram? "subdermatoglyphic"))))
(is (isogram/isogram? "subdermatoglyphic"))))

(deftest isogram_test_6
(testing "word with duplicated character in mixed case"
(is (not (isogram/isogram? "Alphabet"))))
(is (not (isogram/isogram? "Alphabet")))))

(deftest isogram_test_7
(testing "word with duplicated character in mixed case, lowercase first"
(is (not (isogram/isogram? "alphAbet"))))
(is (not (isogram/isogram? "alphAbet")))))

(deftest isogram_test_8
(testing "hypothetical isogrammic word with hyphen"
(is (isogram/isogram? "thumbscrew-japingly"))))
(is (isogram/isogram? "thumbscrew-japingly"))))

(deftest isogram_test_9
(testing "hypothetical word with duplicated character following hyphen"
(is (not (isogram/isogram? "thumbscrew-jappingly"))))
(is (not (isogram/isogram? "thumbscrew-jappingly")))))

(deftest isogram_test_10
(testing "isogram with duplicated hyphen"
(is (isogram/isogram? "six-year-old"))))
(is (isogram/isogram? "six-year-old"))))

(deftest isogram_test_11
(testing "made-up name that is an isogram"
(is (isogram/isogram? "Emily Jung Schwartzkopf"))))
(is (isogram/isogram? "Emily Jung Schwartzkopf"))))

(deftest isogram_test_12
(testing "duplicated character in the middle"
(is (not (isogram/isogram? "accentor"))))
(is (not (isogram/isogram? "accentor")))))

(deftest isogram_test_13
(testing "same first and last characters"
(is (not (isogram/isogram? "angola"))))
(is (not (isogram/isogram? "angola")))))

(deftest isogram_test_14
(testing "word with duplicated character and with two hyphens"
(is (not (isogram/isogram? "up-to-date"))))
(is (not (isogram/isogram? "up-to-date")))))

12 changes: 7 additions & 5 deletions exercises/practice/two-fer/.meta/generator.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
(ns two-fer-test
(:require [clojure.test :refer [deftest testing is]]
two-fer))
{% for test_case in test_cases %}
(deftest two-fer_test_{{forloop.counter}}
(testing "{{test_case.path|join:" - "}}"
(is (= "{{test_case.expected}}" (two-fer/two-fer{% if test_case.input.name %} "{{test_case.input.name}}"{% endif %})))))
{% endfor %}

{{#test_cases}}
(deftest two-fer_test_{{idx}}
(testing "{{description}}"
(is (= "{{expected}}" (two-fer/two-fer{{#input.name}} "{{input.name}}"{{/input.name}})))))

{{/test_cases}}
4 changes: 2 additions & 2 deletions generators/deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:paths ["src"]
:deps {org.clojure/data.json {:mvn/version "2.5.1"}
selmer/selmer {:mvn/version "1.12.61"}
pogonos/pogonos {:mvn/version "0.2.1"}
io.github.tonsky/toml-clj {:mvn/version "0.1.0"}
clj-jgit/clj-jgit {:mvn/version "1.1.0"}}}
clj-jgit/clj-jgit {:mvn/version "1.1.0"}}}
13 changes: 8 additions & 5 deletions generators/src/canonical_data.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[toml-clj.core :as toml]
[clj-jgit.porcelain :refer [git-clone git-pull load-repo]]
[log]
[paths]))
[paths]
[clojure.string :as str]))

(def git-url "https://github.com/exercism/problem-specifications.git")

Expand Down Expand Up @@ -41,9 +42,11 @@
(let [excluded (excluded-uuids slug)]
(fn [node] (contains? excluded (:uuid node)))))

(defn- node->test-case [node path]
(defn- node->test-case [idx node]
(-> node
(assoc :path path :error (get-in node [:expected :error]))
(assoc :idx (inc idx)
:description (str/join " - " (:path node))
:error (get-in node [:expected :error]))
(dissoc :reimplements :comments :scenarios)))

(defn- test-case-nodes
Expand All @@ -54,12 +57,12 @@
updated-path (if description (conj path description) path)]
(if children
(mapcat #(test-case-nodes % updated-path) children)
[(node->test-case node updated-path)]))))
[(assoc node :path updated-path)]))))

(defn test-cases [slug]
(->> slug
(canonical-data)
(test-case-nodes)
(remove (excluded? slug))
(map-indexed node->test-case)
(into [])))

17 changes: 6 additions & 11 deletions generators/src/templates.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns templates
(:require [selmer.parser :as selmer]
(:require [pogonos.core :as pg]
[pogonos.output :as output]
[log]
[paths]))

Expand All @@ -11,14 +12,8 @@
(map #(-> % (.getParentFile) (.getParentFile) (.getName)))
(set)))

(defn- render-template [data template]
(selmer/render (slurp template) data))

(defn- render [slug test-cases]
(let [data {:slug slug :test_cases test-cases}]
(render-template data (paths/generator-template-file slug))))

(defn generate-tests-file [slug test-cases]
(->> test-cases
(render slug)
(spit (paths/tests-file slug))))
(pg/render-file
(paths/generator-template-file slug)
{:test_cases test-cases}
{:output (output/to-file (paths/tests-file slug))}))

0 comments on commit 66cb526

Please sign in to comment.