Skip to content

Refactored testParseNormal #4805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,62 @@
*/
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 testParseNormal() throws Exception {
String[] argv = new String[] {"-a", "foo", "-bc", "--", "-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());
}

@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()]);
}
Expand Down