publicstaticvoid main(String[] args) throws Exception { // Replace with same line
test("a", 1, 1, null, "a", "a\n"); // Replace with different line
test("a", 1, 1, null, "z", "z\n"); // Replace with same line based on line match
test("a", 1, 1, "a", "a", "a\n"); // Replace with different line based on line match
test("a", 1, 1, "a", "z", "z\n"); // Replace single line with multiple lines
test("a", 1, 1, null, "x\ny\nz", "x\ny\nz\n"); // Replace single line with multiple lines based on lines match
test("a", 1, 1, "a", "x\ny\nz", "x\ny\nz\n"); // Replace all lines
test("a\nb\nc", 1, 3, null, "x\ny\nz", "x\ny\nz\n"); // Replace all lines based on lines match
test("a\nb\nc", 1, 3, "a\nb\nc", "x\ny\nz", "x\ny\nz\n"); // Replace all lines with single line based on lines match
test("a\nb\nc", 1, 3, "a\nb\nc", "z", "z\n"); // Replace single line
test("a\nb\nc", 1, 1, null, "z", "z\nb\nc\n"); // Replace single line based on line match
test("a\nb\nc", 1, 1, "a", "z", "z\nb\nc\n"); // Replace multiple lines
test("a\nb\nc", 1, 2, null, "z", "z\nc\n"); // Replace multiple lines based on line match
test("a\nb\nc", 1, 2, "a\nb", "z", "z\nc\n"); // Replace multiple lines based on line match
test("a\nb\nc", 1, 2, "a\nb", "x\ny\nz", "x\ny\nz\nc\n");
// Test with space characters // Replace with space line
test("\n", 1, 1, null, " ", " \n"); // Replace empty line with another line based on line match
test("\n", 1, 1, " ", "a", "a\n"); // Replace with space line
test(" \na\nb\nc", 1, 1, null, "a", "a\na\nb\nc\n"); // Replace empty line with different line based on line match
test(" \na\nb\nc", 1, 1, " ", "a", "a\na\nb\nc\n"); // Replace range of lines with space to different lines based on line match
test(" \na\nb\nc", 1, 2, " \na", "x\ny\nz", "x\ny\nz\nb\nc\n");
test("a\nb\nc\n", 1, 2, "a\nb", "1\n2", "1\n2\nc\n");
test("a\nb\nc\n", 1, 2, " a\nb", "1\n2", "1\n2\nc\n"); // free to add spaces around a line in from
test("a\nb\nc\n", 1, 2, "a \nb", "1\n2", "1\n2\nc\n");
test("a\nb\nc\n", 1, 2, "a\n b ", "1\n2", "1\n2\nc\n");
test("a\nb\nc\n", 1, 2, "\na\nb", "1\n2", "1\n2\nc\n"); // free to add empty lines at both ends of from
test("a\nb\nc\n", 1, 2, "a\nb\n", "1\n2", "1\n2\nc\n");
test("a\nb\nc\n", 1, 2, "\na\nb\n", "1\n2", "1\n2\nc\n");
test("a\nb\nc\n", 1, 2, "\n\na\nb\n\n", "1\n2", "1\n2\nc\n");
test("a\nb\nc\n", 1, 2, "a\nb", "1\n2\n", "1\n2\nc\n"); // ok to add a new line at end of to
test("a\nb\nc\n", 1, 2, "a\nb", "1\n2\n\n", "1\n2\n\nc\n"); // more will change result
test("a\nb\nc\n", 1, 2, "a\nb", "\n1\n2", "\n1\n2\nc\n");
test("a\nb\nc\n", 1, 2, "a\n\nb", "1\n2\n", null); // no extra new line inside
test("a\nb\nc\n", 1, 2, "ab", "1\n2\n", null); // new line inside must preserve
test("a\nb\nc\n", 1, 2, "a", "1\n2\n", null); // must be all
test("a\nb\nc\n", 1, 2, "b", "1\n2\n", null); // must be all
test("a\nb\nc\n", 1, 2, "a\nb", "", "c\n"); // just remove
test("a\nb\nc\n", 1, 0, "", "1\n2", "1\n2\na\nb\nc\n"); // just add
// Mismatched range with "replace" lines Tests // Replace all lines with mismatched line
test("a", 1, 1, "z", "z", null); // Replace all lines with mismatched lines
test("a\nb\nc", 1, 3, "x\ny\nz", "x\ny\nz", null); // Replace single line with mismatched line
test("a\nb\nc", 1, 1, "z", "z", null); // Replace a range of lines with mismatched lines
test("a\nb\nc", 1, 3, "ab", "x\ny\nz", null);
}
privatestaticvoid test(String content, int from, int to, String replace,
String replaceTo, String expected) throws IOException {
String name = "Test-" + new Exception().getStackTrace()[1].getLineNumber();
Path path = Files.writeString(Paths.get(name), content);
String output = null; try {
FileUtils.patch(path, from, to, replace, replaceTo);
output = Files.readString(path);
} catch (IOException e) { // output is null
}
Asserts.assertEQ(output, (expected != null) ? expected.replaceAll("\n", System.lineSeparator()) : null);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.