Skip to content

Commit 5424cdd

Browse files
authored
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
1 parent 2b6c97d commit 5424cdd

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

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

generatorv2/lib/generator.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def generate(result_path = "./exercises/practice/#{@exercise}/#{underscore(@exer
2121
additional_json(json)
2222
json["cases"] = remove_tests(uuid, json)
2323
status = proc { status }
24-
template = ERB.new File.read("./exercises/practice/#{@exercise}/.meta/test_template.erb")
24+
template = ERB.new(File.read("./exercises/practice/#{@exercise}/.meta/test_template.erb"), trim_mode: '-')
2525

2626
result = template.result(binding)
2727

2828
File.write(result_path, result)
2929
RuboCop::CLI.new.
30-
run(['-x', '-c', '.rubocop.yml', '-o', NullDevice.path, result_path])
30+
run(['-a', '-c', '.rubocop.yml', '-o', NullDevice.path, result_path])
3131
end
3232

3333
def underscore(str)
34-
str.gsub(/[-\s]/, '_').downcase
34+
str.gsub(/[^\w\s-]/, '').gsub(' ', ' ').gsub(/[-\s]/, '_').downcase
3535
end
3636

3737
def camel_case(str)

generatorv2/lib/utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def additional_json(json)
4747
def remove_tests(uuid, json)
4848
json["cases"].each_with_object([]) do |x, acc|
4949
if x["cases"]
50-
acc << { "cases" => remove_tests(uuid, x) }
50+
acc << { "cases" => remove_tests(uuid, x), "description" => x["description"] }
5151
elsif uuid.include?(x["uuid"])
5252
acc << x
5353
end

generatorv2/test/utils_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def test_underscore_with_two_words
2222
Generator.new("two-fer").underscore("two-fer")
2323
end
2424

25+
def test_underscore_with_special_characters
26+
assert_equal "two_fer",
27+
Generator.new("two-fer").underscore("two,!@#$%^&*()-fer")
28+
end
29+
30+
def test_underscore_with_special_characters_should_not_create_multiple_spaces
31+
assert_equal "two_fer",
32+
Generator.new("two-fer").underscore("two = fer")
33+
end
34+
2535
def test_first_time_includes_hastag
2636
assert_equal "# skip",
2737
Generator.new("acronym").skip?

0 commit comments

Comments
 (0)