<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> from pyjamas import DOM <EOL> from pyjamas . Timer import Timer <EOL> from pyjamas . HTTPRequest import HTTPRequest <EOL> from pyjamas . ui . RootPanel import RootPanel <EOL> class GetTestOutput : <EOL> def __init__ ( self , unittest , test_name , output ) : <EOL> self . test_name = test_name <EOL> self . unittest = unittest <EOL> self . output = output <EOL> def onCompletion ( self , responseText ) : <EOL> print "<STR_LIT>" , self . unittest . tests_outstanding , self . test_name <EOL> self . unittest . async_test_name = self . test_name <EOL> e1 = DOM . getElementById ( '<STR_LIT>' ) <EOL> e2 = DOM . getElementById ( '<STR_LIT>' ) <EOL> e1 . innerHTML = responseText <EOL> e2 . innerHTML = responseText <EOL> i1 = DOM . walkChildren ( e1 ) <EOL> i2 = DOM . walkChildren ( e2 ) <EOL> ok = True <EOL> while ok : <EOL> try : <EOL> ec1 = i1 . next ( ) <EOL> ec2 = i2 . next ( ) <EOL> except StopIteration : <EOL> break <EOL> ok = ok and ( ec1 . nodeType == ec2 . nodeType ) <EOL> if not ok : <EOL> break <EOL> print ec1 . nodeName , ec1 . nodeValue <EOL> ok = ok and ( ec1 . nodeName == ec2 . nodeName ) <EOL> ok = ok and ( ec1 . nodeValue == ec2 . nodeValue ) <EOL> if not ok : <EOL> break <EOL> if hasattr ( ec1 , '<STR_LIT>' ) and hasattr ( ec2 , '<STR_LIT>' ) : <EOL> print ec1 . getInnerText ( ) <EOL> ok = ok and ( ec1 . getInnerText ( ) == ec2 . getInnerText ( ) ) <EOL> if not ok : <EOL> break <EOL> if hasattr ( ec1 , '<STR_LIT>' ) and hasattr ( ec2 , '<STR_LIT>' ) : <EOL> a1 = ec1 . attributes <EOL> a2 = ec2 . attributes <EOL> ok = hasattr ( a1 , '<STR_LIT>' ) and hasattr ( a2 , '<STR_LIT>' ) <EOL> if not ok : <EOL> break <EOL> ok = ok and ( a1 . length == a2 . length ) <EOL> if not ok : <EOL> break <EOL> for i in range ( a1 . length ) : <EOL> ai1 = a1 . item ( i ) <EOL> ai2 = a2 . item ( i ) <EOL> ok = ok and ( ai1 . nodeValue == ai2 . nodeValue ) <EOL> ok = ok and ( ai1 . nodeName == ai2 . nodeName ) <EOL> self . unittest . assertTrue ( ok ) <EOL> self . unittest . tests_outstanding -= <NUM_LIT:1> <EOL> e1 . innerHTML = '<STR_LIT>' <EOL> e2 . innerHTML = '<STR_LIT>' <EOL> def onError ( self , responseText , status ) : <EOL> self . unittest . async_test_name = self . test_name <EOL> self . unittest . fail ( """<STR_LIT>""" % ( self . test_name , str ( status ) , repr ( responseText ) ) ) <EOL> self . unittest . tests_outstanding -= <NUM_LIT:1> <EOL> class UnitTest : <EOL> def __init__ ( self , test_gen_folder ) : <EOL> self . test_gen_folder = test_gen_folder <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 . tests_outstanding = 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 . assertRaises = self . failUnlessRaises <EOL> self . assert_ = self . assertTrue = self . failUnless <EOL> self . assertFalse = self . failIf <EOL> def do_test ( self , output , name ) : <EOL> handler = GetTestOutput ( self , self . current_test_name , output ) <EOL> if name : <EOL> fname = "<STR_LIT>" % ( self . current_test_name , name ) <EOL> else : <EOL> fname = "<STR_LIT>" % self . current_test_name <EOL> HTTPRequest ( ) . asyncGet ( fname , handler ) <EOL> def write_test_output ( self , name = None , div_id = None ) : <EOL> if div_id is None : <EOL> div_id = '<STR_LIT>' <EOL> element = DOM . getElementById ( div_id ) <EOL> output = element . innerHTML <EOL> if self . test_gen_folder is None : <EOL> if self . tests_outstanding is None : <EOL> self . tests_outstanding = <NUM_LIT:0> <EOL> self . tests_outstanding += <NUM_LIT:1> <EOL> self . do_test ( output , name ) <EOL> return <EOL> import os <EOL> if name : <EOL> fname = "<STR_LIT>" % ( self . current_test_name , name ) <EOL> else : <EOL> fname = "<STR_LIT>" % self . current_test_name <EOL> f = open ( os . path . join ( self . test_gen_folder , fname ) , "<STR_LIT:w>" ) <EOL> f . write ( output ) <EOL> f . close ( ) <EOL> if self . tests_outstanding is None : <EOL> self . tests_outstanding = <NUM_LIT:0> <EOL> return False <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> test_method ( ) <EOL> self . tearDown ( ) <EOL> self . current_test_name = None <EOL> self . async_test_name = None <EOL> def run ( self ) : <EOL> self . getTestMethods ( ) <EOL> self . test_idx = <NUM_LIT:0> <EOL> Timer ( <NUM_LIT:1> , self ) <EOL> def onTimer ( self , timer ) : <EOL> print self . test_idx <EOL> if self . test_idx is '<STR_LIT>' : <EOL> self . check_start_next_test ( ) <EOL> return <EOL> for i in range ( <NUM_LIT:1> ) : <EOL> if self . test_idx >= len ( self . test_methods ) : <EOL> self . test_idx = '<STR_LIT>' <EOL> self . check_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:1> ) <EOL> def check_start_next_test ( self ) : <EOL> if self . tests_outstanding is None : <EOL> return <EOL> if self . tests_outstanding == <NUM_LIT:0> : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . lastTestsCheck ( ) <EOL> self . displayStats ( ) <EOL> self . start_next_test ( ) <EOL> return <EOL> Timer ( <NUM_LIT:100> , self ) <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> test_name = self . async_test_name or self . current_test_name <EOL> if self . getName ( ) : <EOL> if msg : <EOL> msg = "<STR_LIT:U+0020>" + str ( msg ) <EOL> if test_name : <EOL> msg += "<STR_LIT>" % test_name <EOL> return self . getName ( ) + msg + "<STR_LIT>" <EOL> return "<STR_LIT>" <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> title = "<STR_LIT>" + self . getNameFmt ( "<STR_LIT>" ) + "<STR_LIT>" <EOL> writebr ( title + msg ) <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> return self . fail ( msg ) <EOL> def failUnlessRaises ( self , excClass , callableObj , * args , ** kwargs ) : <EOL> try : <EOL> callableObj ( * args , ** kwargs ) <EOL> except excClass : <EOL> return <EOL> else : <EOL> if hasattr ( excClass , '<STR_LIT>' ) : excName = excClass . __name__ <EOL> else : excName = str ( excClass ) <EOL> self . fail ( "<STR_LIT>" % excName ) <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 assertRaises ( self , excClass , callableObj , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . startTest ( ) <EOL> try : <EOL> callableObj ( * args , ** kwargs ) <EOL> except excClass , exc : <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 , do_escape = False ) </s>
