Skip to content

Commit 7eb23d4

Browse files
committed
Increase integ tests coverage for parameter parsers
1 parent 6091043 commit 7eb23d4

File tree

4 files changed

+519
-40
lines changed

4 files changed

+519
-40
lines changed

src/javacordIntegTest/groovy/net/kautler/command/integ/test/javacord/parameter/TypedParameterParserIntegTest.groovy

Lines changed: 128 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package net.kautler.command.integ.test.javacord.parameter
1919
import net.kautler.command.Internal.Literal
2020
import net.kautler.command.api.Command
2121
import net.kautler.command.api.CommandContext
22+
import net.kautler.command.api.annotation.Alias
2223
import net.kautler.command.api.annotation.Usage
2324
import net.kautler.command.api.parameter.ParameterConverter
25+
import net.kautler.command.api.parameter.ParameterParseException
2426
import net.kautler.command.api.parameter.ParameterParser
2527
import net.kautler.command.api.parameter.ParameterParser.Typed
2628
import net.kautler.command.api.parameter.ParameterType
@@ -48,13 +50,16 @@ import static java.util.UUID.randomUUID
4850
class 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

src/javacordIntegTest/groovy/net/kautler/command/integ/test/javacord/parameter/UntypedParameterParserIntegTest.groovy

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package net.kautler.command.integ.test.javacord.parameter
1818

1919
import net.kautler.command.api.Command
2020
import net.kautler.command.api.CommandContext
21+
import net.kautler.command.api.annotation.Alias
2122
import net.kautler.command.api.annotation.Usage
23+
import net.kautler.command.api.parameter.ParameterParseException
2224
import net.kautler.command.api.parameter.ParameterParser
2325
import net.kautler.command.integ.test.spock.AddBean
2426
import net.kautler.command.integ.test.spock.VetoBean
@@ -41,7 +43,7 @@ import static java.util.UUID.randomUUID
4143
@VetoBean(MissingDependencyParameterParser)
4244
class UntypedParameterParserIntegTest extends Specification {
4345
@AddBean(PingCommand)
44-
def 'untyped parameter parser should work properly'(
46+
def 'untyped parameter parser should work properly with correct arguments'(
4547
ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
4648
given:
4749
def random1 = randomUUID()
@@ -52,8 +54,7 @@ class UntypedParameterParserIntegTest extends Specification {
5254
def listenerManager = serverTextChannelAsBot.addMessageCreateListener {
5355
if (it.message.author.yourself && (it.message.content == """
5456
pong:
55-
bar: $random2
56-
foo: $random1
57+
foo: [$random1, $random2]
5758
""".stripIndent().trim())) {
5859
responseReceived.set(true)
5960
}
@@ -71,7 +72,104 @@ class UntypedParameterParserIntegTest extends Specification {
7172
listenerManager?.remove()
7273
}
7374

74-
@Usage('<foo> <bar>')
75+
@AddBean(PingCommand)
76+
def 'untyped parameter parser should throw exception with wrong number of arguments [arguments: #arguments]'(
77+
arguments, ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
78+
given:
79+
def responseReceived = new BlockingVariable<Boolean>(System.properties.testResponseTimeout as double)
80+
81+
and:
82+
def listenerManager = serverTextChannelAsBot.addMessageCreateListener {
83+
if (it.message.author.yourself && (it.message.content == '''
84+
pong:
85+
Wrong arguments for command `!ping`
86+
Usage: `!ping <foo> <foo>`
87+
'''.stripIndent().trim())) {
88+
responseReceived.set(true)
89+
}
90+
}
91+
92+
when:
93+
serverTextChannelAsUser
94+
.sendMessage("!ping $arguments")
95+
.join()
96+
97+
then:
98+
responseReceived.get()
99+
100+
cleanup:
101+
listenerManager?.remove()
102+
103+
where:
104+
arguments << ['', 'foo', 'foo bar baz']
105+
106+
and:
107+
serverTextChannelAsBot = null
108+
serverTextChannelAsUser = null
109+
}
110+
111+
@AddBean(UsagelessPingCommand)
112+
def 'untyped parameter parser should work properly without arguments'(
113+
ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
114+
given:
115+
def responseReceived = new BlockingVariable<Boolean>(System.properties.testResponseTimeout as double)
116+
117+
and:
118+
def listenerManager = serverTextChannelAsBot.addMessageCreateListener {
119+
if (it.message.author.yourself && (it.message.content == '''
120+
pong:
121+
'''.stripIndent().trim())) {
122+
responseReceived.set(true)
123+
}
124+
}
125+
126+
when:
127+
serverTextChannelAsUser
128+
.sendMessage('!ping')
129+
.join()
130+
131+
then:
132+
responseReceived.get()
133+
134+
cleanup:
135+
listenerManager?.remove()
136+
}
137+
138+
@AddBean(UsagelessPingCommand)
139+
def 'untyped parameter parser should throw exception with unexpected arguments'(
140+
ServerTextChannel serverTextChannelAsBot, ServerTextChannel serverTextChannelAsUser) {
141+
given:
142+
def responseReceived = new BlockingVariable<Boolean>(System.properties.testResponseTimeout as double)
143+
144+
and:
145+
def listenerManager = serverTextChannelAsBot.addMessageCreateListener {
146+
if (it.message.author.yourself && (it.message.content == '''
147+
pong:
148+
Command `!ping` does not expect arguments
149+
'''.stripIndent().trim())) {
150+
responseReceived.set(true)
151+
}
152+
}
153+
154+
when:
155+
serverTextChannelAsUser
156+
.sendMessage('!ping foo')
157+
.join()
158+
159+
then:
160+
responseReceived.get()
161+
162+
cleanup:
163+
listenerManager?.remove()
164+
}
165+
166+
@Alias('ping')
167+
@Vetoed
168+
@ApplicationScoped
169+
static class UsagelessPingCommand extends PingCommand {
170+
}
171+
172+
@Usage('<foo> <foo>')
75173
@Vetoed
76174
@ApplicationScoped
77175
static class PingCommand implements Command<Message> {
@@ -80,12 +178,17 @@ class UntypedParameterParserIntegTest extends Specification {
80178

81179
@Override
82180
void execute(CommandContext<? extends Message> commandContext) {
83-
def parameters = parameterParser
84-
.parse(commandContext)
85-
.with { it.entries }
86-
.collect { "$it.key: $it.value" }
87-
.sort()
88-
.join('\n')
181+
def parameters
182+
try {
183+
parameters = parameterParser
184+
.parse(commandContext)
185+
.with { it.entries }
186+
.collect { "$it.key: $it.value" }
187+
.sort()
188+
.join('\n')
189+
} catch (ParameterParseException ppe) {
190+
parameters = ppe.message
191+
}
89192

90193
commandContext
91194
.message

0 commit comments

Comments
 (0)