Sha256: 0e2c1c91afbc5ed6ea6a0e71134367966002c43c9426e6be06abbafef41d4af1

Contents?: true

Size: 1.96 KB

Versions: 32

Compression:

Stored size: 1.96 KB

Contents

# -*- encoding : utf-8 -*-
# simple testing support
#
  require 'test/unit'

  def Testing(*args, &block)
    Class.new(Test::Unit::TestCase) do
      eval("This=self")

      def self.slug_for(*args)
        string = args.flatten.compact.join('-')
        words = string.to_s.scan(%r/\w+/)
        words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
        words.delete_if{|word| word.nil? or word.strip.empty?}
        words.join('-').downcase
      end

      def This.testing_subclass_count
        @testing_subclass_count ||= 1
      ensure
        @testing_subclass_count += 1
      end

      slug = slug_for(*args).gsub(%r/-/,'_')
      name = ['TESTING', '%03d' % This.testing_subclass_count, slug].delete_if{|part| part.empty?}.join('_')
      name = name.upcase!
      const_set(:Name, name)
      def self.name() const_get(:Name) end

      def self.testno()
        '%05d' % (@testno ||= 0)
      ensure
        @testno += 1
      end

      def self.testing(*args, &block)
        method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
        define_method("test_#{ testno }_#{ slug_for(*args) }", &block)
      end

      alias_method '__assert__', 'assert'

      def assert(*args, &block)
        if block
          label = "assert(#{ args.join ' ' })"
          result = nil
          assert_nothing_raised{ result = block.call }
          __assert__(result, label)
          result
        else
          result = args.shift
          label = "assert(#{ args.join ' ' })"
          __assert__(result, label)
          result
        end
      end

      def subclass_of exception
        class << exception
          def ==(other) super or self > other end
        end
        exception
      end

      alias_method '__assert_raises__', 'assert_raises'

      def assert_raises(*args, &block)
        args.push(subclass_of(Exception)) if args.empty?
        __assert_raises__(*args, &block)
      end

      module_eval &block
      self
    end
  end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
sekrets-1.13.0 test/lib/testing.rb
sekrets-1.12.0 test/lib/testing.rb
sekrets-1.11.0 test/lib/testing.rb
sekrets-1.10.0 test/lib/testing.rb
sekrets-1.9.1 test/lib/testing.rb
sekrets-1.9.0 test/lib/testing.rb
sekrets-1.8.1 test/lib/testing.rb
sekrets-1.8.0 test/lib/testing.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/sekrets-1.7.0/test/lib/testing.rb
sekrets-1.7.0 test/lib/testing.rb
map-6.4.2 test/lib/testing.rb
map-6.4.1 test/lib/testing.rb
map-6.4.0 test/lib/testing.rb
sekrets-1.6.0 test/lib/testing.rb
sekrets-1.4.0 test/lib/testing.rb
sekrets-1.3.0 test/lib/testing.rb
sekrets-1.2.0 test/lib/testing.rb
sekrets-1.1.0 test/lib/testing.rb
sekrets-1.0.1 test/lib/testing.rb
sekrets-1.0.0 test/lib/testing.rb