Skip to content

Commit 470834d

Browse files
meatball133kotp
andauthored
Sync (#1778)
* Fix exercise naming in generator (#1770) * Enhance underscore method to filter invalid characters and update test file naming convention * Add test case which test underscore for special characters * Use character classes in regex. Also expose description for nested test cases. * Underscore must not create multiple underscores around special characters * Add templates to exercises batch 1 (#1771) * Make test name unique in allergies * Changed exercises to use single quotes. Changed template for allergies so it should match the old version. * Update acronym solution to work on the new test * Fix import and class in atbash test * Fix exercise naming in generator (#1770) (#1774) * Enhance underscore method to filter invalid characters and update test file naming convention * Add test case which test underscore for special characters * Use character classes in regex. Also expose description for nested test cases. * Underscore must not create multiple underscores around special characters --------- Co-authored-by: Victor Goff <keeperotphones+github@gmail.com> --------- Co-authored-by: Victor Goff <keeperotphones+github@gmail.com>
1 parent 0b99a72 commit 470834d

22 files changed

Lines changed: 364 additions & 128 deletions

File tree

bin/generate

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def exercises
99
.select { |file| File.directory? File.join('./exercises/practice', file) }
1010
end
1111

12+
def underscore(str)
13+
str.gsub(/[^\w\s-]/, '').gsub(/[-\s]/, '_').downcase
14+
end
15+
1216
class VerificationError < StandardError
1317
MESSAGE = 'The result generated for %<exercise>s, does not match the current file'
1418

@@ -39,7 +43,7 @@ end
3943
parser.on('--verify', 'Verify all exercises') do
4044
exercises.each do |exercise|
4145
if File.exist?("./exercises/practice/#{exercise}/.meta/test_template.erb")
42-
current_code = File.read("./exercises/practice/#{exercise}/#{exercise}_test.rb")
46+
current_code = File.read("./exercises/practice/#{exercise}/#{underscore(exercise)}_test.rb")
4347
f = File.new("./exercises/practice/#{exercise}/temp_test.rb", 'w+')
4448
Generator.new(exercise).generate(f.path)
4549
generated_code = f.read

exercises/practice/acronym/.meta/example.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ def self.abbreviate(phrase)
77
end.join
88
end
99

10-
def self.each_word(phrase)
11-
phrase.scan(/[A-Z]+[a-z]*|[a-z]+/) do |word|
12-
yield word
13-
end
10+
def self.each_word(phrase, &block)
11+
phrase.scan(/[A-Za-z]+(?:'[A-Za-z]+)*/, &block)
1412
end
1513
end

exercises/practice/acronym/.meta/test_template.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AcronymTest < Minitest::Test
55
<% json["cases"].each do |cases| %>
66
def test_<%= underscore(cases["description"]) %>
77
<%= skip? %>
8-
assert_equal '<%= cases["expected"] %>', <%= camel_case(json["exercise"]) %>.<%= underscore(cases["property"]) %>('<%= cases["input"]["phrase"] %>')
8+
assert_equal '<%= cases["expected"] %>', <%= camel_case(json["exercise"]) %>.<%= underscore(cases["property"]) %>('<%= cases["input"]["phrase"].gsub("'", "\\\\'") %>')
99
end
1010
<% end %>
1111
end

exercises/practice/acronym/acronym_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ def test_consecutive_delimiters
3636
skip
3737
assert_equal 'SIMUFTA', Acronym.abbreviate('Something - I made up from thin air')
3838
end
39+
40+
def test_apostrophes
41+
skip
42+
assert_equal 'HC', Acronym.abbreviate('Halley\'s Comet')
43+
end
44+
45+
def test_underscore_emphasis
46+
skip
47+
assert_equal 'TRNT', Acronym.abbreviate('The Road _Not_ Taken')
48+
end
3949
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'minitest/autorun'
2+
require_relative 'affine_cipher'
3+
4+
class AffineCipherTest < Minitest::Test
5+
<% json["cases"].each do |group| %>
6+
<% group["cases"].each do |sub_case| %>
7+
def test_<%= underscore(sub_case["description"]) %>
8+
<%= skip? %>
9+
<%- if sub_case["expected"].is_a?(Hash) && sub_case["expected"].key?("error") -%>
10+
assert_raises(ArgumentError) { Affine.new(<%= sub_case["input"]["key"]["a"] %>, <%= sub_case["input"]["key"]["b"] %>) }
11+
<%- else -%>
12+
cipher = Affine.new(<%= sub_case["input"]["key"]["a"] %>, <%= sub_case["input"]["key"]["b"] %>)
13+
<%- if sub_case["property"] == "encode" -%>
14+
plaintext = '<%= sub_case["input"]["phrase"] %>'
15+
ciphertext = '<%= sub_case["expected"] %>'
16+
assert_equal ciphertext, cipher.encode(plaintext)
17+
<%- elsif sub_case["property"] == "decode" -%>
18+
ciphertext = '<%= sub_case["input"]["phrase"] %>'
19+
plaintext = '<%= sub_case["expected"] %>'
20+
assert_equal plaintext, cipher.decode(ciphertext)
21+
<%- end -%>
22+
<%- end -%>
23+
end
24+
<% end %>
25+
<% end %>
26+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'minitest/autorun'
2+
require_relative 'all_your_base'
3+
4+
class AllYourBaseTest < Minitest::Test
5+
<% json["cases"].each do |cases| %>
6+
def test_<%= underscore(cases["description"]) %>
7+
<%= skip? %>
8+
digits = <%= cases["input"]["digits"] %>
9+
input_base = <%= cases["input"]["inputBase"] %>
10+
output_base = <%= cases["input"]["outputBase"] %>
11+
<% if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") %>
12+
assert_raises(ArgumentError) do
13+
BaseConverter.convert(input_base, digits, output_base)
14+
end
15+
<% else %>expected = <%= cases["expected"] %>
16+
17+
converted = BaseConverter.convert(input_base, digits, output_base)
18+
19+
hint = "Input base: #{input_base}, output base #{output_base}. " \
20+
"Expected #{expected} but got #{converted}."
21+
22+
assert_equal expected, converted, hint
23+
<% end %>
24+
end
25+
<% end %>
26+
end

exercises/practice/all-your-base/all_your_base_test.rb

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def test_single_bit_one_to_decimal
1111

1212
converted = BaseConverter.convert(input_base, digits, output_base)
1313

14-
hint = "Input base: 2, output base 10. " \
15-
"Expected #{expected} but got #{converted}."
14+
hint = "Input base: #{input_base}, output base #{output_base}. " \
15+
"Expected #{expected} but got #{converted}."
1616

1717
assert_equal expected, converted, hint
1818
end
@@ -26,8 +26,8 @@ def test_binary_to_single_decimal
2626

2727
converted = BaseConverter.convert(input_base, digits, output_base)
2828

29-
hint = "Input base: 2, output base 10. " \
30-
"Expected #{expected} but got #{converted}."
29+
hint = "Input base: #{input_base}, output base #{output_base}. " \
30+
"Expected #{expected} but got #{converted}."
3131

3232
assert_equal expected, converted, hint
3333
end
@@ -41,8 +41,8 @@ def test_single_decimal_to_binary
4141

4242
converted = BaseConverter.convert(input_base, digits, output_base)
4343

44-
hint = "Input base: 10, output base 2. " \
45-
"Expected #{expected} but got #{converted}."
44+
hint = "Input base: #{input_base}, output base #{output_base}. " \
45+
"Expected #{expected} but got #{converted}."
4646

4747
assert_equal expected, converted, hint
4848
end
@@ -56,8 +56,8 @@ def test_binary_to_multiple_decimal
5656

5757
converted = BaseConverter.convert(input_base, digits, output_base)
5858

59-
hint = "Input base: 2, output base 10. " \
60-
"Expected #{expected} but got #{converted}."
59+
hint = "Input base: #{input_base}, output base #{output_base}. " \
60+
"Expected #{expected} but got #{converted}."
6161

6262
assert_equal expected, converted, hint
6363
end
@@ -71,8 +71,8 @@ def test_decimal_to_binary
7171

7272
converted = BaseConverter.convert(input_base, digits, output_base)
7373

74-
hint = "Input base: 10, output base 2. " \
75-
"Expected #{expected} but got #{converted}."
74+
hint = "Input base: #{input_base}, output base #{output_base}. " \
75+
"Expected #{expected} but got #{converted}."
7676

7777
assert_equal expected, converted, hint
7878
end
@@ -86,8 +86,8 @@ def test_trinary_to_hexadecimal
8686

8787
converted = BaseConverter.convert(input_base, digits, output_base)
8888

89-
hint = "Input base: 3, output base 16. " \
90-
"Expected #{expected} but got #{converted}."
89+
hint = "Input base: #{input_base}, output base #{output_base}. " \
90+
"Expected #{expected} but got #{converted}."
9191

9292
assert_equal expected, converted, hint
9393
end
@@ -101,8 +101,8 @@ def test_hexadecimal_to_trinary
101101

102102
converted = BaseConverter.convert(input_base, digits, output_base)
103103

104-
hint = "Input base: 16, output base 3. " \
105-
"Expected #{expected} but got #{converted}."
104+
hint = "Input base: #{input_base}, output base #{output_base}. " \
105+
"Expected #{expected} but got #{converted}."
106106

107107
assert_equal expected, converted, hint
108108
end
@@ -116,8 +116,8 @@ def test_15_bit_integer
116116

117117
converted = BaseConverter.convert(input_base, digits, output_base)
118118

119-
hint = "Input base: 97, output base 73. " \
120-
"Expected #{expected} but got #{converted}."
119+
hint = "Input base: #{input_base}, output base #{output_base}. " \
120+
"Expected #{expected} but got #{converted}."
121121

122122
assert_equal expected, converted, hint
123123
end
@@ -131,8 +131,8 @@ def test_empty_list
131131

132132
converted = BaseConverter.convert(input_base, digits, output_base)
133133

134-
hint = "Input base: 2, output base 10. " \
135-
"Expected #{expected} but got #{converted}."
134+
hint = "Input base: #{input_base}, output base #{output_base}. " \
135+
"Expected #{expected} but got #{converted}."
136136

137137
assert_equal expected, converted, hint
138138
end
@@ -146,8 +146,8 @@ def test_single_zero
146146

147147
converted = BaseConverter.convert(input_base, digits, output_base)
148148

149-
hint = "Input base: 10, output base 2. " \
150-
"Expected #{expected} but got #{converted}."
149+
hint = "Input base: #{input_base}, output base #{output_base}. " \
150+
"Expected #{expected} but got #{converted}."
151151

152152
assert_equal expected, converted, hint
153153
end
@@ -161,8 +161,8 @@ def test_multiple_zeros
161161

162162
converted = BaseConverter.convert(input_base, digits, output_base)
163163

164-
hint = "Input base: 10, output base 2. " \
165-
"Expected #{expected} but got #{converted}."
164+
hint = "Input base: #{input_base}, output base #{output_base}. " \
165+
"Expected #{expected} but got #{converted}."
166166

167167
assert_equal expected, converted, hint
168168
end
@@ -176,8 +176,8 @@ def test_leading_zeros
176176

177177
converted = BaseConverter.convert(input_base, digits, output_base)
178178

179-
hint = "Input base: 7, output base 10. " \
180-
"Expected #{expected} but got #{converted}."
179+
hint = "Input base: #{input_base}, output base #{output_base}. " \
180+
"Expected #{expected} but got #{converted}."
181181

182182
assert_equal expected, converted, hint
183183
end
@@ -187,6 +187,7 @@ def test_input_base_is_one
187187
digits = [0]
188188
input_base = 1
189189
output_base = 10
190+
190191
assert_raises(ArgumentError) do
191192
BaseConverter.convert(input_base, digits, output_base)
192193
end
@@ -197,6 +198,7 @@ def test_input_base_is_zero
197198
digits = []
198199
input_base = 0
199200
output_base = 10
201+
200202
assert_raises(ArgumentError) do
201203
BaseConverter.convert(input_base, digits, output_base)
202204
end
@@ -207,6 +209,7 @@ def test_input_base_is_negative
207209
digits = [1]
208210
input_base = -2
209211
output_base = 10
212+
210213
assert_raises(ArgumentError) do
211214
BaseConverter.convert(input_base, digits, output_base)
212215
end
@@ -217,6 +220,7 @@ def test_negative_digit
217220
digits = [1, -1, 1, 0, 1, 0]
218221
input_base = 2
219222
output_base = 10
223+
220224
assert_raises(ArgumentError) do
221225
BaseConverter.convert(input_base, digits, output_base)
222226
end
@@ -227,6 +231,7 @@ def test_invalid_positive_digit
227231
digits = [1, 2, 1, 0, 1, 0]
228232
input_base = 2
229233
output_base = 10
234+
230235
assert_raises(ArgumentError) do
231236
BaseConverter.convert(input_base, digits, output_base)
232237
end
@@ -237,6 +242,7 @@ def test_output_base_is_one
237242
digits = [1, 0, 1, 0, 1, 0]
238243
input_base = 2
239244
output_base = 1
245+
240246
assert_raises(ArgumentError) do
241247
BaseConverter.convert(input_base, digits, output_base)
242248
end
@@ -247,6 +253,7 @@ def test_output_base_is_zero
247253
digits = [7]
248254
input_base = 10
249255
output_base = 0
256+
250257
assert_raises(ArgumentError) do
251258
BaseConverter.convert(input_base, digits, output_base)
252259
end
@@ -257,6 +264,7 @@ def test_output_base_is_negative
257264
digits = [1]
258265
input_base = 2
259266
output_base = -7
267+
260268
assert_raises(ArgumentError) do
261269
BaseConverter.convert(input_base, digits, output_base)
262270
end
@@ -267,6 +275,7 @@ def test_both_bases_are_negative
267275
digits = [1]
268276
input_base = -2
269277
output_base = -7
278+
270279
assert_raises(ArgumentError) do
271280
BaseConverter.convert(input_base, digits, output_base)
272281
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'minitest/autorun'
2+
require_relative 'allergies'
3+
4+
class AllergiesTest < Minitest::Test
5+
<% json["cases"].each do |group| %>
6+
<% group["cases"].each do |sub_case| %>
7+
def test_<%= underscore(group["description"]) %>_<%= underscore(sub_case["description"]) %>
8+
<%= skip? %>
9+
allergies = Allergies.new(<%= sub_case["input"]["score"] %>)
10+
<%- if sub_case["property"] == "allergicTo" -%>
11+
<%= sub_case["expected"] ? "assert" : "refute" %> allergies.allergic_to?('<%= sub_case["input"]["item"] %>'), 'Tom is<%= sub_case["expected"] ? "" : " not" %> allergic, but it says he is<%= sub_case["expected"] ? " not" : "" %>.'
12+
<%- else -%>
13+
expected = %w[<%= sub_case["expected"].join(" ") %>]
14+
assert_equal expected, allergies.list
15+
<% end %>
16+
end
17+
<% end %>
18+
<% end %>
19+
end

0 commit comments

Comments
 (0)