8000 Add `initialParseExceptionTestWithListTextArgument` · CommandAPI/CommandAPI@3c72dde · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c72dde

Browse files
committed
Add initialParseExceptionTestWithListTextArgument
Also some test javadocs tweaks
1 parent f7cc6d8 commit 3c72dde

File tree

5 files changed

+190
-8
lines changed

5 files changed

+190
-8
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentListTests.java

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import dev.jorel.commandapi.arguments.StringArgument;
1010
import dev.jorel.commandapi.arguments.parseexceptions.ArgumentParseExceptionContext;
1111
import dev.jorel.commandapi.test.arguments.parseexceptions.ArgumentParseExceptionContextVerifier;
12+
import dev.jorel.commandapi.test.arguments.parseexceptions.InitialParseExceptionTextArgumentVerifier;
1213
import org.bukkit.Material;
1314
import org.bukkit.command.CommandSender;
1415
import org.junit.jupiter.api.AfterEach;
@@ -445,10 +446,10 @@ public ArgumentParseExceptionListArgumentVerifier(TestBase testBase) {
445446
}
446447

447448
public void assertCorrectContext(
448-
String exceptionMessage, CommandSender sender, String input, Map<String, Object> previousArgsMap,
449-
ListArgumentCommon.ArgumentParseExceptionInformation.Exceptions type,
450-
List<T> listSoFar, String rawItem, T currentItem,
451-
ArgumentParseExceptionContext<String, ListArgumentCommon.ArgumentParseExceptionInformation<T>, CommandSender> actual
449+
String exceptionMessage, CommandSender sender, String input, Map<String, Object> previousArgsMap,
450+
ListArgumentCommon.ArgumentParseExceptionInformation.Exceptions type,
451+
List<T> listSoFar, String rawItem, T currentItem,
452+
ArgumentParseExceptionContext<String, ListArgumentCommon.ArgumentParseExceptionInformation<T>, CommandSender> actual
452453
) {
453454
super.assertCorrectContext(exceptionMessage, sender, input, previousArgsMap, actual);
454455

@@ -562,7 +563,97 @@ void argumentParseExceptionTestWithListArgument() {
562563
"1 2 3 a 5", Map.of("buffer", "b123456789012345"), List.of(1, 2, 3), "a"
563564
);
564565
}
565-
566+
567+
@Test
568+
void initialParseExceptionTestWithListTextArgument() {
569+
PlayerMock player = server.addPlayer();
570+
InitialParseExceptionTextArgumentVerifier verifier = new InitialParseExceptionTextArgumentVerifier(this);
571+
572+
new CommandAPICommand("test")
573+
.withArguments(
574+
new StringArgument("buffer"),
575+
new ListArgumentBuilder<Integer>("list")
576+
.withList(1, 2, 3, 4, 5)
577+
.withStringMapper()
578+
.buildText()
579+
.withInitialParseExceptionHandler(verifier.getExceptionHandler())
580+
)
581+
.executesPlayer(P_EXEC)
582+
.register();
583+
584+
// Test INVALID_ESCAPE cases: Backslash not followed by backslash or the same quote that started argument
585+
verifier.testInvalidEscapeCase(
586+
player, "test b123 \"\\abc\"",
587+
"Invalid escape sequence '\\a' in quoted string at position 12: ...st b123 \"\\<--[HERE]",
588+
10, 12
589+
);
590+
verifier.testInvalidEscapeCase(
591+
player, "test b123 \"ab\\c\"",
592+
"Invalid escape sequence '\\c' in quoted string at position 14: ... b123 \"ab\\<--[HERE]",
593+
10, 14
594+
);
595+
596+
verifier.testInvalidEscapeCase(
597+
player, "test b123 \"\\'bc\"",
598+
"Invalid escape sequence '\\'' in quoted string at position 12: ...st b123 \"\\<--[HERE]",
599+
10, 12
600+
);
601+
verifier.testInvalidEscapeCase(
602+
player, "test b123 '\\\"bc'",
603+
"Invalid escape sequence '\\\"' in quoted string at position 12: ...st b123 '\\<--[HERE]",
604+
10, 12
605+
);
606+
607+
608+
// Test EXPECTED_QUOTE_END cases: Quoted string never ended by same quote that started argument
609+
verifier.testExpectedQuoteEndCase(
610+
player, "test b123 \"abc",
611+
"Unclosed quoted string at position 14: ... b123 \"abc<--[HERE]",
612+
10
613+
);
614+
verifier.testExpectedQuoteEndCase(
615+
player, "test b123 'abcde",
616+
"Unclosed quoted string at position 16: ...123 'abcde<--[HERE]",
617+
10
618+
);
619+
620+
verifier.testExpectedQuoteEndCase(
621+
player, "test b123 \"abc'",
622+
"Unclosed quoted string at position 15: ...b123 \"abc'<--[HERE]",
623+
10
624+
);
625+
verifier.testExpectedQuoteEndCase(
626+
player, "test b123 'abcde\"",
627+
"Unclosed quoted string at position 17: ...23 'abcde\"<--[HERE]",
628+
10
629+
);
630+
631+
632+
// Increasing characters in buffer argument increases cursor start
633+
verifier.testInvalidEscapeCase(
634+
player, "test b12345 '\\\"bc'",
635+
"Invalid escape sequence '\\\"' in quoted string at position 14: ... b12345 '\\<--[HERE]",
636+
12, 14
637+
);
638+
verifier.testInvalidEscapeCase(
639+
player, "test b123456789012345 '\\\"bc'",
640+
"Invalid escape sequence '\\\"' in quoted string at position 24: ...9012345 '\\<--[HERE]",
641+
22, 24
642+
);
643+
644+
verifier.testExpectedQuoteEndCase(
645+
player, "test b12345 \"abc'",
646+
"Unclosed quoted string at position 17: ...2345 \"abc'<--[HERE]",
647+
12
648+
);
649+
650+
verifier.testExpectedQuoteEndCase(
651+
player, "test b123456789012345 \"abc'",
652+
"Unclosed quoted string at position 27: ...2345 \"abc'<--[HERE]",
653+
22
654+
);
655+
}
656+
566657
/********************
567658
* Suggestion tests *
568659
********************/

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/parseexceptions/ArgumentParseExceptionContextVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void verifyGeneratedContext(
8181
}
8282

