#!/usr/bin/env python # -*- coding: utf-8 -*- from runner.koan import * import jims import joes counter = 0 # Global class AboutScope(Koan): # # NOTE: # Look in jims.py and joes.py to see definitions of Dog used # for this set of tests # def test_dog_is_not_available_in_the_current_scope(self): try: fido = Dog() except Exception as ex: self.assertMatch("global name 'Dog' is not defined", ex[0]) def test_you_can_reference_nested_classes_using_the_scope_operator(self): fido = jims.Dog() # name 'jims' module name is taken from jim.py filename rover = joes.Dog() self.assertEqual('jims dog', fido.identify()) self.assertEqual('joes dog', rover.identify()) self.assertEqual(False, type(fido) == type(rover)) self.assertEqual(False, jims.Dog == joes.Dog) # ------------------------------------------------------------------ class str(object): pass def test_bare_bones_class_names_do_not_assume_the_current_scope(self): self.assertEqual(False, AboutScope.str == str) def test_nested_string_is_not_the_same_as_the_system_string(self): self.assertEqual(False, self.str == type("HI")) def test_str_without_self_prefix_stays_in_the_global_scope(self): self.assertEqual(True, str == type("HI")) # ------------------------------------------------------------------ PI = 3.1416 def test_constants_are_defined_with_an_initial_uppercase_letter(self): self.assertAlmostEqual(3.1416, self.PI) # Note, floating point numbers in python are not precise. # assertAlmostEqual will check that it is 'close enough' def test_constants_are_assumed_by_convention_only(self): self.PI = "rhubarb" self.assertEqual("rhubarb", self.PI) # There aren't any real constants in python. Its up to the developer # to keep to the convention and not modify them. # ------------------------------------------------------------------ def increment_using_local_counter(self, counter): counter = counter + 1 def increment_using_global_counter(self): global counter counter = counter + 1 def test_incrementing_with_local_counter(self): global counter start = counter self.increment_using_local_counter(start) self.assertEqual(False, counter == start + 1) def test_incrementing_with_global_counter(self): global counter start = counter self.increment_using_global_counter() self.assertEqual(True, counter == start + 1) # ------------------------------------------------------------------ global deadly_bingo deadly_bingo = [4, 8, 15, 16, 23, 42] def test_global_attributes_can_be_created_in_the_middle_of_a_class(self): self.assertEqual(42, deadly_bingo[5])