Sha256: f9733eeabf1b193da40209b16ecc22d17e099d237cfc22c1ee016a018683e780

Contents?: true

Size: 1.67 KB

Versions: 10

Compression:

Stored size: 1.67 KB

Contents

module Minitest
  module Utils
    module Assertions
      def assert(test, message = nil)
        message ||= "expected: truthy value\ngot: #{mu_pp(test)}"
        super(test, message)
      end

      def refute(test, message = nil)
        message ||= "expected: falsy value\ngot: #{mu_pp(test)}"
        super(test, message)
      end
    end
  end

  class Test
    include ::Minitest::Utils::Assertions

    def self.test(name, &block)
      test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
      defined = method_defined? test_name

      raise "#{test_name} is already defined in #{self}" if defined

      if block_given?
        define_method(test_name, &block)
      else
        define_method(test_name) do
          flunk "No implementation provided for #{name}"
        end
      end
    end

    def self.setup(&block)
      mod = Module.new
      mod.module_eval do
        define_method :setup do
          super()
          instance_eval(&block)
        end
      end

      include mod
    end

    def self.teardown(&block)
      mod = Module.new
      mod.module_eval do
        define_method :teardown do
          super()
          instance_eval(&block)
        end
      end

      include mod
    end

    def self.let(name, &block)
      target = instance_method(name) rescue nil
      message = "Cannot define let(:#{name});"

      raise ArgumentError, "#{message} method cannot begin with 'test'." if name.to_s.start_with?("test")
      raise ArgumentError, "#{message} method already defined by #{target.owner}." if target

      define_method(name) do
        @_memoized ||= {}
        @_memoized.fetch(name) {|k| @_memoized[k] = instance_eval(&block) }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
minitest-utils-0.4.4 lib/minitest/utils/extension.rb
minitest-utils-0.4.3 lib/minitest/utils/extension.rb
minitest-utils-0.4.2 lib/minitest/utils/extension.rb
minitest-utils-0.4.1 lib/minitest/utils/extension.rb
minitest-utils-0.4.0 lib/minitest/utils/extension.rb
minitest-utils-0.3.1 lib/minitest/utils/extension.rb
minitest-utils-0.3.0 lib/minitest/utils/extension.rb
minitest-utils-0.2.9 lib/minitest/utils/extension.rb
minitest-utils-0.2.8 lib/minitest/utils/extension.rb
minitest-utils-0.2.7 lib/minitest/utils/extension.rb