Sha256: a7407bc2fb36c8b64d64202d43b21d873a408cd00902ae5e2acbaea0c71e15e1

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'test/unit'

class Test::Unit::TestCase

  # probe is a surgical instrument useful for testing private methods. It
  # takes a class and a list of instance methods and makes any of them that
  # are private into public methods, yields control, and then changes them
  # back to private. This allows testing of private methods with minimum
  # exposure to any aberrations caused by a change in visibility.
  def probe(klass,*methods)
    methods = [ *methods ].flatten
    target_methods = klass.publicize_private_instance_methods methods
    yield
    klass.privatize_public_instance_methods target_methods
  end

end


class Class

  # Take a list of instance methods and make any that are private public.
  # Returns the list of methods that changed visibility.
  def publicize_private_instance_methods(methods)
    target_methods = methods.select { |method|
      private_instance_methods.include? method.to_s }
    public(*target_methods) if target_methods.size > 0
    target_methods
  end

  # Take a list of instance methods and make any that are public private.
  # Returns the list of methods that changed visibility.
  def privatize_public_instance_methods(methods)
    target_methods = methods.select { |method|
      instance_methods.include? method.to_s }
    private(*target_methods) if target_methods.size > 0
    target_methods
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eymiha_test-1.0.0 lib/eymiha/test/probe.rb
eymiha_test-1.0.1 lib/eymiha/test/probe.rb