Class: Test::Unit::AutoRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/test/unit/autorunner.rb

Constant Summary collapse

RUNNERS =
{}
COLLECTORS =
{}
ADDITIONAL_OPTIONS =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(standalone) {|_self| ... } ⇒ AutoRunner

Returns a new instance of AutoRunner.

Yields:

  • (_self)

Yield Parameters:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/test/unit/autorunner.rb', line 81

def initialize(standalone)
  Unit.run = true
  @standalone = standalone
  @runner = default_runner
  @collector = default_collector
  @filters = []
  @to_run = []
  @runner_options = {}
  @workdir = nil
  yield(self) if block_given?
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



78
79
80
# File 'lib/test/unit/autorunner.rb', line 78

def base
  @base
end

#collector=(value) ⇒ Object (writeonly)

Sets the attribute collector

Parameters:

  • value

    the value to set the attribute collector to.



79
80
81
# File 'lib/test/unit/autorunner.rb', line 79

def collector=(value)
  @collector = value
end

#excludeObject

Returns the value of attribute exclude.



78
79
80
# File 'lib/test/unit/autorunner.rb', line 78

def exclude
  @exclude
end

#filtersObject

Returns the value of attribute filters.



78
79
80
# File 'lib/test/unit/autorunner.rb', line 78

def filters
  @filters
end

#patternObject

Returns the value of attribute pattern.



78
79
80
# File 'lib/test/unit/autorunner.rb', line 78

def pattern
  @pattern
end

#runner=(value) ⇒ Object (writeonly)

Sets the attribute runner

Parameters:

  • value

    the value to set the attribute runner to.



79
80
81
# File 'lib/test/unit/autorunner.rb', line 79

def runner=(value)
  @runner = value
end

#runner_optionsObject (readonly)

Returns the value of attribute runner_options.



77
78
79
# File 'lib/test/unit/autorunner.rb', line 77

def runner_options
  @runner_options
end

#suiteObject (readonly)

Returns the value of attribute suite.



77
78
79
# File 'lib/test/unit/autorunner.rb', line 77

def suite
  @suite
end

#to_runObject

Returns the value of attribute to_run.



78
79
80
# File 'lib/test/unit/autorunner.rb', line 78

def to_run
  @to_run
end

#workdirObject

Returns the value of attribute workdir.



78
79
80
# File 'lib/test/unit/autorunner.rb', line 78

def workdir
  @workdir
end

Class Method Details

.register_collector(id, collector_builder = Proc.new) ⇒ Object



16
17
18
# File 'lib/test/unit/autorunner.rb', line 16

def register_collector(id, collector_builder=Proc.new)
  COLLECTORS[id] = collector_builder
end

.register_runner(id, runner_builder = Proc.new) ⇒ Object



12
13
14
# File 'lib/test/unit/autorunner.rb', line 12

def register_runner(id, runner_builder=Proc.new)
  RUNNERS[id] = runner_builder
end

.run(force_standalone = false, default_dir = nil, argv = ARGV, &block) ⇒ Object



25
26
27
28
29
30
# File 'lib/test/unit/autorunner.rb', line 25

def self.run(force_standalone=false, default_dir=nil, argv=ARGV, &block)
  r = new(force_standalone || standalone?, &block)
  r.base = default_dir
  r.process_args(argv)
  r.run
end

.setup_option(option_builder = Proc.new) ⇒ Object



20
21
22
# File 'lib/test/unit/autorunner.rb', line 20

def setup_option(option_builder=Proc.new)
  ADDITIONAL_OPTIONS << option_builder
end

.standalone?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/test/unit/autorunner.rb', line 32

def self.standalone?
  return false unless("-e" == $0)
  ObjectSpace.each_object(Class) do |klass|
    return false if(klass < TestCase)
  end
  true
end

Instance Method Details

#keyword_display(array) ⇒ Object



218
219
220
221
222
# File 'lib/test/unit/autorunner.rb', line 218