8383
/**
84-
* Asserts that the given {@code actual} context has all the given expected attributes
84+
* Asserts that the given {@code actual} context has all the given expected attributes.
8585
*
8686
* @param exceptionMessage The message of the initial parse exception.
8787
* @param sender The {@link CommandSender} who sent the command.

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/parseexceptions/InitialParseExceptionContextVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void verifyGeneratedContext(
7979
}
8080

8181
/**
82-
* Asserts that the given {@code actual} context has all the given expected attributes
82+
* Asserts that the given {@code actual} context has all the given expected attributes.
8383
*
8484
* @param exceptionMessage The message of the initial parse exception.
8585
* @param readerString The command string being parsed.

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/parseexceptions/InitialParseExceptionNumberArgumentVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class InitialParseExceptionNumberArgumentVerifier<N extends Number>
1616
extends InitialParseExceptionContextVerifier<N, InitialParseExceptionNumberArgument.ExceptionInformation<N>> {
1717

1818
/**
19-
* Creates a new {@link InitialParseExceptionContextVerifier}.
19+
* Creates a new {@link InitialParseExceptionNumberArgumentVerifier}.
2020
*
2121
* @param testBase The {@link TestBase} object running the test.
2222
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package dev.jorel.commandapi.test.arguments.parseexceptions;
2+
3+
import dev.jorel.commandapi.arguments.parseexceptions.InitialParseExceptionContext;
4+
import dev.jorel.commandapi.arguments.parseexceptions.InitialParseExceptionTextArgument;
5+
import dev.jorel.commandapi.test.TestBase;
6+
import org.bukkit.command.CommandSender;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
/**
11+
* A helper class for verifying {@link InitialParseExceptionContext} objects generated by
12+
* {@link InitialParseExceptionTextArgument}s.
13+
*/
14+
public class InitialParseExceptionTextArgumentVerifier
15+
extends InitialParseExceptionContextVerifier<String, InitialParseExceptionTextArgument.ExceptionTypes> {
16+
/**
17+
* Creates a new {@link InitialParseExceptionTextArgumentVerifier}.
18+
*
19+
* @param testBase The {@link TestBase} object running the test.
20+
*/
21+
public InitialParseExceptionTextArgumentVerifier(TestBase testBase) {
22+
super(testBase);
23+
}
24+
25+
/**
26+
* Asserts that the given {@code actual} context has all the given expected attributes.
27+
*
28+
* @param exceptionMessage The message of the initial parse exception.
29+
* @param readerString The command string being parsed.
30+
* @param cursorStart The place where the StringReader started parsing the ArgumentType.
31+
* @param readerCursor The place where the StringReader ended when the exception was handled.
32+
* @param exceptionType The type of exception that occurred.
33+
* @param actual The actual {@link InitialParseExceptionContext} generated.
34+
*/
35+
public void assertCorrectContext(
36+
String exceptionMessage, String readerString, int cursorStart, int readerCursor F42D ,
37+
InitialParseExceptionTextArgument.ExceptionTypes exceptionType,
38+
InitialParseExceptionContext<InitialParseExceptionTextArgument.ExceptionTypes> actual) {
39+
super.assertCorrectContext(exceptionMessage, readerString, cursorStart, readerCursor, actual);
40+
41+
assertEquals(exceptionType, actual.exceptionInformation());
42+
}
43+
44+
/**
45+
* Tests a case where the {@code exceptionType} is {@code INVALID_ESCAPE}, when a quoted String contains the escape
46+
* character {@code \} (backslash) is followed by anything other than another backslash or the same character that
47+
* started the quoted string.
48+
*
49+
* @param sender The sender for the command.
50+
* @param command The command to execute.
51+
* @param exceptionMessage The message of the exception that should be thrown.
52+
* @param cursorStart The index where the StringReader's cursor should start and end.
53+
* @param readerCursor The place where the StringReader ended when the exception was handled.
54+
*/
55+
public void testInvalidEscapeCase(
56+
CommandSender sender, String command, String exceptionMessage,
57+
int cursorStart, int readerCursor
58+
) {
59+
verifyGeneratedContext(
60+
sender, command, exceptionMessage,
61+
context -> assertCorrectContext(
62+
exceptionMessage, command, cursorStart, readerCursor,
63+
InitialParseExceptionTextArgument.ExceptionTypes.INVALID_ESCAPE,
64+
context
65+
)
66+
);
67+
}
68+
69+
/**
70+
* Tests a case where the {@code exceptionType} is {@code EXPECTED_QUOTE_END}, when a quoted String reaches the end
71+
* of input before the String is closed. {@code readerCursor} should be at the end of the input command.
72+
*
73+
* @param sender The sender for the command.
74+
* @param command The command to execute.
75+
* @param exceptionMessage The message of the exception that should be thrown.
76+
* @param cursorStart The index where the StringReader's cursor should start and end.
77+
*/
78+
public void testExpectedQuoteEndCase(
79+
CommandSender sender, String command, String exceptionMessage,
80+
int cursorStart
81+
) {
82+
verifyGeneratedContext(
83+
sender, command, exceptionMessage,
84+
context -> assertCorrectContext(
85+
exceptionMessage, command, cursorStart, command.length(),
86+
InitialParseExceptionTextArgument.ExceptionTypes.EXPECTED_QUOTE_END,
87+
context
88+
)
89+
);
90+
}
91+
}

0 commit comments

Comments
 (0)
0