Skip to content

Commit 949bdbc

Browse files
committed
Completed about_methods
1 parent 4c20af6 commit 949bdbc

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

python2/koans/about_methods.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def my_global_function(a, b):
1414

1515
class AboutMethods(Koan):
1616
def test_calling_a_global_function(self):
17-
self.assertEqual(__, my_global_function(2, 3))
17+
self.assertEqual(5, my_global_function(2, 3))
1818

1919
# NOTE: Wrong number of arguments is not a SYNTAX error, but a
2020
# runtime error.
@@ -24,7 +24,7 @@ def test_calling_functions_with_wrong_number_of_arguments(self):
2424
except Exception as exception:
2525
# NOTE: The .__name__ attribute will convert the class
2626
# into a string value.
27-
self.assertEqual(__, exception.__class__.__name__)
27+
self.assertEqual('TypeError', exception.__class__.__name__)
2828
self.assertMatch(
2929
r'my_global_function\(\) takes exactly 2 arguments \(0 given\)',
3030
exception[0])
@@ -34,15 +34,15 @@ def test_calling_functions_with_wrong_number_of_arguments(self):
3434
except Exception as e:
3535

3636
# Note, watch out for parenthesis. They need slashes in front!
37-
self.assertMatch(__, e[0])
37+
self.assertMatch('.* 2 arguments .*', e[0])
3838

3939
# ------------------------------------------------------------------
4040

4141
def pointless_method(self, a, b):
4242
sum = a + b
4343

4444
def test_which_does_not_return_anything(self):
45-
self.assertEqual(__, self.pointless_method(1, 2))
45+
self.assertEqual(None, self.pointless_method(1, 2))
4646
# Notice that methods accessed from class scope do not require
4747
# you to pass the first "self" argument?
4848

@@ -52,18 +52,19 @@ def method_with_defaults(self, a, b='default_value'):
5252
return [a, b]
5353

5454
def test_calling_with_default_values(self):
55-
self.assertEqual(__, self.method_with_defaults(1))
56-
self.assertEqual(__, self.method_with_defaults(1, 2))
55+
self.assertEqual([1, 'default_value'], self.method_with_defaults(1))
56+
self.assertEqual([1, 2], self.method_with_defaults(1, 2))
5757

5858
# ------------------------------------------------------------------
5959

6060
def method_with_var_args(self, *args):
6161
return args
6262

6363
def test_calling_with_variable_arguments(self):
64-
self.assertEqual(__, self.method_with_var_args())
64+
# Var args are converted into a tuple that contains them
65+
self.assertEqual((), self.method_with_var_args())
6566
self.assertEqual(('one', ), self.method_with_var_args('one'))
66-
self.assertEqual(__, self.method_with_var_args('one', 'two'))
67+
self.assertEqual(('one', 'two'), self.method_with_var_args('one', 'two'))
6768

6869
# ------------------------------------------------------------------
6970

@@ -74,13 +75,13 @@ def test_functions_without_self_arg_are_global_functions(self):
7475
def function_with_the_same_name(a, b):
7576
return a * b
7677

77-
self.assertEqual(__, function_with_the_same_name(3, 4))
78+
self.assertEqual(12, function_with_the_same_name(3, 4))
7879

7980
def test_calling_methods_in_same_class_with_explicit_receiver(self):
8081
def function_with_the_same_name(a, b):
8182
return a * b
8283

83-
self.assertEqual(__, self.function_with_the_same_name(3, 4))
84+
self.assertEqual(7, self.function_with_the_same_name(3, 4))
8485

8586
# ------------------------------------------------------------------
8687

@@ -93,32 +94,32 @@ def another_method_with_the_same_name(self):
9394
return 42
9495

9596
def test_that_old_methods_are_hidden_by_redefinitions(self):
96-
self.assertEqual(__, self.another_method_with_the_same_name())
97+
self.assertEqual(42, self.another_method_with_the_same_name())
9798

9899
def test_that_overlapped_method_is_still_there(self):
99-
self.assertEqual(__, self.link_to_overlapped_method())
100+
self.assertEqual(10, self.link_to_overlapped_method())
100101

101102
# ------------------------------------------------------------------
102103

103104
def empty_method(self):
104105
pass
105106

106107
def test_methods_that_do_nothing_need_to_use_pass_as_a_filler(self):
107-
self.assertEqual(__, self.empty_method())
108+
self.assertEqual(None, self.empty_method())
108109

109110
def test_pass_does_nothing_at_all(self):
110111
"You"
111112
"shall"
112113
"not"
113114
pass
114-
self.assertEqual(____, "Still got to this line" != None)
115+
self.assertEqual(True, "Still got to this line" != None)
115116

116117
# ------------------------------------------------------------------
117118

118119
def one_line_method(self): return 'Madagascar'
119120

120121
def test_no_indentation_required_for_one_line_statement_bodies(self):
121-
self.assertEqual(__, self.one_line_method())
122+
self.assertEqual('Madagascar', self.one_line_method())
122123

123124
# ------------------------------------------------------------------
124125

@@ -127,7 +128,8 @@ def method_with_documentation(self):
127128
return "ok"
128129

129130
def test_the_documentation_can_be_viewed_with_the_doc_method(self):
130-
self.assertMatch(__, self.method_with_documentation.__doc__)
131+
self.assertMatch("A string placed at the beginning of a function is " + \
132+
"used for documentation", self.method_with_documentation.__doc__)
131133

132134
# ------------------------------------------------------------------
133135

@@ -144,13 +146,13 @@ def __password(self):
144146

145147
def test_calling_methods_in_other_objects(self):
146148
rover = self.Dog()
147-
self.assertEqual(__, rover.name())
149+
self.assertEqual("Fido", rover.name())
148150

149151
def test_private_access_is_implied_but_not_enforced(self):
150152
rover = self.Dog()
151153

152154
# This is a little rude, but legal
153-
self.assertEqual(__, rover._tail())
155+
self.assertEqual("wagging", rover._tail())
154156

155157
def test_double_underscore_attribute_prefixes_cause_name_mangling(self):
156158
"""Attributes names that start with a double underscore get
@@ -160,10 +162,10 @@ def test_double_underscore_attribute_prefixes_cause_name_mangling(self):
160162
#This may not be possible...
161163
password = rover.__password()
162164
except Exception as ex:
163-
self.assertEqual(__, ex.__class__.__name__)
165+
self.assertEqual('AttributeError', ex.__class__.__name__)
164166

165167
# But this still is!
166-
self.assertEqual(__, rover._Dog__password())
168+
self.assertEqual('password', rover._Dog__password())
167169

168170
# Name mangling exists to avoid name clash issues when subclassing.
169171
# It is not for providing effective access protection

0 commit comments

Comments
 (0)