def test_real_world(self): f_file = CodeFile( join(dirname(__file__), "../../test/check/implicit_none_real.f90")) c = CheckImplicitNone(f_file) c.check() self.assertListEqual([43, 51], sorted(c._occurences))
def test_edgecase(self): f_file = CodeFile( join(dirname(__file__), "../../test/check/implicit_none_edge.f90")) c = CheckImplicitNone(f_file) c.check() self.assertListEqual([1, 4, 7], sorted(c._occurences))
def test_read_in(self): f_file = CodeFile( join(dirname(__file__), "../test/simplest_fortran_file.f90")) original_content = [ "PROGRAM test_program\n", " integer :: zahl ! Wir definieren eine Zahl\n", "END PROGRAM test_program\n", ] insensitive_content = [ "program test_program\n", " integer :: zahl ! wir definieren eine zahl\n", "end program test_program\n", ] self.assertListEqual(original_content, f_file.original_lines()) self.assertListEqual(insensitive_content, f_file.insensitive_lines())
def test_real_formatting(self): f_file = CodeFile( join(dirname(__file__), "../../test/format/format_real.f90")) f = FormatAlignColon(f_file) expected = [ " subroutine locate(l,n,array,var,pos)\n", " !> Search the index of given value in sorted 1D array of unique values.\n", " !>\n", " !> It uses a binary search algorithm.\n", " implicit none\n", " integer(int_p), intent(in) :: l !> Initial guess of lower limit.\n", " integer(int_p), intent(in) :: n !> Size of array.\n", " real(real_p), dimension(n), intent(in) :: array !> Sorted array to search.\n", " real(real_p), intent(in) :: var !> Value to find.\n", " integer(int_p), intent(out) :: pos !> Resulting index.\n", "\n", " ! interrupting comment\n", "\n", " integer(int_p) :: jl !> Lower limit.\n", " integer(int_p) :: jm !> Midpoint\n", " integer(int_p) :: ju !> Upper limit\n", " ! Initialize lower limit (L=0 FOR A NORMAL VECTOR!).\n", " jl=l\n", " ! Initialize upper limit.\n", " ju=jl+n+1\n", " ! If we are not yet done compute a midpoint.\n", " pos=max(l+1,pos)\n", " return\n", " end subroutine locate\n", "\n", "\n", " pure function equal(arg1,arg2) result(res)\n", " !> Returns whether the arguments are equal to machine precision\n", " real(real_p), intent(in) :: arg1, arg2\n", " logical :: res\n", " res = (abs(arg1-arg2) < REALTOL)\n", " end function equal\n", "\n", " subroutine print_assertion_error(filename, line, error_msg)\n", " implicit none\n", "\n", " character(len=*), intent(in) :: filename\n", " integer(int_p), intent(in) :: line\n", " character(len=*), intent(in) :: error_msg\n", " character(len=10) :: line_string\n", "\n", " write (line_string, '(I2)') line\n", " write (*,*) trim(filename) // \": \" // trim(line_string) // \": \" // trim(error_msg)\n", " end subroutine print_assertion_error\n" ] f.format() new_file = f.formatted_lines() self.assertListEqual(expected, new_file)
def handle_formatting(args): """ Implement all formatting related functions :args: Command line arguments passed to the command. """ all_formatters = { "align-double-colon": FormatAlignColon, "align-trailing-comment": FormatAlignTrailingComment, } # If listing is wanted, only that will be done. if args.list_formatters: for name, class_ref in all_formatters.items(): print("{}: {}".format(name, class_ref.help())) return # Reaching this point means, static analysis shall be done. # There is a default list of checks. assert args.formatters, "Logic error!" # Separate the list of checks and ensure they do exist. enabled_formatters = args.formatters.split(",") for formatter in enabled_formatters: if formatter not in all_formatters: print( "Configured formatter '{}' does not exist!".format(formatter), file=sys.stderr) sys.exit(1) if not args.files: print("Provide at least one file for analysis!", file=sys.stderr) sys.exit(1) for file in args.files: f_file = CodeFile(file) for formatter in enabled_formatters: format_instance = all_formatters[formatter](f_file) formatted_lines = format_instance.format() f_file.update_lines(formatted_lines) f_file.write()
def handle_analysis(args): """ Implement all static analysis related functions :args: Command line arguments passed to the command. """ all_checks = { "implicit-none": CheckImplicitNone, "format-label": CheckFormatLabel, } # If listing is wanted, only that will be done. if args.list_checks: for name, class_ref in all_checks.items(): print("{}: {}".format(name, class_ref.help())) return # Reaching this point means, static analysis shall be done. # There is a default list of checks. assert args.checks, "Logic error!" # Separate the list of checks and ensure they do exist. enabled_checks = args.checks.split(",") for check in enabled_checks: if check not in all_checks: print("Configured check '{}' does not exist!".format(check), file=sys.stderr) sys.exit(1) if not args.files: print("Provide at least one file for analysis!", file=sys.stderr) sys.exit(1) for file in args.files: f_file = CodeFile(file) for check in enabled_checks: check_instance = all_checks[check](f_file) check_instance.check() check_instance.report()
def setUp(self): self.f_file = CodeFile( join(dirname(__file__), "../test/simplest_fortran_file.f90"))
def test_file_path(self): f_file = CodeFile("mod_functions.f90") self.assertTrue(f_file.path().startswith('/'))