Sha256: ce9d8915932914dc3d5f4c1b7dd33ea8f7fb5ec6385d632a4c23f443ade88434

Contents?: true

Size: 1.55 KB

Versions: 58

Compression:

Stored size: 1.55 KB

Contents

require "test/unit"

# Test::Unit loads a default test if the suite is empty, and the only
# purpose of that test is to fail. As having empty contexts is a common
# practice, we decided to overwrite TestSuite#empty? in order to
# allow them. Having a failure when no tests have been defined seems
# counter-intuitive.
class Test::Unit::TestSuite
  unless method_defined?(:empty?)
    def empty?
      false
    end
  end
end

# We added setup, test and context as class methods, and the instance
# method setup now iterates on the setup blocks. Note that all setup
# blocks must be defined with the block syntax. Adding a setup instance
# method defeats the purpose of this library.
class Test::Unit::TestCase
  def self.setup(&block)
    setup_blocks << block
  end

  def setup
    self.class.setup_blocks.each do |block|
      instance_eval(&block)
    end
  end

  def self.context(name, &block)
    subclass = Class.new(self.superclass)
    subclass.setup_blocks.unshift(*setup_blocks)
    subclass.class_eval(&block)
    const_set(context_name(name), subclass)
  end

  def self.test(name, &block)
    define_method(test_name(name), &block)
  end

  class << self
    alias_method :should, :test
    alias_method :describe, :context
  end

private

  def self.setup_blocks
    @setup_blocks ||= []
  end

  def self.context_name(name)
    "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
  end

  def self.test_name(name)
    "test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
  end

  def self.sanitize_name(name)
    name.gsub(/\W+/, ' ').strip
  end
end

Version data entries

58 entries across 57 versions & 11 rubygems

Version Path
sinatra-1.0.a test/contest.rb
sinatra-resources-0.1.1 test/contest.rb
sinatra-bundles-0.1.1 vendor/gems/sinatra-0.10.1/test/contest.rb
sinatra-bundles-0.1.0 vendor/gems/sinatra-0.10.1/test/contest.rb
sinatra-bundles-0.0.2 vendor/gems/sinatra-0.10.1/test/contest.rb
darkhelmet-sinatra-0.10.1 test/contest.rb
sinatra-resources-0.1.0 test/contest.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.12.0.pre lib/middleman/vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.10.17 vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.10.16 vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.10.15 vendor/gems/gems/sinatra-0.9.4/test/contest.rb
middleman-0.10.14 vendor/gems/gems/sinatra-0.9.4/test/contest.rb
sinatra-0.9.4 test/contest.rb
sinatra-0.9.2 test/contest.rb