Fix typo of subclasses (from subcases)
[ruby_koans.git] / src / about_inheritance.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutInheritance < EdgeCase::Koan
4   class Dog
5     attr_reader :name
6
7     def initialize(name)
8       @name = name
9     end
10
11     def bark
12       "WOOF"
13     end
14   end
15
16   class Chihuahua < Dog
17     def wag
18       :happy
19     end
20
21     def bark
22       "yip"
23     end
24   end
25
26   def test_subclasses_have_the_parent_as_an_ancestor
27     assert_equal __(true), Chihuahua.ancestors.include?(Dog)
28   end
29
30   def test_all_classes_ultimately_inherit_from_object
31     assert_equal __(true), Chihuahua.ancestors.include?(Object)
32   end
33
34   def test_subclasses_inherit_behavior_from_parent_class
35     chico = Chihuahua.new("Chico")
36     assert_equal __("Chico"), chico.name
37   end
38
39   def test_subclasses_add_new_behavior
40     chico = Chihuahua.new("Chico")
41     assert_equal __(:happy), chico.wag
42
43     assert_raise(___(NoMethodError)) do
44       fido = Dog.new("Fido")
45       fido.wag
46     end
47   end
48
49   def test_subclasses_can_modify_existing_behavior
50     chico = Chihuahua.new("Chico")
51     assert_equal __("yip"), chico.bark
52
53     fido = Dog.new("Fido")
54     assert_equal __("WOOF"), fido.bark
55   end
56
57   # ------------------------------------------------------------------
58
59   class BullDog < Dog
60     def bark
61       super + ", GROWL"
62     end
63   end
64
65   def test_subclasses_can_invoke_parent_behavior_via_super
66     ralph = BullDog.new("Ralph")
67     assert_equal __("WOOF, GROWL"), ralph.bark
68   end
69
70   # ------------------------------------------------------------------
71
72   class GreatDane < Dog
73     def growl
74       super.bark + ", GROWL"
75     end
76   end
77
78   def test_super_does_not_work_cross_method
79     george = GreatDane.new("George")
80     assert_raise(___(NoMethodError)) do
81       george.growl
82     end
83   end
84
85 end