def test_foreach_scope(self): """Test that setting a variable in an foreach statements propagates.""" script = ("function (foo)\n" " foreach (VAR LISTVAR)\n" " endforeach ()\n" "endfunction ()\n") global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].scopes[0].set_vars[0].node, MatchesStructure(contents=Equals("VAR"))) foreach_type = find_variables_in_scopes.ScopeType.Foreach self.assertThat(global_scope.scopes[0].scopes[0].info, MatchesStructure(type=Equals(foreach_type)))
def test_global_scope(self, matcher): """Test setting and finding vars with {} at global scope.""" script = "{0}".format(gen_source_line(matcher)) global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.set_vars[0].node, MatchesStructure(contents=Equals("VALUE")))
def test_used_in_macro(self, call): """Test that a variable is marked as used in a macro.""" script = ("macro (name)\n" " f ({0})\n" "endmacro ()\n").format(call) global_scope = find_variables_in_scopes.used_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].used_vars[0], MatchesStructure(source=Equals(VarSrc.MacroVar)))
def test_in_func_scope(self, matcher): """Test setting and finding vars with {} in function scope.""" script = ("function (foo)\n" " {0}\n" "endfunction ()\n").format(gen_source_line(matcher)) global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].set_vars[0].node, MatchesStructure(contents=Equals("VALUE")))
def test_used_in_func_foreach(self, call): """Test that a variable is marked as used in a function when nested.""" script = ("function (name)\n" " foreach (VAR {0})" " endforeach ()" "endfunction ()\n").format(call) global_scope = find_variables_in_scopes.used_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].used_vars[0], MatchesStructure(source=Equals(VarSrc.FunctionVar)))
def test_while_in_func(self, matcher): """Test that using {} in a while block propagates variable to func.""" script = ("function (foo)\n" " while (CONDITION)\n" " {0}\n" " endwhile (CONDITION)\n" "endfunction ()\n").format(gen_source_line(matcher)) global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].set_vars[0].node, MatchesStructure(contents=Equals("VALUE")))
def test_global_setprop_scope(self): """Test that setting a variable in the set_property scope is global.""" script = ("function (foo)\n" " function (other)\n" " set_property (GLOBAL PROPERTY VARIABLE OTHER)\n" " endfunction ()\n" "endfunction ()\n") global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.set_vars[0].node, MatchesStructure(contents=Equals("VARIABLE")))
def test_parent_scope(self): """Test that setting a variable in the parent scope propagates.""" script = ("function (foo)\n" " function (other)\n" " set (VARIABLE OTHER PARENT_SCOPE)\n" " endfunction ()\n" "endfunction ()\n") global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].set_vars[0].node, MatchesStructure(contents=Equals("VARIABLE")))
def test_foreach_in_func(self, matcher): """Test that using {} in an foreach statements propagates variable.""" script = ("function (foo)\n" " foreach (VAR LISTVAR)\n" " {0}\n" " endforeach ()\n" "endfunction ()\n").format(gen_source_line(matcher)) global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].set_vars[0].node, MatchesStructure(contents=Equals("VALUE")))
def test_exclude_if_kws(self, keyword): """Test that there is no use for an if keyword.""" script = ("if ({0} OTHER)\n" "endif ()").format(keyword) global_scope = find_variables_in_scopes.used_in_tree(ast.parse(script)) self.assertThat(global_scope.used_vars[0].node, MatchesStructure(contents=Not(Equals(keyword))))
def test_exclude_foreach_kws(self, keyword): """Test that there is no use for a foreach keyword.""" script = ("foreach (VAR {0} LIST)\n" "endforeach ()").format(keyword) global_scope = find_variables_in_scopes.used_in_tree(ast.parse(script)) self.assertThat(global_scope.used_vars[0].node, MatchesStructure(contents=Not(Equals(keyword))))
def test_no_use_by_foreach_var(self): """Test that there is no use for a foreach var.""" script = ("foreach (VAR ${LIST})\n" "endforeach ()") global_scope = find_variables_in_scopes.used_in_tree(ast.parse(script)) self.assertThat(global_scope.used_vars[0].node, MatchesStructure(contents=Not(Equals("VAR"))))
def test_use_at_toplevel(self, call): """Test that a variable is marked as used at the toplevel.""" script = "f ({0})".format(call) global_scope = find_variables_in_scopes.used_in_tree(ast.parse(script)) self.assertThat(global_scope.used_vars[0], MatchesStructure(source=Equals(VarSrc.GlobalVar)))
def test_macro_var_scope(self): """Test macro variable scope.""" script = ("macro (foo VAR)\n" "endmacro ()") global_scope = find_variables_in_scopes.set_in_tree(ast.parse(script)) self.assertThat(global_scope.scopes[0].set_vars[0].node, MatchesStructure(contents=Equals("VAR")))