Skip to content

Commit c0b457b

Browse files
Add individual test case transform exit point (#763)
1 parent f777a9a commit c0b457b

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

docs/GENERATORS.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,50 @@ The test generator will remove any test cases that are marked as excluded (`incl
3131
Some exercises might need some tweaks before rendering the data.
3232
For example, you might want to make the description less verbose.
3333

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

37-
Example:
37+
- Update test case(s)
38+
- Add/remove test case(s)
39+
40+
#### Update test case(s)
41+
42+
To update individual test cases, define the following function:
43+
44+
```clojure
45+
(defn- transform-test-case
46+
"Update a test case"
47+
[test-case]
48+
;; function body
49+
)
50+
```
51+
52+
##### Example
53+
54+
This example removes all but the last element of the `:path` value (shortening the description):
3855

3956
```clojure
4057
(ns difference-of-squares-generator)
4158

42-
(defn- update-path [path]
43-
(take-last 1 path))
59+
(defn- transform-test-case [test-case]
60+
(update test-case :path #(take-last 1 %)))
61+
```
62+
63+
#### Add or remove test case(s)
4464

45-
(defn transform [test-cases]
46-
(map #(update % :path update-path) test-cases))
65+
To update individual test cases, define the following function:
66+
67+
```clojure
68+
(defn- transform-test-cases
69+
"Add/remove test case(s)"
70+
[test-cases]
71+
;; function body
72+
)
4773
```
4874

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

5179
### Step 4: render the test cases
5280

generators/src/templates.clj

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,24 @@
5454
:error (get-in node [:expected :error]))
5555
(dissoc :reimplements :comments :scenarios)))
5656

57+
(defn- transform-all-test-cases [generator-ns test-cases]
58+
(if-let [transform-fn (ns-resolve generator-ns (symbol "transform"))]
59+
(transform-fn test-cases)
60+
test-cases))
61+
62+
(defn- transform-individual-test-cases [generator-ns test-cases]
63+
(if-let [transform-test-case-fn (ns-resolve generator-ns (symbol "transform-test-case"))]
64+
(mapv transform-test-case-fn test-cases)
65+
test-cases))
66+
5767
(defn- transform [slug test-cases]
5868
(let [transform-file (paths/generator-clojure-file slug)]
5969
(if (.exists transform-file)
6070
(let [generator-ns (symbol (str slug "-generator"))]
6171
(load-file (str transform-file))
62-
(if-let [transform-fn (ns-resolve generator-ns (symbol "transform"))]
63-
(transform-fn test-cases)
64-
test-cases))
72+
(->> test-cases
73+
(transform-all-test-cases generator-ns)
74+
(transform-individual-test-cases generator-ns)))
6575
test-cases)))
6676

6777
(defn- test-cases->data [slug test-cases]

0 commit comments

Comments
 (0)