@@ -19,8 +19,10 @@ package net.kautler.command.integ.test.javacord.parameter
1919import net.kautler.command.Internal.Literal
2020import net.kautler.command.api.Command
2121import net.kautler.command.api.CommandContext
22+ import net.kautler.command.api.annotation.Alias
2223import net.kautler.command.api.annotation.Usage
2324import net.kautler.command.api.parameter.ParameterConverter
25+ import net.kautler.command.api.parameter.ParameterParseException
2426import net.kautler.command.api.parameter.ParameterParser
2527import net.kautler.command.api.parameter.ParameterParser.Typed
2628import net.kautler.command.api.parameter.ParameterType
@@ -48,13 +50,16 @@ import static java.util.UUID.randomUUID
4850class TypedParameterParserIntegTest extends Specification {
4951 @AddBean (PingCommand )
5052 @AddBean (CustomStringsConverter )
51- def ' typed parameter parser should work properly' (
53+ def ' typed parameter parser should work properly with correct arguments ' (
5254 ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
5355 given :
5456 def random1 = randomUUID()
5557 def random2 = randomUUID()
5658 def random3 = randomUUID()
5759 def random4 = randomUUID()
60+ def random5 = randomUUID()
61+ def random6 = randomUUID()
62+ def random7 = randomUUID()
5863 def responseReceived = new BlockingVariable<Boolean > (System . properties. testResponseTimeout as double )
5964
6065 and :
@@ -79,6 +84,7 @@ class TypedParameterParserIntegTest extends Specification {
7984 .name
8085 } ]
8186 moo: $serverTextChannelAsBot [${ serverTextChannelAsBot.getClass().name} ]
87+ noo: [$random5 , $random6 , $random7 ] [java.util.ArrayList]
8288 """ . stripIndent(). trim())) {
8389 responseReceived. set(true )
8490 }
@@ -102,7 +108,10 @@ class TypedParameterParserIntegTest extends Specification {
102108 .getHighestRole(serverTextChannelAsBot. api. yourself)
103109 .orElseThrow { new AssertionError () }
104110 .mentionTag,
105- serverTextChannelAsBot. mentionTag
111+ serverTextChannelAsBot. mentionTag,
112+ random5,
113+ random6,
114+ random7
106115 ]. join(' ' ))
107116 .join()
108117
@@ -113,6 +122,97 @@ class TypedParameterParserIntegTest extends Specification {
113122 listenerManager?. remove()
114123 }
115124
125+ @AddBean (PingCommand )
126+ def ' typed parameter parser should throw exception with wrong number of arguments [arguments: #arguments]' (
127+ arguments, ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
128+ given :
129+ def responseReceived = new BlockingVariable<Boolean > (System . properties. testResponseTimeout as double )
130+
131+ and :
132+ def listenerManager = serverTextChannelAsBot. addMessageCreateListener {
133+ if (it. message. author. yourself && (it. message. content == '''
134+ pong:
135+ Wrong arguments for command `!ping`
136+ Usage: `!ping <foo:number> <bar:decimal> <baz:user_mention> <bam:string> <boo> <hoo:strings> <loo:role_mention> <moo:channel_mention> <noo:string> <noo:string> <noo:string>`
137+ ''' . stripIndent(). trim())) {
138+ responseReceived. set(true )
139+ }
140+ }
141+
142+ when :
143+ serverTextChannelAsUser
144+ .sendMessage(" !ping $arguments " )
145+ .join()
146+
147+ then :
148+ responseReceived. get()
149+
150+ cleanup :
151+ listenerManager?. remove()
152+
153+ where :
154+ arguments << [' ' , ' foo' , ' foo bar baz bam boo hoo loo moo noo noo noo doo' ]
155+
156+ and :
157+ serverTextChannelAsBot = null
158+ serverTextChannelAsUser = null
159+ }
160+
161+ @AddBean (UsagelessPingCommand )
162+ def ' typed parameter parser should work properly without arguments' (
163+ ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
164+ given :
165+ def responseReceived = new BlockingVariable<Boolean > (System . properties. testResponseTimeout as double )
166+
167+ and :
168+ def listenerManager = serverTextChannelAsBot. addMessageCreateListener {
169+ if (it. message. author. yourself && (it. message. content == '''
170+ pong:
171+ ''' . stripIndent(). trim())) {
172+ responseReceived. set(true )
173+ }
174+ }
175+
176+ when :
177+ serverTextChannelAsUser
178+ .sendMessage(' !ping' )
179+ .join()
180+
181+ then :
182+ responseReceived. get()
183+
184+ cleanup :
185+ listenerManager?. remove()
186+ }
187+
188+ @AddBean (UsagelessPingCommand )
189+ def ' typed parameter parser should throw exception with unexpected arguments' (
190+ ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
191+ given :
192+ def responseReceived = new BlockingVariable<Boolean > (System . properties. testResponseTimeout as double )
193+
194+ and :
195+ def listenerManager = serverTextChannelAsBot. addMessageCreateListener {
196+ if (it. message. author. yourself && (it. message. content == '''
197+ pong:
198+ Command `!ping` does not expect arguments
199+ ''' . stripIndent(). trim())) {
200+ responseReceived. set(true )
201+ }
202+ }
203+
204+ when :
205+ serverTextChannelAsUser
206+ .sendMessage(' !ping foo' )
207+ .join()
208+
209+ then :
210+ responseReceived. get()
211+
212+ cleanup :
213+ listenerManager?. remove()
214+ }
215+
116216 @AddBean (PingCommand )
117217 @AddBean (CustomStringConverter )
118218 @AddBean (CustomStringsConverter )
@@ -123,6 +223,9 @@ class TypedParameterParserIntegTest extends Specification {
123223 def random2 = randomUUID()
124224 def random3 = randomUUID()
125225 def random4 = randomUUID()
226+ def random5 = randomUUID()
227+ def random6 = randomUUID()
228+ def random7 = randomUUID()
126229 def responseReceived = new BlockingVariable<Boolean > (System . properties. testResponseTimeout as double )
127230
128231 and :
@@ -147,6 +250,7 @@ class TypedParameterParserIntegTest extends Specification {
147250 .name
148251 } ]
149252 moo: $serverTextChannelAsBot [${ serverTextChannelAsBot.getClass().name} ]
253+ noo: [custom: $random5 , custom: $random6 , custom: $random7 ] [java.util.ArrayList]
150254 """ . stripIndent(). trim())) {
151255 responseReceived. set(true )
152256 }
@@ -170,7 +274,10 @@ class TypedParameterParserIntegTest extends Specification {
170274 .getHighestRole(serverTextChannelAsBot. api. yourself)
171275 .orElseThrow { new AssertionError () }
172276 .mentionTag,
173- serverTextChannelAsBot. mentionTag
277+ serverTextChannelAsBot. mentionTag,
278+ random5,
279+ random6,
280+ random7
174281 ]. join(' ' ))
175282 .join()
176283
@@ -207,7 +314,13 @@ class TypedParameterParserIntegTest extends Specification {
207314 listenerManager?. remove()
208315 }
209316
210- @Usage (' <foo:number> <bar:decimal> <baz:user_mention> <bam:string> <boo> <hoo:strings> <loo:role_mention> <moo:channel_mention>' )
317+ @Alias (' ping' )
318+ @Vetoed
319+ @ApplicationScoped
320+ static class UsagelessPingCommand extends PingCommand {
321+ }
322+
323+ @Usage (' <foo:number> <bar:decimal> <baz:user_mention> <bam:string> <boo> <hoo:strings> <loo:role_mention> <moo:channel_mention> <noo:string> <noo:string> <noo:string>' )
211324 @Vetoed
212325 @ApplicationScoped
213326 static class PingCommand implements Command<Message > {
@@ -217,12 +330,17 @@ class TypedParameterParserIntegTest extends Specification {
217330
218331 @Override
219332 void execute (CommandContext<? extends Message > commandContext ) {
220- def parameters = parameterParser
221- .parse(commandContext)
222- .with { it. entries }
223- .collect { " $it . key : $it . value [${ it.value.getClass().name} ]" }
224- .sort()
225- .join(' \n ' )
333+ def parameters
334+ try {
335+ parameters = parameterParser
336+ .parse(commandContext)
337+ .with { it. entries }
338+ .collect { " $it . key : $it . value [${ it.value.getClass().name} ]" }
339+ .sort()
340+ .join(' \n ' )
341+ } catch (ParameterParseException ppe) {
342+ parameters = ppe. message
343+ }
226344
227345 commandContext
228346 .message
0 commit comments