Skip to content

Commit 01ca48d

Browse files
ono-maxko1
authored andcommittedMar 15, 2022
Add the option to specify the file name in the test generator
1 parent 37ba27a commit 01ca48d

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed
 

‎bin/gentest

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ OptionParser.new do |opt|
1414
opt.on('-c CLASS', 'Class name in the test file') do |c|
1515
options[:class] = c
1616
end
17+
opt.on('-f FILENAME', 'File name of the test file') do |name|
18+
options[:file_name] = name
19+
end
1720
opt.on('--open=FRONTEND', 'Start remote debugging with opening the network port.',
1821
'Prepare for the given FRONTEND.') do |f|
1922
options[:open] = f.downcase
@@ -25,9 +28,9 @@ exit if ARGV.empty?
2528

2629
case options[:open]
2730
when 'vscode'
28-
DEBUGGER__::DAPTestBuilder.new(ARGV, options[:method], options[:class]).start
31+
DEBUGGER__::DAPTestBuilder.new(ARGV, options[:method], options[:class], options[:file_name]).start
2932
when 'chrome'
30-
DEBUGGER__::CDPTestBuilder.new(ARGV, options[:method], options[:class]).start
33+
DEBUGGER__::CDPTestBuilder.new(ARGV, options[:method], options[:class], options[:file_name]).start
3134
else
32-
DEBUGGER__::LocalTestBuilder.new(ARGV, options[:method], options[:class]).start
35+
DEBUGGER__::LocalTestBuilder.new(ARGV, options[:method], options[:class], options[:file_name]).start
3336
end

‎test/tool/test_builder.rb

+9-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestBuilderBase
1111
end
1212

1313
class LocalTestBuilder < TestBuilderBase
14-
def initialize(target, m, c)
14+
def initialize(target, m, c, name)
1515
@target_path = File.absolute_path(target[0])
1616
@current_time = Time.now.to_i
1717
m = "test_#{@current_time}" if m.nil?
@@ -23,6 +23,7 @@ def initialize(target, m, c)
2323
else
2424
@class = "#{c_upcase}Test"
2525
end
26+
@file_name = name || @class.sub(/(?i:t)est/, '').gsub(/([[:upper:]])/) {"_#{$1.downcase}"}.delete_prefix('_')
2627
end
2728

2829
def start
@@ -211,12 +212,8 @@ def make_content
211212
" end" + create_scenario_and_program
212213
end
213214

214-
def file_name
215-
@class.sub(/(?i:t)est/, '').gsub(/([[:upper:]])/) {"_#{$1.downcase}"}.delete_prefix('_')
216-
end
217-
218215
def create_file
219-
path = "#{__dir__}/../debug/#{file_name}_test.rb"
216+
path = "#{__dir__}/../debug/#{@file_name}_test.rb"
220217
if File.exist?(path)
221218
@inserted_src = File.read(path)
222219
content = @inserted_src.split("\n")[0..-3].join("\n") + "\n#{make_content}\nend\n" if @inserted_src.include? @class
@@ -239,7 +236,7 @@ class DAPTestBuilder < TestBuilderBase
239236
RUBY = ENV['RUBY'] || RbConfig.ruby
240237
RDBG_EXECUTABLE = "#{RUBY} #{__dir__}/../../exe/rdbg"
241238

242-
def initialize(target, m, c)
239+
def initialize(target, m, c, name)
243240
@target_path = File.absolute_path(target[0])
244241
@current_time = Time.now.to_i
245242
m = "test_#{@current_time}" if m.nil?
@@ -251,6 +248,7 @@ def initialize(target, m, c)
251248
else
252249
@class = "#{c_upcase}Test"
253250
end
251+
@file_name = name || @class.sub(/(?i:t)est/, '').gsub(/([[:upper:]])/) {"_#{$1.downcase}"}.delete_prefix('_')
254252
end
255253

256254
def start
@@ -423,7 +421,7 @@ def file_name
423421
end
424422

425423
def create_file
426-
path = "#{__dir__}/../protocol/#{file_name}_test.rb"
424+
path = "#{__dir__}/../protocol/#{@file_name}_test.rb"
427425
if File.exist?(path)
428426
@inserted_src = File.read(path)
429427
content = @inserted_src.split("\n")[0..-3].join("\n") + "\n#{make_content}\nend\n" if @inserted_src.include? @class
@@ -446,7 +444,7 @@ class CDPTestBuilder < TestBuilderBase
446444
RUBY = ENV['RUBY'] || RbConfig.ruby
447445
RDBG_EXECUTABLE = "#{RUBY} #{__dir__}/../../exe/rdbg"
448446

449-
def initialize(target, m, c)
447+
def initialize(target, m, c, name)
450448
@target_path = File.absolute_path(target[0])
451449
@current_time = Time.now.to_i
452450
m = "test_#{@current_time}" if m.nil?
@@ -458,6 +456,7 @@ def initialize(target, m, c)
458456
else
459457
@class = "#{c_upcase}Test"
460458
end
459+
@file_name = name || @class.sub(/(?i:t)est/, '').gsub(/([[:upper:]])/) {"_#{$1.downcase}"}.delete_prefix('_')
461460
end
462461

463462
def start
@@ -658,12 +657,8 @@ def make_content
658657
" end" + create_scenario_and_program
659658
end
660659

661-
def file_name
662-
@class.sub(/(?i:t)est/, '').gsub(/([[:upper:]])/) {"_#{$1.downcase}"}.delete_prefix('_')
663-
end
664-
665660
def create_file
666-
path = "#{__dir__}/../protocol/#{file_name}_test.rb"
661+
path = "#{__dir__}/../protocol/#{@file_name}_test.rb"
667662
if File.exist?(path)
668663
@inserted_src = File.read(path)
669664
content = @inserted_src.split("\n")[0..-3].join("\n") + "\n#{make_content}\nend\n" if @inserted_src.include? @class

0 commit comments

Comments
 (0)
Please sign in to comment.