Skip to content

Commit

Permalink
Improve naming of extension functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Jan 18, 2025
1 parent c666ad5 commit 1176404
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 25 deletions.
12 changes: 6 additions & 6 deletions docs/GENERATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It uses the fact that most exercises defined in the [problem-specifications repo

To generate a practice exercise's tests, the test generator:

1. Reads the exercise's test cases from its [`canonical-data.json` file]
1. Reads the exercise's test cases from its `canonical-data.json` file
2. Uses `tests.toml` file to omit and excluded test cases
3. Transforms the test cases (optional)
4. Renders the test cases using the exercise's generator template
Expand Down Expand Up @@ -42,7 +42,7 @@ There are two ways in which you can transform test cases:
To update individual test cases, define the following function:

```clojure
(defn- transform-test-case
(defn update-test-case
"Update a test case"
[test-case]
;; function body
Expand All @@ -56,24 +56,24 @@ This example removes all but the last element of the `:path` value (shortening t
```clojure
(ns difference-of-squares-generator)

(defn- transform-test-case [test-case]
(defn update-test-case [test-case]
(update test-case :path #(take-last 1 %)))
```

#### Add or remove test case(s)

To update individual test cases, define the following function:
To add or remove test cases, define the following function:

```clojure
(defn- transform-test-cases
(defn add-remove-test-cases
"Add/remove test case(s)"
[test-cases]
;; function body
)
```

```exercism/note
If you define _both_ functions, `transform-test-cases` is called first and `transform-test-case` second.
If you define _both_ functions, `add-remove-test-cases` is called first and `update-test-case` second.
```

### Step 4: render the test cases
Expand Down
7 changes: 2 additions & 5 deletions exercises/practice/difference-of-squares/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
(ns difference-of-squares-generator)

(defn- update-path [path]
(take-last 1 path))

(defn transform [test-cases]
(map #(update % :path update-path) test-cases))
(defn update-test-case [test-case]
(update test-case :path #(take-last 1 %)))
2 changes: 1 addition & 1 deletion exercises/practice/high-scores/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns high-scores-generator)

(defn- transform-test-case [test-case]
(defn update-test-case [test-case]
(-> test-case
(update-in [:input :scores] #(apply list %))
(update-in [:expected] #(if (vector? %) (apply list %) %))))
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns largest-series-product-generator)

(defn transform-test-case [test-case]
(defn update-test-case [test-case]
(if-let [error (get-in test-case [:expected :error])]
(assoc-in test-case [:expected :error] (str "^" error "$"))
test-case))
2 changes: 1 addition & 1 deletion exercises/practice/saddle-points/.meta/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(defn- transform-input [input]
(update input :matrix #(if (empty? (flatten %)) [] %)))

(defn- transform-test-case [test-case]
(defn update-test-case [test-case]
(-> test-case
(update :input transform-input)
(update :expected transform-expected)))
2 changes: 1 addition & 1 deletion exercises/practice/sum-of-multiples/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sum-of-multiples-generator)

(defn- transform-test-case [test-case]
(defn update-test-case [test-case]
(update-in test-case [:input :factors] #(apply list %)))
2 changes: 1 addition & 1 deletion exercises/practice/wordy/.meta/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"^syntax error$"
(str "^" error "$")))

(defn transform-test-case [test-case]
(defn update-test-case [test-case]
(if-let [error (get-in test-case [:expected :error])]
(assoc-in test-case [:expected :error] (normalize-error test-case error))
test-case))
2 changes: 1 addition & 1 deletion exercises/practice/zebra-puzzle/.meta/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
(:require [hbs.helper :refer [safe-str]]
[clojure.string :as str]))

(defn transform-test-case [test-case]
(defn update-test-case [test-case]
(update test-case :expected #(safe-str (str/lower-case (keyword %)))))
16 changes: 8 additions & 8 deletions generators/src/templates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
:error (get-in node [:expected :error]))
(dissoc :reimplements :comments :scenarios)))

(defn- transform-all-test-cases [generator-ns test-cases]
(if-let [transform-fn (ns-resolve generator-ns (symbol "transform"))]
(transform-fn test-cases)
(defn- add-remove-test-cases [generator-ns test-cases]
(if-let [add-remove-test-cases-fn (ns-resolve generator-ns (symbol "add-remove-test-cases"))]
(add-remove-test-cases-fn test-cases)
test-cases))

(defn- transform-individual-test-cases [generator-ns test-cases]
(if-let [transform-test-case-fn (ns-resolve generator-ns (symbol "transform-test-case"))]
(mapv transform-test-case-fn test-cases)
(defn- update-test-cases [generator-ns test-cases]
(if-let [update-test-case-fn (ns-resolve generator-ns (symbol "update-test-case"))]
(mapv update-test-case-fn test-cases)
test-cases))

(defn- transform [slug test-cases]
Expand All @@ -77,8 +77,8 @@
(let [generator-ns (symbol (str slug "-generator"))]
(load-file (str transform-file))
(->> test-cases
(transform-all-test-cases generator-ns)
(transform-individual-test-cases generator-ns)))
(add-remove-test-cases generator-ns)
(update-test-cases generator-ns)))
test-cases)))

(defn- test-cases->data [slug test-cases]
Expand Down

0 comments on commit 1176404

Please sign in to comment.