1 require 'runit/cui/testrunner'
  2 require 'runit/testcase'
  3 
  4 # A unit test suite for public, protected, private, and secret instance
  5 # methods.
  6 #
  7 # TODO: Write tests for class methods.
  8 #
  9 
 10 class Foo
 11   include RUNIT::Assert
 12 public
 13   def public_foo
 14     return :foo
 15   end
 16 
 17 protected
 18   def protected_foo
 19     return :foo
 20   end
 21 
 22 private
 23   def private_foo
 24     return :foo
 25   end
 26 
 27 secret
 28   def secret_foo
 29     return :foo
 30   end
 31 
 32 public
 33   def public_self_test
 34     self.public_foo
 35   end
 36 
 37   def protected_self_test
 38     self.protected_foo
 39   end
 40 
 41   def private_self_test
 42     assert_exception(NoMethodError) do
 43       self.private_foo
 44     end
 45   end
 46 
 47   def secret_self_test
 48     assert_exception(NoMethodError) do
 49       self.secret_foo
 50     end
 51   end
 52 
 53   def public_test
 54     public_foo
 55   end
 56 
 57   def protected_test
 58     protected_foo
 59   end
 60 
 61   def private_test
 62     private_foo
 63   end
 64 
 65   def secret_test
 66     secret_foo
 67   end
 68 end
 69 
 70 class Bar < Foo
 71   def bar_public_self_test
 72     self.public_foo
 73   end
 74 
 75   def bar_protected_self_test
 76     self.protected_foo
 77   end
 78 
 79   def bar_private_self_test
 80     assert_exception(NoMethodError) do
 81       self.private_foo
 82     end
 83   end
 84 
 85   def bar_secret_self_test
 86     assert_exception(NoMethodError) do
 87       self.secret_foo
 88     end
 89   end
 90 
 91   def bar_public_test
 92     public_foo
 93   end
 94 
 95   def bar_protected_test
 96     protected_foo
 97   end
 98 
 99   def bar_private_test
100     private_foo
101   end
102 
103   def bar_secret_test
104     assert_exception(NoMethodError) do
105       secret_foo
106     end
107   end
108 end
109 
110 class SafeMethodTest < RUNIT::TestCase
111   def test_public_access
112     f = Foo.new
113     
114     f.public_foo();
115     
116     assert_exception(NoMethodError) do
117       f.protected_foo()
118     end
119     
120     assert_exception(NoMethodError) do
121       f.private_foo()
122     end
123     
124     assert_exception(NoMethodError) do
125       f.secret_foo()
126     end
127   end
128 
129   def test_same_class_access
130     f = Foo.new
131     f.public_methods.each do |m|
132       if m =~ /_test/ then
133         f.__send__(m)
134       end
135     end
136   end
137   
138   def test_subclass_access
139     b = Bar.new
140     b.public_methods.each do |m|
141       if m =~ /_test/ then
142         b.__send__(m)
143       end
144     end
145   end
146 
147   def test_instance_methods
148     foo_pub_im = Foo.public_instance_methods
149     foo_prot_im = Foo.protected_instance_methods
150     foo_priv_im = Foo.private_instance_methods
151     foo_secr_im = Foo.secret_instance_methods
152     assert_not_nil foo_pub_im.index("public_foo")
153     assert_not_nil foo_prot_im.index("protected_foo")
154     assert_not_nil foo_secr_im.index("secret_foo")
155     assert_nil foo_secr_im.index("public_foo")
156   end
157 
158   def test_subclass_instance_methods
159     bar_pub_im = Bar.public_instance_methods
160     bar_prot_im = Bar.protected_instance_methods
161     bar_priv_im = Bar.private_instance_methods
162     bar_secr_im = Bar.secret_instance_methods
163     assert_nil bar_pub_im.index("public_foo")
164     assert_nil bar_prot_im.index("protected_foo")
165     assert_nil bar_secr_im.index("secret_foo")
166     assert_nil bar_secr_im.index("public_foo")
167   end
168 
169   def test_methods
170     f = Foo.new
171     foo_pub = f.public_methods
172     foo_prot = f.protected_methods
173     foo_priv = f.private_methods
174     foo_secr = f.secret_methods
175     assert_not_nil foo_pub.index("public_foo")
176     assert_not_nil foo_prot.index("protected_foo")
177     assert_not_nil foo_secr.index("secret_foo")
178     assert_nil foo_secr.index("public_foo")
179   end
180 
181   def test_subclass_methods
182     b = Bar.new
183     bar_pub = b.public_methods
184     bar_prot = b.protected_methods
185     bar_priv = b.private_methods
186     bar_secr = b.secret_methods
187     assert_not_nil bar_pub.index("public_foo")
188     assert_not_nil bar_prot.index("protected_foo")
189     assert_not_nil bar_secr.index("secret_foo")
190     assert_nil bar_secr.index("public_foo")
191   end
192 end
193 
194 RUNIT::CUI::TestRunner.run(SafeMethodTest.suite)