Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/src/main/java/hexlet/code/Differ.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static String generate(String filepath1, String filepath2, String format)
return (diff);
}

private static String getFormat(String filePath) {
static String getFormat(String filePath) {
String fileName = Paths.get(filePath).getFileName().toString();
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
Expand All @@ -101,4 +101,6 @@ private static String getFormat(String filePath) {
throw new IllegalArgumentException("Cannot determine format: " + filePath);
}



}
15 changes: 15 additions & 0 deletions app/src/test/java/hexlet/code/DifferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ public void testInvalidJson() throws IOException {
);
}

@Test
void testGetFormatMultipleDots() {
String filePath = "config.dev.json";
String expected = "json";
String actual = Differ.getFormat(filePath);
assertEquals(expected, actual, "Format should be extracted from last extension");
}

@Test
void testGetFormatNoExtension() {
String filePath = "config";
assertThrows(IllegalArgumentException.class, () -> {
Differ.getFormat(filePath);
}, "Should throw IllegalArgumentException for file without extension");
}

private String normalizeSpaces(String input) {
return input.replaceAll("\\s+", " ").trim();
Expand Down