def test_fail_no_quotes(self, matcher): # suppress(no-self-use) """Check that style/set_var_case fails with {}, because lowercase.""" line = gen_source_line(matcher, match_transform=lambda x: x.lower()) with ExpectedException(LinterFailure): run_linter_throw(line, whitelist=["style/set_var_case"])
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_pass_compound_unused(self, matcher): """Check unused/var_in_func passes if compound_lit var passed in {}.""" call = gen_source_line(matcher, match_transform=lambda x: "${" + x + "}/Other") script = ("function (f)\n" " {0}\n" "endfunction ()\n").format(call) self.assertTrue( run_linter_throw(script, whitelist=["unused/var_in_func"]))
def test_fail_variable_unused(self, matcher): # suppress(no-self-use) """Check unused/var_in_func fails when var is unused in {}.""" call = gen_source_line(matcher) script = ("function (f)\n" " {0}\n" "endfunction ()\n").format(call) with ExpectedException(LinterFailure): run_linter_throw(script, whitelist=["unused/var_in_func"])
def test_pass_deref_unused(self, matcher): """Check unused/var_in_func passes when deref var is set with {}.""" call = gen_source_line(matcher, match_transform=lambda x: "${" + x + "}") script = ("function (f)\n" " {0}\n" "endfunction ()\n").format(call) self.assertTrue( run_linter_throw(script, whitelist=["unused/var_in_func"]))
def test_replace_no_quotes(self, matcher): """Check that style/set_var_case replaces {} with uppercase var. Replacement should have uppercase matched argument """ correct = gen_source_line(matcher) incorrect = gen_source_line(matcher, match_transform=lambda x: x.lower()) def get_replacement(): """Replacement for lowercase variable.""" run_linter_throw(incorrect, whitelist=["style/set_var_case"]) exception = self.assertRaises(LinterFailure, get_replacement) self.assertEqual(replacement(exception), (1, correct))
def test_pass_no_quotes(self, matcher): """Check that style/set_var_case passes with {}. Variables set by another CMake command should only be uppercase """ result = run_linter_throw(gen_source_line(matcher), whitelist=["style/set_var_case"]) self.assertTrue(result)
def test_pass_compound_unused(self, matcher): """Check unused/var_in_func passes if compound_lit var passed in {}.""" call = gen_source_line(matcher, match_transform=lambda x: "${" + x + "}/Other") script = ("function (f)\n" " {0}\n" "endfunction ()\n").format(call) self.assertTrue(run_linter_throw(script, whitelist=["unused/var_in_func"]))
def test_pass_other_quotes(self, matcher): """Check that style/set_var_case pass with other args quoted in {}.""" quote = "\"{0}\"" xform = lambda x: quote.format(x) # suppress(unnecessary-lambda,E731) line = gen_source_line(matcher, other_transform=xform) result = run_linter_throw(line, whitelist=["style/set_var_case"]) self.assertTrue(result)
def test_pass_variable_used(self, matcher): """Check unused/var_in_func passes when var is used with {}.""" script = ("function (f)\n" " {0}\n" " message ({1})\n" "endfunction ()\n").format(gen_source_line(matcher), "${VALUE}") self.assertTrue( run_linter_throw(script, whitelist=["unused/var_in_func"]))
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_pass_variable_used(self, matcher): """Check unused/var_in_func passes when var is used with {}.""" script = ("function (f)\n" " {0}\n" " message ({1})\n" "endfunction ()\n").format(gen_source_line(matcher), "${VALUE}") self.assertTrue(run_linter_throw(script, whitelist=["unused/var_in_func"]))
def test_pass_deref_unused(self, matcher): """Check unused/var_in_func passes when deref var is set with {}.""" call = gen_source_line(matcher, match_transform=lambda x: "${" + x + "}") script = ("function (f)\n" " {0}\n" "endfunction ()\n").format(call) self.assertTrue(run_linter_throw(script, whitelist=["unused/var_in_func"]))
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_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_fail_other_quotes(self, matcher): # suppress(no-self-use) """Check that style/set_var_case fails with other args quoted in {}.""" quote = "\"{0}\"" xform = lambda x: quote.format(x) # suppress(unnecessary-lambda,E731) line = gen_source_line(matcher, match_transform=lambda x: x.lower(), other_transform=xform) with ExpectedException(LinterFailure): run_linter_throw(line, whitelist=["style/set_var_case"])
def test_pass_inside_deref(self, matcher): """Check that style/set_var_case passes when var in deref, like {}. Pass if variable is uppercase and inside of a deref, because variable dereferences are not sink variables. """ xform = lambda x: "${" + x + "}" # suppress(E731) result = run_linter_throw(gen_source_line(matcher, match_transform=xform), whitelist=["style/set_var_case"]) self.assertTrue(result)
def test_pass_nested_use(self, matcher): """Check unused/var_in_func passes with {}, with use nested.""" script = ("function (f)\n" " {0}\n" " foreach (VAR LIST)\n" " message ({1} VAR)\n" " endforeach ()\n" "endfunction ()\n").format(gen_source_line(matcher), "${VALUE}") self.assertTrue( run_linter_throw(script, whitelist=["unused/var_in_func"]))
def test_pass_nested_use(self, matcher): """Check unused/var_in_func passes with {}, with use nested.""" script = ("function (f)\n" " {0}\n" " foreach (VAR LIST)\n" " message ({1} VAR)\n" " endforeach ()\n" "endfunction ()\n").format(gen_source_line(matcher), "${VALUE}") self.assertTrue(run_linter_throw(script, whitelist=["unused/var_in_func"]))