def test_count_multiple_lines_from_contents_with_threshold(): """Check that counting lines in contents works correctly.""" hello_contents = ( '/* hello world */\nString s = new String("hello");\n//this is a comment' ) ( ( exceeds_threshold, actual_count, ), _, ) = fragments.specified_source_greater_than_count(2, "", "", hello_contents) assert actual_count == 3 assert exceeds_threshold[0] is True ( ( exceeds_threshold, actual_count, ), _, ) = fragments.specified_source_greater_than_count(3, "", "", hello_contents) assert actual_count == 3 assert exceeds_threshold[0] is True ( ( exceeds_threshold, actual_count, ), _, ) = fragments.specified_source_greater_than_count(100, "", "", hello_contents) assert actual_count == 3 assert exceeds_threshold[0] is False
def test_count_single_line_from_file_with_threshold(tmpdir): """Check that counting lines in a file with threshold works correctly.""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.java") hello_file.write("/* hello world */") assert hello_file.read() == "/* hello world */" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "Hello.java" ( ( exceeds_threshold, actual_count, ), _, ) = fragments.specified_source_greater_than_count(1, hello_file, directory, "") assert actual_count == 1 assert exceeds_threshold[0] is True ( ( exceeds_threshold, actual_count, ), _, ) = fragments.specified_source_greater_than_count(100, hello_file, directory, "") assert actual_count == 1 assert exceeds_threshold[0] is False
def test_count_multiple_lines_from_file_with_threshold(tmpdir): """Check that counting lines in a file works correctly.""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.java") hello_file.write( '/* hello world */\nString s = new String("hello");\n//this is a comment' ) assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "Hello.java" ( exceeds_threshold, actual_count, ), _ = fragments.specified_source_greater_than_count( 2, hello_file, directory, "") assert actual_count == 3 assert exceeds_threshold[0] is True ( exceeds_threshold, actual_count, ), _ = fragments.specified_source_greater_than_count( 3, hello_file, directory, "") assert actual_count == 3 assert exceeds_threshold[0] is True ( exceeds_threshold, actual_count, ), _ = fragments.specified_source_greater_than_count( 100, hello_file, directory, "") assert actual_count == 3 assert exceeds_threshold[0] is False
def invoke_all_count_checks(expected_count, filecheck=NOTHING, directory=NOTHING, contents=NOTHING, exact=False): """Perform the check for the count of lines in file or contents and return the results""" met_or_exceeded_count = 0 met_or_exceeded_count, actual_count = fragments.specified_source_greater_than_count( expected_count, filecheck, directory, contents, exact) # create a message for a file in directory if filecheck is not NOTHING and directory is not NOTHING: if exact is not True: message = ("The " + filecheck + " in " + directory + " has at least " + str(expected_count) + " line(s)") else: message = ("The " + filecheck + " in " + directory + " has exactly " + str(expected_count) + " line(s)") # create a message for a string (normally from program execution) else: if exact is not True: message = ("The command output" + " has at least " + str(expected_count) + " lines") else: message = ("The command output" + " has exactly " + str(expected_count) + " lines") diagnostic = ("Found " + str(actual_count) + " line(s) in the output or the specified file") update_report(met_or_exceeded_count, message, diagnostic) return met_or_exceeded_count
def invoke_all_count_checks( expected_count, filecheck=constants.markers.Nothing, directory=constants.markers.Nothing, contents=constants.markers.Nothing, exact=False, ): """Perform the check for the count of lines in file or contents and return the results.""" met_or_exceeded_count = 0 ( ( met_or_exceeded_count, actual_count, ), actual_count_dictionary, ) = fragments.specified_source_greater_than_count(expected_count, filecheck, directory, contents, exact) # create a message for a file in directory if (filecheck is not constants.markers.Nothing and directory is not constants.markers.Nothing): if exact is not True: message = ("The " + filecheck + " in " + directory + " has at least " + str(expected_count) + " line(s)") else: message = ("The " + filecheck + " in " + directory + " has exactly " + str(expected_count) + " line(s)") # create a message for a string (normally from program execution) else: if exact is not True: message = ("The command output" + " has at least " + str(expected_count) + " lines") else: message = ("The command output" + " has exactly " + str(expected_count) + " lines") # Produce the diagnostic and report the result. # If a wildcard (i.e., "*.py") was given for the filename, then # this diagnostic is customized for the file that first breaks the check. fragment_diagnostic = util.get_file_diagnostic(actual_count_dictionary) # when the file is "unknown" then this means that the content is from a command # and thus it is better to use the generic phrase "file" instead of this default fragment_diagnostic = fragment_diagnostic.replace( constants.markers.Unknown_File, constants.markers.File) diagnostic = ("Found " + str(actual_count) + constants.markers.Space + "line(s)" + constants.markers.Space + fragment_diagnostic + constants.markers.Space + "or the output") # extract the result as to whether or not the check passed extracted_result = met_or_exceeded_count[0] # use the created diagnostic to report the result report_result(extracted_result, message, diagnostic) return extracted_result
def test_count_no_line_from_incorrect_file(tmpdir): """Ensure that counting lines in a garbage file works correctly.""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.java") hello_file.write("/* hello world */") assert hello_file.read() == "/* hello world */" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "#$#@ll*.java&&@@@" ( exceeds_threshold, actual_count, ), _ = fragments.specified_source_greater_than_count( 1, hello_file, directory, "") assert actual_count == 0 assert exceeds_threshold[0] is False