
from __future__ import with_statement

from nose.tools import istest as test, assert_equal, raises
from flaskext.genshi import render_response, render_template
from genshi.filters import Transformer

from flask_genshi_testapp import app, genshi

context = dict(name='Rudolf')


def setup():
    global request_context
    request_context = app.test_request_context()
    request_context.push()

def teardown():
    request_context.pop()


@test
def renders_html():
    """A html extension results in an HTML doctype and mimetype"""
    rendered = render_response('test.html', context)
    assert_equal(rendered.mimetype, 'text/html')
    assert_equal(rendered.data,
        '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" '
        '"http://www.w3.org/TR/html4/strict.dtd">\n'
        '<body>Hi Rudolf</body>')

@test
def renders_text():
    """A txt extension results in no doctype and a text/plain mimetype"""
    rendered = render_response('test.txt', context)
    assert_equal(rendered.mimetype, 'text/plain')
    assert_equal(rendered.data, 'Hi Rudolf\n')


@test
def renders_xml():
    """An xml extension results in no doctype and a application/xml mimetype"""
    rendered = render_response('test.xml', context)
    assert_equal(rendered.mimetype, 'application/xml')
    assert_equal(rendered.data, '<name>Rudolf</name>')


@test
def renders_js():
    """A js extension results in no doctype
    and a application/javascript mimetype"""
    rendered = render_response('test.js', context)
    assert_equal(rendered.mimetype, 'application/javascript')
    assert_equal(rendered.data, 'alert("Rudolf");\n')


@test
def renders_css():
    """A css extension results in no doctype and a text/css mimetype"""
    rendered = render_response('test.css', context)
    assert_equal(rendered.mimetype, 'text/css')
    assert_equal(rendered.data, 'h1:after { content: " Rudolf"; }\n')


@test
def renders_svg():
    """An svg extension results in an SVG doctype and mimetype"""
    rendered = render_response('test.svg', context)
    assert_equal(rendered.mimetype, 'image/svg+xml')
    assert_equal(rendered.data,
        '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
        '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
        '<svg viewBox="0 0 1000 300">\n'
        '<text x="250" y="150" font-size="55">Hi Rudolf</text>\n'
        '</svg>')


@genshi.filter('html')
def prepend_title(template):
    return template | Transformer('head/title').prepend('Flask-Genshi - ')

@test
def applies_filters():
    """Filters are applied for generated and rendered templates"""
    rendered = render_template('filter.html')
    assert_equal(rendered,
        '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" '
        '"http://www.w3.org/TR/html4/strict.dtd">\n'
        '<html><head><title>Flask-Genshi - Hi!</title></head></html>')


@app.context_processor
def inject_rudolf():
    return dict(rudolf='The red-nosed reindeer')

@test
def updates_context():
    """Render calls update the template context with context processors"""
    rendered = render_response('context.html')


@test
def renders_strings():
    """Strings can be rendered as templates directly"""
    rendered = render_response(string='The name is $name',
                               context=context, method='text')
    assert_equal(rendered.data, 'The name is Rudolf')

@test
@raises(RuntimeError)
def fails_without_template_or_string():
    """A template or string must be provided to render"""
    render_response(context=context, method='text')


@test
def loads_module_templates():
    """Templates can be loaded from module packages"""
    rendered = render_template('package_mod/module-template.txt', context)
    assert_equal(rendered, 'Hello modular Rudolf\n')

@test
def overrides_module_templates():
    """Module templates can be overridden with application templates"""
    rendered = render_template('package_mod/nonmodule-template.txt', context)
    assert_equal(rendered, 'Hello nonmodular Rudolf\n')


@@ -71,16 +71,28 @@
 as test,
+ with_setup,
  assert_
@@ -105,16 +105,16 @@
  raises%0A
-
 from fla
@@ -443,32 +443,61 @@
 t.pop()%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_html
@@ -848,32 +848,61 @@
 /body%3E')%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_text
@@ -1128,32 +1128,61 @@
 olf%5Cn')%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_xml(
@@ -1426,32 +1426,61 @@
 name%3E')%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_js()
@@ -1737,32 +1737,61 @@
 %22);%5Cn')%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_css(
@@ -2035,32 +2035,61 @@
 ; %7D%5Cn')%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_svg(
@@ -2673,24 +2673,53 @@
 - ')%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def applies_
@@ -3152,24 +3152,53 @@
 er')%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def updates_
@@ -3334,24 +3334,53 @@
 l')%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def renders_
@@ -3629,16 +3629,45 @@
 %0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 @raises(
@@ -3834,24 +3834,53 @@
 t')%0A%0A%0A@test%0A
+@with_setup(setup, teardown)%0A
 def loads_mo
@@ -4081,22 +4081,51 @@
 lf%5Cn')%0A%0A
-
 @test%0A
+@with_setup(setup, teardown)%0A
 def over

Make tests run on 2.7 for real (nose.collector bug?)