1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Linq . Expressions ;
4
5
using NUnit . Framework ;
@@ -67,6 +68,11 @@ public void MethodWithInputs(ContainerType subContainer)
67
68
68
69
}
69
70
71
+ public void MethodWithNullableArg ( decimal ? nullableInput )
72
+ {
73
+
74
+ }
75
+
70
76
public Bar Foo { get ; set ; }
71
77
72
78
public class Bar
@@ -77,9 +83,14 @@ public void Baz()
77
83
}
78
84
}
79
85
80
- object [ ] GetArguments ( Expression < Action < ClassUnderTest > > action , ClassUnderTest instance )
86
+ List < object > GetArgumentValues ( Expression < Action < ClassUnderTest > > action , ClassUnderTest instance )
87
+ {
88
+ return action . ExtractArguments ( instance ) . Select ( o => o . Value ) . ToList ( ) ;
89
+ }
90
+
91
+ List < StepArgument > GetArguments ( Expression < Action < ClassUnderTest > > action , ClassUnderTest instance )
81
92
{
82
- return action . ExtractArguments ( instance ) . Select ( o => o . Value ) . ToArray ( ) ;
93
+ return action . ExtractArguments ( instance ) . ToList ( ) ;
83
94
}
84
95
85
96
int _input1 = 1 ;
@@ -124,13 +135,13 @@ string GetInput2(string someInput)
124
135
[ Fact ]
125
136
public void NoArguments ( )
126
137
{
127
- var arguments = GetArguments ( x => x . MethodWithoutArguments ( ) , new ClassUnderTest ( ) ) ;
128
- arguments . Length . ShouldBe ( 0 ) ;
138
+ var arguments = GetArgumentValues ( x => x . MethodWithoutArguments ( ) , new ClassUnderTest ( ) ) ;
139
+ arguments . Count . ShouldBe ( 0 ) ;
129
140
}
130
141
131
- void AssertReturnedArguments ( object [ ] arguments , params object [ ] expectedArgs )
142
+ void AssertReturnedArguments ( List < object > arguments , params object [ ] expectedArgs )
132
143
{
133
- arguments . Length . ShouldBe ( expectedArgs . Length ) ;
144
+ arguments . Count . ShouldBe ( expectedArgs . Length ) ;
134
145
for ( int i = 0 ; i < expectedArgs . Length ; i ++ )
135
146
{
136
147
arguments [ i ] . ShouldBe ( expectedArgs [ i ] ) ;
@@ -140,7 +151,7 @@ void AssertReturnedArguments(object[] arguments, params object[] expectedArgs)
140
151
[ Fact ]
141
152
public void InputArgumentsPassedInline ( )
142
153
{
143
- var arguments = GetArguments ( x => x . MethodWithInputs ( 1 , "2" ) , new ClassUnderTest ( ) ) ;
154
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( 1 , "2" ) , new ClassUnderTest ( ) ) ;
144
155
AssertReturnedArguments ( arguments , 1 , "2" ) ;
145
156
}
146
157
@@ -149,41 +160,60 @@ public void InputArgumentsProvidedUsingVariables()
149
160
{
150
161
int input1 = 1 ;
151
162
const string input2 = "2" ;
152
- var arguments = GetArguments ( x => x . MethodWithInputs ( input1 , input2 ) , new ClassUnderTest ( ) ) ;
163
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( input1 , input2 ) , new ClassUnderTest ( ) ) ;
153
164
AssertReturnedArguments ( arguments , input1 , input2 ) ;
154
165
}
155
166
156
167
[ Fact ]
157
168
public void InputArgumentsProvidedUsingFields ( )
158
169
{
159
- var arguments = GetArguments ( x => x . MethodWithInputs ( _input1 , ConstInput2 ) , new ClassUnderTest ( ) ) ;
170
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( _input1 , ConstInput2 ) , new ClassUnderTest ( ) ) ;
160
171
AssertReturnedArguments ( arguments , _input1 , ConstInput2 ) ;
161
172
}
162
173
174
+ [ Fact ]
175
+ public void InputArgumentsProvidedWhenCastIsInvolved ( )
176
+ {
177
+ // For some reason default(decimal) will cause a different expression when passing to a nullable method than
178
+ // if we have input1 = 1m; No idea why...
179
+ var input1 = default ( decimal ) ;
180
+ var arguments = GetArguments ( x => x . MethodWithNullableArg ( input1 ) , new ClassUnderTest ( ) ) ;
181
+ input1 = 1 ;
182
+ AssertReturnedArguments ( arguments . Select ( a => a . Value ) . ToList ( ) , input1 ) ;
183
+ }
184
+
185
+ [ Fact ]
186
+ public void InputArgWithImplicitCast ( )
187
+ {
188
+ int input1 = 1 ;
189
+ var arguments = GetArgumentValues ( x => x . MethodWithNullableArg ( input1 ) , new ClassUnderTest ( ) ) ;
190
+ AssertReturnedArguments ( arguments , input1 ) ;
191
+ }
192
+
163
193
[ Fact ]
164
194
public void InputArgumentsProvidedUsingProperty ( )
165
195
{
166
- var arguments = GetArguments ( x => x . MethodWithInputs ( Input1 , Input2 ) , new ClassUnderTest ( ) ) ;
196
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( Input1 , Input2 ) , new ClassUnderTest ( ) ) ;
167
197
AssertReturnedArguments ( arguments , Input1 , Input2 ) ;
168
198
}
169
199
170
200
[ Fact ]
171
201
public void InputArgumentsProvidedUsingInheritedFields ( )
172
202
{
173
- var arguments = GetArguments ( x => x . MethodWithInputs ( InheritedInput1 , InheritedInput2 ) , new ClassUnderTest ( ) ) ;
203
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( InheritedInput1 , InheritedInput2 ) , new ClassUnderTest ( ) ) ;
174
204
AssertReturnedArguments ( arguments , InheritedInput1 , InheritedInput2 ) ;
175
205
}
176
206
177
207
[ Fact ]
178
208
public void InputArgumentsProvidedUsingMethodCallDoesNotThrow ( )
179
209
{
180
- Should . NotThrow ( ( ) => GetArguments ( x => x . MethodWithInputs ( GetInput1 ( 10 ) , GetInput2 ( "Test" ) ) , new ClassUnderTest ( ) ) ) ;
210
+ Should . NotThrow ( ( ) => GetArgumentValues ( x => x . MethodWithInputs ( GetInput1 ( 10 ) , GetInput2 ( "Test" ) ) , new ClassUnderTest ( ) ) ) ;
181
211
}
182
212
183
213
[ Fact ]
184
214
public void ArrayInputsArgumentsProvidedInline ( )
185
215
{
186
- var arguments = GetArguments ( x => x . MethodWithArrayInputs ( new [ ] { 1 , 2 } , new [ ] { "3" , "4" } ) , new ClassUnderTest ( ) ) ;
216
+ var arguments = GetArgumentValues ( x => x . MethodWithArrayInputs ( new [ ] { 1 , 2 } , new [ ] { "3" , "4" } ) , new ClassUnderTest ( ) ) ;
187
217
AssertReturnedArguments ( arguments , new [ ] { 1 , 2 } , new [ ] { "3" , "4" } ) ;
188
218
}
189
219
@@ -192,21 +222,21 @@ public void ArrayInputArgumentsProvidedUsingVariables()
192
222
{
193
223
var input1 = new [ ] { 1 , 2 } ;
194
224
var input2 = new [ ] { "3" , "4" } ;
195
- var arguments = GetArguments ( x => x . MethodWithArrayInputs ( input1 , input2 ) , new ClassUnderTest ( ) ) ;
225
+ var arguments = GetArgumentValues ( x => x . MethodWithArrayInputs ( input1 , input2 ) , new ClassUnderTest ( ) ) ;
196
226
AssertReturnedArguments ( arguments , input1 , input2 ) ;
197
227
}
198
228
199
229
[ Fact ]
200
230
public void ArrayInputArgumentsProvidedUsingFields ( )
201
231
{
202
- var arguments = GetArguments ( x => x . MethodWithArrayInputs ( _arrayInput1 , _arrayInput2 ) , new ClassUnderTest ( ) ) ;
232
+ var arguments = GetArgumentValues ( x => x . MethodWithArrayInputs ( _arrayInput1 , _arrayInput2 ) , new ClassUnderTest ( ) ) ;
203
233
AssertReturnedArguments ( arguments , _arrayInput1 , _arrayInput2 ) ;
204
234
}
205
235
206
236
[ Fact ]
207
237
public void ArrayInputArgumentsProvidedUsingProperty ( )
208
238
{
209
- var arguments = GetArguments ( x => x . MethodWithArrayInputs ( ArrayInput1 , ArrayInput2 ) , new ClassUnderTest ( ) ) ;
239
+ var arguments = GetArgumentValues ( x => x . MethodWithArrayInputs ( ArrayInput1 , ArrayInput2 ) , new ClassUnderTest ( ) ) ;
210
240
AssertReturnedArguments ( arguments , ArrayInput1 , ArrayInput2 ) ;
211
241
}
212
242
@@ -216,7 +246,7 @@ public void ComplexArgument()
216
246
container . Target = 1 ;
217
247
container . SubContainer = new ContainerType { Target2 = "Foo" } ;
218
248
219
- var arguments = GetArguments ( x => x . MethodWithInputs ( container . Target , container . SubContainer . Target2 ) , new ClassUnderTest ( ) ) ;
249
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( container . Target , container . SubContainer . Target2 ) , new ClassUnderTest ( ) ) ;
220
250
AssertReturnedArguments ( arguments , 1 , "Foo" ) ;
221
251
}
222
252
@@ -226,7 +256,7 @@ public void ComplexArgumentMethodCall()
226
256
container . Target = 1 ;
227
257
container . SubContainer = new ContainerType { Target2 = "Foo" } ;
228
258
229
- var arguments = GetArguments ( x => x . MethodWithInputs ( container . Target , container . SubContainer . ToString ( ) ) , new ClassUnderTest ( ) ) ;
259
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( container . Target , container . SubContainer . ToString ( ) ) , new ClassUnderTest ( ) ) ;
230
260
AssertReturnedArguments ( arguments , 1 , "Foo" ) ;
231
261
}
232
262
@@ -235,29 +265,29 @@ public void ComplexArgument2()
235
265
{
236
266
container . SubContainer = new ContainerType { Target2 = "Foo" } ;
237
267
238
- var arguments = GetArguments ( x => x . MethodWithInputs ( container . SubContainer ) , new ClassUnderTest ( ) ) ;
268
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( container . SubContainer ) , new ClassUnderTest ( ) ) ;
239
269
AssertReturnedArguments ( arguments , container . SubContainer ) ;
240
270
}
241
271
242
272
[ Fact ]
243
273
public void ComplexArgumentWhenContainerIsNull ( )
244
274
{
245
275
ContainerType nullContainer = null ;
246
- var arguments = GetArguments ( x => x . MethodWithInputs ( nullContainer . SubContainer ) , new ClassUnderTest ( ) ) ;
276
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( nullContainer . SubContainer ) , new ClassUnderTest ( ) ) ;
247
277
AssertReturnedArguments ( arguments , new object [ ] { null } ) ;
248
278
}
249
279
250
280
[ Fact ]
251
281
public void MethodCallValue ( )
252
282
{
253
- var arguments = GetArguments ( x => x . MethodWithInputs ( GetNumberThree ( ) , GetFooString ( ) ) , new ClassUnderTest ( ) ) ;
283
+ var arguments = GetArgumentValues ( x => x . MethodWithInputs ( GetNumberThree ( ) , GetFooString ( ) ) , new ClassUnderTest ( ) ) ;
254
284
AssertReturnedArguments ( arguments , new object [ ] { 3 , "Foo" } ) ;
255
285
}
256
286
257
287
[ Fact ]
258
288
public void DeepPropertyCall ( )
259
289
{
260
- var arguments = GetArguments ( x => x . Foo . Baz ( ) , new ClassUnderTest ( ) ) ;
290
+ var arguments = GetArgumentValues ( x => x . Foo . Baz ( ) , new ClassUnderTest ( ) ) ;
261
291
arguments . ShouldBeEmpty ( ) ;
262
292
}
263
293
@@ -274,14 +304,14 @@ private int GetNumberThree()
274
304
[ Fact ]
275
305
public void ArrayInputArgumentsProvidedUsingInheritedProperty ( )
276
306
{
277
- var arguments = GetArguments ( x => x . MethodWithArrayInputs ( InheritedArrayInput1 , InheritedArrayInput2 ) , new ClassUnderTest ( ) ) ;
307
+ var arguments = GetArgumentValues ( x => x . MethodWithArrayInputs ( InheritedArrayInput1 , InheritedArrayInput2 ) , new ClassUnderTest ( ) ) ;
278
308
AssertReturnedArguments ( arguments , InheritedArrayInput1 , InheritedArrayInput2 ) ;
279
309
}
280
310
281
311
[ Fact ]
282
312
public void StaticField ( )
283
313
{
284
- Should . NotThrow ( ( ) => GetArguments ( x => x . MethodWithInputs ( GetInput1 ( 10 ) , GetInput2 ( string . Empty ) ) , new ClassUnderTest ( ) ) ) ;
314
+ Should . NotThrow ( ( ) => GetArgumentValues ( x => x . MethodWithInputs ( GetInput1 ( 10 ) , GetInput2 ( string . Empty ) ) , new ClassUnderTest ( ) ) ) ;
285
315
}
286
316
}
287
317
}
0 commit comments