1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 def my_global_method(a,b)
7 class AboutMethods < EdgeCase::Koan
9 def test_calling_global_methods
10 assert_equal __(5), my_global_method(2,3)
13 def test_calling_global_methods_without_parentheses
14 result = my_global_method 2, 3
15 assert_equal __(5), result
18 # (NOTE: We are Using eval below because the example code is
19 # considered to be syntactically invalid).
20 def test_sometimes_missing_parentheses_are_ambiguous
22 eval "assert_equal 5, my_global_method(2, 3)" # REMOVE CHECK # __
25 eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK # __
30 # Ruby doesn't know if you mean:
32 # assert_equal(5, my_global_method(2), 3)
34 # assert_equal(5, my_global_method(2, 3))
36 # Rewrite the eval string to continue.
40 # NOTE: wrong number of argument is not a SYNTAX error, but a
42 def test_calling_global_methods_with_wrong_number_of_arguments
43 exception = assert_raise(___(ArgumentError)) do
47 pattern = "wrong (number|#) of arguments"
49 assert_match(/#{__(pattern)}/, exception.message)
51 exception = assert_raise(___(ArgumentError)) do
52 my_global_method(1,2,3)
54 assert_match(/#{__(pattern)}/, exception.message)
57 # ------------------------------------------------------------------
59 def method_with_defaults(a, b=:default_value)
63 def test_calling_with_default_values
64 assert_equal [1, __(:default_value)], method_with_defaults(1)
65 assert_equal [1, __(2)], method_with_defaults(1, 2)
68 # ------------------------------------------------------------------
70 def method_with_var_args(*args)
74 def test_calling_with_variable_arguments
75 assert_equal __([]), method_with_var_args
76 assert_equal __([:one]), method_with_var_args(:one)
77 assert_equal __([:one, :two]), method_with_var_args(:one, :two)
80 # ------------------------------------------------------------------
82 def method_with_explicit_return
85 :another_non_return_value
88 def test_method_with_explicit_return
89 assert_equal __(:return_value), method_with_explicit_return
92 # ------------------------------------------------------------------
94 def method_without_explicit_return
99 def test_method_without_explicit_return
100 assert_equal __(:return_value), method_without_explicit_return
103 # ------------------------------------------------------------------
105 def my_method_in_the_same_class(a, b)
109 def test_calling_methods_in_same_class
110 assert_equal __(12), my_method_in_the_same_class(3,4)
113 def test_calling_methods_in_same_class_with_explicit_receiver
114 assert_equal __(12), self.my_method_in_the_same_class(3,4)
117 # ------------------------------------------------------------------
119 def my_private_method
122 private :my_private_method
124 def test_calling_private_methods_without_receiver
125 assert_equal __("a secret"), my_private_method
128 def test_calling_private_methods_with_an_explicit_receiver
129 exception = assert_raise(___(NoMethodError)) do
130 self.my_private_method
132 assert_match /#{__("private method `my_private_method' called ")}/, exception.message
135 # ------------------------------------------------------------------
149 def test_calling_methods_in_other_objects_require_explicit_receiver
151 assert_equal __("Fido"), rover.name
154 def test_calling_private_methods_in_other_objects
156 assert_raise(___(NoMethodError)) do