@@ -37,14 +37,17 @@ protected void Invoke(object arg1, object arg2)
37
37
}
38
38
}
39
39
40
- private readonly Dictionary < string , ( MethodInfo method , object target ) > _commands
41
- = new Dictionary < string , ( MethodInfo method , object target ) > ( ) ;
42
-
43
40
private string _invokedCommandName ;
44
41
45
- private CommandHandler _commandHandler ;
42
+ private CommandParsingSteps . CommandHandler _commandHandler ;
46
43
47
44
private static readonly Regex Whitespace = new Regex ( "\\ s+" ) ;
45
+ private readonly Application _application ;
46
+
47
+ public CommandParsingSteps ( )
48
+ {
49
+ _application = new Application ( ) ;
50
+ }
48
51
49
52
[ Given ( @"I have a handler for the command ""(.*)"" which doesn't take arguments" ) ]
50
53
public void GivenIHaveAHandlerForTheCommandWhichDoesnTTakeArguments ( string commandName )
@@ -53,29 +56,17 @@ public void GivenIHaveAHandlerForTheCommandWhichDoesnTTakeArguments(string comma
53
56
54
57
Action cmd = Command ;
55
58
56
- _commands . Add ( commandName , ( cmd . Method , cmd . Target ) ) ;
59
+ _application . AddCommand ( commandName , cmd ) ;
57
60
}
58
-
61
+
59
62
[ When ( @"I run my application with the args ""(.*)""" ) ]
60
63
public void WhenIRunMyApplicationWithTheArgs ( string args )
61
64
{
62
65
var values = Whitespace . Split ( args ) ;
63
66
64
- var handler = _commands [ values [ 0 ] ] ;
65
-
66
- var arguments = new object [ values . Length - 1 ] ;
67
-
68
- // Convert argument strings to expected types.
69
- var parameters = handler . method . GetParameters ( ) ;
70
-
71
- for ( var i = 0 ; i < parameters . Length ; i ++ )
72
- {
73
- arguments [ i ] = Convert . ChangeType ( values [ i + 1 ] , parameters [ i ] . ParameterType ) ;
74
- }
75
-
76
- handler . method . Invoke ( handler . target , arguments ) ;
67
+ _application . Execute ( values ) ;
77
68
}
78
-
69
+
79
70
[ Then ( @"the ""(.*)"" command is invoked" ) ]
80
71
public void ThenTheCommandIsInvoked ( string commandName )
81
72
{
@@ -110,7 +101,7 @@ public void GivenIHaveAHandlerForTheCommandWhichTakesTheFollowingArguments(strin
110
101
var typeBuilder = moduleBuilder . DefineType (
111
102
"DynamicCommandHandler" ,
112
103
TypeAttributes . Public | TypeAttributes . Class ,
113
- typeof ( CommandHandler ) ) ;
104
+ typeof ( CommandParsingSteps . CommandHandler ) ) ;
114
105
115
106
var constructorBuilder = typeBuilder . DefineConstructor (
116
107
MethodAttributes . Public | MethodAttributes . SpecialName | MethodAttributes . RTSpecialName ,
@@ -119,7 +110,7 @@ public void GivenIHaveAHandlerForTheCommandWhichTakesTheFollowingArguments(strin
119
110
120
111
var constructorBodyGenerator = constructorBuilder . GetILGenerator ( ) ;
121
112
122
- var baseCtor = typeof ( CommandHandler ) . GetConstructors ( BindingFlags . NonPublic | BindingFlags . Instance ) . Single ( ) ;
113
+ var baseCtor = typeof ( CommandParsingSteps . CommandHandler ) . GetConstructors ( BindingFlags . NonPublic | BindingFlags . Instance ) . Single ( ) ;
123
114
constructorBodyGenerator . Emit ( OpCodes . Ldarg_0 ) ;
124
115
constructorBodyGenerator . Emit ( OpCodes . Ldarg_1 ) ;
125
116
constructorBodyGenerator . Emit ( OpCodes . Call , baseCtor ) ;
@@ -153,7 +144,7 @@ public void GivenIHaveAHandlerForTheCommandWhichTakesTheFollowingArguments(strin
153
144
}
154
145
155
146
// Call the protected "Invoke" method to save the arguments.
156
- var captureMethod = typeof ( CommandHandler )
147
+ var captureMethod = typeof ( CommandParsingSteps . CommandHandler )
157
148
. GetMethods ( BindingFlags . Instance | BindingFlags . NonPublic )
158
149
. Where ( method => method . Name == "Invoke" )
159
150
. Single ( method => method . GetParameters ( ) . Length == parameters . Length ) ;
@@ -167,11 +158,11 @@ public void GivenIHaveAHandlerForTheCommandWhichTakesTheFollowingArguments(strin
167
158
168
159
void Command ( ) => _invokedCommandName = commandName ;
169
160
170
- _commandHandler = ( CommandHandler ) Activator . CreateInstance ( handlerType , ( Action ) Command ) ;
161
+ _commandHandler = ( CommandParsingSteps . CommandHandler ) Activator . CreateInstance ( handlerType , ( Action ) Command ) ;
171
162
172
163
var handlerMethod = handlerType . GetMethod ( "Handle" ) ;
173
164
174
- _commands . Add ( commandName , ( handlerMethod , _commandHandler ) ) ;
165
+ _application . AddCommand ( commandName , handlerMethod , _commandHandler ) ;
175
166
}
176
167
177
168
[ Then ( @"the ""(.*)"" command is invoked with the following arguments" ) ]
0 commit comments