From 0b86cf02fbc438f27166c6b1df6e7792c07b5bc6 Mon Sep 17 00:00:00 2001 From: VOsadchii <909404@gmail.com> Date: Sat, 27 Sep 2025 16:47:09 +0300 Subject: [PATCH] add tests for getFormat method --- app/src/main/java/hexlet/code/Differ.java | 4 +++- app/src/test/java/hexlet/code/DifferTest.java | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/hexlet/code/Differ.java b/app/src/main/java/hexlet/code/Differ.java index 44859f2..9bfd5d8 100644 --- a/app/src/main/java/hexlet/code/Differ.java +++ b/app/src/main/java/hexlet/code/Differ.java @@ -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) { @@ -101,4 +101,6 @@ private static String getFormat(String filePath) { throw new IllegalArgumentException("Cannot determine format: " + filePath); } + + } diff --git a/app/src/test/java/hexlet/code/DifferTest.java b/app/src/test/java/hexlet/code/DifferTest.java index 019941c..98d4c20 100644 --- a/app/src/test/java/hexlet/code/DifferTest.java +++ b/app/src/test/java/hexlet/code/DifferTest.java @@ -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();