Skip to content

Commit

Permalink
Auto-format code rendered from template (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Jan 19, 2025
1 parent f4c2373 commit ad4007a
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 121 deletions.
13 changes: 11 additions & 2 deletions docs/GENERATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ To generate a practice exercise's tests, the test generator:
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
5. Writes the rendered template to the exercise's test file
5. Format the rendered template using [cljfmt](https://github.com/weavejester/cljfmt)
6. Writes the formatted template to the exercise's test file

### Step 1: read `canonical-data.json` file

Expand Down Expand Up @@ -80,7 +81,15 @@ If you define _both_ functions, `add-remove-test-cases` is called first and `upd

The (potentially transformed) test cases are then passed to the `.meta/generator.tpl` file, which defines how the tests should be rendered based on those test cases.

### Step 5: write the rendered template to the exercise's test file
### Step 5: format the rendered template using cljfmt

The rendered template is then formatted using [cljfmt](https://github.com/weavejester/cljfmt).
This has the following benefits:

- Exercises are formatted consistently
- You're not required to worry much about whitespace and alignment when writing templates

### Step 6: write the rendered template to the exercise's test file

Finally, the output of the rendered template is written to the exercise's test file.

Expand Down
25 changes: 12 additions & 13 deletions exercises/practice/change/test/change_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,63 @@
(testing "change for 1 cent"
(is (= '(1)
(change/issue 1 #{1 5 10 25})))))

(deftest issue_test_2
(testing "single coin change"
(is (= '(25)
(change/issue 25 #{1 5 10 25 100})))))

(deftest issue_test_3
(testing "multiple coin change"
(is (= '(5 10)
(change/issue 15 #{1 5 10 25 100})))))

(deftest issue_test_4
(testing "change with Lilliputian Coins"
(is (= '(4 4 15)
(change/issue 23 #{1 4 15 20 50})))))

(deftest issue_test_5
(testing "change with Lower Elbonia Coins"
(is (= '(21 21 21)
(change/issue 63 #{1 5 10 21 25})))))

(deftest issue_test_6
(testing "large target values"
(is (= '(2 2 5 20 20 50 100 100 100 100 100 100 100 100 100)
(change/issue 999 #{1 2 5 10 20 50 100})))))

(deftest issue_test_7
(testing "possible change without unit coins available"
(is (= '(2 2 2 5 10)
(change/issue 21 #{2 5 10 20 50})))))

(deftest issue_test_8
(testing "another possible change without unit coins available"
(is (= '(4 4 4 5 5 5)
(change/issue 27 #{4 5})))))

(deftest issue_test_9
(testing "a greedy approach is not optimal"
(is (= '(10 10)
(change/issue 20 #{1 10 11})))))

(deftest issue_test_10
(testing "no coins make 0 change"
(is (= '()
(change/issue 0 #{1 5 10 21 25})))))

(deftest issue_test_11
(testing "error testing for change smaller than the smallest of coins"
(is (thrown-with-msg? IllegalArgumentException #"^can't make target with given coins$"
(change/issue 3 #{5 10})))))

(deftest issue_test_12
(testing "error if no combination can add up to target"
(is (thrown-with-msg? IllegalArgumentException #"^can't make target with given coins$"
(change/issue 94 #{5 10})))))

(deftest issue_test_13
(testing "cannot find negative change values"
(is (thrown-with-msg? IllegalArgumentException #"^target can't be negative$"
(change/issue -5 #{1 2 5})))))

Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,51 @@
(deftest largest-product_test_1
(testing "finds the largest product if span equals length"
(is (= 18 (largest-series-product/largest-product 2 "29")))))

(deftest largest-product_test_2
(testing "can find the largest product of 2 with numbers in order"
(is (= 72 (largest-series-product/largest-product 2 "0123456789")))))

(deftest largest-product_test_3
(testing "can find the largest product of 2"
(is (= 48 (largest-series-product/largest-product 2 "576802143")))))

(deftest largest-product_test_4
(testing "can find the largest product of 3 with numbers in order"
(is (= 504 (largest-series-product/largest-product 3 "0123456789")))))

(deftest largest-product_test_5
(testing "can find the largest product of 3"
(is (= 270 (largest-series-product/largest-product 3 "1027839564")))))

(deftest largest-product_test_6
(testing "can find the largest product of 5 with numbers in order"
(is (= 15120 (largest-series-product/largest-product 5 "0123456789")))))

(deftest largest-product_test_7
(testing "can get the largest product of a big number"
(is (= 23520 (largest-series-product/largest-product 6 "73167176531330624919225119674426574742355349194934")))))

(deftest largest-product_test_8
(testing "reports zero if the only digits are zero"
(is (= 0 (largest-series-product/largest-product 2 "0000")))))

(deftest largest-product_test_9
(testing "reports zero if all spans include zero"
(is (= 0 (largest-series-product/largest-product 3 "99099")))))

(deftest largest-product_test_10
(testing "rejects span longer than string length"
(is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 4 "123")))))

(deftest largest-product_test_11
(testing "rejects empty string and nonzero span"
(is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 1 "")))))

(deftest largest-product_test_12
(testing "rejects invalid character in digits"
(is (thrown-with-msg? IllegalArgumentException #"^digits input must only contain digits$" (largest-series-product/largest-product 2 "1234a5")))))

(deftest largest-product_test_13
(testing "rejects negative span"
(is (thrown-with-msg? IllegalArgumentException #"^span must not be negative$" (largest-series-product/largest-product -1 "12345")))))

63 changes: 23 additions & 40 deletions exercises/practice/saddle-points/test/saddle_points_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,66 @@
(testing "Can identify single saddle point"
(is (= #{[2 1]}
(saddle-points/saddle-points
[
[9 8 7]
[5 3 2]
[6 6 7]
])))))
[[9 8 7]
[5 3 2]
[6 6 7]])))))

(deftest saddle-points_test_2
(testing "Can identify that empty matrix has no saddle points"
(is (= #{}
(saddle-points/saddle-points
[
])))))
[])))))

(deftest saddle-points_test_3
(testing "Can identify lack of saddle points when there are none"
(is (= #{}
(saddle-points/saddle-points
[
[1 2 3]
[3 1 2]
[2 3 1]
])))))
[[1 2 3]
[3 1 2]
[2 3 1]])))))

(deftest saddle-points_test_4
(testing "Can identify multiple saddle points in a column"
(is (= #{[2 2] [1 2] [3 2]}
(saddle-points/saddle-points
[
[4 5 4]
[3 5 5]
[1 5 4]
])))))
[[4 5 4]
[3 5 5]
[1 5 4]])))))

(deftest saddle-points_test_5
(testing "Can identify multiple saddle points in a row"
(is (= #{[2 2] [2 3] [2 1]}
(saddle-points/saddle-points
[
[6 7 8]
[5 5 5]
[7 5 6]
])))))
[[6 7 8]
[5 5 5]
[7 5 6]])))))

(deftest saddle-points_test_6
(testing "Can identify saddle point in bottom right corner"
(is (= #{[3 3]}
(saddle-points/saddle-points
[
[8 7 9]
[6 7 6]
[3 2 5]
])))))
[[8 7 9]
[6 7 6]
[3 2 5]])))))

(deftest saddle-points_test_7
(testing "Can identify saddle points in a non square matrix"
(is (= #{[1 1] [1 3]}
(saddle-points/saddle-points
[
[3 1 3]
[3 2 4]
])))))
[[3 1 3]
[3 2 4]])))))

(deftest saddle-points_test_8
(testing "Can identify that saddle points in a single column matrix are those with the minimum value"
(is (= #{[4 1] [2 1]}
(saddle-points/saddle-points
[
[2]
[1]
[4]
[1]
])))))
[[2]
[1]
[4]
[1]])))))

(deftest saddle-points_test_9
(testing "Can identify that saddle points in a single row matrix are those with the maximum value"
(is (= #{[1 4] [1 2]}
(saddle-points/saddle-points
[
[2 5 3 5]
])))))
[[2 5 3 5]])))))
55 changes: 27 additions & 28 deletions exercises/practice/simple-cipher/test/simple_cipher_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,46 @@
(deftest encode_test_1
(testing "Random key cipher ▶ Can encode"
(let [key (simple-cipher/rand-key)]
(is (= (subs key 0 (count "aaaaaaaaaa")) (simple-cipher/encode key "aaaaaaaaaa"))))))
(is (= (subs key 0 (count "aaaaaaaaaa")) (simple-cipher/encode key "aaaaaaaaaa"))))))

(deftest encode_test_2
(testing "Substitution cipher ▶ Can encode"
(is (= "abcdefghij" (simple-cipher/encode "abcdefghij" "aaaaaaaaaa")))))

(deftest encode_test_3
(testing "Substitution cipher ▶ Can double shift encode"
(is (= "qayaeaagaciai" (simple-cipher/encode "iamapandabear" "iamapandabear")))))

(deftest encode_test_4
(testing "Substitution cipher ▶ Can wrap on encode"
(is (= "zabcdefghi" (simple-cipher/encode "abcdefghij" "zzzzzzzzzz")))))

(deftest encode_test_5
(testing "Substitution cipher ▶ Can encode messages longer than the key"
(is (= "iboaqcnecbfcr" (simple-cipher/encode "abc" "iamapandabear")))))

(deftest decode_test_1
(testing "Random key cipher ▶ Can decode"
(let [key (simple-cipher/rand-key)]
(is (= "aaaaaaaaaa" (simple-cipher/decode key (subs key 0 (count "aaaaaaaaaa")))))))

(deftest decode_test_2
(testing "Random key cipher ▶ Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method"
(let [key (simple-cipher/rand-key)]
(is (= "abcdefghij" (simple-cipher/decode key (simple-cipher/encode key "abcdefghij")))))

(deftest decode_test_3
(testing "Substitution cipher ▶ Can decode"
(is (= "aaaaaaaaaa" (simple-cipher/decode "abcdefghij" "abcdefghij"))))))

(deftest decode_test_4
(testing "Substitution cipher ▶ Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method"
(is (= "abcdefghij" (simple-cipher/decode "abcdefghij" (simple-cipher/encode "abcdefghij" "abcdefghij"))))))

(deftest decode_test_5
(testing "Substitution cipher ▶ Can wrap on decode"
(is (= "zzzzzzzzzz" (simple-cipher/decode "abcdefghij" "zabcdefghi"))))))

(deftest decode_test_6
(testing "Substitution cipher ▶ Can decode messages longer than the key"
(is (= "iamapandabear" (simple-cipher/decode "abc" "iboaqcnecbfcr"))))))


(deftest decode_test_2
(testing "Random key cipher ▶ Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method"
(let [key (simple-cipher/rand-key)]
(is (= "abcdefghij" (simple-cipher/decode key (simple-cipher/encode key "abcdefghij")))))

(deftest decode_test_3
(testing "Substitution cipher ▶ Can decode"
(is (= "aaaaaaaaaa" (simple-cipher/decode "abcdefghij" "abcdefghij"))))))

(deftest decode_test_4
(testing "Substitution cipher ▶ Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method"
(is (= "abcdefghij" (simple-cipher/decode "abcdefghij" (simple-cipher/encode "abcdefghij" "abcdefghij"))))))

(deftest decode_test_5
(testing "Substitution cipher ▶ Can wrap on decode"
(is (= "zzzzzzzzzz" (simple-cipher/decode "abcdefghij" "zabcdefghi"))))))

(deftest decode_test_6
(testing "Substitution cipher ▶ Can decode messages longer than the key"
(is (= "iamapandabear" (simple-cipher/decode "abc" "iboaqcnecbfcr"))))))
Loading

0 comments on commit ad4007a

Please sign in to comment.