few more enlightening tweaks
[ruby_koans.git] / koans / 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 __, 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 __, 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     eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
22     #
23     # Ruby doesn't know if you mean:
24     #
25     #   assert_equal(5, my_global_method(2), 3)
26     # or
27     #   assert_equal(5, my_global_method(2, 3))
28     #
29     # Rewrite the eval string to continue.
30     #
31   end
32
33   # NOTE: wrong number of argument is not a SYNTAX error, but a
34   # runtime error.
35   def test_calling_global_methods_with_wrong_number_of_arguments
36     exception = assert_raise(___) do
37       my_global_method
38     end
39     assert_match(/__/, exception.message)
40
41     exception = assert_raise(___) do
42       my_global_method(1,2,3)
43     end
44     assert_match(/__/, exception.message)
45   end
46
47   # ------------------------------------------------------------------
48
49   def method_with_defaults(a, b=:default_value)
50     [a, b]
51   end
52
53   def test_calling_with_default_values
54     assert_equal [1, __], method_with_defaults(1)
55     assert_equal [1, __], method_with_defaults(1, 2)
56   end
57
58   # ------------------------------------------------------------------
59
60   def method_with_var_args(*args)
61     args
62   end
63
64   def test_calling_with_variable_arguments
65     assert_equal __, method_with_var_args.class
66     assert_equal __, method_with_var_args
67     assert_equal __, method_with_var_args(:one)
68     assert_equal __, method_with_var_args(:one, :two)
69   end
70
71   # ------------------------------------------------------------------
72
73   def method_with_explicit_return
74     :a_non_return_value
75     return :return_value
76     :another_non_return_value
77   end
78
79   def test_method_with_explicit_return
80     assert_equal __, method_with_explicit_return
81   end
82
83   # ------------------------------------------------------------------
84
85   def method_without_explicit_return
86     :a_non_return_value
87     :return_value
88   end
89
90   def test_method_without_explicit_return
91     assert_equal __, method_without_explicit_return
92   end
93
94   # ------------------------------------------------------------------
95
96   def my_method_in_the_same_class(a, b)
97     a * b
98   end
99
100   def test_calling_methods_in_same_class
101     assert_equal __, my_method_in_the_same_class(3,4)
102   end
103
104   def test_calling_methods_in_same_class_with_explicit_receiver
105     assert_equal __, self.my_method_in_the_same_class(3,4)
106   end
107
108   # ------------------------------------------------------------------
109
110   def my_private_method
111     "a secret"
112   end
113   private :my_private_method
114
115   def test_calling_private_methods_without_receiver
116     assert_equal __, my_private_method
117   end
118
119   def test_calling_private_methods_with_an_explicit_receiver
120     exception = assert_raise(___) do
121       self.my_private_method
122     end
123     assert_match /__/, exception.message
124   end
125
126   # ------------------------------------------------------------------
127
128   class Dog
129     def name
130       "Fido"
131     end
132
133     private
134
135     def tail
136       "tail"
137     end
138   end
139
140   def test_calling_methods_in_other_objects_require_explicit_receiver
141     rover = Dog.new
142     assert_equal __, rover.name
143   end
144
145   def test_calling_private_methods_in_other_objects
146     rover = Dog.new
147     assert_raise(___) do
148       rover.tail
149     end
150   end
151 end