def test__main__(self): e = Engine(script=trim(""" if __name__=='__main__': print >> RES, 'sup?' """)) e.run() assert e.response.buffer == "sup?\n"
def test_exit(self): try: eng = Engine(script="raise SystemExit") eng.run() gotError = 0 except SystemExit: gotError = 1 assert not gotError, "Engine doesn't trap sys.exit()!"
def test__main__(self): e = Engine(script=trim( """ if __name__=='__main__': print >> RES, 'sup?' """)) e.run() assert e.response.buffer =="sup?\n"
def test_runtwice(self): eng = Engine(script='print >> RES, "hello"') eng.run() try: eng.run() gotError = 0 except: gotError = 1 assert gotError, "you should not be able to use engine twice!"
def test_globals(self): myscript = trim(""" import weblib assert isinstance(REQ, weblib.Request) assert isinstance(RES, weblib.Response) assert isinstance(ENG, weblib.Engine) """) eng = Engine(script=myscript) eng.run() assert eng.result == Engine.SUCCESS, eng.error
def test_globals(self): myscript = trim( """ import weblib assert isinstance(REQ, weblib.Request) assert isinstance(RES, weblib.Response) assert isinstance(ENG, weblib.Engine) """) eng = Engine(script=myscript) eng.run() assert eng.result == Engine.SUCCESS, eng.error
def test_print(self): import sys, StringIO eng = Engine(script=trim(""" import weblib print >> RES, 'this should show' print 'this should not' """)) tempout, sys.stdout = sys.stdout, StringIO.StringIO() eng.run() sys.stdout, tempout = tempout, sys.stdout assert eng.response.buffer == "this should show\n", \ "doesn't grab prints after import weblib!" assert tempout.getvalue() == "this should not\n", \ "doesn't print rest to stdout"
def test_print(self): import sys, StringIO eng = Engine(script=trim( """ import weblib print >> RES, 'this should show' print 'this should not' """)) tempout, sys.stdout = sys.stdout, StringIO.StringIO() eng.run() sys.stdout, tempout = tempout, sys.stdout assert eng.response.buffer == "this should show\n", \ "doesn't grab prints after import weblib!" assert tempout.getvalue() == "this should not\n", \ "doesn't print rest to stdout"
def test_hadProblem(self): e = Engine(script="x = 1"); e.run() assert not e.hadProblem() e = Engine(script="raise hell"); e.run() assert e.hadProblem() e = Engine(script="assert 0"); e.run() assert e.hadProblem()
def test_hadProblem(self): e = Engine(script="x = 1") e.run() assert not e.hadProblem() e = Engine(script="raise hell") e.run() assert e.hadProblem() e = Engine(script="assert 0") e.run() assert e.hadProblem()
def test_result(self): eng = Engine(script='1+1') assert eng.result == None, \ "engine.result doesn't default to None" eng.run() assert eng.result == eng.SUCCESS, \ "engine.result doesn't return SUCCESS on success" eng = Engine(script="print 'cat' + 5") eng.run() assert eng.result == eng.EXCEPTION, \ "engine.result doesn't return EXCEPTION on error." eng = Engine("assert 1==0, 'math is working.. :('") eng.run() assert eng.result == eng.FAILURE, \ "engine.result doesn't return FAILURE on assertion failure." eng = Engine("import weblib; raise weblib.Redirect, 'url?query'") eng.run() assert eng.result == eng.REDIRECT, \ "engine.result doesn't return REDIRECT on redirect."
def test_redirectToQuerystring(self): eng = Engine("import weblib; raise weblib.Redirect('?x=1')", request=RequestBuilder().build(path="test.app")) eng.run() assert ('Location', 'test.app?x=1') in eng.response.headers
def wrap(self, code): eng = Engine(script=code) eng.run() out = OutputDecorator(eng) return out