Updated require with yucky File.dirname(__FILE__) hacks.
[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_parenthesis
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_parenthesis_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     assert_match(/#{__("wrong number of arguments")}/, exception.message)
47
48     exception = assert_raise(___(ArgumentError)) do
49       my_global_method(1,2,3)
50     end
51     assert_match(/#{__("wrong number of arguments")}/, exception.message)
52   end
53
54   # ------------------------------------------------------------------
55
56   def method_with_defaults(a, b=:default_value)
57     [a, b]
58   end
59
60   def test_calling_with_default_values
61     assert_equal [1, __(:default_value)], method_with_defaults(1)
62     assert_equal [1, __(2)], method_with_defaults(1, 2)
63   end
64
65   # ------------------------------------------------------------------
66
67   def method_with_var_args(*args)
68     args
69   end
70
71   def test_calling_with_variable_arguments
72     assert_equal __([]), method_with_var_args
73     assert_equal __([:one]), method_with_var_args(:one)
74     assert_equal __([:one, :two]), method_with_var_args(:one, :two)
75   end
76
77   # ------------------------------------------------------------------
78
79   def method_with_explicit_return
80     :a_non_return_value
81     return :return_value
82     :another_non_return_value
83   end
84
85   def test_method_with_explicit_return
86     assert_equal __(:return_value), method_with_explicit_return
87   end
88
89   # ------------------------------------------------------------------
90
91   def method_without_explicit_return
92     :a_non_return_value
93     :return_value
94   end
95
96   def test_method_without_explicit_return
97     assert_equal __(:return_value), method_without_explicit_return
98   end
99
100   # ------------------------------------------------------------------
101
102   def my_same_class_method(a, b)
103     a * b
104   end
105
106   def test_calling_methods_in_same_class
107     assert_equal __(12), my_same_class_method(3,4)
108   end
109
110   def test_calling_methods_in_same_class_with_explicit_receiver
111     assert_equal __(12), self.my_same_class_method(3,4)
112   end
113
114   # ------------------------------------------------------------------
115
116   def my_private_method
117     "a secret"
118   end
119   private :my_private_method
120
121   def test_calling_private_methods_without_receiver
122     assert_equal __("a secret"), my_private_method
123   end
124
125   def test_calling_private_methods_with_an_explicit_receiver
126     exception = assert_raise(___(NoMethodError)) do
127       self.my_private_method
128     end
129     assert_match /#{__("private method `my_private_method' called ")}/, exception.message
130   end
131
132   # ------------------------------------------------------------------
133
134   class Dog
135     def name
136       "Fido"
137     end
138
139     private
140
141     def tail
142       "tail"
143     end
144   end
145   
146   def test_calling_methods_in_other_objects_require_explicit_receiver
147     rover = Dog.new
148     assert_equal __("Fido"), rover.name
149   end
150
151   def test_calling_private_methods_in_other_objects
152     rover = Dog.new
153     assert_raise(___(NoMethodError)) do
154       rover.tail
155     end
156   end
157 end