Skip to content

Commit d11989b

Browse files
committed
py2: Split up about_strings. Removed __name__ tests in favor of educating on __class__ in a wiki page. Added an extra __class__ koan in about_asserts as a primer
1 parent dd4afa6 commit d11989b

11 files changed

+122
-87
lines changed

python2/koans/about_asserts.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,31 @@ def test_a_better_way_of_asserting_equality(self):
4444

4545
def test_that_unittest_asserts_work_the_same_way_as_python_asserts(self):
4646
"""
47-
Knowing how things really work is half the battle
47+
Understand what lies within.
4848
"""
4949

5050
# This throws an AssertionError exception
5151
assert False
52+
53+
def test_that_sometimes_we_need_to_know_the_class_type(self):
54+
"""
55+
What is in a class name?
56+
"""
57+
58+
# Sometimes we will ask you what the class type of an object is.
59+
#
60+
# For example, contemplate the text string "naval". What is it's class type?
61+
# The koans runner will include this feedback for this koan:
62+
#
63+
# AssertionError: '-=> FILL ME IN! <=-' != <type 'str'>
64+
#
65+
# So "naval".__class__ is equal to <type 'str'>? No not quite. This
66+
# is just what it displays. The answer is simply str.
67+
#
68+
# See for yourself:
69+
70+
self.assertEqual(__, "naval".__class__) # It's str, not <type 'str'>
71+
72+
# Need an illustration? More reading can be found here:
73+
#
74+
# https://github.com/gregmalcolm/python_koans/wiki/Class-Attribute

python2/koans/about_attribute_access.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_calling_undefined_functions_normally_results_in_errors(self):
1919
try:
2020
typical.foobar()
2121
except Exception as exception:
22-
self.assertEqual(__, type(exception).__name__)
22+
self.assertEqual(__, exception.__class__)
2323
self.assertMatch(__, exception[0])
2424

2525
def test_calling_getattribute_causes_an_attribute_error(self):
@@ -160,7 +160,7 @@ def test_getattr_only_catches_unknown_attributes(self):
160160
catcher.free_pie()
161161

162162
self.assertEqual(__,
163-
type(catcher.give_me_duff_or_give_me_death()).__name__)
163+
catcher.give_me_duff_or_give_me_death().__class__)
164164

165165
self.assertEqual(__, catcher.no_of_getattr_calls)
166166

python2/koans/about_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Dog(object):
1010

1111
def test_instances_of_classes_can_be_created_adding_parentheses(self):
1212
fido = self.Dog()
13-
self.assertEqual(__, type(fido).__name__)
13+
self.assertEqual(__, fido.__class__)
1414

1515
def test_classes_have_docstrings(self):
1616
self.assertMatch(__, self.Dog.__doc__)

python2/koans/about_method_bindings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def test_get_descriptor_resolves_attribute_binding(self):
7777
# binding_owner = obj
7878
# owner_type = cls
7979

80-
self.assertEqual(__, type(bound_obj).__name__)
81-
self.assertEqual(__, type(binding_owner).__name__)
80+
self.assertEqual(__, bound_obj.__class__)
81+
self.assertEqual(__, binding_owner.__class__)
8282
self.assertEqual(AboutMethodBindings, owner_type)
8383

8484
# ------------------------------------------------------------------

python2/koans/about_methods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_calling_functions_with_wrong_number_of_arguments(self):
2222
try:
2323
my_global_function()
2424
except Exception as exception:
25-
self.assertEqual(__, type(exception).__name__)
25+
self.assertEqual(__, exception.__class__)
2626
self.assertMatch(
2727
r'my_global_function\(\) takes exactly 2 arguments \(0 given\)',
2828
exception[0])
@@ -158,7 +158,7 @@ def test_double_underscore_attribute_prefixes_cause_name_mangling(self):
158158
#This may not be possible...
159159
password = rover.__password()
160160
except Exception as ex:
161-
self.assertEqual(__, type(ex).__name__)
161+
self.assertEqual(__, ex.__class__)
162162

163163
# But this still is!
164164
self.assertEqual(__, rover._Dog__password())

python2/koans/about_new_style_classes.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_new_style_classes_have_more_attributes(self):
3434
# ------------------------------------------------------------------
3535

3636
def test_old_style_classes_have_type_but_no_class_attribute(self):
37-
self.assertEqual(__, type(self.OldStyleClass).__name__)
37+
self.assertEqual(__, self.OldStyleClass.__class__)
3838

3939
try:
4040
cls = self.OldStyleClass.__class__
@@ -45,7 +45,7 @@ def test_old_style_classes_have_type_but_no_class_attribute(self):
4545

