@@ -9,7 +9,8 @@ module DEBUGGER__
99 class TestBuilder
1010 def initialize ( target , m , c )
1111 @target_path = File . absolute_path ( target [ 0 ] )
12- m = "test_#{ Time . now . to_i } " if m . nil?
12+ @current_time = Time . now . to_i
13+ m = "test_#{ @current_time } " if m . nil?
1314 @method = m
1415 c = 'FooTest' if c . nil?
1516 c_upcase = c . sub ( /(^[a-z])/ ) { Regexp . last_match ( 1 ) . upcase }
@@ -138,7 +139,7 @@ def format_scenario
138139 end
139140
140141 def create_scenario
141- <<-TEST
142+ <<-TEST . chomp
142143
143144 def #{ @method }
144145 debug_code(program) do
@@ -148,8 +149,23 @@ def #{@method}
148149 TEST
149150 end
150151
152+ def create_scenario_and_program
153+ <<-TEST . chomp
154+
155+ class #{ @class } #{ @current_time } < TestCase
156+ def program
157+ <<~RUBY
158+ #{ format_program }
159+ RUBY
160+ end
161+ #{ create_scenario }
162+ end
163+ TEST
164+ end
165+
151166 def format_program
152- lines = File . read ( @target_path ) . split ( "\n " )
167+ src = @target_src || File . read ( @target_path )
168+ lines = src . split ( "\n " )
153169 indent_num = 8
154170 if lines . length > 9
155171 first_l = " 1| #{ lines [ 0 ] } \n "
@@ -177,22 +193,25 @@ def content_with_module
177193 require_relative '../support/test_case'
178194
179195 module DEBUGGER__
180- class #{ @class } < TestCase
181- def program
182- <<~RUBY
183- #{ format_program }
184- RUBY
185- end
186- #{ create_scenario } end
196+ #{ create_scenario_and_program }
187197 end
188198 TEST
189199 end
190200
201+ def make_content
202+ @target_src = File . read ( @target_path )
203+ target = @target_src . gsub ( /\r |\n |\s / , '' )
204+ @inserted_src . scan ( /<<~RUBY(.*?)RUBY/m ) . each do |p |
205+ return create_scenario + "\n end" if p [ 0 ] . gsub ( /\r |\n |\s |\d +\| / , '' ) == target
206+ end
207+ " end" + create_scenario_and_program
208+ end
209+
191210 def create_file
192211 path = "#{ __dir__ } /../debug/#{ @class . sub ( /(?i:t)est/ , '' ) . downcase } _test.rb"
193212 if File . exist? ( path )
194- lines = File . read ( path )
195- content = lines . split ( "\n " ) [ 0 ..-3 ] . join ( "\n " ) + "\n #{ create_scenario } end \n end\n " if lines . include? @class
213+ @inserted_src = File . read ( path )
214+ content = @inserted_src . split ( "\n " ) [ 0 ..-3 ] . join ( "\n " ) + "\n #{ make_content } \n end\n " if @inserted_src . include? @class
196215 end
197216 if content
198217 puts "appended: #{ path } "
0 commit comments