Module: Regtest

Extended by:
Regtest, Colors
Included in:
Regtest
Defined in:
lib/regtest.rb,
lib/regtest/colors.rb,
lib/regtest/version.rb

Defined Under Namespace

Modules: Colors

Constant Summary collapse

VERSION =
'2.2.1'
Colorize =
Regtest::Colors

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

apply, codes, report

Class Attribute Details

.exit_codesObject (readonly)

Returns the value of attribute exit_codes.



97
98
99
# File 'lib/regtest.rb', line 97

def exit_codes
  @exit_codes
end

.log_filenamesObject (readonly)

Returns the value of attribute log_filenames.



97
98
99
# File 'lib/regtest.rb', line 97

def log_filenames
  @log_filenames
end

.resultsObject (readonly)

Returns the value of attribute results.



97
98
99
# File 'lib/regtest.rb', line 97

def results
  @results
end

.startObject (readonly)

Returns the value of attribute start.



97
98
99
# File 'lib/regtest.rb', line 97

def start
  @start
end

Class Method Details

.check_resultsObject

Checking results, should be overwritten by SCM plugins e.g. regtest/git

Returns:

  • Symbol with check result (one of :success, :unknown_result or :fail)



128
129
130
131
# File 'lib/regtest.rb', line 128

def check_results
  report "\nPlease check result files manually. Regtest isn't able to do that.", type: :unknown_result
  :unknown_result
end

.determine_filename_from_caller(ext) ⇒ Object

Determine a filename which is derived from the filename of the “real” caller of the calling method

Parameters:

  • ext

    new extension (i.e. ‘.yml’)



102
103
104
# File 'lib/regtest.rb', line 102

def determine_filename_from_caller ext
  caller_locations(2, 1).first.path.sub(/\.rb$/, '') << ext.to_s
end

.report(*args, type: nil) ⇒ Object

Report text to output with possible type, could be overwritten by plugins e.g. regtest/colors.



135
136
137
# File 'lib/regtest.rb', line 135

def report *args, type: nil
  puts *args
end

.report_statisticsObject

Report some statistics, could be overwritten by plugins.



107
108
109
110
111
112
# File 'lib/regtest.rb', line 107

def report_statistics
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  time = now - start
  sample_count = results.values.map(&:size).reduce(0, &:+)
  report format("\n\n%d samples executed in %.2f s (%d samples/s)", sample_count, time, sample_count / time), type: :statistics
end

.saveObject

Save all results to the corresponding files.



115
116
117
118
119
120
121
122
123
# File 'lib/regtest.rb', line 115

def save
  results.each_pair do |filename, arr|
    File.open(filename, 'w') do |f|
      arr.each do |h|
        f.write h.to_yaml
      end
    end
  end
end

Instance Method Details

#combinations(hashy) ⇒ Object

Build all combinations of a Hash-like object with arrays as values. Return value is an array of OpenStruct instances.

Example:

require 'ostruct'
require 'regtest'

o = OpenStruct.new
o.a = [1,2,3]
o.b = [:x, :y]
Regtest.combinations(o)
# => [#<OpenStruct a=1, b=:x>, #<OpenStruct a=1, b=:y>,
#     #<OpenStruct a=2, b=:x>, #<OpenStruct a=2, b=:y>,
#     #<OpenStruct a=3, b=:x>, #<OpenStruct a=3, b=:y>]


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/regtest.rb', line 60

def combinations hashy
  h = hashy.to_h
  a = h.values[0].product(*h.values[1..-1])
  res = []
  a.each do |e|
    o = OpenStruct.new
    h.keys.zip(e) do |k, v|
      o[k] = v
    end
    res << o
  end
  res
end

#log(s, mode: nil) ⇒ Object

Write (temporary) informations to a log file By default the log file is truncated at the first call of Regtest.log for each run of regtest, and all following calls appends to the log file. So you have a log for one run of regtest. You can use mode (‘a’ or ‘w’) to change this behaviour for each call of Regtest.log.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/regtest.rb', line 79

def log s, mode: nil
  log_filename = Regtest.determine_filename_from_caller('.log')
  case mode
  when nil
    mode = Regtest.log_filenames.include?(log_filename) ? 'a' : 'w'
  when 'a', 'w'
    # ok
  else
    raise ArgumentError.new(format('Mode %s is not allowed.', mode))
  end
  Regtest.log_filenames << log_filename
  File.open log_filename, mode do |f|
    f.puts s
  end
end

#sample(name) ⇒ Object

Define a sample



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/regtest.rb', line 26

def sample name
  h = {}
  name = name.to_s if name.kind_of?(Symbol)
  h['sample'] = name
  begin
    h['result'] = yield
  rescue Exception => e
    h['exception'] = e.message
  end
  output_filename = Regtest.determine_filename_from_caller('.yml')
  unless Regtest.results[output_filename]
    Regtest.report "\n", type: :filename unless Regtest.results.empty?
    Regtest.report output_filename, type: :filename
    Regtest.results[output_filename] = []
  end
  Regtest.results[output_filename] << h
  print '.'; $stdout.flush
  h
end