4646
def test_new_style_classes_have_same_class_as_type(self):
4747
new_style = self.NewStyleClass()
48-
self.assertEqual(__, type(self.NewStyleClass).__name__)
48+
self.assertEqual(__, self.NewStyleClass.__class__)
4949
self.assertEqual(
5050
__,
5151
type(self.NewStyleClass) == self.NewStyleClass.__class__)
@@ -54,10 +54,9 @@ def test_new_style_classes_have_same_class_as_type(self):
5454

5555
def test_in_old_style_instances_class_is_different_to_type(self):
5656
old_style = self.OldStyleClass()
57-
self.assertEqual(__, type(old_style).__name__)
58-
self.assertEqual(__, old_style.__class__.__name__)
57+
self.assertEqual(__, old_style.__class__)
5958

6059
def test_new_style_instances_have_same_class_as_type(self):
6160
new_style = self.NewStyleClass()
62-
self.assertEqual(__, type(new_style).__name__)
61+
self.assertEqual(__, new_style.__class__)
6362
self.assertEqual(__, type(new_style) == new_style.__class__)

python2/koans/about_none.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def test_what_exception_do_you_get_when_calling_nonexistent_methods(self):
3333
None.some_method_none_does_not_know_about()
3434
except Exception as ex:
3535
# What exception has been caught?
36-
self.assertEqual(__, ex.__class__.__name__)
36+
#
37+
# Need a recap on how to evaluate __class__ attributes?
38+
# https://github.com/gregmalcolm/python_koans/wiki/Class-Attribute
39+
40+
self.assertEqual(__, ex.__class__)
3741

3842
# What message was attached to the exception?
3943
# (HINT: replace __ with part of the error message.)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from runner.koan import *
5+
6+
7+
class AboutStringManipulation(Koan):
8+
9+
def test_use_format_to_interpolate_variables(self):
10+
value1 = 'one'
11+
value2 = 2
12+
string = "The values are {0} and {1}".format(value1, value2)
13+
self.assertEqual(__, string)
14+
15+
def test_formatted_values_can_be_shown_in_any_order_or_be_repeated(self):
16+
value1 = 'doh'
17+
value2 = 'DOH'
18+
string = "The values are {1}, {0}, {0} and {1}!".format(value1, value2)
19+
self.assertEqual(__, string)
20+
21+
def test_any_python_expression_may_be_interpolated(self):
22+
import math # import a standard python module with math functions
23+
24+
decimal_places = 4
25+
string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \
26+
decimal_places)
27+
self.assertEqual(__, string)
28+
29+
def test_you_can_get_a_substring_from_a_string(self):
30+
string = "Bacon, lettuce and tomato"
31+
self.assertEqual(__, string[7:10])
32+
33+
def test_you_can_get_a_single_character_from_a_string(self):
34+
string = "Bacon, lettuce and tomato"
35+
self.assertEqual(__, string[1])
36+
37+
def test_single_characters_can_be_represented_by_integers(self):
38+
self.assertEqual(__, ord('a'))
39+
self.assertEqual(__, ord('b') == (ord('a') + 1))
40+
41+
def test_strings_can_be_split(self):
42+
string = "Sausage Egg Cheese"
43+
words = string.split()
44+
self.assertEqual([__, __, __], words)
45+
46+
def test_strings_can_be_split_with_different_patterns(self):
47+
import re # import python regular expression library
48+
49+
string = "the,rain;in,spain"
50+
pattern = re.compile(',|;')
51+
52+
words = pattern.split(string)
53+
54+
self.assertEqual([__, __, __, __], words)
55+
56+
# `pattern` is a Python regular expression pattern which matches
57+
# ',' or ';'
58+
59+
def test_raw_strings_do_not_interpret_escape_characters(self):
60+
string = r'\n'
61+
self.assertNotEqual('\n', string)
62+
self.assertEqual(__, string)
63+
self.assertEqual(__, len(string))
64+
65+
# Useful in regular expressions, file paths, URLs, etc.
66+
67+
def test_strings_can_be_joined(self):
68+
words = ["Now", "is", "the", "time"]
69+
self.assertEqual(__, ' '.join(words))
70+
71+
def test_strings_can_change_case(self):
72+
self.assertEqual(__, 'guido'.capitalize())
73+
self.assertEqual(__, 'guido'.upper())
74+
self.assertEqual(__, 'TimBot'.lower())
75+
self.assertEqual(__, 'guido van rossum'.title())
76+
self.assertEqual(__, 'ToTaLlY aWeSoMe'.swapcase())

