<s> from django import forms <EOL> from django . template import Context , Template , TemplateSyntaxError <EOL> from django . template . loader import get_template <EOL> from django . test import SimpleTestCase <EOL> from django . test . utils import setup_test_template_loader , restore_template_loaders <EOL> class TestForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> name = forms . CharField ( label = "<STR_LIT:Name>" ) <EOL> is_cool = forms . BooleanField ( label = '<STR_LIT>' ) <EOL> gender = forms . ChoiceField ( <EOL> label = "<STR_LIT>" , <EOL> widget = forms . RadioSelect ( ) <EOL> ) <EOL> hidden_gender = forms . ChoiceField ( <EOL> widget = forms . HiddenInput , <EOL> choices = [ ( '<STR_LIT:m>' , '<STR_LIT>' ) , ( '<STR_LIT:f>' , '<STR_LIT>' ) ] <EOL> ) <EOL> class SelectForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> CHOICES = [ <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ] <EOL> model = forms . TypedChoiceField ( choices = CHOICES ) <EOL> radio = forms . TypedChoiceField ( choices = CHOICES , widget = forms . RadioSelect ) <EOL> multiple = forms . TypedMultipleChoiceField ( choices = CHOICES ) <EOL> gender = forms . MultipleChoiceField ( choices = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> class TemplateTestMixin ( object ) : <EOL> TEMPLATE_BASE = '''<STR_LIT>''' <EOL> TEMPLATES = { } <EOL> PARTIALS = { } <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . context = Context ( { '<STR_LIT>' : TestForm ( ) } ) <EOL> for key , tmpl in cls . PARTIALS . items ( ) : <EOL> cls . TEMPLATES [ key ] = cls . TEMPLATE_BASE . format ( tmpl ) <EOL> setup_test_template_loader ( cls . TEMPLATES ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> restore_template_loaders ( ) <EOL> STOCK_TEMPLATE = '''<STR_LIT>''' <EOL> class FieldTagTest ( TemplateTestMixin , SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> } <EOL> PARTIALS = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> def test_use_correct_block ( self ) : <EOL> """<STR_LIT>""" <EOL> template = get_template ( '<STR_LIT>' ) <EOL> result = template . render ( self . context ) <EOL> self . assertEqual ( result , """<STR_LIT>""" ) <EOL> def test_unknown_block ( self ) : <EOL> """<STR_LIT>""" <EOL> template = get_template ( '<STR_LIT>' ) <EOL> with self . assertRaises ( TemplateSyntaxError ) : <EOL> template . render ( self . context ) <EOL> def test_auto_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> template = get_template ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> template . render ( self . context ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> template . render ( self . context ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> template . render ( self . context ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> def test_force_text_widgets ( self ) : <EOL> """<STR_LIT>""" <EOL> initial1 = { <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> } <EOL> initial2 = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:1>' , '<STR_LIT:2>' ] <EOL> } <EOL> initial3 = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:1>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> ctx1 = Context ( { '<STR_LIT>' : SelectForm ( initial = initial1 ) } ) <EOL> ctx2 = Context ( { '<STR_LIT>' : SelectForm ( initial = initial2 ) } ) <EOL> ctx3 = Context ( { '<STR_LIT>' : SelectForm ( initial = initial3 ) } ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> expected_html = """<STR_LIT>""" <EOL> self . assertInHTML ( expected_html , template . render ( ctx1 ) ) <EOL> self . assertInHTML ( expected_html , template . render ( ctx2 ) ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> expected_html = """<STR_LIT>""" <EOL> self . assertInHTML ( expected_html , template . render ( ctx1 ) ) <EOL> self . assertInHTML ( expected_html , template . render ( ctx2 ) ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> expected_html1 = """<STR_LIT>""" <EOL> expected_html2 = """<STR_LIT>""" <EOL> self . assertInHTML ( expected_html1 , template . render ( ctx1 ) ) <EOL> self . assertInHTML ( expected_html2 , template . render ( ctx1 ) ) <EOL> self . assertInHTML ( expected_html1 , template . render ( ctx2 ) ) <EOL> self . assertInHTML ( expected_html2 , template . render ( ctx2 ) ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> expected_multiple = """<STR_LIT>""" <EOL> expected_multiple2 = """<STR_LIT>""" <EOL> self . assertInHTML ( expected_multiple , template . render ( ctx3 ) ) <EOL> self . assertInHTML ( expected_multiple2 , template . render ( ctx3 ) ) <EOL> def test_force_text_on_None ( self ) : <EOL> """<STR_LIT>""" <EOL> context = Context ( { '<STR_LIT>' : TestForm ( ) } ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> rendered = template . render ( context ) <EOL> self . assertEqual ( rendered , u'<STR_LIT>' ) <EOL> class UseTagTest ( TemplateTestMixin , SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> } <EOL> PARTIALS = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> def test_use_tag ( self ) : <EOL> """<STR_LIT>""" <EOL> template = get_template ( '<STR_LIT>' ) <EOL> self . assertEqual ( template . render ( self . context ) , "<STR_LIT>" ) <EOL> def test_use_tag_inherits_context ( self ) : <EOL> """<STR_LIT>""" <EOL> template = get_template ( '<STR_LIT>' ) <EOL> context = Context ( { '<STR_LIT:test>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( template . render ( context ) , '<STR_LIT>' ) <EOL> class FlatAttrsFilterTest ( TemplateTestMixin , SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : '''<STR_LIT:U+0020>''' , <EOL> '<STR_LIT>' : STOCK_TEMPLATE . format ( """<STR_LIT>""" ) , <EOL> } <EOL> def test_flat_attrs_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> template = get_template ( '<STR_LIT>' ) <EOL> context = Context ( { '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> } } ) <EOL> self . assertEqual ( template . render ( context ) , """<STR_LIT>""" ) <EOL> class DefaultTemplateTest ( SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> template = """<STR_LIT>""" <EOL> def _render_string ( self , template , context = None ) : <EOL> t = Template ( template ) <EOL> if context is None : <EOL> context = Context ( { '<STR_LIT>' : TestForm ( ) } ) <EOL> return t . render ( context ) <EOL> def test_default_template ( self ) : <EOL> """<STR_LIT>""" <EOL> template = self . template . format ( '<STR_LIT>' ) <EOL> try : <EOL> render = self . _render_string ( template ) <EOL> except TemplateSyntaxError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class InheritanceTest ( TemplateTestMixin , SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> TEMPLATE_BASE = '''<STR_LIT>''' <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> } <EOL> PARTIALS = { <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> } <EOL> def test_inherited_form ( self ) : <EOL> template = get_template ( '<STR_LIT>' ) <EOL> self . assertEqual ( template . render ( self . context ) , '<STR_LIT>' ) <EOL> class MultipleFormsTest ( TemplateTestMixin , SimpleTestCase ) : <EOL> """<STR_LIT>""" <EOL> TEMPLATE_BASE = "<STR_LIT>" <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> } <EOL> def test_multiple_forms ( self ) : <EOL> template = get_template ( '<STR_LIT>' ) <EOL> rendered = template . render ( self . context ) <EOL> self . assertIn ( '<STR_LIT:foo>' , rendered ) <EOL> self . assertIn ( '<STR_LIT:bar>' , rendered ) </s>
