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
100 # ------------------------------------------------------------------
102 LastExpressionInClassStatement = class Dog
106 def test_class_statements_return_the_value_of_their_last_expression
107 assert_equal __(21), LastExpressionInClassStatement
110 # ------------------------------------------------------------------
112 SelfInsideOfClassStatement = class Dog
116 def test_self_while_inside_class_is_class_object_not_instance
117 assert_equal __(true), Dog == SelfInsideOfClassStatement
120 # ------------------------------------------------------------------
123 def self.class_method2
124 :another_way_to_write_class_methods
128 def test_you_can_use_self_instead_of_an_explicit_reference_to_dog
129 assert_equal __(:another_way_to_write_class_methods), Dog.class_method2
132 # ------------------------------------------------------------------
136 def another_class_method
142 def test_heres_still_another_way_to_write_class_methods
143 assert_equal __(:still_another_way), Dog.another_class_method
148 # The two major ways to write class methods are:
159 # Which do you prefer and why?
160 # Are there times you might prefer one over the other?
162 # ------------------------------------------------------------------
164 def test_heres_an_easy_way_to_call_class_methods_from_instance_methods
166 assert_equal __(:still_another_way), fido.class.another_class_method