Commits

Greg Malcolm committed 2e9d571

Changed Py3.1 so that it shows multiple line errors. Also made more minor improvements to Py2.6.

  • Participants
  • Parent commits d492598

Comments (0)

Files changed (8)

File python 2_6/koans/about_lists.py

         knight = ['you', 'shall', 'pass']
         knight.insert(2, 'not')
         self.assertEqual(__, knight)
+
         knight.insert(0, 'Arthur,')
         self.assertEqual(__, knight)  
             

File python 2_6/koans/about_methods.py

             my_unbound_function()
         except Exception as exception:
             self.assertEqual(__, type(exception).__name__)
-            
-            # Note, assertMatch() uses regular expression pattern matching,
-            # so you don't have to copy the whole message. Also parenthesis need
-            # a backslash in front.
-            self.assertMatch("takes exactly 2 arguments \(" + \
-                __ ,exception[0])
+            self.assertMatch(r'my_unbound_function\(\) takes exactly 2 arguments \(0 given\)'
+                , exception[0])
         
         try:
             my_unbound_function(1, 2, 3)
         except Exception as e:
+            
+            # Note, watch out for parenthesis. They need slashes in front!
             self.assertMatch(__, e[0])        
     
     # ------------------------------------------------------------------

File python 2_6/koans/about_none.py

 
     def test_what_exception_do_you_get_when_calling_nonexistent_methods_on_None(self):
         """
-        What is the Excaption that is thrown when you call a method that does
+        What is the Exception that is thrown when you call a method that does
         not exist?
         
         Hint: launch python command console and try the code in the block below.

File python 2_6/koans/about_strings.py

         hi += there
         self.assertEqual(__, original)
 
+    def test_most_strings_interpret_escape_characters(self):
+        string = "\n"
+        self.assertEqual('\n', string)
+        self.assertEqual("""\n""", string)
+        self.assertEqual(__, len(string))
+    
+    def test_use_format_to_interpolate_variables(self):
+        value1 = 'one'
+        value2 = 2
+        string = "The values are {0} and {1}".format(value1, value2)
+        self.assertEqual(__, string)
+
+    def test_formatted_values_con_be_shown_in_any_order_or_be_repeated(self):
+        value1 = 'doh'
+        value2 = 'DOH'
+        string = "The values are {1}, {0}, {0} and {1}!".format(value1, value2)
+        self.assertEqual(__, string)
+    
+    def test_any_python_expression_may_be_interpolated(self):
+        import math # import a standard python module with math functions
+        
+        decimal_places = 4
+        string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \
+            decimal_places)
+        self.assertEqual(__, string)
+    
+    def test_you_can_get_a_substring_from_a_string(self):
+        string = "Bacon, lettuce and tomato"
+        self.assertEqual(__, string[7:10])
+    
+    def test_you_can_get_a_single_character_from_a_string(self):
+        string = "Bacon, lettuce and tomato"
+        self.assertEqual(__, string[1])
+    
+    def test_single_characters_can_be_represented_by_integers(self):
+        self.assertEqual(__, ord('a'))      
+        self.assertEqual(__, ord('b') == (ord('a') + 1))
+    
+    def test_strings_can_be_split(self):
+        string = "Sausage Egg Cheese"
+        words = string.split()
+        self.assertEqual([__, __, __], words)
+    
+    def test_strings_can_be_split_with_different_patterns(self):
+        import re #import python regular expression library
+        
+        string = "the,rain;in,spain"
+        pattern = re.compile(r',|;')
+        
+        words = pattern.split(string)
+        
+        self.assertEqual([__, __, __, __], words)
+        
+        # Pattern is a Python regular expression pattern which matches ',' or ';'
+
+    def test_regular_expression_strings_do_not_interpret_escape_characters(self):
+        string = r'\n'
+        self.assertNotEqual('\n', string)
+        self.assertEqual(__, string)
+        self.assertEqual(__, len(string))
+        
+        # This is to make patterns easier to read
+      
+    def test_strings_can_be_joined(self):
+        words = ["Now", "is", "the", "time"]
+        self.assertEqual(__, ' '.join(words))

File python 2_6/koans/about_tuples.py

         try:
             count_of_three.append("boom")
         except Exception as ex:
-            pass
-        
-        self.assertEqual(AttributeError, type(ex))
-        self.assertMatch(__, ex[0])
+            self.assertEqual(AttributeError, type(ex))
+            
+            # Note, assertMatch() uses regular expression pattern matching,
+            # so you don't have to copy the whole message. 
+            self.assertMatch(__, ex[0])
         
         # Tuples are less flexible thon lists, but faster.
 

File python 3_1/runner/koan.py

 
 __all__ = [ "__", "___", "____", "_____", "Koan" ]
 
-class FillMeInError(Exception):
-    pass
-
 __ = "-=> FILL ME IN! <=-"
 
-def ___(value = FillMeInError):
-    return value
+class ___(Exception):
+    pass
 
 ____ = "-=> TRUE OR FALSE? <=-"
 

File python 3_1/runner/runner_tests/test_sensei.py

         self.assertEqual("", self.sensei.scrapeAssertionError(None))
         
     def test_that_scraping_the_assertion_error_with_messaged_assert(self):
-        self.assertEqual("AssertionError: Another fine mess you've got me into Stanley...",
+        self.assertEqual("  AssertionError: Another fine mess you've got me into Stanley...",
             self.sensei.scrapeAssertionError(error_assertion_with_message))
         
     def test_that_scraping_the_assertion_error_with_assert_equals(self):
-        self.assertEqual("AssertionError: 4 != 99",
+        self.assertEqual("  AssertionError: 4 != 99",
             self.sensei.scrapeAssertionError(error_assertion_equals))
         
     def test_that_scraping_the_assertion_error_with_assert_true(self):
-        self.assertEqual("AssertionError",
+        self.assertEqual("  AssertionError",
             self.sensei.scrapeAssertionError(error_assertion_true))
         
     def test_that_scraping_the_assertion_error_with_syntax_error(self):
-        self.assertEqual("SyntaxError: invalid syntax",
+        self.assertEqual("  SyntaxError: invalid syntax",
             self.sensei.scrapeAssertionError(error_mess))
 
     def test_that_scraping_the_assertion_error_with_list_error(self):
-        self.assertEqual("AssertionError: Lists differ: [1, 9] != [1, 2]",
+        self.assertEqual("""  AssertionError: Lists differ: [1, 9] != [1, 2]
+
+  First differing element 1:
+  9
+  2
+
+  - [1, 9]
+  ?     ^
+
+  + [1, 2]
+  ?     ^""",
             self.sensei.scrapeAssertionError(error_with_list))
 
     def test_that_scraping_a_non_existent_stack_dump_gives_you_nothing(self):

File python 3_1/runner/sensei.py

 
         self.stream.writeln("")
         self.stream.writeln("You have not yet reached enlightenment ...")
-        self.stream.writeln("  {0}".format(self.scrapeAssertionError(err)))
+        self.stream.writeln(self.scrapeAssertionError(err))
         self.stream.writeln("")
         self.stream.writeln("Please meditate on the following code:")
         self.stream.writeln(self.scrapeInterestingStackDump(err))
     def scrapeAssertionError(self, err):
         if not err: return ""
 
+        error_text = ""
         count = 0
         for line in err.splitlines():
             m = re.search("^[^^ ].*$",line)
             if m and m.group(0):
                 count+=1
             
-            if count==2:
-                return line.strip()
-        return ""
+            if count>1:
+                error_text += ("  " + line.strip()).rstrip() + '\n'
+        return error_text.strip('\n')
 
     def scrapeInterestingStackDump(self, err):
         if not err: