<s> "<STR_LIT>" <EOL> from __future__ import unicode_literals <EOL> from contextlib import contextmanager <EOL> from django . test import TestCase <EOL> from django . template import RequestContext , Template <EOL> from django . test . client import RequestFactory <EOL> from . test_views import BaseViewTestCase <EOL> from scribbler . utils import _flatten , get_variables <EOL> @ contextmanager <EOL> def context_context_manager ( context ) : <EOL> if hasattr ( context , '<STR_LIT>' ) : <EOL> template = Template ( '<STR_LIT>' ) <EOL> with context . bind_template ( template ) : <EOL> yield <EOL> else : <EOL> yield <EOL> class FlattenTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_flattens_inner_list ( self ) : <EOL> "<STR_LIT>" <EOL> nested_list = [ <NUM_LIT:1> , [ <NUM_LIT:2> , [ <NUM_LIT:3> , <NUM_LIT:4> , [ <NUM_LIT:5> ] , ] , <NUM_LIT:6> , <NUM_LIT:7> ] , <NUM_LIT:8> ] <EOL> self . assertEqual ( list ( _flatten ( nested_list ) ) , list ( range ( <NUM_LIT:1> , <NUM_LIT:9> ) ) ) <EOL> def test_flattens_tuples ( self ) : <EOL> "<STR_LIT>" <EOL> nested_tuples = ( <NUM_LIT:1> , ( <NUM_LIT:2> , <NUM_LIT:3> , ( <NUM_LIT:4> , ) , <NUM_LIT:5> ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( list ( _flatten ( nested_tuples ) ) , list ( range ( <NUM_LIT:1> , <NUM_LIT:7> ) ) ) <EOL> def test_flattens_sets ( self ) : <EOL> "<STR_LIT>" <EOL> nested_sets = set ( [ <NUM_LIT:1> , frozenset ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) , <NUM_LIT:4> ] ) <EOL> self . assertEqual ( list ( _flatten ( nested_sets ) ) , list ( range ( <NUM_LIT:1> , <NUM_LIT:5> ) ) ) <EOL> def test_flatten_nested_combinations ( self ) : <EOL> "<STR_LIT>" <EOL> nested = [ <NUM_LIT:1> , frozenset ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) , ( <NUM_LIT:4> , ( <NUM_LIT:5> , ) , <NUM_LIT:6> ) , [ <NUM_LIT:7> ] , <NUM_LIT:8> ] <EOL> self . assertEqual ( list ( _flatten ( nested ) ) , list ( range ( <NUM_LIT:1> , <NUM_LIT:9> ) ) ) <EOL> def test_flatten_generator ( self ) : <EOL> "<STR_LIT>" <EOL> gens = [ <NUM_LIT:1> , list ( range ( <NUM_LIT:2> , <NUM_LIT:4> ) ) , ( num for num in ( <NUM_LIT:4> , list ( range ( <NUM_LIT:5> , <NUM_LIT:7> ) ) ) ) ] <EOL> self . assertEqual ( list ( _flatten ( gens ) ) , list ( range ( <NUM_LIT:1> , <NUM_LIT:7> ) ) ) <EOL> def test_flatten_string_unchanged ( self ) : <EOL> "<STR_LIT>" <EOL> data = [ '<STR_LIT:abc>' , [ '<STR_LIT:abc>' , [ '<STR_LIT:abc>' ] ] , '<STR_LIT:abc>' ] <EOL> self . assertEqual ( list ( _flatten ( data ) ) , [ '<STR_LIT:abc>' , '<STR_LIT:abc>' , '<STR_LIT:abc>' , '<STR_LIT:abc>' ] ) <EOL> def test_processor ( request ) : <EOL> return { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> class GetVariablesTestCase ( BaseViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> factory = RequestFactory ( ) <EOL> self . request = factory . get ( '<STR_LIT>' ) <EOL> self . known_globals = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:user>' ] <EOL> def _get_context ( self , dict_ = None , processors = None ) : <EOL> return RequestContext ( self . request , dict_ , processors = processors ) <EOL> def test_global_context_processors ( self ) : <EOL> """<STR_LIT>""" <EOL> context = self . _get_context ( ) <EOL> with context_context_manager ( context ) : <EOL> variables = set ( get_variables ( context ) ) <EOL> self . assertTrue ( variables . issuperset ( set ( self . known_globals ) ) ) <EOL> def test_returned_variable ( self ) : <EOL> """<STR_LIT>""" <EOL> context = self . _get_context ( { } ) <EOL> with context_context_manager ( context ) : <EOL> variables = get_variables ( context ) <EOL> self . assertTrue ( '<STR_LIT:a>' not in variables ) <EOL> context = self . _get_context ( { '<STR_LIT:a>' : <NUM_LIT:3> } ) <EOL> with context_context_manager ( context ) : <EOL> variables = get_variables ( context ) <EOL> self . assertTrue ( '<STR_LIT:a>' in variables ) <EOL> def test_custom_processors ( self ) : <EOL> context = self . _get_context ( { } , processors = [ ] ) <EOL> with context_context_manager ( context ) : <EOL> variables = get_variables ( context ) <EOL> self . assertTrue ( '<STR_LIT>' not in variables ) <EOL> context = self . _get_context ( { } , processors = [ test_processor ] ) <EOL> with context_context_manager ( context ) : <EOL> variables = get_variables ( context ) <EOL> self . assertTrue ( '<STR_LIT>' in variables ) </s>
