Professional Documents
Culture Documents
/usr/bin/env python
# -*- coding: utf-8 -*#
# Partially based on AboutMethods in the Ruby Koans
#
from runner.koan import *
def my_global_function(a, b):
return a + b
class AboutMethods(Koan):
def test_calling_a_global_function(self):
self.assertEqual(5, my_global_function(2, 3))
# NOTE: Wrong number of arguments is not a SYNTAX error, but a
# runtime error.
def test_calling_functions_with_wrong_number_of_arguments(self):
try:
my_global_function()
except Exception as exception:
# NOTE: The .__name__ attribute will convert the class
# into a string value.
self.assertEqual(exception.AboutMethods, exception.__class__.__name_
_)
self.assertMatch(
r'my_global_function\(\) takes exactly 2 arguments \(0 given\)',
exception[0])
try:
my_global_function(1, 2, 3)
except Exception as e:
# Note, watch out for parenthesis. They need slashes in front!
self.assertMatch(__, e[0])
# -----------------------------------------------------------------def pointless_method(self, a, b):
sum = a + b
def test_which_does_not_return_anything(self):
self.assertEqual(__, self.pointless_method(1, 2))
# Notice that methods accessed from class scope do not require
# you to pass the first "self" argument?
# -----------------------------------------------------------------def method_with_defaults(self, a, b='default_value'):
return [a, b]
def test_calling_with_default_values(self):
self.assertEqual(__, self.method_with_defaults(1))
self.assertEqual(__, self.method_with_defaults(1, 2))
# ------------------------------------------------------------------
def test_no_indentation_required_for_one_line_statement_bodies(self):
self.assertEqual(__, self.one_line_method())
# -----------------------------------------------------------------def method_with_documentation(self):
"A string placed at the beginning of a function is used for documentatio
n"
return "ok"
def test_the_documentation_can_be_viewed_with_the_doc_method(self):
self.assertMatch(__, self.method_with_documentation.__doc__)
# -----------------------------------------------------------------class Dog(object):
def name(self):
return "Fido"
def _tail(self):
# Prefixing a method with an underscore implies private scope
return "wagging"
def __password(self):
return 'password' # Genius!
def test_calling_methods_in_other_objects(self):
rover = self.Dog()
self.assertEqual(__, rover.name())
def test_private_access_is_implied_but_not_enforced(self):
rover = self.Dog()
# This is a little rude, but legal
self.assertEqual(__, rover._tail())
def test_double_underscore_attribute_prefixes_cause_name_mangling(self):
"""Attributes names that start with a double underscore get
mangled when an instance is created."""
rover = self.Dog()
try:
#This may not be possible...
password = rover.__password()
except Exception as ex:
self.assertEqual(__, ex.__class__.__name__)
# But this still is!
self.assertEqual(__, rover._Dog__password())
# Name mangling exists to avoid name clash issues when subclassing.
# It is not for providing effective access protection