Module: TestUnitHelper::ClassMethods

Included in:
Test::Unit::TestCase
Defined in:
lib/test_unit_helper/class_methods.rb

Overview

Container for methods that are added to Test::Unit::TestCase as class methods.

Instance Method Summary collapse

Instance Method Details

#test(test_name, &block) ⇒ Object

Converts a string to a function that is used as a test.

Input

test_name : String

The name to use for the test.

&block : Block

The code that will be run.

Examples

The best examples are in the tests.



44
45
46
47
48
49
50
51
52
53
# File 'lib/test_unit_helper/class_methods.rb', line 44

def test(test_name, &block)
  class_name = test_class

  test_name = "#{class_name}#{test_name}" if test_name.match(/^[#\.]/)
  test_name = "test #{test_name} "

  test_method_name = test_name.to_sym

  define_method(test_method_name, &block)
end

#test_classObject

Returns the name of the class that is being tested.

Output

String

The class that is being tested. nil if not found.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/test_unit_helper/class_methods.rb', line 58

def test_class
  return unless self.name.match(/^Test[A-Z]|Test$/)

  prefix_class = self.name.gsub!(/^Test([A-Z])/, '\\1')
  suffix_class = self.name.gsub!(/Test$/, '')

  case true
  when prefix_class && Kernel.const_defined?(prefix_class)
    prefix_class
  when suffix_class && Kernel.const_defined?(suffix_class)
    suffix_class
  end
end