Class: TestKit123

Inherits:
Object
  • Object
show all
Defined in:
lib/testkit123.rb

Instance Method Summary collapse

Constructor Details

#initialize(templates: {testdata: nil, testx: nil}, project: nil, debug: false, localpath: nil, datapath: nil, gemtest_url: nil, rubyver: 'ruby-2.5.1') ⇒ TestKit123

Returns a new instance of TestKit123.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/testkit123.rb', line 19

def initialize(templates: {testdata: nil, testx: nil}, project: nil, 
      debug: false, localpath: nil, datapath: nil, 
      gemtest_url: nil, rubyver: 'ruby-2.5.1')

  @h = templates
  
  @project_config = if project =~ /\.txt$/ then
    project
  elsif project
    File.join(localpath, project + '.txt')      
  end
  
  @debug = debug
  @localpath, @datapath, @gemtest_url = localpath, datapath, gemtest_url
  @rubyver = rubyver

end

Instance Method Details

#create_files(project = @project_config) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/testkit123.rb', line 37

def create_files(project=@project_config)

  puts 'running create_files ...' if @debug

  puts '1) Reading the config file' if @debug
  raise "missing the config file " unless @project_config
  raise "config file not found" unless File.exists? @project_config

  config_file = @project_config
  
  puts 'config_file: ' + config_file.inspect if @debug
  
  config = SimpleConfig.new(config_file).to_h
  puts 'config : ' + config.inspect if @debug

  rbfile_buffer = config[:test][:items].join "\n"

  puts 'before project' if @debug
  proj = config[:project]
  classname = config[:classname]

  puts 'proj: ' + proj.inspect if @debug

  filepath = config[:local_path] + '/' + proj
  puts 'filepath: ' + filepath.inspect if @debug
  datafilepath = config[:data_path] + '/' + proj

  puts 'before ext' if @debug

  ext = config[:testdata_ext][/\.?td$/] ? 'td' : 'xml'

  puts 'before stage 2' if @debug

  puts '2) Making directory for file path ' + filepath.inspect if @debug
  FileUtils.mkdir_p filepath

  puts '3) Writing the .testdata file' if @debug
  buffer = config[:http_gemtest] + "%s/testdata_%s.%s" % ([proj, proj, ext])
  File.write filepath + '/.testdata', buffer

  puts '4) Creating the working code tests from templates' if @debug
  # fetch the template testdata

  testdata = @h[:testdata].gsub(/(?<=\.)\w+$/, ext)
  puts 'testdata: ' + testdata.inspect if @debug
  buffer_testdata, _ = RXFHelper.read(testdata)
  
  FileUtils.mkdir_p datafilepath
  
  testdata_file = File.join(datafilepath, "testdata_#{proj}.#{ext}")
  File.write testdata_file, eval('%{' + buffer_testdata + '}') 

  test_template_rsf = @h[:testx]
  buffer_test, _ = RXFHelper.read(test_template_rsf)

  test_rsffile = File.join(config[:data_path], "#{proj}.rsf")
  File.write test_rsffile, eval('%{' + buffer_test + '}') 

buffer_rb = %{#!/usr/bin/env ruby

# file: test_#{proj}.rb

class Test#{config[:classname]} < Testdata::Base

def tests()

#{rbfile_buffer.lines.map {|x| ' '*4 + x}.join}

end
end
}

  test_rbfile = File.join(datafilepath, "test_#{proj}.rb")
  puts 'reading the ruby template file ...' if @debug
  File.write test_rbfile, eval('%{' + buffer_rb + '}') 

  "finished processing #{proj}"
end

#create_standalone(n) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/testkit123.rb', line 116

def create_standalone(n)
  
  # use the test number to find the test to copy from the test file
  
  config = SimpleConfig.new(@project_config).to_h
  
  proj = config[:project]
  datafilepath = config[:data_path] + '/' + proj
  test_rbfile = File.join(datafilepath, "test_#{proj}.rb")    
  
  ext = config[:testdata_ext][/\.?td$/] ? 'td' : 'xml'
  testdata_file = File.join(datafilepath, "testdata_#{proj}.#{ext}")
  puts 'testdata_file: ' + testdata_file.inspect if @debug
  
  s = File.read test_rbfile
  a = s.split(/(?=    test )/)
  a.shift

  tests = a.map do |x|
    r = a[0].match(/(?<=['"])(?<test>[^"']+)['"]\s+do\s+\|(?<raw_args>[^\|]+)/)  
    
    [r[:test], r[:raw_args].split(/, */)]
  end

  s2 , filetype = RXFHelper.read testdata_file
  puts 'filetype: ' + filetype.inspect if @debug

  xml = if s2.lstrip[0] == '<' then
    s2
  else
    TestdataText.parse s2
  end

  puts 'xml: ' + xml.inspect if @debug
  
  doc = Rexle.new(xml)
  testnode = doc.root.xpath('records//test')[n-1]
  title = testnode.text('summary/type')
  i = tests.index tests.assoc(title)
  
  testcode = a[i].strip.lines[1..-2].map do |line|
    line.sub(/^ {6}/,'')
  end

  args = testnode.xpath('records/input/summary/*').map do |input|
    "%s = '%s'" % [input.name, input.texts.join.gsub(/'/,"\'")]
  end
  
  puts 'args: ' + args.inspect if @debug
  
  "require '#{proj}'\n\n" + args.join("\n") + testcode.join

end

#delete_files(s = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/testkit123.rb', line 170

def delete_files(s=nil)
  
  filepath = if s =~ /\.txt$/ then
    project
  elsif s
    File.join(@localpath, s + '.txt')      
  else
    @project_config
  end
  
  puts 'filepath: ' + filepath.inspect if @debug
  
  config = SimpleConfig.new(filepath).to_h
  
  proj = config[:project]
  datafilepath = config[:data_path] + '/' + proj
  FileUtils.rm_rf datafilepath
      
  filepath = config[:local_path] + '/' + proj
  FileUtils.rm_rf filepath
  
  FileUtils.rm File.join(config[:data_path], "#{proj}.rsf")
  
  proj + ' test files deleted'
  
end

#new_project(project = 'myproject', classname = nil, save: false) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/testkit123.rb', line 197

def new_project(project='myproject', classname=nil, save: false)

  classname ||= project.camelize

s ="project: \#{project}\nclassname: \#{classname}\nlocal_path: \#{@localpath || '~/test-ruby'}\ndata_path: \#{@datapath || '~/gemtest'}\nhttp_gemtest: \#{@gemtest_url || 'http://mywebsite.com/r/gemtest/'}\nruby_version: \#{@rubyver}\n\n# testdata_ext defaults to .xml\n\ntestdata_ext: .xml\ntest:\n\ntest 'to_s only' do |filepath|\n\n  \#{classname}.new(filepath).to_s\n\nend\n"


  if save then
    filepath = File.join(@localpath, project + '.txt')
    File.write filepath, s
    puts 'file saved ' + filepath if @debug
    @project_config = filepath
    self
  else
    s
  end

end