<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . template import Context , Template <EOL> from django . test import TestCase <EOL> from haystack . utils import Highlighter <EOL> class BorkHighlighter ( Highlighter ) : <EOL> def render_html ( self , highlight_locations = None , start_offset = None , end_offset = None ) : <EOL> highlighted_chunk = self . text_block [ start_offset : end_offset ] <EOL> for word in self . query_words : <EOL> highlighted_chunk = highlighted_chunk . replace ( word , '<STR_LIT>' ) <EOL> return highlighted_chunk <EOL> class TemplateTagTestCase ( TestCase ) : <EOL> def render ( self , template , context ) : <EOL> t = Template ( template ) <EOL> c = Context ( context ) <EOL> return t . render ( c ) <EOL> class HighlightTestCase ( TemplateTagTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( HighlightTestCase , self ) . setUp ( ) <EOL> self . sample_entry = """<STR_LIT>""" <EOL> def test_simple ( self ) : <EOL> template = """<STR_LIT>""" <EOL> context = { <EOL> '<STR_LIT>' : self . sample_entry , <EOL> '<STR_LIT>' : '<STR_LIT:index>' , <EOL> } <EOL> self . assertEqual ( self . render ( template , context ) , u'<STR_LIT>' ) <EOL> template = """<STR_LIT>""" <EOL> context = { <EOL> '<STR_LIT>' : self . sample_entry , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( self . render ( template , context ) , u'<STR_LIT>' ) <EOL> template = """<STR_LIT>""" <EOL> context = { <EOL> '<STR_LIT>' : self . sample_entry , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( self . render ( template , context ) , u'<STR_LIT>' ) <EOL> template = """<STR_LIT>""" <EOL> context = { } <EOL> self . assertEqual ( self . render ( template , context ) , u'<STR_LIT>' ) <EOL> def test_custom ( self ) : <EOL> old_custom_highlighter = getattr ( settings , '<STR_LIT>' , None ) <EOL> settings . HAYSTACK_CUSTOM_HIGHLIGHTER = '<STR_LIT>' <EOL> template = """<STR_LIT>""" <EOL> context = { <EOL> '<STR_LIT>' : self . sample_entry , <EOL> '<STR_LIT>' : '<STR_LIT:index>' , <EOL> } <EOL> self . assertRaises ( ImproperlyConfigured , self . render , template , context ) <EOL> settings . HAYSTACK_CUSTOM_HIGHLIGHTER = '<STR_LIT>' <EOL> template = """<STR_LIT>""" <EOL> context = { <EOL> '<STR_LIT>' : self . sample_entry , <EOL> '<STR_LIT>' : '<STR_LIT:index>' , <EOL> } <EOL> self . assertEqual ( self . render ( template , context ) , u'<STR_LIT>' ) <EOL> settings . HAYSTACK_CUSTOM_HIGHLIGHTER = None </s>
