Class: Test::Unit::TestCase

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

Overview

These subsets facilitate testing by using the ENV variables specified on the command line to indicate which tests to run. The ENV variables are set by rake, so this code implicitly assumes that you’re running your tests through rake.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.match_platform?(*platforms) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/test/unit/subsets.rb', line 16

def match_platform?(*platforms)
  platforms.each do |platform|
    platform.to_s =~ /^(non_)?(.*)/
  
    non = true if $1
    match_platform = !RUBY_PLATFORM.index($2).nil?
    return false unless (non && !match_platform) || (!non && match_platform)
  end
  
  true
end

.original_suiteObject



28
# File 'lib/test/unit/subsets.rb', line 28

alias :original_suite :suite

.require_platform(*platforms) ⇒ Object



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

def require_platform(*platforms)
  @platforms = platforms
end

.suiteObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/test/unit/subsets.rb', line 31

def self.suite
  if match_platform?(*@platforms)
    original_suite
  else
    method_names = public_instance_methods(true)
    suite = Test::Unit::TestSuite.new(name)
          method_names.each do |method_name| 
      catch(:invalid_test) do
        suite << new('on_test_skipped') if method_name =~ /^test/
      end
    end
    suite
  end
end

Instance Method Details

#benchmark_test(&block) ⇒ Object

Subset test declaration for benchmard tests – type: BENCHMARK Prints ‘b’ unless run.



62
63
64
65
66
67
# File 'lib/test/unit/subsets.rb', line 62

def benchmark_test(&block) 
  subset_test("BENCHMARK") do
    puts calling_method
    block.call
  end
end

#case_test(hash, &block) ⇒ Object

Case tests take a hash of testcases and expected values. Each pair in the hash will be passed to the block if type CASE_TEST is specified. Individual cases tests can be specified by providing a regexp in CASE; the testcase will run if the pretty-print of the testcase matches the provided regexp. Example:

case_test(
  [1,2,3] => 'an array testcase',
  '[1, 2, 3]' => 'the pretty print of the array',
  'another testcase' => 'some third testcase'
).do |testcase, expected|
   ...your test code...
end

ENV['CASE_TEST']=true  => all tests run
ENV['CASE']='1, 2, 3' => first two tests run
ENV['CASE']='another' => only last test runs


85
86
87
88
89
90
91
# File 'lib/test/unit/subsets.rb', line 85

def case_test(hash, &block)
  if match_regexp?("CASE_TEST", calling_method)
    hash.each_pair do |testcase, expected|
      yield(testcase, expected) if match_regexp?("CASE", testcase)
    end
  end
end

#extended_test(&block) ⇒ Object

Subset test declaration for extended tests – type: EXTENDED Prints ‘x’ unless run.



56
57
58
# File 'lib/test/unit/subsets.rb', line 56

def extended_test(&block) 
  subset_test("EXTENDED", "x", &block)
end

#platform_test(*platforms, &block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/test/unit/subsets.rb', line 46

def platform_test(*platforms, &block)
  if self.class.match_platform?(*platforms)
    block.call 
  else
    print ' '
  end
end

#prompt_test(*array, &block) ⇒ Object



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

def prompt_test(*array, &block)
  subset_test("PROMPT", "p") do
    puts "This test requires your input.\nEnter 'exit' to skip the test."
  
    config = {}
    array.each do |key|
      print "#{key}: "
      value = gets.strip
      flunk "exited test" if value =~ /exit/i
      
      config[key] = value
    end
      
    yield(config)
  end
end