8000 Add a MapArgument to the CommandAPI by JorelAli · Pull Request #418 · CommandAPI/CommandAPI · GitHub
[go: up one dir, main page]

Skip to content

Add a MapArgument to the CommandAPI #418

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

Merged
merged 48 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
fe6f87b
Initial commit of the MapArgument
DerEchtePilz Jan 29, 2023
f12f055
Remove continue statements
DerEchtePilz Jan 29, 2023
b06ed20
Revert "Remove continue statements"
DerEchtePilz Jan 29, 2023
3274f0c
Limit the type of key values
DerEchtePilz Jan 30, 2023
43c25f6
Adds tests for float and integer keys
DerEchtePilz Jan 30, 2023
eefe64b
Fix JavaDocs of the MapArgument
DerEchtePilz Jan 30, 2023
a2a7658
Improve JavaDocs of the MapArgument
DerEchtePilz Jan 30, 2023
d10e29a
Prepare to implement suggestions
DerEchtePilz Jan 30, 2023
18adf8e
First commit of implementing suggestions (doesn't work correctly)
DerEchtePilz Feb 2, 2023
3e5023e
Fix suggestions (the suggestions, depite being correct, show up at th…
DerEchtePilz Feb 2, 2023
bbe69d9
Finish suggestions for the MapArgument
DerEchtePilz Feb 3, 2023
674d96a
Remove test command from CommandAPIMain.java
DerEchtePilz Feb 3, 2023
2fbe304
Fix MapArgument tests
DerEchtePilz Feb 3, 2023
d7b9bd5
Removes MapArgumentKeyType
DerEchtePilz Feb 3, 2023
12c9fb0
Finally make suggestions work correctly
DerEchtePilz Feb 3, 2023
483690c
Make the MapArgument return a LinkedHashMap
DerEchtePilz Feb 3, 2023
b6dedf7
Enable commandapi-bukkit-kotlin-test again
DerEchtePilz Feb 3, 2023
8cc72e0
Fix values actually being removed from the list of suggestions if all…
DerEchtePilz Feb 3, 2023
9e5869b
Refactor MapArgument#parseArgument and MapArgument#getSuggestionCode
DerEchtePilz Feb 3, 2023
574096c
More SonarCloud stuff
DerEchtePilz Feb 3, 2023
5e7fb47
Revert the latest refactoring commits and implement an enum to determ…
DerEchtePilz Feb 3, 2023
25a3c73
Throws a CommandSyntaxException if keys or values were provided which…
DerEchtePilz Feb 4, 2023
005b210
Implement tests for invalid keys and values
DerEchtePilz Feb 4, 2023
5134349
Adds tests while using withoutKeyList()/withoutValueList()
DerEchtePilz Feb 4, 2023
249d1ea
Create documentation page. Rename withKeyType() to withKeyMapper()
DerEchtePilz Feb 4, 2023
6a2f2dc
Adds documentation for the MapArgument
DerEchtePilz Feb 4, 2023
5871989
Adds a check to validate keys when building the MapArgument
DerEchtePilz Feb 4, 2023
0b826b7
Fix check that validates keys when building the MapArgument
DerEchtePilz Feb 4, 2023
aa1afed
Throw a CommandSyntaxException if duplicate values have been entered …
DerEchtePilz Feb 4, 2023
b58341b
Removes AMapArgument.java
DerEchtePilz Feb 4, 2023
081eb0b
Adds MapArgument entry to arguments.md
DerEchtePilz Feb 4, 2023
8d8dba7
Improve removing suggestions when duplicates are not allowed
DerEchtePilz Feb 5, 2023
3611281
Removes an unused field
DerEchtePilz Feb 5, 2023
01279b0
Fix documentation for the MapArgument
DerEchtePilz Feb 5, 2023
d490f2b
Fix some formatting in argument_map.md
DerEchtePilz Feb 6, 2023
037a21a
Fix MapArgument allowing incomplete maps
DerEchtePilz Feb 8, 2023
1ca3653
Does this fix a 'bug' SonarCloud is complaining about?
DerEchtePilz Feb 12, 2023
0c2205e
Use the same StringBuilder for both keyBuilder and valueBuilder
JorelAli Feb 12, 2023
1caabb6
Merge branch 'dev/dev' into dev/map-argument
DerEchtePilz Feb 17, 2023
129b2bc
Fix MapArgument#parseArgument to use the CommandArguments class
DerEchtePilz Feb 17, 2023
4f0dd4a
Adding some more tests
DerEchtePilz Feb 18, 2023
91aab9f
Implement suggestion tests for the MapArgument
DerEchtePilz Feb 24, 2023
4f75e95
Adds suggestion tests without values being removed
DerEchtePilz Feb 24, 2023
0ebb148
Catch exception that is thrown when the mapping function fails and th…
DerEchtePilz Mar 1, 2023
fb5e5b5
Adds a missing test when parsing arguments
DerEchtePilz Mar 2, 2023
03a256f
Wrap map argument syntax in a code block
JorelAli Mar 3, 2023
c8d67c8
Merge dev/dev into dev/map-argument
DerEchtePilz Mar 4, 2023
47d6c61
Merge JorelAli:dev/map-argument into dev/map-argument
DerEchtePilz Mar 4, 2023
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
Prev Previous commit
Next Next commit
Implement tests for invalid keys and values
  • Loading branch information
DerEchtePilz committed Feb 4, 2023
commit 005b210ead2bbcc17a96c2b5cfa87fdfdf58b8f5
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@ private CommandSyntaxException throwValueEarlyStart(StringBuilder visitedCharact

private CommandSyntaxException throwInvalidKey(StringBuilder visitedCharacters, String key) {
String context = visitedCharacters.toString();
StringReader reader = new StringReader(context);
StringReader reader = new StringReader(context.substring(0, context.length() - 1));
reader.setCursor(context.length());
return CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherParseException().createWithContext(reader, "Invalid key: " + key);
}

private CommandSyntaxException throwInvalidValue(StringBuilder visitedCharacters, String value) {
String context = visitedCharacters.toString();
StringReader reader = new StringReader(context);
StringReader reader = new StringReader(context.substring(0, context.length() - 1));
reader.setCursor(context.length());
return CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherParseException().createWithContext(reader, "Invalid value: " + value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -43,7 +44,7 @@ public void tearDown() {

@Test
public void executionTestWithMapArgument() {
Mut<HashMap<String, String>> results = Mut.of();
Mut<LinkedHashMap<String, String>> results = Mut.of();

new CommandAPICommand("test")
.withArguments(new MapArgumentBuilder<String, String>("map", ':')
Expand All @@ -54,15 +55,15 @@ public void executionTestWithMapArgument() {
.build()
)
.executesPlayer((player, args) -> {
results.set((HashMap<String, String>) args.get("map"));
results.set((LinkedHashMap<String, String>) args.get("map"));
})
.register();

PlayerMock player = server.addPlayer();

// /test map:"cool map"
server.dispatchCommand(player, "test map:\"cool map\"");
Map<String, String> testMap = new HashMap<>();
Map<String, String> testMap = new LinkedHashMap<>();
testMap.put("map", "cool map");
assertEquals(testMap, results.get());

Expand All @@ -81,7 +82,7 @@ public void executionTestWithMapArgument() {

@Test
public void executionTestWithMapArgumentAndSpecialValues() {
Mut<HashMap<String, String>> results = Mut.of();
Mut<LinkedHashMap<String, String>> results = Mut.of();

new CommandAPICommand("test")
.withArguments(new MapArgumentBuilder<String, String>("map", ':')
Expand All @@ -92,15 +93,15 @@ public void executionTestWithMapArgumentAndSpecialValues() {
.build()
)
.executesPlayer((player, args) -> {
results.set((HashMap<String, String>) args.get("map"));
results.set((LinkedHashMap<String, String>) args.get("map"));
})
.register();

PlayerMock player = server.addPlayer();

// /test map:"\"hello\""
server.dispatchCommand(player, "test map:\"\\\"hello\\\"\"");
Map<String, String> testMap = new HashMap<>();
Map<String, String> testMap = new LinkedHashMap<>();
testMap.put("map", "\"hello\"");
assertEquals(testMap, results.get());

Expand Down Expand Up @@ -162,7 +163,7 @@ public void exceptionTestWithMapArgument() {

@Test
public void executionTestWithOtherKeyValuePairs() {
Mut<HashMap<String, Integer>> results = Mut.of();
Mut<LinkedHashMap<String, Integer>> results = Mut.of();

new CommandAPICommand("test")
.withArguments(new MapArgumentBuilder<String, Integer>("map")
Expand All @@ -173,15 +174,15 @@ public void executionTestWithOtherKeyValuePairs() {
.build()
)
.executesPlayer((player, args) -> {
results.set((HashMap<String, Integer>) args.get("map"));
results.set((LinkedHashMap<String, Integer>) args.get("map"));
})
.register();

PlayerMock player = server.addPlayer();

// /test map:"598"
server.dispatchCommand(player, "test map:\"598\"");
Map<String, Integer> testMap = new HashMap<>();
Map<String, Integer> testMap = new LinkedHashMap<>();
testMap.put("map", 598);
assertEquals(testMap, results.get());

Expand All @@ -200,7 +201,7 @@ public void executionTestWithOtherKeyValuePairs() {

@Test< 8000 /td>
public void executionTestWithFloatKey() {
Mut<HashMap<Float, String>> results = Mut.of();
Mut<LinkedHashMap<Float, String>> results = Mut.of();

new CommandAPICommand("test")
.withArguments(new MapArgumentBuilder<Float, String>("map")
Expand All @@ -211,15 +212,15 @@ public void executionTestWithFloatKey() {
.build()
)
.executesPlayer((player, args) -> {
results.set((HashMap<Float, String>) args.get(0));
results.set((LinkedHashMap<Float, String>) args.get(0));
})
.register();

PlayerMock player = server.addPlayer();

// /test 3.5:"Hello World" 12.25:"This is a test!"
server.dispatchCommand(player, "test 3.5:\"Hello World\" 12.25:\"This is a test!\"");
HashMap<Float, String> testMap = new HashMap<>();
LinkedHashMap<Float, String> testMap = new LinkedHashMap<>();
testMap.put(3.5F, "Hello World");
testMap.put(12.25F, "This is a test!");
assertEquals(testMap, results.get());
Expand All @@ -237,7 +238,7 @@ public void executionTestWithFloatKey() {

@Test
public void executionTestWithIntegerKey() {
Mut<HashMap<Integer, String>> results = Mut.of();
Mut<LinkedHashMap<Integer, String>> results = Mut.of();

new CommandAPICommand("test")
.withArguments(new MapArgumentBuilder<Integer, String>("map")
Expand All @@ -248,15 +249,15 @@ public void executionTestWithIntegerKey() {
.build()
)
.executesPlayer((player, args) -> {
results.set((HashMap<Integer, String>) args.get(0));
results.set((LinkedHashMap<Integer, String>) args.get(0));
})
.register();

PlayerMock player = server.addPlayer();

// /test 3:"Hello World" 12:"This is a test!"
server.dispatchCommand(player, "test 3:\"Hello World\" 12:\"This is a test!\"");
HashMap<Integer, String> testMap = new HashMap<>();
LinkedHashMap<Integer, String> testMap = new LinkedHashMap<>();
testMap.put(3, "Hello World");
testMap.put(12, "This is a test!");
assertEquals(testMap, results.get());
Expand All @@ -272,4 +273,32 @@ public void executionTestWithIntegerKey() {
assertNoMoreResults(results);
}

@Test
public void executionTestWithSuggestions() {

new CommandAPICommand("test")
.withArguments(new MapArgumentBuilder<String, String>("map")
.withKeyType(s -> s)
.withValueMapper(s -> s)
.withKeyList(List.of("optionOne", "optionTwo", "optionThree"))
.withValueList(List.of("solutionOne", "solutionTwo", "solutionThree"))
.build()
)
.executesPlayer((player, args) -> {
})
.register();

PlayerMock player = server.addPlayer();

// From previous test we know everything works so here only exceptions will be tested

// Test invalid key
// /test optionOne:"solutionTwo" optionFour:"solutionOne"
assertCommandFailsWith(player, "test optionOne:\"solutionTwo\" optionFour:\"solutionOne\"", "Could not parse command: Invalid key: optionFour at position 35: ...optionFour<--[HERE]");

// Test invalid value
// /test optionOne:"solutionOne" optionTwo:"solutionFour"
assertCommandFailsWith(player, "test optionOne:\"solutionOne\" optionTwo:\"solutionFour\"", "Could not parse command: Invalid value: solutionFour at position 48: ...lutionFour<--[HERE]");
}

}
0