Added src directory

This commit is contained in:
Jim Weirich
2009-12-21 15:03:20 -05:00
parent f3af22b96f
commit 47c6c6f8da
38 changed files with 2745 additions and 0 deletions

85
src/about_inheritance.rb Normal file
View File

@@ -0,0 +1,85 @@
require 'edgecase'
class AboutInheritance < EdgeCase::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_subcases_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