module TestUnitHelper::ClassMethods

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

Public Instance Methods

test(test_name, &block) click to toggle source

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.

# 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_class() click to toggle source

Returns the name of the class that is being tested.

Output

String

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

# 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