Module: Probatio

Extended by:
DebugMethods
Defined in:
lib/probatio/debug.rb,
lib/probatio.rb,
lib/probatio/more.rb,
lib/probatio/plug.rb,
lib/probatio/mangle.rb,
lib/probatio/plugins.rb,
lib/probatio/plugins.rb

Overview

probatio/mangle.rb

Defined Under Namespace

Modules: Colours, DebugMethods, ExtraErrorMethods, Helpers, SeedReporter, Waiters Classes: After, Around, AssertionError, Beeper, Before, Chronometer, Context, DotReporter, Event, Exitter, Group, Leaf, Node, ProbaOutputter, Recorder, Section, Setup, Teardown, Test, VanillaSummarizer

Constant Summary collapse

VERSION =
'1.2.1'

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from DebugMethods

dbg_m, dbg_s, debug

Class Attribute Details

.mapObject (readonly)

Returns the value of attribute map.



23
24
25
# File 'lib/probatio.rb', line 23

def map
  @map
end

.rngObject (readonly)

Returns the value of attribute rng.



22
23
24
# File 'lib/probatio.rb', line 22

def rng
  @rng
end

.seedObject (readonly)

Returns the value of attribute seed.



22
23
24
# File 'lib/probatio.rb', line 22

def seed
  @seed
end

Class Method Details

._beepObject

For easy override…



242
243
244
245
# File 'lib/probatio/plugins.rb', line 242

def self._beep

  STDOUT.print("\a")
end

.beep(count = 1) ⇒ Object



247
248
249
250
# File 'lib/probatio/plugins.rb', line 247

def self.beep(count=1)

  (count || 0).times { Probatio._beep; sleep 0.5 }
end

.cObject



25
# File 'lib/probatio.rb', line 25

def c; $_PROBATIO_COLOURS; end

.despatch(event_name, *details) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/probatio.rb', line 150

def despatch(event_name, *details)

#p [ :despatch, event_name ]
  en = event_name.to_sym
  me = "on_#{event_name}"
  ev = Probatio::Event.new(en, details)
#p [ :despatch, event_name, ev.delta ]

  dbg_m { '  ' + [ :despatch, en, ev.node && ev.node.full_name ].inspect }

  @plugouts ||= @plugins.reverse

  (ev.leave? ? @plugouts : @plugins).each do |plugin|

    plugin.record(ev) if plugin.respond_to?(:record)
    plugin.send(me, ev) if plugin.respond_to?(me)
  end
end

.epathObject



169
# File 'lib/probatio.rb', line 169

def epath; '.probatio-environments.rb'; end

.initObject



142
143
144
145
146
147
148
# File 'lib/probatio.rb', line 142

def init

  @read = Set.new

  @plugins = []
  @plugouts = nil
end

.mangle(dirs, files, switches) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/probatio/mangle.rb', line 9

