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 ;
916
1017namespace TS3ABotUnitTests
1118{
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-
2819 [ TestFixture ]
2920 public class BotCommandTests
3021 {
31- private readonly CommandManager cmdMgr ;
32-
33- public BotCommandTests ( )
34- {
35- cmdMgr = new CommandManager ( null ) ;
36- cmdMgr . RegisterCollection ( MainCommands . Bag ) ;
37- }
38-
3922 [ Test ]
4023 public void BotCommandTest ( )
4124 {
4225 var execInfo = Utils . GetExecInfo ( "ic3" ) ;
4326 string CallCommand ( string command )
4427 {
45- return cmdMgr . CommandSystem . ExecuteCommand ( execInfo , command ) ;
28+ return CommandManager . ExecuteCommand ( execInfo , command ) ;
4629 }
4730
4831 var output = CallCommand ( "!help" ) ;
@@ -93,6 +76,23 @@ string CallCommand(string command)
9376 Assert . Throws < CommandException > ( ( ) => CallCommand ( "!if a == b text (!)" ) ) ;
9477 }
9578
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+
9696 [ Test ]
9797 public void XCommandSystemFilterTest ( )
9898 {
@@ -145,32 +145,30 @@ public void XCommandSystemFilterTest()
145145 [ Test ]
146146 public void XCommandSystemTest ( )
147147 {
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 ;
151150 group . AddCommand ( "one" , new FunctionCommand ( ( ) => "ONE" ) ) ;
152151 group . AddCommand ( "two" , new FunctionCommand ( ( ) => "TWO" ) ) ;
153152 group . AddCommand ( "echo" , new FunctionCommand ( s => s ) ) ;
154153 group . AddCommand ( "optional" , new FunctionCommand ( GetType ( ) . GetMethod ( nameof ( OptionalFunc ) , BindingFlags . NonPublic | BindingFlags . Static ) ) ) ;
155154
156155 // 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" ) ) ;
163161
164162 // 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" ) ) ;
168166
169167 // 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)" ) ) ;
174172
175173 // Command overloading
176174 var intCom = new Func < int , string > ( _ => "INT" ) ;
@@ -180,17 +178,16 @@ public void XCommandSystemTest()
180178 new FunctionCommand ( strCom . Method , strCom . Target )
181179 } ) ) ;
182180
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" ) ) ;
186184 }
187185
188186 [ Test ]
189187 public void XCommandSystemTest2 ( )
190188 {
191189 var execInfo = Utils . GetExecInfo ( "exact" ) ;
192- var commandSystem = new XCommandSystem ( ) ;
193- var group = commandSystem . RootCommand ;
190+ var group = execInfo . GetModule < CommandManager > ( ) . RootGroup ;
194191
195192 var o1 = new OverloadedFunctionCommand ( ) ;
196193 o1 . AddCommand ( new FunctionCommand ( new Action < int > ( ( _ ) => { } ) ) ) ;
@@ -204,25 +201,25 @@ public void XCommandSystemTest2()
204201 o2 . AddCommand ( "b" , new FunctionCommand ( new Action ( ( ) => { } ) ) ) ;
205202 group . AddCommand ( "three" , o2 ) ;
206203
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" ) ) ;
226223 }
227224
228225 [ Test ]
@@ -231,10 +228,16 @@ public void EnsureAllCommandsHaveEnglishDocumentationEntry()
231228 Thread . CurrentThread . CurrentCulture = CultureInfo . GetCultureInfo ( "en" ) ;
232229 Thread . CurrentThread . CurrentUICulture = CultureInfo . GetCultureInfo ( "en" ) ;
233230
231+ var execInfo = Utils . GetExecInfo ( "exact" ) ;
232+ var cmdMgr = execInfo . GetModule < CommandManager > ( ) ;
233+ var errors = new List < string > ( ) ;
234234 foreach ( var cmd in cmdMgr . AllCommands )
235235 {
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") ;
237238 }
239+ if ( errors . Count > 0 )
240+ Assert . Fail ( string . Join ( "\n " , errors ) ) ;
238241 }
239242
240243 [ Test ]
@@ -269,19 +272,15 @@ public static void TestStringParsing(string inp, string outp)
269272
270273 internal static class Utils
271274 {
272- private static readonly CommandManager cmdMgr ;
273-
274- static Utils ( )
275+ public static ExecutionInformation GetExecInfo ( string matcher , bool addMainCommands = true )
275276 {
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 ) ;
279280
280- public static ExecutionInformation GetExecInfo ( string matcher )
281- {
282281 var execInfo = new ExecutionInformation ( ) ;
283282 execInfo . AddModule ( new CallerInfo ( false ) { SkipRightsChecks = true , CommandComplexityMax = int . MaxValue } ) ;
284- execInfo . AddModule ( new InvokerData ( "InvokerUid" ) ) ;
283+ execInfo . AddModule ( new InvokerData ( ( Uid ) "InvokerUid" ) ) ;
285284 execInfo . AddModule ( Filter . GetFilterByName ( matcher ) ) ;
286285 execInfo . AddModule ( cmdMgr ) ;
287286 return execInfo ;
0 commit comments