Skip to content

Commit

Permalink
Use tests.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Jan 10, 2025
1 parent a8f6ac2 commit 9009d81
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions generators/src/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,74 @@
(println message)
(System/exit 1))

(def prob-specs-dir ".problem-specifications")
(def root-dir (.getCanonicalPath (io/file *file* ".." ".." "..")))
(def prob-specs-dir (io/file root-dir ".problem-specifications"))
(defn exercise-dir [slug] (io/file root-dir "exercises" "practice" slug))

(defn sync-prob-specs []
(if (.isDirectory (io/file prob-specs-dir))
(if (.isDirectory prob-specs-dir)
(sh "git" "pull" :dir prob-specs-dir)
(sh "git" "clone" "https://github.com/exercism/problem-specifications.git" prob-specs-dir)))

(defn canonical-data-file [slug] (io/file prob-specs-dir "exercises" slug "canonical-data.json"))
(defn canonical-data [slug]
(let [file (io/file prob-specs-dir "exercises" slug "canonical-data.json")]
(let [file (canonical-data-file slug)]
(if (.exists file)
(json/read (io/reader file) :key-fn keyword)
(error (str "No canonical data found for slug '" slug "'")))))
(error (str "No canonical-data.json found for exercise '" slug "'")))))

(defn filter-reimplemented [case-nodes]
(let [reimplemented (set (remove nil? (map #(:reimplements %) case-nodes)))]
(remove #(contains? reimplemented (:uuid %)) case-nodes)))
(defn tests-toml-file [slug] (io/file (exercise-dir slug) ".meta" "tests.toml"))
(defn excluded-uuids [slug]
(let [file (tests-toml-file slug)]
(if (.exists file)
(->> file
(io/reader)
(toml/read)
(filter #(= false (get (last %) "include")))
(map first)
(set))
(error (str "No tests.toml data found for exercise '" slug "'")))))

(defn excluded? [slug]
(let [excluded (excluded-uuids slug)]
(fn [node] (contains? excluded (:uuid node)))))

(defn node->case [node]
(defn node->test-case [node path]
(-> node
(assoc :error (get-in node [:expected :error]))
(assoc :path path :error (get-in node [:expected :error]))
(dissoc :reimplements :comments :scenarios)))

(defn case-nodes
([node] (case-nodes node []))
(defn test-case-nodes
([node] (test-case-nodes node []))
([node path]
(let [description (:description node)
children (:cases node)
updated-path (if description (conj path description) path)]
(if children
(mapcat #(case-nodes % updated-path) children)
[(assoc node :path updated-path)]))))

(defn cases [node]
(->> node
(case-nodes)
(filter-reimplemented)
(map node->case)
(mapcat #(test-case-nodes % updated-path) children)
[(node->test-case node updated-path)]))))

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

(sync-prob-specs)
(cases (canonical-data "isogram"))
(test-cases "isogram")

(def data {:slug "isogram"
:cases (cases (canonical-data "isogram"))})

(def template "/Users/erik/Code/exercism/tracks/clojure/exercises/practice/collatz-conjecture/.meta/tests.template")
(def tests "/Users/erik/Code/exercism/tracks/clojure/exercises/practice/collatz-conjecture/.meta/tests.clj")

(def toml-file "/home/erik/exercism/tracks/clojure/exercises/practice/collatz-conjecture/.meta/tests.toml")

(spit tests (render (slurp template) data))

(def t (toml/read (io/reader toml-file)))
(set (map first (filter #(not= false (get (last %) "include")) t)))

(.getAbsolutePath (io/file *file* ".." ".."))
(.getCanonicalPath (io/file *file* ".." ".." ".."))

0 comments on commit 9009d81

Please sign in to comment.