Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 82 lines (59 sloc) 1.115 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#!/usr/bin/env ruby

require 'test/unit'

class ClassAccessTest < Test::Unit::TestCase
  
  def test_private_method
    begin
      c = Parent.new
      c.only_me
      flunk("Should raise NoMethodError")
    rescue NoMethodError
      assert(true)
    end
  end

  def test_protected_method
    begin
      c = Parent.new
      c.me_and_subclasses
      flunk("Should raise NoMethodError")
    rescue NoMethodError
      assert(true)
    end
  end

  def test_child_calls_parent_private
    c = Child.new
    c.call_parent_private
    assert(true)
  end

  def test_child_calls_parent_protected
    c = Child.new
    c.call_parent_protected
    assert(true)
  end
 
end


class Parent
  
  public

    def everybody
    end

    def everybody_2
    end

  protected

    def me_and_subclasses
    end

  private

    def only_me
    end

  #
  # Alternative access control declarations:
  #
  public :everybody, :everybody_2
  protected :me_and_subclasses
  private :only_me
   
end

class Child < Parent

  def call_parent_private
    only_me
  end

  def call_parent_protected
    me_and_subclasses
  end

end
Something went wrong with that request. Please try again.