Skip to content

Commit

Permalink
Add individual test case transform exit point (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Jan 15, 2025
1 parent f777a9a commit c0b457b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
44 changes: 36 additions & 8 deletions docs/GENERATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,50 @@ The test generator will remove any test cases that are marked as excluded (`incl
Some exercises might need some tweaks before rendering the data.
For example, you might want to make the description less verbose.

To tweak the test cases, define a `.meta/generator.clj` file with a `<slug>-generator` namespace .
Then, define a function called `transform` that takes a single argument — the parsed test cases — and returns the transformed test cases.
To tweak test cases, define a `.meta/generator.clj` file with a `<slug>-generator` namespace.
There are two ways in which you can transform test cases:

Example:
- Update test case(s)
- Add/remove test case(s)

#### Update test case(s)

To update individual test cases, define the following function:

```clojure
(defn- transform-test-case
"Update a test case"
[test-case]
;; function body
)
```

##### Example

This example removes all but the last element of the `:path` value (shortening the description):

```clojure
(ns difference-of-squares-generator)

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

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

(defn transform [test-cases]
(map #(update % :path update-path) test-cases))
To update individual test cases, define the following function:

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

This step is entirely optional.
```exercism/note
If you define _both_ functions, `transform-test-cases` is called first and `transform-test-case` second.
```

### Step 4: render the test cases

Expand Down
16 changes: 13 additions & 3 deletions generators/src/templates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@
: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)
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)
test-cases))

(defn- transform [slug test-cases]
(let [transform-file (paths/generator-clojure-file slug)]
(if (.exists transform-file)
(let [generator-ns (symbol (str slug "-generator"))]
(load-file (str transform-file))
(if-let [transform-fn (ns-resolve generator-ns (symbol "transform"))]
(transform-fn test-cases)
test-cases))
(->> test-cases
(transform-all-test-cases generator-ns)
(transform-individual-test-cases generator-ns)))
test-cases)))

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

0 comments on commit c0b457b

Please sign in to comment.