def test_header_files_get_analyzed(self): cpp_file = test_support.create_tempfile(['#include "header.h"', ""], "cpp_file.cpp") # Just so it is available _ = test_support.create_tempfile( get_simple_non_conforming_suffix_source(), "header.h") violations, diagnostics = self.apply_checker( samo_suffix_case_checker.token_based_rule, cpp_file) self.assertEqual(len(violations), 1) self.assertEqual(violations[0].file_name, "header.h") self.assertEqual(len(diagnostics), 0)
def test_two_severity_3(self): source_file = test_support.create_tempfile(["int8 a;", "int8 b;"]) translation_unit = tu_parser.create_translation_unit(source_file) self.assertTrue(translation_unit) diags = diagnostics.get_diagnostics_by_severity(translation_unit) self.assertEqual(diags[source_file]["3"], 2)
def test_fix_violation_per_line(self): filename = test_support.create_tempfile(["std::uint8_t var = 1u;"]) violation = Violation("TIDY_DUMMY", "", filename, 1, 0) fixit.fix_violation_per_line(violation, sample_fix_function.fix_function) self.assert_fix(violation, "Deleted Line")
def test_fix_violation_per_line_for_missing_const(self): filename = test_support.create_tempfile(["int var = 0;"]) violation = Violation("TIDY_SAMO_MISSING_CONST", "", filename, 1, 5) fixit.fix_violation_per_line(violation, samo_missing_const_checker.fix_rule) self.assert_fix(violation, "int const var = 0;")
def test_negativ(self): violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, test_support.create_tempfile(["namespace a {} namespace b {}"]), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 0)
def test_check_for_multiple_classes_negativ(self): violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, test_support.create_tempfile(["class A", "{", "};"]), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 0)
def test_get_diagnostics_by_severity_one_tu(self): source_file = test_support.create_tempfile(["int a;"]) translation_unit = tu_parser.create_translation_unit(source_file) self.assertTrue(translation_unit) diags = diagnostics.get_diagnostics_by_severity_one_tu( translation_unit) self.assertEqual(diags["2"], 1)
def test_temp_file_int(self): violations, diagnostics = self.apply_checker( the_checker.token_based_rule, test_support.create_tempfile( ["int F();", "int F()", "{", "return 0;", "}"]), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 0)
def test_check_for_missing_const_simple_var_used(self): violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, test_support.create_tempfile( ["int main()", "{", "int a = 0;", "a = 1;", "return a;", "}"]), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 0)
def test_fixit_template_for_suffix_invalid(self): filename = test_support.create_tempfile(["std::uint8_t var = 1u;"]) violation = Violation("TIDY_SAMO_INVALID_ID", "", filename, 1, 20) fixed_lines = fixit.fix_violation_per_line( violation, samo_suffix_case_checker.fix_rule) self.assertFalse(fixed_lines) self.assert_fix(violation, "std::uint8_t var = 1u;")
def test_unused_variable(self): tu = tu_parser.create_translation_unit( test_support.create_tempfile( ["int main()", "{", "int a;", "return 0;", "}"])) violations = clang_warning_checker.check_for_clang_warnings(tu) self.assertEqual(len(violations), 1) self.assertEqual(violations[0].id, "TIDY_CLANG_UNUSED_VARIABLE") self.assertEqual(violations[0].message, "unused variable 'a'")
def test_fixit_template_for_suffix(self): filename = test_support.create_tempfile(["std::uint8_t var = 1u;"]) violation = Violation("TIDY_SAMO_SUFFIX_CASE", "", filename, 1, 20) fixed_lines = fixit.fix_violation_per_line( violation, samo_suffix_case_checker.fix_rule) self.assertTrue(fixed_lines) self.assert_fix(violation, "std::uint8_t var = 1U;")
def test_check_for_multiple_classes_only_usage(self): violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, test_support.create_tempfile( ["class A", "{", "public:", "int b;", "};", "int main()", "{", "A a;", "return a.b;", "}"] ), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 0)
def test_ignored_file_name_positive(self): violations, diagnostics = self.apply_checker( samo_suffix_case_checker.token_based_rule, test_support.create_tempfile( get_simple_non_conforming_suffix_source(), "/tmp/usr/ignore_me.cpp", ), ) self.assertEqual(len(violations), 0) self.assertEqual(len(diagnostics), 0)
def test_temp_file_uint_conversion_fine(self): violations, diagnostics = self.apply_checker( the_checker.token_based_rule, test_support.create_tempfile([ "#include <cstdint>", "int main()", "{", "std::uint8_t b = 123;", "return b;" "}" ]), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 0)
def test_check_for_multiple_classes_positiv(self): violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, test_support.create_tempfile(["class A", "{", "};", "class B", "{", "};"]), ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 2) self.assertEqual(violations[0].id, "TIDY_SAMO_MULTIPLE_CLASSES") self.assertIn("Multiple of 2 classes", violations[0].message) self.assertIn("'class A'", violations[0].message) self.assertIn("'class B'", violations[1].message)
def test_three_levels(self): filename = test_support.create_tempfile( ["namespace a { namespace b { namespace c { } } }"]) violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, filename, ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 2) self.assertIn("2 nested", violations[0].message) self.assertIn("3 nested", violations[1].message)
def test_positiv(self): filename = test_support.create_tempfile( ["namespace a {", "namespace b {", "}", "}"]) violations, diagnostics = self.apply_checker( the_checker.translation_unit_based_rule, filename, ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 1) self.assertEqual(violations[0].id, "TIDY_SAMO_NESTED_NAMESPACE") self.assertEqual(violations[0].line, 2) self.assertEqual(violations[0].column, 11)
def test_temp_file_uint_conversion(self): violations, diagnostics = self.apply_checker( the_checker.token_based_rule, test_support.create_tempfile([ "#include <cstdint>", "int main()", "{", "int a = 12;", "std::uint8_t b = a;", "}" ]), ) self.assertEqual(len(diagnostics), 2) self.assertEqual("-Wimplicit-int-conversion", diagnostics[0].option) self.assertEqual("-Wunused-variable", diagnostics[1].option) self.assertEqual(len(violations), 0)
def test_temp_file_uint(self): file_name = test_support.create_tempfile([ "#include <cstdint>", "std::uint8_t F();", "std::uint8_t F()", "{", "return 0u;", "}" ]) violations, diagnostics = self.apply_checker( the_checker.token_based_rule, file_name, ) self.assertEqual(len(diagnostics), 0) self.assertEqual(len(violations), 1) self.assertIn(file_name, violations[0].file_path) self.assertEqual("TIDY_SAMO_SUFFIX_CASE", violations[0].id)
def test_fixit_template_for_suffix_multiple(self): filename = test_support.create_tempfile([ "std::uint8_t my_first_var = 1u;", "std::uint32_t my_second_var = 123u;" ]) violation1 = Violation("TIDY_SAMO_SUFFIX_CASE", "", filename, 1, 29) violation2 = Violation("TIDY_SAMO_SUFFIX_CASE", "", filename, 2, 33) violations = [violation1, violation2] fixit.fix_violations_per_line(violations, samo_suffix_case_checker.fix_rule) self.assert_fix(violation1, "std::uint8_t my_first_var = 1U;") self.assert_fix(violation2, "std::uint32_t my_second_var = 123U;")
def test_impl_conversion(self): tu = tu_parser.create_translation_unit( test_support.create_tempfile([ "#include <cstdint>", "std::uint8_t F();", "uint8_t F()", "{", "int a=1;", "return a;", "}" ])) violations = clang_warning_checker.check_for_clang_warnings(tu) self.assertEqual(len(violations), 1) self.assertEqual(violations[0].id, "TIDY_CLANG_IMPLICIT_INT_CONVERSION") self.assertEqual( violations[0].message, "implicit conversion loses integer precision: 'int' to 'uint8_t' (aka 'unsigned char')", )
def test_non_location_warning(self): tu = tu_parser.create_translation_unit(test_support.create_tempfile( [""]), args=["clang++"]) violations = clang_warning_checker.check_for_clang_warnings(tu) self.assertEqual(len(violations), 0)