Skip to content

Commit ad4007a

Browse files
Auto-format code rendered from template (#790)
1 parent f4c2373 commit ad4007a

File tree

9 files changed

+117
-121
lines changed

9 files changed

+117
-121
lines changed

docs/GENERATORS.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ To generate a practice exercise's tests, the test generator:
1111
2. Uses `tests.toml` file to omit and excluded test cases
1212
3. Transforms the test cases (optional)
1313
4. Renders the test cases using the exercise's generator template
14-
5. Writes the rendered template to the exercise's test file
14+
5. Format the rendered template using [cljfmt](https://github.com/weavejester/cljfmt)
15+
6. Writes the formatted template to the exercise's test file
1516

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

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

8182
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.
8283

83-
### Step 5: write the rendered template to the exercise's test file
84+
### Step 5: format the rendered template using cljfmt
85+
86+
The rendered template is then formatted using [cljfmt](https://github.com/weavejester/cljfmt).
87+
This has the following benefits:
88+
89+
- Exercises are formatted consistently
90+
- You're not required to worry much about whitespace and alignment when writing templates
91+
92+
### Step 6: write the rendered template to the exercise's test file
8493

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

exercises/practice/change/test/change_test.clj

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,63 @@
66
(testing "change for 1 cent"
77
(is (= '(1)
88
(change/issue 1 #{1 5 10 25})))))
9-
9+
1010
(deftest issue_test_2
1111
(testing "single coin change"
1212
(is (= '(25)
1313
(change/issue 25 #{1 5 10 25 100})))))
14-
14+
1515
(deftest issue_test_3
1616
(testing "multiple coin change"
1717
(is (= '(5 10)
1818
(change/issue 15 #{1 5 10 25 100})))))
19-
19+
2020
(deftest issue_test_4
2121
(testing "change with Lilliputian Coins"
2222
(is (= '(4 4 15)
2323
(change/issue 23 #{1 4 15 20 50})))))
24-
24+
2525
(deftest issue_test_5
2626
(testing "change with Lower Elbonia Coins"
2727
(is (= '(21 21 21)
2828
(change/issue 63 #{1 5 10 21 25})))))
29-
29+
3030
(deftest issue_test_6
3131
(testing "large target values"
3232
(is (= '(2 2 5 20 20 50 100 100 100 100 100 100 100 100 100)
3333
(change/issue 999 #{1 2 5 10 20 50 100})))))
34-
34+
3535
(deftest issue_test_7
3636
(testing "possible change without unit coins available"
3737
(is (= '(2 2 2 5 10)
3838
(change/issue 21 #{2 5 10 20 50})))))
39-
39+
4040
(deftest issue_test_8
4141
(testing "another possible change without unit coins available"
4242
(is (= '(4 4 4 5 5 5)
4343
(change/issue 27 #{4 5})))))
44-
44+
4545
(deftest issue_test_9
4646
(testing "a greedy approach is not optimal"
4747
(is (= '(10 10)
4848
(change/issue 20 #{1 10 11})))))
49-
49+
5050
(deftest issue_test_10
5151
(testing "no coins make 0 change"
5252
(is (= '()
5353
(change/issue 0 #{1 5 10 21 25})))))
54-
54+
5555
(deftest issue_test_11
5656
(testing "error testing for change smaller than the smallest of coins"
5757
(is (thrown-with-msg? IllegalArgumentException #"^can't make target with given coins$"
5858
(change/issue 3 #{5 10})))))
59-
59+
6060
(deftest issue_test_12
6161
(testing "error if no combination can add up to target"
6262
(is (thrown-with-msg? IllegalArgumentException #"^can't make target with given coins$"
6363
(change/issue 94 #{5 10})))))
64-
64+
6565
(deftest issue_test_13
6666
(testing "cannot find negative change values"
6767
(is (thrown-with-msg? IllegalArgumentException #"^target can't be negative$"
6868
(change/issue -5 #{1 2 5})))))
69-

exercises/practice/largest-series-product/test/largest_series_product_test.clj

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,51 @@
55
(deftest largest-product_test_1
66
(testing "finds the largest product if span equals length"
77
(is (= 18 (largest-series-product/largest-product 2 "29")))))
8-
8+
99
(deftest largest-product_test_2
1010
(testing "can find the largest product of 2 with numbers in order"
1111
(is (= 72 (largest-series-product/largest-product 2 "0123456789")))))
12-
12+
1313
(deftest largest-product_test_3
1414
(testing "can find the largest product of 2"
1515
(is (= 48 (largest-series-product/largest-product 2 "576802143")))))
16-
16+
1717
(deftest largest-product_test_4
1818
(testing "can find the largest product of 3 with numbers in order"
1919
(is (= 504 (largest-series-product/largest-product 3 "0123456789")))))
20-
20+
2121
(deftest largest-product_test_5
2222
(testing "can find the largest product of 3"
2323
(is (= 270 (largest-series-product/largest-product 3 "1027839564")))))
24-
24+
2525
(deftest largest-product_test_6
2626
(testing "can find the largest product of 5 with numbers in order"
2727
(is (= 15120 (largest-series-product/largest-product 5 "0123456789")))))
28-
28+
2929
(deftest largest-product_test_7
3030
(testing "can get the largest product of a big number"
3131
(is (= 23520 (largest-series-product/largest-product 6 "73167176531330624919225119674426574742355349194934")))))
32-
32+
3333
(deftest largest-product_test_8
3434
(testing "reports zero if the only digits are zero"
3535
(is (= 0 (largest-series-product/largest-product 2 "0000")))))
36-
36+
3737
(deftest largest-product_test_9
3838
(testing "reports zero if all spans include zero"
3939
(is (= 0 (largest-series-product/largest-product 3 "99099")))))
40-
40+
4141
(deftest largest-product_test_10
4242
(testing "rejects span longer than string length"
4343
(is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 4 "123")))))
44-
44+
4545
(deftest largest-product_test_11
4646
(testing "rejects empty string and nonzero span"
4747
(is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 1 "")))))
48-
48+
4949
(deftest largest-product_test_12
5050
(testing "rejects invalid character in digits"
5151
(is (thrown-with-msg? IllegalArgumentException #"^digits input must only contain digits$" (largest-series-product/largest-product 2 "1234a5")))))
52-
52+
5353
(deftest largest-product_test_13
5454
(testing "rejects negative span"
5555
(is (thrown-with-msg? IllegalArgumentException #"^span must not be negative$" (largest-series-product/largest-product -1 "12345")))))
56-

exercises/practice/saddle-points/test/saddle_points_test.clj

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,66 @@
66
(testing "Can identify single saddle point"
77
(is (= #{[2 1]}
88
(saddle-points/saddle-points
9-
[
10-
[9 8 7]
11-
[5 3 2]
12-
[6 6 7]
13-
])))))
9+
[[9 8 7]
10+
[5 3 2]
11+
[6 6 7]])))))
1412

1513
(deftest saddle-points_test_2
1614
(testing "Can identify that empty matrix has no saddle points"
1715
(is (= #{}
1816
(saddle-points/saddle-points
19-
[
20-
])))))
17+
[])))))
2118

2219
(deftest saddle-points_test_3
2320
(testing "Can identify lack of saddle points when there are none"
2421
(is (= #{}
2522
(saddle-points/saddle-points
26-
[
27-
[1 2 3]
28-
[3 1 2]
29-
[2 3 1]
30-
])))))
23+
[[1 2 3]
24+
[3 1 2]
25+
[2 3 1]])))))
3126

3227
(deftest saddle-points_test_4
3328
(testing "Can identify multiple saddle points in a column"
3429
(is (= #{[2 2] [1 2] [3 2]}
3530
(saddle-points/saddle-points
36-
[
37-
[4 5 4]
38-
[3 5 5]
39-
[1 5 4]
40-
])))))
31+
[[4 5 4]
32+
[3 5 5]
33+
[1 5 4]])))))
4134

4235
(deftest saddle-points_test_5
4336
(testing "Can identify multiple saddle points in a row"
4437
(is (= #{[2 2] [2 3] [2 1]}
4538
(saddle-points/saddle-points
46-
[
47-
[6 7 8]
48-
[5 5 5]
49-
[7 5 6]
50-
])))))
39+
[[6 7 8]
40+
[5 5 5]
41+
[7 5 6]])))))
5142

5243
(deftest saddle-points_test_6
5344
(testing "Can identify saddle point in bottom right corner"
5445
(is (= #{[3 3]}
5546
(saddle-points/saddle-points
56-
[
57-
[8 7 9]
58-
[6 7 6]
59-
[3 2 5]
60-
])))))
47+
[[8 7 9]
48+
[6 7 6]
49+
[3 2 5]])))))
6150

6251
(deftest saddle-points_test_7
6352
(testing "Can identify saddle points in a non square matrix"
6453
(is (= #{[1 1] [1 3]}
6554
(saddle-points/saddle-points
66-
[
67-
[3 1 3]
68-
[3 2 4]
69-
])))))
55+
[[3 1 3]
56+
[3 2 4]])))))
7057

7158
(deftest saddle-points_test_8
7259
(testing "Can identify that saddle points in a single column matrix are those with the minimum value"
7360
(is (= #{[4 1] [2 1]}
7461
(saddle-points/saddle-points
75-
[
76-
[2]
77-
[1]
78-
[4]
79-
[1]
80-
])))))
62+
[[2]
63+
[1]
64+
[4]
65+
[1]])))))
8166

8267
(deftest saddle-points_test_9
8368
(testing "Can identify that saddle points in a single row matrix are those with the maximum value"
8469
(is (= #{[1 4] [1 2]}
8570
(saddle-points/saddle-points
86-
[
87-
[2 5 3 5]
88-
])))))
71+
[[2 5 3 5]])))))

exercises/practice/simple-cipher/test/simple_cipher_test.clj

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,46 @@
99
(deftest encode_test_1
1010
(testing "Random key cipher ▶ Can encode"
1111
(let [key (simple-cipher/rand-key)]
12-
(is (= (subs key 0 (count "aaaaaaaaaa")) (simple-cipher/encode key "aaaaaaaaaa"))))))
13-
12+
(is (= (subs key 0 (count "aaaaaaaaaa")) (simple-cipher/encode key "aaaaaaaaaa"))))))
13+
1414
(deftest encode_test_2
1515
(testing "Substitution cipher ▶ Can encode"
1616
(is (= "abcdefghij" (simple-cipher/encode "abcdefghij" "aaaaaaaaaa")))))
17-
17+
1818
(deftest encode_test_3
1919
(testing "Substitution cipher ▶ Can double shift encode"
2020
(is (= "qayaeaagaciai" (simple-cipher/encode "iamapandabear" "iamapandabear")))))
21-
21+
2222
(deftest encode_test_4
2323
(testing "Substitution cipher ▶ Can wrap on encode"
2424
(is (= "zabcdefghi" (simple-cipher/encode "abcdefghij" "zzzzzzzzzz")))))
25-
25+
2626
(deftest encode_test_5
2727
(testing "Substitution cipher ▶ Can encode messages longer than the key"
2828
(is (= "iboaqcnecbfcr" (simple-cipher/encode "abc" "iamapandabear")))))
29-
29+
3030
(deftest decode_test_1
3131
(testing "Random key cipher ▶ Can decode"
3232
(let [key (simple-cipher/rand-key)]
3333
(is (= "aaaaaaaaaa" (simple-cipher/decode key (subs key 0 (count "aaaaaaaaaa")))))))
34-
35-
(deftest decode_test_2
36-
(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"
37-
(let [key (simple-cipher/rand-key)]
38-
(is (= "abcdefghij" (simple-cipher/decode key (simple-cipher/encode key "abcdefghij")))))
39-
40-
(deftest decode_test_3
41-
(testing "Substitution cipher ▶ Can decode"
42-
(is (= "aaaaaaaaaa" (simple-cipher/decode "abcdefghij" "abcdefghij"))))))
43-
44-
(deftest decode_test_4
45-
(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"
46-
(is (= "abcdefghij" (simple-cipher/decode "abcdefghij" (simple-cipher/encode "abcdefghij" "abcdefghij"))))))
47-
48-
(deftest decode_test_5
49-
(testing "Substitution cipher ▶ Can wrap on decode"
50-
(is (= "zzzzzzzzzz" (simple-cipher/decode "abcdefghij" "zabcdefghi"))))))
51-
52-
(deftest decode_test_6
53-
(testing "Substitution cipher ▶ Can decode messages longer than the key"
54-
(is (= "iamapandabear" (simple-cipher/decode "abc" "iboaqcnecbfcr"))))))
55-
34+
35+
(deftest decode_test_2
36+
(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"
37+
(let [key (simple-cipher/rand-key)]
38+
(is (= "abcdefghij" (simple-cipher/decode key (simple-cipher/encode key "abcdefghij")))))
39+
40+
(deftest decode_test_3
41+
(testing "Substitution cipher ▶ Can decode"
42+
(is (= "aaaaaaaaaa" (simple-cipher/decode "abcdefghij" "abcdefghij"))))))
43+
44+
(deftest decode_test_4
45+
(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"
46+
(is (= "abcdefghij" (simple-cipher/decode "abcdefghij" (simple-cipher/encode "abcdefghij" "abcdefghij"))))))
47+
48+
(deftest decode_test_5
49+
(testing "Substitution cipher ▶ Can wrap on decode"
50+
(is (= "zzzzzzzzzz" (simple-cipher/decode "abcdefghij" "zabcdefghi"))))))
51+
52+
(deftest decode_test_6
53+
(testing "Substitution cipher ▶ Can decode messages longer than the key"
54+
(is (= "iamapandabear" (simple-cipher/decode "abc" "iboaqcnecbfcr"))))))

0 commit comments

Comments
 (0)