<s> from write import write , writebr <EOL> import sys <EOL> IN_BROWSER = sys . platform in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> IN_JS = sys . platform in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> PY27_BEHAVIOUR = not IN_JS and sys . version_info [ <NUM_LIT:0> : <NUM_LIT:2> ] >= ( <NUM_LIT:2> , <NUM_LIT:7> ) <EOL> if IN_BROWSER : <EOL> from pyjamas . Timer import Timer <EOL> class UnitTest : <EOL> def __init__ ( self ) : <EOL> self . tests_completed = <NUM_LIT:0> <EOL> self . tests_failed = <NUM_LIT:0> <EOL> self . tests_passed = <NUM_LIT:0> <EOL> self . test_methods = [ ] <EOL> self . test_idx = None <EOL> self . assertEqual = self . assertEquals = self . failUnlessEqual <EOL> self . assertNotEqual = self . assertNotEquals = self . failIfEqual <EOL> self . assertAlmostEqual = self . assertAlmostEquals = self . failUnlessAlmostEqual <EOL> self . assertNotAlmostEqual = self . assertNotAlmostEquals = self . failIfAlmostEqual <EOL> self . failUnlessRaises = self . assertRaises <EOL> self . assert_ = self . assertTrue = self . failUnless <EOL> self . assertFalse = self . failIf <EOL> def _run_test ( self , test_method_name ) : <EOL> self . getTestMethods ( ) <EOL> test_method = getattr ( self , test_method_name ) <EOL> self . current_test_name = test_method_name <EOL> self . setUp ( ) <EOL> try : <EOL> try : <EOL> test_method ( ) <EOL> except Exception , e : <EOL> self . fail ( "<STR_LIT>" + str ( e ) ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . tearDown ( ) <EOL> self . current_test_name = None <EOL> def run ( self ) : <EOL> self . getTestMethods ( ) <EOL> if not IN_BROWSER : <EOL> for test_method_name in self . test_methods : <EOL> self . _run_test ( test_method_name ) <EOL> self . displayStats ( ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> self . start_next_test ( ) <EOL> return <EOL> self . test_idx = <NUM_LIT:0> <EOL> Timer ( <NUM_LIT:10> , self ) <EOL> def onTimer ( self , timer ) : <EOL> for i in range ( <NUM_LIT:1> ) : <EOL> if self . test_idx >= len ( self . test_methods ) : <EOL> self . displayStats ( ) <EOL> self . test_idx = '<STR_LIT>' <EOL> self . start_next_test ( ) <EOL> return <EOL> self . _run_test ( self . test_methods [ self . test_idx ] ) <EOL> self . test_idx += <NUM_LIT:1> <EOL> timer . schedule ( <NUM_LIT:10> ) <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> pass <EOL> def getName ( self ) : <EOL> return self . __class__ . __name__ <EOL> def getNameFmt ( self , msg = "<STR_LIT>" ) : <EOL> if self . getName ( ) : <EOL> if msg : <EOL> msg = "<STR_LIT:U+0020>" + str ( msg ) <EOL> if self . current_test_name : <EOL> msg += "<STR_LIT>" % self . getCurrentTestID ( ) <EOL> return self . getName ( ) + msg + "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> def getCurrentTestID ( self ) : <EOL> return "<STR_LIT>" % ( self . current_test_name , self . tests_completed ) <EOL> def getTestMethods ( self ) : <EOL> self . test_methods = [ ] <EOL> for m in dir ( self ) : <EOL> if self . isTestMethod ( m ) : <EOL> self . test_methods . append ( m ) <EOL> def isTestMethod ( self , method ) : <EOL> if callable ( getattr ( self , method ) ) : <EOL> if method . find ( "<STR_LIT:test>" ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def fail ( self , msg = None ) : <EOL> self . startTest ( ) <EOL> self . tests_failed += <NUM_LIT:1> <EOL> if not msg : <EOL> msg = "<STR_LIT>" <EOL> else : <EOL> msg = str ( msg ) <EOL> octothorp = msg . find ( "<STR_LIT:#>" ) <EOL> has_bugreport = octothorp >= <NUM_LIT:0> and msg [ octothorp + <NUM_LIT:1> ] . isdigit ( ) <EOL> if has_bugreport : <EOL> name_fmt = "<STR_LIT>" <EOL> bg_colour = "<STR_LIT>" <EOL> fg_colour = "<STR_LIT>" <EOL> else : <EOL> bg_colour = "<STR_LIT>" <EOL> fg_colour = "<STR_LIT>" <EOL> name_fmt = "<STR_LIT>" <EOL> output = "<STR_LIT>" + bg_colour + "<STR_LIT>" + fg_colour + "<STR_LIT>" <EOL> write ( output ) <EOL> title = "<STR_LIT>" + self . getNameFmt ( name_fmt ) + "<STR_LIT>" <EOL> write ( title + msg ) <EOL> output = "<STR_LIT>" <EOL> output += "<STR_LIT:\n>" <EOL> write ( output ) <EOL> if sys . platform in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> from __pyjamas__ import JS <EOL> JS ( """<STR_LIT>""" ) <EOL> return False <EOL> def startTest ( self ) : <EOL> self . tests_completed += <NUM_LIT:1> <EOL> def failIf ( self , expr , msg = None ) : <EOL> self . startTest ( ) <EOL> if expr : <EOL> return self . fail ( msg ) <EOL> def failUnless ( self , expr , msg = None ) : <EOL> self . startTest ( ) <EOL> if not expr : <EOL> if not msg : <EOL> msg = "<STR_LIT>" <EOL> return self . fail ( msg ) <EOL> def failUnlessEqual ( self , first , second , msg = None ) : <EOL> self . startTest ( ) <EOL> if not first == second : <EOL> if not msg : <EOL> msg = repr ( first ) + "<STR_LIT>" + repr ( second ) <EOL> return self . fail ( msg ) <EOL> def failIfEqual ( self , first , second , msg = None ) : <EOL> self . startTest ( ) <EOL> if first == second : <EOL> if not msg : <EOL> msg = repr ( first ) + "<STR_LIT>" + repr ( second ) <EOL> return self . fail ( msg ) <EOL> def failUnlessAlmostEqual ( self , first , second , places = <NUM_LIT:7> , msg = None ) : <EOL> self . startTest ( ) <EOL> if round ( second - first , places ) != <NUM_LIT:0> : <EOL> if not msg : <EOL> msg = repr ( first ) + "<STR_LIT>" + repr ( second ) + "<STR_LIT>" + repr ( places ) + "<STR_LIT>" <EOL> return self . fail ( msg ) <EOL> def failIfAlmostEqual ( self , first , second , places = <NUM_LIT:7> , msg = None ) : <EOL> self . startTest ( ) <EOL> if round ( second - first , places ) is <NUM_LIT:0> : <EOL> if not msg : <EOL> msg = repr ( first ) + "<STR_LIT>" + repr ( second ) + "<STR_LIT>" + repr ( places ) + "<STR_LIT>" <EOL> return self . fail ( msg ) <EOL> def assertIn ( self , member , container , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if member not in container : <EOL> standardMsg = '<STR_LIT>' % ( repr ( member ) [ : <NUM_LIT> ] , <EOL> repr ( container ) [ : <NUM_LIT> ] ) <EOL> self . fail ( msg if msg else standardMsg ) <EOL> def assertNotIn ( self , member , container , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if member in container : <EOL> standardMsg = '<STR_LIT>' % ( repr ( member ) [ : <NUM_LIT> ] , <EOL> repr ( container ) [ : <NUM_LIT> ] ) <EOL> self . fail ( msg if msg else standardMsg ) <EOL> def assertRaises ( self , excClass , callableObj , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . startTest ( ) <EOL> try : <EOL> callableObj ( * args , ** kwargs ) <EOL> except excClass : <EOL> return <EOL> else : <EOL> if hasattr ( excClass , '<STR_LIT>' ) : <EOL> excName = excClass . __name__ <EOL> else : <EOL> excName = str ( excClass ) <EOL> self . fail ( "<STR_LIT>" % excName ) <EOL> def displayStats ( self ) : <EOL> if self . tests_failed : <EOL> bg_colour = "<STR_LIT>" <EOL> fg_colour = "<STR_LIT>" <EOL> else : <EOL> bg_colour = "<STR_LIT>" <EOL> fg_colour = "<STR_LIT>" <EOL> tests_passed = self . tests_completed - self . tests_failed <EOL> if sys . platform in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> output = "<STR_LIT>" + bg_colour + "<STR_LIT>" + fg_colour + "<STR_LIT>" <EOL> else : <EOL> output = "<STR_LIT>" <EOL> output += self . getNameFmt ( ) + "<STR_LIT>" % tests_passed + "<STR_LIT>" % self . tests_completed + "<STR_LIT>" <EOL> if self . tests_failed : <EOL> output += "<STR_LIT>" % self . tests_failed <EOL> if sys . platform in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> output += "<STR_LIT>" <EOL> else : <EOL> output += "<STR_LIT:\n>" <EOL> write ( output ) </s>
