<s> import pytest <EOL> from vint . ast . node_type import NodeType <EOL> from vint . ast . plugin . scope_plugin . identifier_classifier import ( <EOL> IDENTIFIER_ATTRIBUTE , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG , <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_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> from vint . ast . plugin . scope_plugin . scope_detector import ( <EOL> ScopeVisibility as Vis , <EOL> detect_scope_visibility , <EOL> normalize_variable_name , <EOL> is_builtin_variable , <EOL> ExplicityOfScopeVisibility , <EOL> get_explicity_of_scope_visibility , <EOL> ) <EOL> def create_scope ( visibility ) : <EOL> return { <EOL> '<STR_LIT>' : visibility , <EOL> } <EOL> def create_scope_visibility_hint ( visibility , is_implicit = False ) : <EOL> return { <EOL> '<STR_LIT>' : visibility , <EOL> '<STR_LIT>' : is_implicit , <EOL> } <EOL> def create_id ( id_value , is_declarative = True , is_function = False , is_autoload = False , <EOL> is_declarative_parameter = False , is_on_str_expr_context = False ) : <EOL> return { <EOL> '<STR_LIT:type>' : NodeType . IDENTIFIER . value , <EOL> '<STR_LIT:value>' : id_value , <EOL> IDENTIFIER_ATTRIBUTE : { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : is_declarative , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : False , <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> } <EOL> def create_env ( env_value ) : <EOL> return { <EOL> '<STR_LIT:type>' : NodeType . ENV . value , <EOL> '<STR_LIT:value>' : env_value , <EOL> IDENTIFIER_ATTRIBUTE : { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : True , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT : False , <EOL> } , <EOL> } <EOL> def create_option ( opt_value ) : <EOL> return { <EOL> '<STR_LIT:type>' : NodeType . OPTION . value , <EOL> '<STR_LIT:value>' : opt_value , <EOL> IDENTIFIER_ATTRIBUTE : { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : True , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT : False , <EOL> } , <EOL> } <EOL> def create_reg ( reg_value ) : <EOL> return { <EOL> '<STR_LIT:type>' : NodeType . REG . value , <EOL> '<STR_LIT:value>' : reg_value , <EOL> IDENTIFIER_ATTRIBUTE : { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : True , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT : False , <EOL> } , <EOL> } <EOL> def create_curlyname ( is_declarative = True ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:type>' : NodeType . CURLYNAME . value , <EOL> '<STR_LIT:value>' : [ <EOL> { <EOL> '<STR_LIT:type>' : NodeType . CURLYNAMEPART . value , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> } , <EOL> { <EOL> '<STR_LIT:type>' : NodeType . CURLYNAMEEXPR . value , <EOL> '<STR_LIT:value>' : { <EOL> '<STR_LIT:type>' : NodeType . CURLYNAMEEXPR . value , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> ] , <EOL> IDENTIFIER_ATTRIBUTE : { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : is_declarative , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : True , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT : False , <EOL> } , <EOL> } <EOL> def create_subscript_member ( is_declarative = True ) : <EOL> return { <EOL> '<STR_LIT:type>' : NodeType . IDENTIFIER . value , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> IDENTIFIER_ATTRIBUTE : { <EOL> IDENTIFIER_ATTRIBUTE_DECLARATION_FLAG : is_declarative , <EOL> IDENTIFIER_ATTRIBUTE_DYNAMIC_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_MEMBER_FLAG : True , <EOL> IDENTIFIER_ATTRIBUTE_FUNCTION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_AUTOLOAD_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_PARAMETER_DECLARATION_FLAG : False , <EOL> IDENTIFIER_ATTRIBUTE_STRING_EXPRESSION_CONTEXT : False , <EOL> } , <EOL> } <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . FUNCTION_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . FUNCTION_LOCAL , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = True , is_declarative_parameter = True ) , Vis . FUNCTION_LOCAL , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , Vis . BUILTIN , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , Vis . BUILTIN , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT:count>' ) , Vis . BUILTIN , True ) , <EOL> ( Vis . SCRIPT_LOCAL , create_curlyname ( ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_curlyname ( ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_subscript_member ( ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_subscript_member ( ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , Vis . INVALID , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . FUNCTION_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . FUNCTION_LOCAL , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . FUNCTION_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . FUNCTION_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . FUNCTION_LOCAL , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . BUILTIN , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , Vis . BUILTIN , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT:count>' , is_declarative = False ) , Vis . BUILTIN , True ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . BUILTIN , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_curlyname ( is_declarative = False ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_curlyname ( is_declarative = False ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_subscript_member ( is_declarative = False ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_subscript_member ( is_declarative = False ) , Vis . UNANALYZABLE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = False , is_autoload = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True , is_autoload = True ) , Vis . GLOBAL_LIKE , True ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . SCRIPT_LOCAL , False ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , Vis . GLOBAL_LIKE , False ) , <EOL> ] <EOL> ) <EOL> def test_detect_scope_visibility ( context_scope_visibility , id_node , expected_scope_visibility , expected_implicity ) : <EOL> scope = create_scope ( context_scope_visibility ) <EOL> scope_visibility_hint = detect_scope_visibility ( id_node , scope ) <EOL> expected_scope_visibility_hint = create_scope_visibility_hint ( expected_scope_visibility , <EOL> is_implicit = expected_implicity ) <EOL> assert expected_scope_visibility_hint == scope_visibility_hint <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_declarative_parameter = True ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . FUNCTION_LOCAL , create_id ( '<STR_LIT:count>' ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_env ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_option ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ( Vis . SCRIPT_LOCAL , create_reg ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> ] <EOL> ) <EOL> def test_normalize_variable_name ( context_scope_visibility , node , expected_variable_name ) : <EOL> scope = create_scope ( context_scope_visibility ) <EOL> normalized_variable_name = normalize_variable_name ( node , scope ) <EOL> assert expected_variable_name == normalized_variable_name <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( '<STR_LIT>' , False , False ) , <EOL> ( '<STR_LIT:count>' , False , True ) , <EOL> ( '<STR_LIT>' , False , True ) , <EOL> ( '<STR_LIT:key>' , False , False ) , <EOL> ( '<STR_LIT>' , False , False ) , <EOL> ( '<STR_LIT>' , True , False ) , <EOL> ( '<STR_LIT>' , True , True ) , <EOL> ] <EOL> ) <EOL> def test_is_builtin_variable ( id_value , is_function , expected_result ) : <EOL> id_node = create_id ( id_value , is_function = is_function ) <EOL> result = is_builtin_variable ( id_node ) <EOL> assert expected_result == result <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( create_id ( '<STR_LIT>' ) , ExplicityOfScopeVisibility . IMPLICIT ) , <EOL> ( create_id ( '<STR_LIT>' ) , ExplicityOfScopeVisibility . EXPLICIT ) , <EOL> ( create_id ( '<STR_LIT>' , is_declarative = True , is_declarative_parameter = True ) , ExplicityOfScopeVisibility . EXPLICIT ) , <EOL> ( create_id ( '<STR_LIT>' , is_declarative = False , is_function = True ) , ExplicityOfScopeVisibility . EXPLICIT ) , <EOL> ( create_curlyname ( ) , ExplicityOfScopeVisibility . UNANALYZABLE ) , <EOL> ] <EOL> ) <EOL> def test_get_explicity_of_scope_visibility ( node , expected_result ) : <EOL> result = get_explicity_of_scope_visibility ( node ) <EOL> assert expected_result == result </s>
