Back ported a lot of changes made to the Koans directory.
[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 __(Array), method_with_var_args.class
76     assert_equal __([]), method_with_var_args
77     assert_equal __([:one]), method_with_var_args(:one)
78     assert_equal __([:one, :two]), method_with_var_args(:one, :two)
79   end
80
81   # ------------------------------------------------------------------
82
83   def method_with_explicit_return
84     :a_non_return_value
85     return :return_value
86     :another_non_return_value
87   end
88
89   def test_method_with_explicit_return
90     assert_equal __(:return_value), method_with_explicit_return
91   end
92
93   # ------------------------------------------------------------------
94
95   def method_without_explicit_return
96     :a_non_return_value
97     :return_value
98   end
99
100   def test_method_without_explicit_return
101     assert_equal __(:return_value), method_without_explicit_return
102   end
103
104   # ------------------------------------------------------------------
105
106   def my_method_in_the_same_class(a, b)
107     a * b
108   end
109
110   def test_calling_methods_in_same_class
111     assert_equal __(12), my_method_in_the_same_class(3,4)
112   end
113
114   def test_calling_methods_in_same_class_with_explicit_receiver
115     assert_equal __(12), self.my_method_in_the_same_class(3,4)
116   end
117
118   # ------------------------------------------------------------------
119
120   def my_private_method
121     "a secret"
122   end
123   private :my_private_method
124
125   def test_calling_private_methods_without_receiver
126     assert_equal __("a secret"), my_private_method
127   end
128
129   def test_calling_private_methods_with_an_explicit_receiver
130     exception = assert_raise(___(NoMethodError)) do
131       self.my_private_method
132     end
133     assert_match /#{__("private method `my_private_method' called ")}/, exception.message
134   end
135
136   # ------------------------------------------------------------------
137
138   class Dog
139     def name
140       "Fido"
141     end
142
143     private
144
145     def tail
146       "tail"
147     end
148   end
149
150   def test_calling_methods_in_other_objects_require_explicit_receiver
151     rover = Dog.new
152     assert_equal __("Fido"), rover.name
153   end
154
155   def test_calling_private_methods_in_other_objects
156     rover = Dog.new
157     assert_raise(___(NoMethodError)) do
158       rover.tail
159     end
160   end
161 end