Skip to content

Commit 85452b8

Browse files
author
Andy Trigg
committed
Completed the about_attribute_access.py koans
1 parent 4b0e6aa commit 85452b8

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

python3/koans/about_attribute_access.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class TypicalObject:
1515
def test_calling_undefined_functions_normally_results_in_errors(self):
1616
typical = self.TypicalObject()
1717

18-
with self.assertRaises(___): typical.foobar()
18+
with self.assertRaises(AttributeError): typical.foobar()
1919

2020
def test_calling_getattribute_causes_an_attribute_error(self):
2121
typical = self.TypicalObject()
2222

23-
with self.assertRaises(___): typical.__getattribute__('foobar')
23+
with self.assertRaises(AttributeError): typical.__getattribute__('foobar')
2424

2525
# THINK ABOUT IT:
2626
#
@@ -36,19 +36,19 @@ def __getattribute__(self, attr_name):
3636
def test_all_attribute_reads_are_caught(self):
3737
catcher = self.CatchAllAttributeReads()
3838

39-
self.assertRegex(catcher.foobar, __)
39+
self.assertRegex(catcher.foobar, "Someone called 'foobar' and it could not be found")
4040

4141
def test_intercepting_return_values_can_disrupt_the_call_chain(self):
4242
catcher = self.CatchAllAttributeReads()
4343

44-
self.assertRegex(catcher.foobaz, __) # This is fine
44+
self.assertRegex(catcher.foobaz, "Someone called 'foobaz' and it could not be found") # This is fine
4545

4646
try:
4747
catcher.foobaz(1)
4848
except TypeError as ex:
4949
err_msg = ex.args[0]
5050

51-
self.assertRegex(err_msg, __)
51+
self.assertRegex(err_msg, "'str' object is not callable")
5252

5353
# foobaz returns a string. What happens to the '(1)' part?
5454
# Try entering this into a python console to reproduce the issue:
@@ -59,7 +59,7 @@ def test_intercepting_return_values_can_disrupt_the_call_chain(self):
5959
def test_changes_to_the_getattribute_implementation_affects_getattr_function(self):
6060
catcher = self.CatchAllAttributeReads()
6161

62-
self.assertRegex(getattr(catcher, 'any_attribute'), __)
62+
self.assertRegex(getattr(catcher, 'any_attribute'), "Someone called 'any_attribute' and it could not be found")
6363

6464
# ------------------------------------------------------------------
6565

@@ -73,13 +73,13 @@ def __getattribute__(self, attr_name):
7373
def test_foo_attributes_are_caught(self):
7474
catcher = self.WellBehavedFooCatcher()
7575

76-
self.assertEqual(__, catcher.foo_bar)
77-
self.assertEqual(__, catcher.foo_baz)
76+
self.assertEqual("Foo to you too", catcher.foo_bar)
77+
self.assertEqual("Foo to you too", catcher.foo_baz)
7878

7979
def test_non_foo_messages_are_treated_normally(self):
8080
catcher = self.WellBehavedFooCatcher()
8181

82-
with self.assertRaises(___): catcher.normal_undefined_attribute
82+
with self.assertRaises(AttributeError): catcher.normal_undefined_attribute
8383

8484
# ------------------------------------------------------------------
8585

@@ -114,7 +114,7 @@ def test_getattribute_is_a_bit_overzealous_sometimes(self):
114114
catcher = self.RecursiveCatcher()
115115
catcher.my_method()
116116
global stack_depth
117-
self.assertEqual(__, stack_depth)
117+
self.assertEqual(11, stack_depth)
118118

119119
# ------------------------------------------------------------------
120120

@@ -135,17 +135,17 @@ def test_getattr_ignores_known_attributes(self):
135135
catcher = self.MinimalCatcher()
136136
catcher.my_method()
137137

138-
self.assertEqual(__, catcher.no_of_getattr_calls)
138+
self.assertEqual(0, catcher.no_of_getattr_calls)
139139

140140
def test_getattr_only_catches_unknown_attributes(self):
141141
catcher = self.MinimalCatcher()
142142
catcher.purple_flamingos()
143143
catcher.free_pie()
144144

145-
self.assertEqual(__,
145+
self.assertEqual("DuffObject",
146146
type(catcher.give_me_duff_or_give_me_death()).__name__)
147147

148-
self.assertEqual(__, catcher.no_of_getattr_calls)
148+
self.assertEqual(3, catcher.no_of_getattr_calls)
149149

150150
# ------------------------------------------------------------------
151151

@@ -166,13 +166,13 @@ def test_setattr_intercepts_attribute_assignments(self):
166166
fanboy.comic = 'The Laminator, issue #1'
167167
fanboy.pie = 'blueberry'
168168

169-
self.assertEqual(__, fanboy.a_pie)
169+
self.assertEqual("blueberry", fanboy.a_pie)
170170

171171
#
172172
# NOTE: Change the prefix to make this next assert pass
173173
#
174174

175-
prefix = '__'
175+
prefix = 'my'
176176
self.assertEqual("The Laminator, issue #1", getattr(fanboy, prefix + '_comic'))
177177

178178
# ------------------------------------------------------------------
@@ -194,17 +194,17 @@ def test_it_modifies_external_attribute_as_expected(self):
194194
setter = self.ScarySetter()
195195
setter.e = "mc hammer"
196196

197-
self.assertEqual(__, setter.altered_e)
197+
self.assertEqual("mc hammer", setter.altered_e)
198198

199199
def test_it_mangles_some_internal_attributes(self):
200200
setter = self.ScarySetter()
201201

202202
try:
203203
coconuts = setter.num_of_coconuts
204204
except AttributeError:
205-
self.assertEqual(__, setter.altered_num_of_coconuts)
205+
self.assertEqual(9, setter.altered_num_of_coconuts)
206206

207207
def test_in_this_case_private_attributes_remain_unmangled(self):
208208
setter = self.ScarySetter()
209209

210-
self.assertEqual(__, setter._num_of_private_coconuts)
210+
self.assertEqual(2, setter._num_of_private_coconuts)

0 commit comments

Comments
 (0)