-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_inheritance.rb
85 lines (66 loc) · 1.65 KB
/
about_inheritance.rb
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
82
83
84
85
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutInheritance < Neo::Koan
class Dog
attr_reader :name
def initialize(name)
@name = name
end
def bark
"WOOF"
end
end
class Chihuahua < Dog
def wag
:happy
end
def bark
"yip"
end
end
def test_subclasses_have_the_parent_as_an_ancestor
assert_equal true, Chihuahua.ancestors.include?(Dog)
end
def test_all_classes_ultimately_inherit_from_object
assert_equal true, Chihuahua.ancestors.include?(Object)
end
def test_subclasses_inherit_behavior_from_parent_class
chico = Chihuahua.new("Chico")
assert_equal 'Chico', chico.name
end
def test_subclasses_add_new_behavior
chico = Chihuahua.new("Chico")
assert_equal :happy, chico.wag
assert_raise(NoMethodError) do
fido = Dog.new("Fido")
fido.wag
end
end
def test_subclasses_can_modify_existing_behavior
chico = Chihuahua.new("Chico")
assert_equal 'yip', chico.bark
fido = Dog.new("Fido")
assert_equal 'WOOF', fido.bark
end
# ------------------------------------------------------------------
class BullDog < Dog
def bark
super + ", GROWL"
end
end
def test_subclasses_can_invoke_parent_behavior_via_super
ralph = BullDog.new("Ralph")
assert_equal 'WOOF, GROWL', ralph.bark
end
# ------------------------------------------------------------------
class GreatDane < Dog
def growl
super.bark + ", GROWL"
end
end
def test_super_does_not_work_cross_method
george = GreatDane.new("George")
assert_raise(NoMethodError) do
george.growl
end
end
end