def mangle(dirs, files, switches)

  dry = switches['-y'] || switches['--dry']

  (
    dirs.collect { |d| Dir[File.join(d, '**', '*_spec.rb')] }.flatten +
    files.select { |f| f.match?(/_spec\.rb$/) }
  )
    .each do |path|

      puts c.dark_gray(". considering #{c.green(path)}")
      p1 = path[0..-8] + 'test.rb'
      puts c.dark_gray("  .. writing to #{c.light_green(p1)}")

      next if dry

      File.open(p1, 'wb') do |f|
        f.write(
          File.read(path)
            .gsub(/^(\s*)describe(\s+)/) { "#{$1}group#{$2}" }
            .gsub(/^(\s*)context(\s+)/) { "#{$1}group#{$2}" }
            .gsub(/^(\s*)it(\s+)/) { "#{$1}test#{$2}" }
            .gsub(/^(\s*)expect(\(|\s)/) { "#{$1}assert#{$2}" }
            .gsub(/\)\.to (eq|match)\(/) { ', ' }
            .gsub(/\n\s*, */) { ",\n" }
        )
      end

      puts c.dark_gray("    .. wrote to #{c.light_green(p1)}")
    end
end

.monowObject



8
# File 'lib/probatio/more.rb', line 8

def monow; @ts = Process.clock_gettime(Process::CLOCK_MONOTONIC); end

.monow_and_deltaObject



9
# File 'lib/probatio/more.rb', line 9

def monow_and_delta; ts0, ts1 = @ts, monow; [ ts1, ts1 - (ts0 || ts1) ]; end

.opathObject



170
# File 'lib/probatio.rb', line 170

def opath; '.probatio-output.rb'; end

.plug(x, position = :last) ⇒ Object



10
11
12
13
14
# File 'lib/probatio/plug.rb', line 10

def plug(x, position=:last)

  @plugins.insert(determine_plugin_pos(position), x)
  @plugouts = nil
end

.pluginsObject



8
# File 'lib/probatio/plug.rb', line 8

def plugins; @plugins; end

.recorder_pluginObject



53
54
55
56
# File 'lib/probatio/plugins.rb', line 53

def recorder_plugin

  @plugins.find { |pl| pl.respond_to?(:events) }
end

.replug(old, new) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/probatio/plug.rb', line 26

def replug(old, new)

  i =
    plug_index(old) ||
    fail(ArgumentError.new("Cannot locate plugin to replace"))

  @plugins[i] = new
  @plugouts = nil
end

.run(run_opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
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
115
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
# File 'lib/probatio.rb', line 27

def run(run_opts)

  @seed = run_opts[:seed]
  @rng = Random.new(@seed)

  Probatio.despatch(:pre, run_opts)

  #
  # construct tree

  root_group = Group.new(nil, __FILE__, '_', {}, nil)

  run_opts[:dirs] ||= []
  run_opts[:files] ||= []

  run_opts[:filez] = []
  run_opts[:filen] = []

  helpers = locate(run_opts, '*_helper.rb', '*_helpers.rb')
  setups = locate(run_opts, 'setup.rb', '*_setup.rb')

  dbg_s do
    " / dirs:     #{run_opts[:dirs].inspect}\n" +
    " / files:    #{run_opts[:files].inspect}\n" +
    " / helpers:  #{helpers.inspect}\n" +
    " / setups:   #{setups.inspect}\n"
  end

  # helpers and setups...

  helpers.each do |path|

    read_helper_file(root_group, path)
  end

  setups.each do |path|

    run_opts[:filez] << path
    read_test_file(root_group, path)
  end

  # tests from dirs...

  run_opts[:dirs].each do |dir|

    ( Dir[File.join(dir, '**', '*_test.rb')] +
      Dir[File.join(dir, '**', '*_tests.rb')]

    ).each do |path|

      run_opts[:filez] << path
      read_test_file(root_group, path)
    end
  end

  # tests from files...

  run_opts[:files].each do |path|

    colons = path.split(':')
    fpath = colons.shift

    if colons.empty?
      run_opts[:filez] << path
    else
      colons.each do |lnum|
        lnum = lnum.match?(/^\d+$/) ? lnum.to_i : false
        run_opts[:filen] << [ fpath, lnum ] if lnum
      end
    end

    read_test_file(root_group, fpath)
  end

  run_opts[:filen] = rework_filen(root_group, run_opts)

  dbg_s { Cerata.vertical_h_to_s(run_opts, ' run_opts| ') }

  #
  # print or map

  if run_opts[:print]

    puts root_group.to_s

    exit 0
  end

  if run_opts[:map]

    root_group.map.each do |path, groups|
      puts ". #{Probatio.c.green(path)}"
      groups.each do |l0, l1, g|
        puts "  #{Probatio.c.dark_grey('%4d %4d')}  %s" % [ l0, l1, g.head ]
      end
    end

    exit 0
  end

  #
  # run

  dbg_s { "---\n" + root_group.to_s + "\n---\n" }

  Probatio.despatch(:start, root_group, run_opts)

  root_group.run(run_opts)

  Probatio.despatch(:over, root_group, run_opts)

  Probatio.despatch(:exit, root_group, run_opts)
    # some plugin will catch that and do `exit 0` or `exit 1`...
end

.seconds_to_time_s(f) ⇒ Object Also known as: to_time_s



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/probatio/more.rb', line 11

def seconds_to_time_s(f)

  i = f.to_i
  d = i / (24 * 3600); i = i % (24 * 3600)
  h = i / 3600; i = i % 3600
  m = i / 60; i = i % 60
  s = i

  ms = ((
    f < 1 ? '%0.6f' :
    f < 60 ? '%0.3f' :
    ''
      ) % (f % 1.0))[2..-1] || ''
  ms = ms.insert(3, '_') if ms.length > 3

  [ d > 0 ? "#{d}d" : nil,
    h > 0 ? "#{h}h" : nil,
    m > 0 ? "#{m}m" : nil,
    "#{s}s",
    "#{ms}" ].compact.join('')
end

.term_widthObject



56
57
58
59
60
61
# File 'lib/probatio/more.rb', line 56

def term_width

  (IO.console.winsize[1] rescue nil) ||
  (`tput cols`.strip.to_i rescue nil) ||
  80
end

.to_rex_or_str(s) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/probatio/more.rb', line 40

def to_rex_or_str(s)

  m = s.match(/^\/(.+)\/([imx]*)$/); return s unless m

  pat = m[1]; opts = m[2]

  ropts = opts.each_char.inject(0) { |r, c|
    case c
    when 'i' then r |= Regexp::IGNORECASE
    #when 'm' then r |= Regexp::MULTILINE
    when 'x' then r |= Regexp::EXTENDED
    else r; end }

  Regexp.new(pat, ropts)
end

.to_rexes_and_strs(a) ⇒ Object



35
36
37
38
# File 'lib/probatio/more.rb', line 35

def to_rexes_and_strs(a)

  a && a.collect { |e| to_rex_or_str(e) }
end

.tpathObject



171
# File 'lib/probatio.rb', line 171

def tpath; '.test-point'; end

.unplug(old) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/probatio/plug.rb', line 16

def unplug(old)

  i =
    plug_index(old) ||
    fail(ArgumentError.new("Cannot locate plugin to remove"))

  @plugins.delete_at(i)
  @plugouts = nil
end