<s> from twisted . internet import defer <EOL> from nevow import context , inevow <EOL> from nevow import testutil <EOL> from nevow . flat import twist <EOL> from nevow . util import Deferred <EOL> from nevow import rend , loaders , tags <EOL> def deferit ( data ) : <EOL> return data . d <EOL> def deferdot ( data ) : <EOL> return data . d2 <EOL> class RenderHelper ( testutil . TestCase ) : <EOL> def renderIt ( self ) : <EOL> req = testutil . FakeRequest ( ) <EOL> self . r . renderHTTP ( context . PageContext ( tag = self . r , parent = context . RequestContext ( tag = req ) ) ) <EOL> return req <EOL> class LaterRenderTest ( RenderHelper ) : <EOL> def setUp ( self ) : <EOL> self . d = Deferred ( ) <EOL> self . d2 = Deferred ( ) <EOL> self . r = rend . Page ( <EOL> docFactory = loaders . stan ( <EOL> tags . html ( data = self ) [ <EOL> '<STR_LIT>' , tags . invisible [ tags . invisible [ tags . invisible [ tags . invisible [ deferit ] ] ] ] , <EOL> deferdot , <EOL> ] <EOL> ) <EOL> ) <EOL> def test_deferredSupport ( self ) : <EOL> req = self . renderIt ( ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d . callback ( "<STR_LIT>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d2 . callback ( "<STR_LIT:.>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> def test_deferredSupport2 ( self ) : <EOL> req = self . renderIt ( ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d2 . callback ( "<STR_LIT:.>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d . callback ( "<STR_LIT>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> def test_deferredSupport3 ( self ) : <EOL> self . r . buffered = True <EOL> req = self . renderIt ( ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d . callback ( "<STR_LIT>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d2 . callback ( "<STR_LIT:.>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> def test_renderNestedDeferredCallables ( self ) : <EOL> """<STR_LIT>""" <EOL> def render_inner ( ctx , data ) : <EOL> return defer . succeed ( '<STR_LIT>' ) <EOL> def render_outer ( ctx , data ) : <EOL> return defer . succeed ( render_inner ) <EOL> ctx = context . WovenContext ( ) <EOL> ctx . remember ( None , inevow . IData ) <EOL> out = [ ] <EOL> d = twist . deferflatten ( render_outer , ctx , out . append ) <EOL> def flattened ( ign ) : <EOL> self . assertEquals ( out , [ '<STR_LIT>' ] ) <EOL> d . addCallback ( flattened ) <EOL> return d <EOL> def test_renderNestedDeferredErrorHandling ( self ) : <EOL> """<STR_LIT>""" <EOL> class NestedException ( Exception ) : <EOL> pass <EOL> def render_inner ( ctx , data ) : <EOL> raise NestedException ( ) <EOL> def render_outer ( ctx , data ) : <EOL> return defer . succeed ( render_inner ) <EOL> ctx = context . WovenContext ( ) <EOL> ctx . remember ( None , inevow . IData ) <EOL> out = [ ] <EOL> d = twist . deferflatten ( render_outer , ctx , out . append ) <EOL> return self . assertFailure ( d , NestedException ) <EOL> class LaterDataTest ( RenderHelper ) : <EOL> def data_later ( self , context , data ) : <EOL> return self . d <EOL> def data_later2 ( self , context , data ) : <EOL> return self . d2 <EOL> def setUp ( self ) : <EOL> self . d = Deferred ( ) <EOL> self . d2 = Deferred ( ) <EOL> self . r = rend . Page ( docFactory = loaders . stan ( <EOL> tags . html ( data = self . data_later ) [ <EOL> '<STR_LIT>' , str , '<STR_LIT>' <EOL> '<STR_LIT>' , str , <EOL> tags . span ( data = self . data_later2 , render = str ) ] ) ) <EOL> def test_deferredSupport ( self ) : <EOL> req = self . renderIt ( ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d . callback ( "<STR_LIT>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> self . d2 . callback ( "<STR_LIT:.>" ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> class SuperLaterDataTest ( RenderHelper ) : <EOL> def test_reusedDeferredSupport ( self ) : <EOL> """<STR_LIT>""" <EOL> doc = tags . html [ <EOL> tags . slot ( '<STR_LIT:foo>' ) , tags . slot ( '<STR_LIT:foo>' ) ] <EOL> doc . fillSlots ( '<STR_LIT:foo>' , defer . succeed ( tags . span [ '<STR_LIT>' ] ) ) <EOL> self . r = rend . Page ( docFactory = loaders . stan ( doc ) ) <EOL> req = self . renderIt ( ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) <EOL> def test_rendererCalledOnce ( self ) : <EOL> """<STR_LIT>""" <EOL> calls = [ ] <EOL> def recorder ( ctx , data ) : <EOL> calls . append ( None ) <EOL> return str ( len ( calls ) ) <EOL> doc = tags . html [ tags . directive ( '<STR_LIT>' ) ] <EOL> class RendererPage ( rend . Page ) : <EOL> docFactory = loaders . stan ( doc ) <EOL> def render_renderer ( self , ctx , data ) : <EOL> return defer . succeed ( recorder ) <EOL> self . r = RendererPage ( ) <EOL> req = self . renderIt ( ) <EOL> self . assertEquals ( req . v , '<STR_LIT>' ) </s>