def keyword_display(array)
  list = array.collect {|e, *| e.to_s}
  Array === array or list.sort!
  list.collect {|e| e.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]')}.join(", ")
end

#optionsObject



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
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
169
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/test/unit/autorunner.rb', line 107

def options
  @options ||= OptionParser.new do |o|
    o.banner = "Test::Unit automatic runner."
    o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"

    o.on
    o.on('-r', '--runner=RUNNER', RUNNERS,
         "Use the given RUNNER.",
         "(" + keyword_display(RUNNERS) + ")") do |r|
      @runner = r
    end

    if(@standalone)
      o.on('-b', '--basedir=DIR', "Base directory of test suites.") do |b|
        @base = b
      end

      o.on('-w', '--workdir=DIR', "Working directory to run tests.") do |w|
        @workdir = w
      end

      o.on('-a', '--add=TORUN', Array,
           "Add TORUN to the list of things to run;",
           "can be a file or a directory.") do |a|
        @to_run.concat(a)
      end

      @pattern = []
      o.on('-p', '--pattern=PATTERN', Regexp,
           "Match files to collect against PATTERN.") do |e|
        @pattern << e
      end

      @exclude = []
      o.on('-x', '--exclude=PATTERN', Regexp,
           "Ignore files to collect against PATTERN.") do |e|
        @exclude << e
      end
    end

    o.on('-n', '--name=NAME', String,
         "Runs tests matching NAME.",
         "(patterns may be used).") do |n|
      n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
      case n
      when Regexp
        @filters << proc{|t| n =~ t.method_name ? true : nil}
      else
        @filters << proc{|t| n == t.method_name ? true : nil}
      end
    end

    o.on('-t', '--testcase=TESTCASE', String,
         "Runs tests in TestCases matching TESTCASE.",
         "(patterns may be used).") do |n|
      n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
      case n
      when Regexp
        @filters << proc{|t| n =~ t.class.name ? true : nil}
      else
        @filters << proc{|t| n == t.class.name ? true : nil}
      end
    end

    priority_filter = Proc.new do |test|
      Priority::Checker.new(test).need_to_run? or nil
    end
    o.on("--[no-]priority-mode",
         "Runs some tests based on their priority.") do |priority_mode|
      if priority_mode
        @filters |= [priority_filter]
      else
        @filters -= [priority_filter]
      end
    end

    o.on('-I', "--load-path=DIR[#{File::PATH_SEPARATOR}DIR...]",
         "Appends directory list to $LOAD_PATH.") do |dirs|
      $LOAD_PATH.concat(dirs.split(File::PATH_SEPARATOR))
    end

    ADDITIONAL_OPTIONS.each do |option_builder|
      option_builder.call(self, o)
    end

    o.on('--',
         "Stop processing options so that the",
         "remaining options will be passed to the",
         "test."){o.terminate}

    o.on('-h', '--help', 'Display this help.'){puts o; exit}

    o.on_tail
    o.on_tail('Deprecated options:')

    o.on_tail('--console', 'Console runner (use --runner).') do
      warn("Deprecated option (--console).")
      @runner = RUNNERS[:console]
    end

    if RUNNERS[:fox]
      o.on_tail('--fox', 'Fox runner (use --runner).') do
        warn("Deprecated option (--fox).")
        @runner = RUNNERS[:fox]
      end
    end

    o.on_tail
  end
end

#process_args(args = ARGV) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/test/unit/autorunner.rb', line 93

def process_args(args = ARGV)
  begin
    options.order!(args) {|arg| @to_run << arg}
  rescue OptionParser::ParseError => e
    puts e
    puts options
    $! = nil
    abort
  else
    @filters << proc{false} unless(@filters.empty?)
  end
  not @to_run.empty?
end

#runObject



224
225
226
227
228
229
230
231
# File 'lib/test/unit/autorunner.rb', line 224

def run
  suite = @collector[self]
  return false if suite.nil?
  runner = @runner[self]
  return false if runner.nil?
  Dir.chdir(@workdir) if @workdir
  runner.run(suite, @runner_options).passed?
end