<s> import unittest <EOL> from pathlib import Path <EOL> from pprint import pprint <EOL> from vint . ast . parsing import Parser <EOL> from vint . ast . plugin . scope_plugin . redir_assignment_parser import traverse <EOL> from vint . ast . node_type import NodeType <EOL> from vint . ast . plugin . scope_plugin . identifier_classifier import ( <EOL> IdentifierClassifier , <EOL> IDENTIFIER_ATTRIBUTE , <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT , <EOL> ) <EOL> FIXTURE_BASE_PATH = Path ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> Fixtures = { <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <EOL> Path ( FIXTURE_BASE_PATH , '<STR_LIT>' ) , <EOL> } <EOL> class TestIdentifierClassifier ( unittest . TestCase ) : <EOL> def create_ast ( self , file_path ) : <EOL> parser = Parser ( ) <EOL> ast = parser . parse_file ( file_path ) <EOL> return ast <EOL> def create_id_attr ( self , is_declarative = False , is_dynamic = False , <EOL> is_member = False , is_function = False , <EOL> is_autoload = False , is_declarative_parameter = False , <EOL> is_on_str_expr_context = False ) : <EOL> return { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : is_declarative , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : is_dynamic , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : is_member , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG : is_function , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG : is_autoload , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG : is_declarative_parameter , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT : is_on_str_expr_context , <EOL> } <EOL> def assertAttributesInIdentifiers ( self , ast , expected_id_attr_map ) : <EOL> footstamps = { id_name : False for id_name in expected_id_attr_map } <EOL> def on_enter_handler ( node ) : <EOL> if IDENTIFIER_ATTRIBUTE not in node : <EOL> return <EOL> id_name = node [ '<STR_LIT:value>' ] <EOL> footstamps [ id_name ] = True <EOL> pprint ( ( NodeType ( node [ '<STR_LIT:type>' ] ) , id_name ) ) <EOL> self . assertEqual ( expected_id_attr_map [ id_name ] , node [ IDENTIFIER_ATTRIBUTE ] ) <EOL> traverse ( ast , on_enter = on_enter_handler ) <EOL> self . assertTrue ( all ( footstamps . values ( ) ) , <EOL> self . _create_fail_message_for_assertion_attr ( footstamps ) ) <EOL> def _create_fail_message_for_assertion_attr ( self , footstamps ) : <EOL> unvisiteds = filter ( lambda item : not item [ <NUM_LIT:1> ] , footstamps . items ( ) ) <EOL> unvisited_id_names = map ( lambda footstamp : footstamp [ <NUM_LIT:0> ] , unvisiteds ) <EOL> return '<STR_LIT>' + '<STR_LIT:U+002CU+0020>' . join ( unvisited_id_names ) <EOL> def test_attach_identifier_attributes_with_declaring_func ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_calling_func ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False , <EOL> is_autoload = True , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False , <EOL> is_member = True , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False ) , <EOL> "<STR_LIT>" : <EOL> self . create_id_attr ( is_declarative = False , <EOL> is_member = True , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = False , <EOL> is_function = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_declaring_func_in_func ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : <EOL> self . create_id_attr ( is_declarative = True , <EOL> is_function = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_declaring_var ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_declaring_var_in_func ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_declaring_with_dict_key ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = False ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , <EOL> is_member = True , <EOL> is_function = True ) , <EOL> "<STR_LIT>" : self . create_id_attr ( is_declarative = True , <EOL> is_member = True , <EOL> is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , <EOL> is_member = True , <EOL> is_function = False ) , <EOL> "<STR_LIT>" : self . create_id_attr ( is_declarative = True , <EOL> is_member = True , <EOL> is_function = False ) , <EOL> "<STR_LIT>" : self . create_id_attr ( is_declarative = False , <EOL> is_member = False , <EOL> is_function = False ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_destructuring_assignment ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = False ) , <EOL> '<STR_LIT:1>' : self . create_id_attr ( is_declarative = True , is_member = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = False , is_dynamic = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_func_param ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_declarative_parameter = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_declarative_parameter = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_declarative_parameter = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_declarative_parameter = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_declarative_parameter = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True , is_function = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_loop_var ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = False ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_redir ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_declarative = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_arithmetic_assignment ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> def test_attach_identifier_attributes_with_map_func ( self ) : <EOL> ast = self . create_ast ( Fixtures [ '<STR_LIT>' ] ) <EOL> id_classifier = IdentifierClassifier ( ) <EOL> expected_id_attr_map = { <EOL> '<STR_LIT>' : self . create_id_attr ( is_on_str_expr_context = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_on_str_expr_context = True ) , <EOL> '<STR_LIT>' : self . create_id_attr ( is_function = True ) , <EOL> } <EOL> attached_ast = id_classifier . attach_identifier_attributes ( ast ) <EOL> self . assertAttributesInIdentifiers ( attached_ast , expected_id_attr_map ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
