1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutClassMethods < EdgeCase::Koan
7 def test_objects_are_objects
9 assert_equal __(true), fido.is_a?(Object)
12 def test_classes_are_classes
13 assert_equal __(true), Dog.is_a?(Class)
16 def test_classes_are_objects_too
17 assert_equal __(true), Dog.is_a?(Object)
20 def test_objects_have_methods
22 assert fido.methods.size > _n_(30)
25 def test_classes_have_methods
26 assert Dog.methods.size > _n_(40)
29 def test_you_can_define_methods_on_individual_objects
34 assert_equal __(:fidos_wag), fido.wag
37 def test_other_objects_are_not_affected_by_these_singleton_methods
44 assert_raise(___(NoMethodError)) do
49 # ------------------------------------------------------------------
61 def test_since_classes_are_objects_you_can_define_singleton_methods_on_them_too
62 assert_equal __(:class_level_wag), Dog2.wag
65 def test_class_methods_are_independent_of_instance_methods
67 assert_equal __(:instance_level_wag), fido.wag
68 assert_equal __(:class_level_wag), Dog2.wag
71 # ------------------------------------------------------------------
81 def test_classes_and_instances_do_not_share_instance_variables
84 assert_equal __("Fido"), fido.name
85 assert_equal __(nil), Dog.name
88 # ------------------------------------------------------------------
91 def Dog.a_class_method
96 def test_you_can_define_class_methods_inside_the_class
97 assert_equal __(:dogs_class_method), Dog.a_class_method
101 # ------------------------------------------------------------------
103 LastExpressionInClassStatement = class Dog
107 def test_class_statements_return_the_value_of_their_last_expression
108 assert_equal __(21), LastExpressionInClassStatement
111 # ------------------------------------------------------------------
113 SelfInsideOfClassStatement = class Dog
117 def test_self_while_inside_class_is_class_object_not_instance
118 assert_equal __(true), Dog == SelfInsideOfClassStatement
121 # ------------------------------------------------------------------
124 def self.class_method2
125 :another_way_to_write_class_methods
129 def test_you_can_use_self_instead_of_an_explicit_reference_to_dog
130 assert_equal __(:another_way_to_write_class_methods), Dog.class_method2
133 # ------------------------------------------------------------------
137 def another_class_method
143 def test_heres_still_another_way_to_write_class_methods
144 assert_equal __(:still_another_way), Dog.another_class_method
149 # The two major ways to write class methods are:
160 # Which do you prefer and why?
161 # Are there times you might prefer one over the other?
163 # ------------------------------------------------------------------
165 def test_heres_an_easy_way_to_call_class_methods_from_instance_methods
167 assert_equal __(:still_another_way), fido.class.another_class_method