Skip to content

Commit 9eb177b

Browse files
committed
Added tests for option and flag parser
1 parent f714918 commit 9eb177b

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Command Framework - Annotation based command framework
3+
* Copyright (C) 2025 Berke Akçen
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package me.despical.commandframework.test;
20+
21+
import me.despical.commandframework.annotations.Flag;
22+
import me.despical.commandframework.parser.OptionParser;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.lang.reflect.Method;
26+
import java.util.Set;
27+
28+
import static org.junit.jupiter.api.Assertions.*;
29+
30+
/**
31+
* @author Despical
32+
* <p>
33+
* Created at 12.10.2025
34+
*/
35+
class FlagParserTest {
36+
37+
private Set<String> parseFlagsForMethod(String methodName, String[] args) throws NoSuchMethodException {
38+
Method method = FlagParserTestMethods.class.getMethod(methodName);
39+
OptionParser parser = new OptionParser(args, method);
40+
return parser.parseFlags();
41+
}
42+
43+
/**
44+
* Flag(value = "verbose", prefix = "--")
45+
*/
46+
@Test
47+
void test_flags_with_default_prefix() throws NoSuchMethodException {
48+
String[] args = {"--", "--verbose", "-nogui", "is-that-even-a-flag"};
49+
Set<String> parsedFlags = parseFlagsForMethod("flagMethodWithDefaultPrefix", args);
50+
51+
assertEquals(1, parsedFlags.size());
52+
assertTrue(parsedFlags.contains("verbose"));
53+
}
54+
55+
/**
56+
* Flag(value = {"verbose", "nogui"}, prefix = ".")
57+
*/
58+
@Test
59+
void test_flags_with_custom_prefix() throws NoSuchMethodException {
60+
String[] args = {".", ".verbose", ".nogui", ".noflag"};
61+
Set<String> parsedFlags = parseFlagsForMethod("flagMethodWithCustomPrefix", args);
62+
63+
assertEquals(2, parsedFlags.size());
64+
assertFalse(parsedFlags.contains("noflag"));
65+
assertTrue(parsedFlags.contains("verbose"));
66+
assertTrue(parsedFlags.contains("nogui"));
67+
}
68+
69+
public static class FlagParserTestMethods {
70+
71+
@Flag(value = "verbose")
72+
public void flagMethodWithDefaultPrefix() {}
73+
74+
@Flag(value = {"verbose", "nogui"}, prefix = ".")
75+
public void flagMethodWithCustomPrefix() {}
76+
}
77+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Command Framework - Annotation based command framework
3+
* Copyright (C) 2025 Berke Akçen
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package me.despical.commandframework.test;
20+
21+
import me.despical.commandframework.annotations.Option;
22+
import me.despical.commandframework.parser.OptionParser;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.lang.reflect.Method;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
/**
33+
* @author Despical
34+
* <p>
35+
* Created at 12.10.2025
36+
*/
37+
class OptionParserTest {
38+
39+
private Map<String, List<String>> parseOptions(String methodName, String[] args) throws NoSuchMethodException {
40+
Method method = OptionParserTestMethods.class.getMethod(methodName);
41+
OptionParser parser = new OptionParser(args, method);
42+
return parser.parseOptions();
43+
}
44+
45+
@Test
46+
void test_defaultOption_multipleValues() throws NoSuchMethodException {
47+
String[] args = {"--players=berke,despical"};
48+
Map<String, List<String>> parsedOptions = parseOptions("defaultOption", args);
49+
50+
assertTrue(parsedOptions.containsKey("players"));
51+
List<String> values = parsedOptions.get("players");
52+
assertEquals(2, values.size());
53+
assertTrue(values.contains("berke"));
54+
assertTrue(values.contains("despical"));
55+
}
56+
57+
@Test
58+
void test_optionNoSeparating_valuesAsSingleString() throws NoSuchMethodException {
59+
String[] args = {"--players=berke,despical"};
60+
Map<String, List<String>> parsedOptions = parseOptions("optionNoSeparating", args);
61+
62+
assertTrue(parsedOptions.containsKey("players"));
63+
List<String> values = parsedOptions.get("players");
64+
assertEquals(1, values.size());
65+
assertEquals("berke,despical", values.getFirst());
66+
}
67+
68+
@Test
69+
void test_customOption_withCustomPrefixAndKeySeparator() throws NoSuchMethodException {
70+
String[] args = {".teams:alpha,beta"};
71+
Map<String, List<String>> parsedOptions = parseOptions("customOption", args);
72+
73+
assertTrue(parsedOptions.containsKey("teams"));
74+
List<String> values = parsedOptions.get("teams");
75+
assertEquals(2, values.size());
76+
assertTrue(values.contains("alpha"));
77+
assertTrue(values.contains("beta"));
78+
}
79+
80+
public static class OptionParserTestMethods {
81+
82+
@Option("players")
83+
public void defaultOption() {}
84+
85+
@Option(value = "players", allowSeparating = false)
86+
public void optionNoSeparating() {}
87+
88+
@Option(value = "teams", prefix = ".", keySeparator = ":")
89+
public void customOption() {}
90+
}
91+
}

0 commit comments

Comments
 (0)