python2/koans/about_strings.py

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -92,72 +92,3 @@ def test_most_strings_interpret_escape_characters(self):
9292
self.assertEqual('\n', string)
9393
self.assertEqual("""\n""", string)
9494
self.assertEqual(__, len(string))
95-
96-
def test_use_format_to_interpolate_variables(self):
97-
value1 = 'one'
98-
value2 = 2
99-
string = "The values are {0} and {1}".format(value1, value2)
100-
self.assertEqual(__, string)
101-
102-
def test_formatted_values_can_be_shown_in_any_order_or_be_repeated(self):
103-
value1 = 'doh'
104-
value2 = 'DOH'
105-
string = "The values are {1}, {0}, {0} and {1}!".format(value1, value2)
106-
self.assertEqual(__, string)
107-
108-
def test_any_python_expression_may_be_interpolated(self):
109-
import math # import a standard python module with math functions
110-
111-
decimal_places = 4
112-
string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \
113-
decimal_places)
114-
self.assertEqual(__, string)
115-
116-
def test_you_can_get_a_substring_from_a_string(self):
117-
string = "Bacon, lettuce and tomato"
118-
self.assertEqual(__, string[7:10])
119-
120-
def test_you_can_get_a_single_character_from_a_string(self):
121-
string = "Bacon, lettuce and tomato"
122-
self.assertEqual(__, string[1])
123-
124-
def test_single_characters_can_be_represented_by_integers(self):
125-
self.assertEqual(__, ord('a'))
126-
self.assertEqual(__, ord('b') == (ord('a') + 1))
127-
128-
def test_strings_can_be_split(self):
129-
string = "Sausage Egg Cheese"
130-
words = string.split()
131-
self.assertEqual([__, __, __], words)
132-
133-
def test_strings_can_be_split_with_different_patterns(self):
134-
import re # import python regular expression library
135-
136-
string = "the,rain;in,spain"
137-
pattern = re.compile(',|;')
138-
139-
words = pattern.split(string)
140-
141-
self.assertEqual([__, __, __, __], words)
142-
143-
# `pattern` is a Python regular expression pattern which matches
144-
# ',' or ';'
145-
146-
def test_raw_strings_do_not_interpret_escape_characters(self):
147-
string = r'\n'
148-
self.assertNotEqual('\n', string)
149-
self.assertEqual(__, string)
150-
self.assertEqual(__, len(string))
151-
152-
# Useful in regular expressions, file paths, URLs, etc.
153-
154-
def test_strings_can_be_joined(self):
155-
words = ["Now", "is", "the", "time"]
156-
self.assertEqual(__, ' '.join(words))
157-
158-
def test_strings_can_change_case(self):
159-
self.assertEqual(__, 'guido'.capitalize())
160-
self.assertEqual(__, 'guido'.upper())
161-
self.assertEqual(__, 'TimBot'.lower())
162-
self.assertEqual(__, 'guido van rossum'.title())
163-
self.assertEqual(__, 'ToTaLlY aWeSoMe'.swapcase())

python2/koans/about_tuples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def test_tuples_can_only_be_changed_through_replacement(self):
3939
self.assertEqual(__, count_of_three)
4040

4141
def test_tuples_of_one_look_peculiar(self):
42-
self.assertEqual(__, (1).__class__.__name__)
43-
self.assertEqual(__, (1,).__class__.__name__)
42+
self.assertEqual(__, (1).__class__)
43+
self.assertEqual(__, (1,).__class__)
4444
self.assertEqual(__, ("Hello comma!", ))
4545

4646
def test_tuple_constructor_can_be_surprising(self):

python2/runner/path_to_enlightenment.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import unittest
77

88
from koans.about_asserts import AboutAsserts
9+
from koans.about_strings import AboutStrings
910
from koans.about_none import AboutNone
1011
from koans.about_lists import AboutLists
1112
from koans.about_list_assignments import AboutListAssignments
13+
from koans.about_string_manipulation import AboutStringManipulation
1214
from koans.about_dictionaries import AboutDictionaries
13-
from koans.about_strings import AboutStrings
1415
from koans.about_tuples import AboutTuples
1516
from koans.about_methods import AboutMethods
1617
from koans.about_control_statements import AboutControlStatements
@@ -48,11 +49,12 @@ def koans():
4849
suite = unittest.TestSuite()
4950
loader.sortTestMethodsUsing = None
5051
suite.addTests(loader.loadTestsFromTestCase(AboutAsserts))
52+
suite.addTests(loader.loadTestsFromTestCase(AboutStrings))
5153
suite.addTests(loader.loadTestsFromTestCase(AboutNone))
5254
suite.addTests(loader.loadTestsFromTestCase(AboutLists))
5355
suite.addTests(loader.loadTestsFromTestCase(AboutListAssignments))
56+
suite.addTests(loader.loadTestsFromTestCase(AboutStringManipulation))
5457
suite.addTests(loader.loadTestsFromTestCase(AboutDictionaries))
55-
suite.addTests(loader.loadTestsFromTestCase(AboutStrings))
5658
suite.addTests(loader.loadTestsFromTestCase(AboutTuples))
5759
suite.addTests(loader.loadTestsFromTestCase(AboutMethods))
5860
suite.addTests(loader.loadTestsFromTestCase(AboutControlStatements))

0 commit comments

Comments
 (0)