def test_fail_priv_out_namespace(self, definition): """structure/namespace fails when priv definition outside namespace.""" script = "{0} (_not_our_call ARGUMENT)\nend{0} ()".format(definition) with ExpectedException(LinterFailure): run_linter_throw(script, namespace="our", whitelist=["structure/namespace"])
def test_fail_align_macro_name(self): # suppress(no-self-use) """style/argument_align fails when args not aligned after second.""" with ExpectedException(LinterFailure): run_linter_throw("macro (name TWO\n" " THREE)\n" "endmacro ()\n", whitelist=["style/argument_align"])
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_fail_bad_kw_align(self): # suppress(no-self-use) """style/argument_align fails if args on same line spaced unevenly.""" with ExpectedException(LinterFailure): run_linter_throw("call (ONE\n" " TWO THREE\n" " FOUR)\n", whitelist=["style/argument_align"])
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_fail_mismatch_if_alt(self, alt): """style/indent fails when else, elseif has mismatched indent.""" with ExpectedException(LinterFailure): script = "if (COND)\n {0} (COND)\nendif ()" run_linter_throw(script.format(alt), whitelist=["style/indent"], indent=1)
def test_fail_set_outside_scope(self): # suppress(no-self-use) """Check access/private_var fails when var set outside scope.""" script = ("foreach (_LOOP_VAR ${LIST})\n" "endforeach ()\n" "message (STATUS \"${_LOOP_VAR}\")\n") with ExpectedException(LinterFailure): run_linter_throw(script, whitelist=["access/private_var"])
def test_fail_if_kw_not_var_align(self, keyword): # suppress(no-self-use) """style/argument_align fails when args not aligned after second.""" kw_len = len(keyword) with ExpectedException(LinterFailure): run_linter_throw("call (ONE\n" " {0} ONE".format(keyword) + " " + " " * kw_len + " TWO)", whitelist=["style/argument_align"])
def test_fail_variable_unused(self, matcher): # suppress(no-self-use) """Check unused/var_in_func fails when private var is unused in {}.""" find = matcher.find xform = lambda x: "_{0}".format(x) # suppress(unnecessary-lambda,E731) script = ("{0} ({1})\n").format( matcher.cmd, find.generate(matcher.sub, lambda x: x, xform)) with ExpectedException(LinterFailure): run_linter_throw(script, whitelist=["unused/private_var"])
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_fail_variable_unused(self, matcher): # suppress(no-self-use) """Check unused/var_in_func fails when private var is unused in {}.""" find = matcher.find xform = lambda x: "_{0}".format(x) # suppress(unnecessary-lambda,E731) script = ("{0} ({1})\n").format(matcher.cmd, find.generate(matcher.sub, lambda x: x, xform)) with ExpectedException(LinterFailure): run_linter_throw(script, whitelist=["unused/private_var"])
def test_lint_fail_excessive_space(self): # suppress(no-self-use) """Check that style/space_before_func fails. Test fails where there is more than one space between a function name and a call, like so function_name () """ with ExpectedException(LinterFailure): run_linter_throw("function_call ()\n", whitelist=["style/space_before_func"])
def test_pass_pub_var_unused(self, matcher): """Check unused/private_var passes when public var is unused in {}.""" find = matcher.find script = ("{0} ({1})\n").format( matcher.cmd, find.generate(matcher.sub, lambda x: x, lambda x: x)) self.assertTrue( run_linter_throw(script, whitelist=["unused/private_var"]))
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_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_pass_foreach_set(self): """Check access/private_var passes when private var set by foreach.""" script = ("foreach (_LOOP_VAR ${LIST})\n" " message (STATUS \"${_LOOP_VAR}\")\n" "endforeach ()\n") self.assertTrue( run_linter_throw(script, whitelist=["access/private_var"]))
def test_pass_use_var_autoderef(self, cmd, generator): """Check that unused/private_var passes when var autodereffed in {}.""" script = ("set (_ARGUMENT 0)\n" "{0} ({1})\n" "end{0} ()\n").format(cmd, generator(lambda x: "_" + x)) self.assertTrue( run_linter_throw(script, whitelist=["unused/private_var"]))
def test_pass_use_var_autoderef(self, cmd, generator): """Check that unused/private_var passes when var autodereffed in {}.""" script = ("set (_ARGUMENT 0)\n" "{0} ({1})\n" "end{0} ()\n").format(cmd, generator(lambda x: "_" + x)) self.assertTrue(run_linter_throw(script, whitelist=["unused/private_var"]))
def test_pass_priv_func_used(self): """Check access/private_var passes when using private func as var.""" script = ("function (_private_function)\n" "endfunction ()\n" "call (_private_function)\n") self.assertTrue( run_linter_throw(script, whitelist=["access/private_var"]))
def test_pass_priv_func_as_var(self, definition): """unused/private passes if private function used as a variable.""" script = ("{0} (_definition ARGUMENT)\n" "end{0} ()\n" "call (_definition)\n").format(definition) self.assertTrue(run_linter_throw(script, whitelist=["unused/private"]))
def test_pass_used_own_priv_def(self, definition): """access/other_private passes if using own private definition.""" script = ("{0} (_definition ARGUMENT)\n" "end{0} ()\n" "_definition (ARGUMENT)\n").format(definition) self.assertTrue( run_linter_throw(script, whitelist=["access/other_private"]))
def test_pass_priv_func_namespaced(self, definition): """structure/namespace passes when private functions namespaced.""" script = "{0} (_our_call ARGUMENT)\nend{0} ()".format(definition) self.assertTrue( run_linter_throw(script, whitelist=["structure/namespace"], namespace="our"))
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_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_pass_pub_var_unused(self, matcher): """Check unused/private_var passes when public var is unused in {}.""" find = matcher.find script = ("{0} ({1})\n").format(matcher.cmd, find.generate(matcher.sub, lambda x: x, lambda x: x)) self.assertTrue(run_linter_throw(script, whitelist=["unused/private_var"]))
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_no_var_set(self, matcher): """Check that style/set_var_case passes with {0.cmd}. Where no variable is actually set, then there is no linter failure """ # This will trip up matchers that match other arguments result = run_linter_throw("{0} ()\n".format(matcher.cmd), whitelist=["style/set_var_case"]) self.assertTrue(result)
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_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_global_priv_used(self): """Check unused/private_var passes when var is used in nested ctx.""" script = ("function (f)\n" " set_property (GLOBAL PROPERTY _VALUE)\n" "endfunction (f)\n" "function (g)\n" " get_property (VAR GLOBAL PROPERTY _VALUE)\n" "endfunction ()\n") self.assertTrue(run_linter_throw(script, whitelist=["unused/private_var"]))
def test_pass_variable_used(self, matcher): """Check access/private_var passes when priv var set by {}.""" # suppress(unnecessary-lambda,E731) xform = lambda x: "_{0}".format(x) private_var = matcher.find.generate(matcher.sub, lambda x: x, xform) script = ("{0} ({1})\n" "message ({2})\n").format(matcher.cmd, private_var, "${_VALUE}") self.assertTrue( run_linter_throw(script, whitelist=["access/private_var"]))
def test_global_priv_used(self): """Check unused/private_var passes when var is used in nested ctx.""" script = ("function (f)\n" " set_property (GLOBAL PROPERTY _VALUE)\n" "endfunction (f)\n" "function (g)\n" " get_property (VAR GLOBAL PROPERTY _VALUE)\n" "endfunction ()\n") self.assertTrue( run_linter_throw(script, whitelist=["unused/private_var"]))
def test_global_used(self): """Check unused/var_in_func passes when global property is used.""" script = ("function (f)\n" " set_property (GLOBAL PROPERTY VALUE)\n" "endfunction (f)\n" "function (g)\n" " get_property (VAR GLOBAL PROPERTY VALUE)\n" " message (${VAR})\n" "endfunction ()\n") self.assertTrue( run_linter_throw(script, whitelist=["unused/var_in_func"]))
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_lint_pass_comment(self): """Check that style/space_before_func passes for commented calls. Test passes where there is no space before a function name and a call, where that line is commented like so: # function_name() """ result = run_linter_throw("# function_call()\n", whitelist=["style/space_before_func"]) self.assertTrue(result)
def test_global_used(self): """Check unused/var_in_func passes when global property is used.""" script = ("function (f)\n" " set_property (GLOBAL PROPERTY VALUE)\n" "endfunction (f)\n" "function (g)\n" " get_property (VAR GLOBAL PROPERTY VALUE)\n" " message (${VAR})\n" "endfunction ()\n") self.assertTrue(run_linter_throw(script, whitelist=["unused/var_in_func"]))
def test_lint_pass_inside_quotes(self): """Check that style/space_before_func passes for quoted calls. Test passes where there is no space before a function name and a call, where that line is inside quotes "function_name()" """ result = run_linter_throw("call (\"function_call()\")\n", whitelist=["style/space_before_func"]) self.assertTrue(result)
def test_lint_pass(self): """Check that style/space_before_func passes. Test passes where there is a single space before a function name and a call, like so: function_name () """ result = run_linter_throw("function_call ()\n", whitelist=["style/space_before_func"]) 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"]))
def test_pass_variable_used(self, matcher): """Check unused/private_var passes when var set by {} is used.""" find = matcher.find xform = lambda x: "_{0}".format(x) # suppress(unnecessary-lambda,E731) script = ("function (f)\n" " {0} ({1})\n" " message ({2})\n" "endfunction ()\n").format( matcher.cmd, find.generate(matcher.sub, lambda x: x, xform), "${_VALUE}") self.assertTrue( run_linter_throw(script, whitelist=["unused/private_var"]))
def test_pass_variable_used(self, matcher): """Check unused/private_var passes when var set by {} is used.""" find = matcher.find xform = lambda x: "_{0}".format(x) # suppress(unnecessary-lambda,E731) script = ("function (f)\n" " {0} ({1})\n" " message ({2})\n" "endfunction ()\n").format(matcher.cmd, find.generate(matcher.sub, lambda x: x, xform), "${_VALUE}") self.assertTrue(run_linter_throw(script, whitelist=["unused/private_var"]))
def test_pass_pub_var_used(self): """Check access/private_var passes if undefined public var is used.""" script = "message (${VALUE})\n" self.assertTrue( run_linter_throw(script, whitelist=["access/private_var"]))
def test_pass_pub_func_unused(self, definition): """unused/private passes if public function unused.""" script = ("{0} (definition ARGUMENT)\n" "end{0} ()\n").format(definition) self.assertTrue(run_linter_throw(script, whitelist=["unused/private"]))
def test_fail_priv_func_unused(self, definition): # suppress(no-self-use) """unused/private passes if private function unused.""" script = ("{0} (_definition ARGUMENT)\n" "end{0} ()\n").format(definition) with ExpectedException(LinterFailure): run_linter_throw(script, whitelist=["unused/private"])
def test_fail_priv_var_used(self): # suppress(no-self-use) """Check access/private_var fails on undefined private var used.""" script = "message (${_VALUE})\n" with ExpectedException(LinterFailure): run_linter_throw(script, whitelist=["access/private_var"])
def test_fail_when_using_slashes(self): # suppress(no-self-use) """Test correctness/quotes fails when using raw unquoted path.""" with ExpectedException(LinterFailure): run_linter_throw("call (abc/def)\n", whitelist=["correctness/quotes"])
def test_fail_deref_certain_vars(self, bad_deref): # suppress(no-self-use) """Test correctness/quotes fails if dereffing some variable names.""" with ExpectedException(LinterFailure): run_linter_throw("call ({0})".format(bad_deref), whitelist=["correctness/quotes"])
def get_replacement(): """Get replacement for un-namespaced private definition.""" run_linter_throw(script, namespace="our", whitelist=["structure/namespace"])
def get_replacement(): """Get the replacement for raw unquoted path.""" run_linter_throw("call (abc/def)\n", whitelist=["correctness/quotes"])
def get_replacement(): """Get the replacement for the dereferenced path.""" run_linter_throw("call ({0})".format(bad_deref), whitelist=["correctness/quotes"])
def test_pass_no_namespace_passed(self, definition): """structure/namespace passes when no namespace specified.""" script = "{0} (call ARGUMENT)\nend{0} ()".format(definition) self.assertTrue( run_linter_throw(script, whitelist=["structure/namespace"]))
def test_pass_deref_nonpath_var(self, deref): """Test correctness/quotes passes with modified path var name.""" self.assertTrue( run_linter_throw("call ({0})".format(deref), whitelist=["correctness/quotes"]))