1
- // TS3AudioBot - An advanced Musicbot for Teamspeak 3
2
- // Copyright (C) 2017 TS3AudioBot contributors
3
- //
4
- // This program is free software: you can redistribute it and/or modify
5
- // it under the terms of the Open Software License v. 3.0
6
- //
7
- // You should have received a copy of the Open Software License along with this
8
- // program. If not, see <https://opensource.org/licenses/OSL-3.0>.
1
+ using NUnit . Framework ;
2
+ using System ;
3
+ using System . Collections . Generic ;
4
+ using System . Globalization ;
5
+ using System . Linq ;
6
+ using System . Reflection ;
7
+ using System . Threading ;
8
+ using TS3AudioBot ;
9
+ using TS3AudioBot . Algorithm ;
10
+ using TS3AudioBot . CommandSystem ;
11
+ using TS3AudioBot . CommandSystem . Ast ;
12
+ using TS3AudioBot . CommandSystem . CommandResults ;
13
+ using TS3AudioBot . CommandSystem . Commands ;
14
+ using TS3AudioBot . Dependency ;
15
+ using TSLib ;
9
16
10
17
namespace TS3ABotUnitTests
11
18
{
12
- using NUnit . Framework ;
13
- using System ;
14
- using System . Collections . Generic ;
15
- using System . Globalization ;
16
- using System . Linq ;
17
- using System . Reflection ;
18
- using System . Threading ;
19
- using TS3AudioBot ;
20
- using TS3AudioBot . Dependency ;
21
- using TS3AudioBot . Algorithm ;
22
- using TS3AudioBot . Audio ;
23
- using TS3AudioBot . CommandSystem ;
24
- using TS3AudioBot . CommandSystem . Ast ;
25
- using TS3AudioBot . CommandSystem . CommandResults ;
26
- using TS3AudioBot . CommandSystem . Commands ;
27
-
28
19
[ TestFixture ]
29
20
public class BotCommandTests
30
21
{
31
- private readonly CommandManager cmdMgr ;
32
-
33
- public BotCommandTests ( )
34
- {
35
- cmdMgr = new CommandManager ( null ) ;
36
- cmdMgr . RegisterCollection ( MainCommands . Bag ) ;
37
- }
38
-
39
22
[ Test ]
40
23
public void BotCommandTest ( )
41
24
{
42
25
var execInfo = Utils . GetExecInfo ( "ic3" ) ;
43
26
string CallCommand ( string command )
44
27
{
45
- return cmdMgr . CommandSystem . ExecuteCommand ( execInfo , command ) ;
28
+ return CommandManager . ExecuteCommand ( execInfo , command ) ;
46
29
}
47
30
48
31
var output = CallCommand ( "!help" ) ;
@@ -93,6 +76,23 @@ string CallCommand(string command)
93
76
Assert . Throws < CommandException > ( ( ) => CallCommand ( "!if a == b text (!)" ) ) ;
94
77
}
95
78
79
+ [ Test ]
80
+ public void TailStringTest ( )
81
+ {
82
+ var execInfo = Utils . GetExecInfo ( "ic3" ) ;
83
+ var group = execInfo . GetModule < CommandManager > ( ) . RootGroup ;
84
+ group . AddCommand ( "cmd" , new FunctionCommand ( s => s ) ) ;
85
+ string CallCommand ( string command )
86
+ {
87
+ return CommandManager . ExecuteCommand ( execInfo , command ) ;
88
+ }
89
+
90
+ Assert . AreEqual ( "a" , CallCommand ( "!cmd a" ) ) ;
91
+ Assert . AreEqual ( "a b" , CallCommand ( "!cmd a b" ) ) ;
92
+ Assert . AreEqual ( "a" , CallCommand ( "!cmd a \" b" ) ) ;
93
+ Assert . AreEqual ( "a b 1" , CallCommand ( "!cmd a b 1" ) ) ;
94
+ }
95
+
96
96
[ Test ]
97
97
public void XCommandSystemFilterTest ( )
98
98
{
@@ -145,32 +145,30 @@ public void XCommandSystemFilterTest()
145
145
[ Test ]
146
146
public void XCommandSystemTest ( )
147
147
{
148
- var execInfo = Utils . GetExecInfo ( "ic3" ) ;
149
- var commandSystem = new XCommandSystem ( ) ;
150
- var group = commandSystem . RootCommand ;
148
+ var execInfo = Utils . GetExecInfo ( "ic3" , false ) ;
149
+ var group = execInfo . GetModule < CommandManager > ( ) . RootGroup ;
151
150
group . AddCommand ( "one" , new FunctionCommand ( ( ) => "ONE" ) ) ;
152
151
group . AddCommand ( "two" , new FunctionCommand ( ( ) => "TWO" ) ) ;
153
152
group . AddCommand ( "echo" , new FunctionCommand ( s => s ) ) ;
154
153
group . AddCommand ( "optional" , new FunctionCommand ( GetType ( ) . GetMethod ( nameof ( OptionalFunc ) , BindingFlags . NonPublic | BindingFlags . Static ) ) ) ;
155
154
156
155
// Basic tests
157
- Assert . AreEqual ( "ONE" , ( ( StringCommandResult ) commandSystem . Execute ( execInfo ,
158
- new ICommand [ ] { new StringCommand ( "one" ) } ) ) . Content ) ;
159
- Assert . AreEqual ( "ONE" , commandSystem . ExecuteCommand ( execInfo , "!one" ) ) ;
160
- Assert . AreEqual ( "TWO" , commandSystem . ExecuteCommand ( execInfo , "!t" ) ) ;
161
- Assert . AreEqual ( "TEST" , commandSystem . ExecuteCommand ( execInfo , "!e TEST" ) ) ;
162
- Assert . AreEqual ( "ONE" , commandSystem . ExecuteCommand ( execInfo , "!o" ) ) ;
156
+ Assert . AreEqual ( "ONE" , CommandManager . ExecuteCommand ( execInfo , new ICommand [ ] { new ResultCommand ( new PrimitiveResult < string > ( "one" ) ) } ) ) ;
157
+ Assert . AreEqual ( "ONE" , CommandManager . ExecuteCommand ( execInfo , "!one" ) ) ;
158
+ Assert . AreEqual ( "TWO" , CommandManager . ExecuteCommand ( execInfo , "!t" ) ) ;
159
+ Assert . AreEqual ( "TEST" , CommandManager . ExecuteCommand ( execInfo , "!e TEST" ) ) ;
160
+ Assert . AreEqual ( "ONE" , CommandManager . ExecuteCommand ( execInfo , "!o" ) ) ;
163
161
164
162
// Optional parameters
165
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!e" ) ) ;
166
- Assert . AreEqual ( "NULL" , commandSystem . ExecuteCommand ( execInfo , "!op" ) ) ;
167
- Assert . AreEqual ( "NOT NULL" , commandSystem . ExecuteCommand ( execInfo , "!op 1" ) ) ;
163
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!e" ) ) ;
164
+ Assert . AreEqual ( "NULL" , CommandManager . ExecuteCommand ( execInfo , "!op" ) ) ;
165
+ Assert . AreEqual ( "NOT NULL" , CommandManager . ExecuteCommand ( execInfo , "!op 1" ) ) ;
168
166
169
167
// Command chaining
170
- Assert . AreEqual ( "TEST" , commandSystem . ExecuteCommand ( execInfo , "!e (!e TEST)" ) ) ;
171
- Assert . AreEqual ( "TWO" , commandSystem . ExecuteCommand ( execInfo , "!e (!t)" ) ) ;
172
- Assert . AreEqual ( "NOT NULL" , commandSystem . ExecuteCommand ( execInfo , "!op (!e TEST)" ) ) ;
173
- Assert . AreEqual ( "ONE" , commandSystem . ExecuteCommand ( execInfo , "!(!e on)" ) ) ;
168
+ Assert . AreEqual ( "TEST" , CommandManager . ExecuteCommand ( execInfo , "!e (!e TEST)" ) ) ;
169
+ Assert . AreEqual ( "TWO" , CommandManager . ExecuteCommand ( execInfo , "!e (!t)" ) ) ;
170
+ Assert . AreEqual ( "NOT NULL" , CommandManager . ExecuteCommand ( execInfo , "!op (!e TEST)" ) ) ;
171
+ Assert . AreEqual ( "ONE" , CommandManager . ExecuteCommand ( execInfo , "!(!e on)" ) ) ;
174
172
175
173
// Command overloading
176
174
var intCom = new Func < int , string > ( _ => "INT" ) ;
@@ -180,17 +178,16 @@ public void XCommandSystemTest()
180
178
new FunctionCommand ( strCom . Method , strCom . Target )
181
179
} ) ) ;
182
180
183
- Assert . AreEqual ( "INT" , commandSystem . ExecuteCommand ( execInfo , "!overlord 1" ) ) ;
184
- Assert . AreEqual ( "STRING" , commandSystem . ExecuteCommand ( execInfo , "!overlord a" ) ) ;
185
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!overlord" ) ) ;
181
+ Assert . AreEqual ( "INT" , CommandManager . ExecuteCommand ( execInfo , "!overlord 1" ) ) ;
182
+ Assert . AreEqual ( "STRING" , CommandManager . ExecuteCommand ( execInfo , "!overlord a" ) ) ;
183
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!overlord" ) ) ;
186
184
}
187
185
188
186
[ Test ]
189
187
public void XCommandSystemTest2 ( )
190
188
{
191
189
var execInfo = Utils . GetExecInfo ( "exact" ) ;
192
- var commandSystem = new XCommandSystem ( ) ;
193
- var group = commandSystem . RootCommand ;
190
+ var group = execInfo . GetModule < CommandManager > ( ) . RootGroup ;
194
191
195
192
var o1 = new OverloadedFunctionCommand ( ) ;
196
193
o1 . AddCommand ( new FunctionCommand ( new Action < int > ( ( _ ) => { } ) ) ) ;
@@ -204,25 +201,25 @@ public void XCommandSystemTest2()
204
201
o2 . AddCommand ( "b" , new FunctionCommand ( new Action ( ( ) => { } ) ) ) ;
205
202
group . AddCommand ( "three" , o2 ) ;
206
203
207
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!one" ) ) ;
208
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!one \" \" " ) ) ;
209
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!one (!print \" \" )" ) ) ;
210
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!one string" ) ) ;
211
- Assert . DoesNotThrow ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!one 42" ) ) ;
212
- Assert . DoesNotThrow ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!one 4200000000000" ) ) ;
213
-
214
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!two" ) ) ;
215
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!two \" \" " ) ) ;
216
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!two (!print \" \" )" ) ) ;
217
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!two 42" ) ) ;
218
- Assert . DoesNotThrow ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!two None" ) ) ;
219
-
220
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!three" ) ) ;
221
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!three \" \" " ) ) ;
222
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!three (!print \" \" )" ) ) ;
223
- Assert . Throws < CommandException > ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!three c" ) ) ;
224
- Assert . DoesNotThrow ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!three a" ) ) ;
225
- Assert . DoesNotThrow ( ( ) => commandSystem . ExecuteCommand ( execInfo , "!three b" ) ) ;
204
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!one" ) ) ;
205
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!one \" \" " ) ) ;
206
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!one (!print \" \" )" ) ) ;
207
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!one string" ) ) ;
208
+ Assert . DoesNotThrow ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!one 42" ) ) ;
209
+ Assert . DoesNotThrow ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!one 4200000000000" ) ) ;
210
+
211
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!two" ) ) ;
212
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!two \" \" " ) ) ;
213
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!two (!print \" \" )" ) ) ;
214
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!two 42" ) ) ;
215
+ Assert . DoesNotThrow ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!two None" ) ) ;
216
+
217
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!three" ) ) ;
218
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!three \" \" " ) ) ;
219
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!three (!print \" \" )" ) ) ;
220
+ Assert . Throws < CommandException > ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!three c" ) ) ;
221
+ Assert . DoesNotThrow ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!three a" ) ) ;
222
+ Assert . DoesNotThrow ( ( ) => CommandManager . ExecuteCommand ( execInfo , "!three b" ) ) ;
226
223
}
227
224
228
225
[ Test ]
@@ -231,10 +228,16 @@ public void EnsureAllCommandsHaveEnglishDocumentationEntry()
231
228
Thread . CurrentThread . CurrentCulture = CultureInfo . GetCultureInfo ( "en" ) ;
232
229
Thread . CurrentThread . CurrentUICulture = CultureInfo . GetCultureInfo ( "en" ) ;
233
230
231
+ var execInfo = Utils . GetExecInfo ( "exact" ) ;
232
+ var cmdMgr = execInfo . GetModule < CommandManager > ( ) ;
233
+ var errors = new List < string > ( ) ;
234
234
foreach ( var cmd in cmdMgr . AllCommands )
235
235
{
236
- Assert . IsFalse ( string . IsNullOrEmpty ( cmd . Description ) , $ "Command { cmd . FullQualifiedName } has no documentation") ;
236
+ if ( string . IsNullOrEmpty ( cmd . Description ) )
237
+ errors . Add ( $ "Command { cmd . FullQualifiedName } has no documentation") ;
237
238
}
239
+ if ( errors . Count > 0 )
240
+ Assert . Fail ( string . Join ( "\n " , errors ) ) ;
238
241
}
239
242
240
243
[ Test ]
@@ -269,19 +272,15 @@ public static void TestStringParsing(string inp, string outp)
269
272
270
273
internal static class Utils
271
274
{
272
- private static readonly CommandManager cmdMgr ;
273
-
274
- static Utils ( )
275
+ public static ExecutionInformation GetExecInfo ( string matcher , bool addMainCommands = true )
275
276
{
276
- cmdMgr = new CommandManager ( null ) ;
277
- cmdMgr . RegisterCollection ( MainCommands . Bag ) ;
278
- }
277
+ var cmdMgr = new CommandManager ( null ) ;
278
+ if ( addMainCommands )
279
+ cmdMgr . RegisterCollection ( MainCommands . Bag ) ;
279
280
280
- public static ExecutionInformation GetExecInfo ( string matcher )
281
- {
282
281
var execInfo = new ExecutionInformation ( ) ;
283
282
execInfo . AddModule ( new CallerInfo ( false ) { SkipRightsChecks = true , CommandComplexityMax = int . MaxValue } ) ;
284
- execInfo . AddModule ( new InvokerData ( "InvokerUid" ) ) ;
283
+ execInfo . AddModule ( new InvokerData ( ( Uid ) "InvokerUid" ) ) ;
285
284
execInfo . AddModule ( Filter . GetFilterByName ( matcher ) ) ;
286
285
execInfo . AddModule ( cmdMgr ) ;
287
286
return execInfo ;
0 commit comments