1497f9ba2e944f4559fafdf5917f31b6000e7176
[ruby_koans.git] / src / about_methods.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 def my_global_method(a,b)
4   a + b
5 end
6
7 class AboutMethods < EdgeCase::Koan
8
9   def test_calling_global_methods
10     assert_equal __(5), my_global_method(2,3)
11   end
12
13   def test_calling_global_methods_without_parentheses
14     result = my_global_method 2, 3
15     assert_equal __(5), result
16   end
17
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
21     #--
22     eval "assert_equal 5, my_global_method(2, 3)" # REMOVE CHECK # __
23     if false
24       #++
25     eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK # __
26       #--
27     end
28     #++
29     #
30     # Ruby doesn't know if you mean:
31     #
32     #   assert_equal(5, my_global_method(2), 3)
33     # or
34     #   assert_equal(5, my_global_method(2, 3))
35     #
36     # Rewrite the eval string to continue.
37     #
38   end
39
40   # NOTE: wrong number of argument is not a SYNTAX error, but a
41   # runtime error.
42   def test_calling_global_methods_with_wrong_number_of_arguments
43     exception = assert_raise(___(ArgumentError)) do
44       my_global_method
45     end
46     #--
47     pattern = "wrong (number|#) of arguments"
48     #++
49     assert_match(/#{__(pattern)}/, exception.message)
50
51     exception = assert_raise(___(ArgumentError)) do
52       my_global_method(1,2,3)
53     end
54     assert_match(/#{__(pattern)}/, exception.message)
55   end
56
57   # ------------------------------------------------------------------
58
59   def method_with_defaults(a, b=:default_value)
60     [a, b]
61   end
62
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)
66   end
67
68   # ------------------------------------------------------------------
69
70   def method_with_var_args(*args)
71     args
72   end
73
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)
78   end
79
80   # ------------------------------------------------------------------
81
82   def method_with_explicit_return
83     :a_non_return_value
84     return :return_value
85     :another_non_return_value
86   end
87
88   def test_method_with_explicit_return
89     assert_equal __(:return_value), method_with_explicit_return
90   end
91
92   # ------------------------------------------------------------------
93
94   def method_without_explicit_return
95     :a_non_return_value
96     :return_value
97   end
98
99   def test_method_without_explicit_return
100     assert_equal __(:return_value), method_without_explicit_return
101   end
102
103   # ------------------------------------------------------------------
104
105   def my_same_class_method(a, b)
106     a * b
107   end
108
109   def test_calling_methods_in_same_class
110     assert_equal __(12), my_same_class_method(3,4)
111   end
112
113   def test_calling_methods_in_same_class_with_explicit_receiver
114     assert_equal __(12), self.my_same_class_method(3,4)
115   end
116
117   # ------------------------------------------------------------------
118
119   def my_private_method
120     "a secret"
121   end
122   private :my_private_method
123
124   def test_calling_private_methods_without_receiver
125     assert_equal __("a secret"), my_private_method
126   end
127
128   def test_calling_private_methods_with_an_explicit_receiver
129     exception = assert_raise(___(NoMethodError)) do
130       self.my_private_method
131     end
132     assert_match /#{__("private method `my_private_method' called ")}/, exception.message
133   end
134
135   # ------------------------------------------------------------------
136
137   class Dog
138     def name
139       "Fido"
140     end
141
142     private
143
144     def tail
145       "tail"
146     end
147   end
148
149   def test_calling_methods_in_other_objects_require_explicit_receiver
150     rover = Dog.new
151     assert_equal __("Fido"), rover.name
152   end
153
154   def test_calling_private_methods_in_other_objects
155     rover = Dog.new
156     assert_raise(___(NoMethodError)) do
157       rover.tail
158     end
159   end
160 end