From 750675b2a96e99eb71a809bf137051763845fa59 Mon Sep 17 00:00:00 2001 From: pedro_Simoes Date: Mon, 30 Jun 2025 23:20:19 -0300 Subject: [PATCH 1/2] Refactored testParseNormal Improves the readability of the test. Splitting assertions into separate tests improves clarity and ensures each test verifies a single scenario or state. This enhances readability, maintainability, and debugging efficiency. --- .../org/opengrok/indexer/util/GetoptTest.java | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java index 250269478d3..b31fa794338 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java @@ -33,23 +33,54 @@ class GetoptTest { - @Test - void testParseNormal() throws Exception { - String[] argv = new String[] {"-a", "foo", "-bc", "--", "-f"}; - Getopt instance = new Getopt(argv, "a:bcr:f"); - + @Test + void testParseOptionA() throws Exception { + String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; + Getopt instance = new Getopt(argv, ""a:bcr:f""); instance.parse(); assertEquals('a', (char) instance.getOpt()); - assertEquals("foo", instance.getOptarg()); + assertEquals(""foo"", instance.getOptarg()); + } + + @Test + void testParseOptionB() throws Exception { + String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; + Getopt instance = new Getopt(argv, ""a:bcr:f""); + instance.parse(); + assertEquals('b', (char) instance.getOpt()); assertNull(instance.getOptarg()); + } + + @Test + void testParseOptionC() throws Exception { + String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; + Getopt instance = new Getopt(argv, ""a:bcr:f""); + instance.parse(); + assertEquals('c', (char) instance.getOpt()); assertNull(instance.getOptarg()); + } + + @Test + void testParseEndOfOptions() throws Exception { + String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; + Getopt instance = new Getopt(argv, ""a:bcr:f""); + instance.parse(); + assertEquals(-1, instance.getOpt()); assertEquals(4, instance.getOptind()); + } + + @Test + void testGetNextArgument() throws Exception { + String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; + Getopt instance = new Getopt(argv, ""a:bcr:f""); + instance.parse(); + assertTrue(instance.getOptind() < argv.length); - assertEquals("-f", argv[instance.getOptind()]); + assertEquals(""-f"", argv[instance.getOptind()]); } @Test From 92ac3a8ffd0afe09ede976a3aaad64e9f5cd9092 Mon Sep 17 00:00:00 2001 From: pedro_Simoes Date: Mon, 30 Jun 2025 23:22:46 -0300 Subject: [PATCH 2/2] fix: commas --- .../org/opengrok/indexer/util/GetoptTest.java | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java b/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java index b31fa794338..159eeedcc70 100644 --- a/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java +++ b/opengrok-indexer/src/test/java/org/opengrok/indexer/util/GetoptTest.java @@ -22,31 +22,30 @@ */ package org.opengrok.indexer.util; -import org.junit.jupiter.api.Test; - import java.text.ParseException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; class GetoptTest { @Test void testParseOptionA() throws Exception { - String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; - Getopt instance = new Getopt(argv, ""a:bcr:f""); + String[] argv = new String[]{"-a", "foo", "-bc", "--", "-f"}; + Getopt instance = new Getopt(argv, "a:bcr:f"); instance.parse(); assertEquals('a', (char) instance.getOpt()); - assertEquals(""foo"", instance.getOptarg()); + assertEquals("foo", instance.getOptarg()); } @Test void testParseOptionB() throws Exception { - String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; - Getopt instance = new Getopt(argv, ""a:bcr:f""); + String[] argv = new String[]{"-a", "foo", "-bc", "--", "-f"}; + Getopt instance = new Getopt(argv, "a:bcr:f"); instance.parse(); assertEquals('b', (char) instance.getOpt()); @@ -55,8 +54,8 @@ void testParseOptionB() throws Exception { @Test void testParseOptionC() throws Exception { - String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; - Getopt instance = new Getopt(argv, ""a:bcr:f""); + String[] argv = new String[]{"-a", "foo", "-bc", "--", "-f"}; + Getopt instance = new Getopt(argv, "a:bcr:f"); instance.parse(); assertEquals('c', (char) instance.getOpt()); @@ -65,8 +64,8 @@ void testParseOptionC() throws Exception { @Test void testParseEndOfOptions() throws Exception { - String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; - Getopt instance = new Getopt(argv, ""a:bcr:f""); + String[] argv = new String[]{"-a", "foo", "-bc", "--", "-f"}; + Getopt instance = new Getopt(argv, "a:bcr:f"); instance.parse(); assertEquals(-1, instance.getOpt()); @@ -75,12 +74,12 @@ void testParseEndOfOptions() throws Exception { @Test void testGetNextArgument() throws Exception { - String[] argv = new String[]{""-a"", ""foo"", ""-bc"", ""--"", ""-f""}; - Getopt instance = new Getopt(argv, ""a:bcr:f""); + String[] argv = new String[]{"-a", "foo", "-bc", "--", "-f"}; + Getopt instance = new Getopt(argv, "a:bcr:f"); instance.parse(); assertTrue(instance.getOptind() < argv.length); - assertEquals(""-f"", argv[instance.getOptind()]); + assertEquals("-f", argv[instance.getOptind()]); } @Test