Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Diff of /branches/1.5/turbogears/tests/test_view.py [r7419] .. [r7420] Maximize Restore

  Switch to side-by-side view

--- a/branches/1.5/turbogears/tests/test_view.py
+++ b/branches/1.5/turbogears/tests/test_view.py
@@ -4,7 +4,7 @@
 
 try:
     from json import dumps, loads
-except ImportError: # Python < 2.6
+except ImportError:  # Python < 2.6
     from simplejson import dumps, loads
 
 from turbogears import view, config
@@ -32,13 +32,16 @@
         except TypeError:
             pass
 
+        params = dict(format=format, fragment=fragment, template=template)
+
         opts = self.options.copy()
         opts.update(options)
 
-        return dumps((info, opts)).encode('utf-8')
+        return dumps((info, params, opts)).encode('utf-8')
 
     def load_template(self, templatename):
         pass
+
     def transform(self, info, template):
         pass
 
@@ -66,9 +69,15 @@
         template = 'testplugin:test'
         options = dict(strict=False, renderer='fast')
         output = view.render(data, template, **options)
-        info, options = loads(output.decode('utf-8'))
+        info, params, options = loads(output.decode('utf-8'))
         assert info['fruit'] == 'apple', \
-            "Data dict passed to view.render() not included in output."
+            "Data dict passed to render() not included in output."
+        assert params['format'] == 'html', \
+            "Missing format parameter to render() not set to html."
+        assert params['fragment'] is False, \
+            "Missing fragment parameter to render() not set to false."
+        assert params['template'] == 'test', \
+            "Template passed to render() not included in output."
         assert options['cache'] is True, \
             "Options passed to __init__() not used."
         assert options['renderer'] == 'fast', \
@@ -80,7 +89,7 @@
         """Test that setting view format sets correct content-type header."""
         view.engines['testplugin'] = TestTemplateEngine()
         template = 'testplugin:foo'
-        info = dict()
+        info = {}
         headers = {}
         view.render(info, template, headers=headers)
         assert headers.get('Content-Type') == 'text/html; charset=utf-8'
@@ -112,6 +121,19 @@
         view.render(info, template, headers=headers, format='xhtml-strict')
         assert headers.get('Content-Type') == 'application/xhtml+xml; charset=utf-8'
         config.update({'global': {'tg.format_mime_types': mime_types}})
+
+    def test_callable_template(self):
+        """Test that the template parameter can be passed as a callable."""
+        view.engines['testplugin'] = TestTemplateEngine()
+        template = lambda: 'testplugin:pie'
+        output = view.render(dict(fruit='apple'), template, cream=True)
+        info, params, options = loads(output.decode('utf-8'))
+        assert info['fruit'] == 'apple', \
+            "Data passed to render() not included in output."
+        assert params['template'] == 'pie', \
+            "Template passed to render() not included in output."
+        assert options['cream'] is True, \
+            "Template passed to render() not included in output."
 
 
 class TestKidView(unittest.TestCase):
@@ -194,6 +216,7 @@
             pass
         else:
             assert False, "'dumbo' should not be accepted as format"
+
 
 class TestGenshiView(unittest.TestCase):
     """Test suite for turbogears.view using Genshi template engine."""
@@ -376,6 +399,7 @@
                               doctype='xhtml-transitional')
             assert xhtml_transitional_url in val
 
+
 def test_template_plugin_output():
     """Test that minimal template engine test plugin returns correct output."""
     def extra_vars():
@@ -383,20 +407,22 @@
     engine = TestTemplateEngine(extra_vars_func=extra_vars)
     data = dict(fruit='apple', beverage='beer')
     output = engine.render(data)
-    info, options = loads(output.decode('utf-8'))
+    info, params, options = loads(output.decode('utf-8'))
     assert info['fruit'] == 'apple', \
         "Data dict passed to render() not included in output."
     assert info['tree'] == 'Rowan', \
         "Data returned from extra_vars_func not included in output."
-    assert isinstance(options, dict) and len(options) == 0, \
+    assert isinstance(options, dict) and not options, \
         "Options dict not included in output or not empty."
+
 
 def test_template_plugin_pass_options():
     """Test that minimal template engine test plugin uses correct options."""
     engine = TestTemplateEngine(options=dict(cache=True, strict=False))
     output = engine.render(dict(), strict=False, renderer='fast')
-    info, options = loads(output.decode('utf-8'))
+    info, params, options = loads(output.decode('utf-8'))
     assert isinstance(info, dict)
+    assert isinstance(params, dict)
     assert isinstance(options, dict)
     assert options['cache'] is True, \
         "Options passed to __init__() not used."
@@ -404,6 +430,7 @@
         "Options passed to render() not used."
     assert options['strict'] is not True, \
         "Options passed to render() do not override options passed to __init__()."
+
 
 def test_get_plugin_options():
     """Test that template engine options and defaults are loaded correctly."""
@@ -427,9 +454,10 @@
     assert 'genshi-text.foo' not in genshi_options, \
         'Options for plugin variant should not be loaded in base plugin.'
 
+
 def test_cycle():
     """Test that view.base.cyle() returns correct values."""
-    oe = view.base.cycle(('odd','even'))
+    oe = view.base.cycle(('odd', 'even'))
     assert str(oe) == str(None)
     assert oe.next() == 'odd'
     assert str(oe) == 'odd'
@@ -438,15 +466,18 @@
     assert oe.next() == 'odd'
     assert oe.value == 'odd'
 
+
 def test_selector():
     """Test that view.base.selector() returns correct values."""
     assert view.base.selector(False) is None
     assert view.base.selector(True) == 'selected'
 
+
 def test_checker():
     """Test that view.base.checker() returns correct values."""
     assert view.base.checker(False) is None
     assert view.base.checker(True) == 'checked'
+
 
 def test_ipeek():
     """Test that view.base.ipeek() returns correct values."""