1- using System ;
1+ using System . Collections ;
22using System . Collections . Generic ;
33using System . Globalization ;
4- using NSubstitute . Exceptions ;
54using NUnit . Framework ;
65
76namespace NSubstitute . Acceptance . Specs ;
@@ -11,31 +10,126 @@ public class GenericArguments
1110{
1211 public interface ISomethingWithGenerics
1312 {
14- void Log < TState > ( int level , TState state ) ;
13+ void SomeAction < TState > ( int level , TState state ) ;
14+ string SomeFunction < TState > ( int level , TState state ) ;
15+ void SomeActionWithGenericConstraints < TState > ( int level , TState state ) where TState : IEnumerable < int > ;
16+ string SomeFunctionWithGenericConstraints < TState > ( int level , TState state ) where TState : IEnumerable < int > ;
17+ }
18+
19+ public abstract class MyAnyType : IEnumerable < int > , Arg . AnyType
20+ {
21+ public abstract IEnumerator < int > GetEnumerator ( ) ;
22+ IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
1523 }
1624
1725 [ Test ]
18- public void Return_result_for_any_argument ( )
26+ public void Any_matcher_works_with_AnyType ( )
27+ {
28+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
29+
30+ something . SomeAction ( 7 , 3409 ) ;
31+
32+ something . Received ( ) . SomeAction ( Arg . Any < int > ( ) , Arg . Any < Arg . AnyType > ( ) ) ;
33+ something . Received ( ) . SomeAction ( 7 , 3409 ) ;
34+ }
35+
36+ [ Test ]
37+ public void When_Do_works_with_AnyType ( )
1938 {
20- string argDoResult = null ;
2139 int ? whenDoResult = null ;
2240 bool whenDoCalled = false ;
2341 ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
24- something . Log ( Arg . Any < int > ( ) , Arg . Do < Arg . AnyType > ( a => argDoResult = ">>" + ( ( int ) a ) . ToString ( "P" , CultureInfo . InvariantCulture ) ) ) ;
2542 something
26- . When ( substitute => substitute . Log ( Arg . Any < int > ( ) , Arg . Any < Arg . AnyType > ( ) ) )
43+ . When ( substitute => substitute . SomeAction ( Arg . Any < int > ( ) , Arg . Any < Arg . AnyType > ( ) ) )
2744 . Do ( info =>
2845 {
2946 whenDoResult = info . ArgAt < int > ( 1 ) ;
3047 whenDoCalled = true ;
3148 } ) ;
3249
33- something . Log ( 7 , 3409 ) ;
50+ something . SomeAction ( 7 , 3409 ) ;
3451
35- something . Received ( ) . Log ( Arg . Any < int > ( ) , Arg . Any < Arg . AnyType > ( ) ) ;
36- something . Received ( ) . Log ( 7 , 3409 ) ;
3752 Assert . That ( whenDoCalled , Is . True ) ;
38- Assert . That ( argDoResult , Is . EqualTo ( ">>340,900.00 %" ) ) ;
3953 Assert . That ( whenDoResult , Is . EqualTo ( 3409 ) ) ;
4054 }
55+
56+ [ Test ]
57+ public void ArgDo_works_with_AnyType ( )
58+ {
59+ string argDoResult = null ;
60+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
61+ something . SomeAction ( Arg . Any < int > ( ) , Arg . Do < Arg . AnyType > ( a => argDoResult = ">>" + ( ( int ) a ) . ToString ( "P" , CultureInfo . InvariantCulture ) ) ) ;
62+
63+ something . SomeAction ( 7 , 3409 ) ;
64+
65+ Assert . That ( argDoResult , Is . EqualTo ( ">>340,900.00 %" ) ) ;
66+ }
67+
68+ [ Test ]
69+ public void Is_matcher_works_with_AnyType ( )
70+ {
71+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
72+
73+ something . SomeFunction ( Arg . Any < int > ( ) , Arg . Is < Arg . AnyType > ( a => ( int ) a == 3409 ) ) . Returns ( "matched" ) ;
74+
75+ var result = something . SomeFunction ( 7 , 3409 ) ;
76+
77+ Assert . That ( result , Is . EqualTo ( "matched" ) ) ;
78+ }
79+
80+ [ Test ]
81+ public void Any_matcher_works_with_AnyType_and_constraints ( )
82+ {
83+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
84+ var state = new [ ] { 3409 } ;
85+ something . SomeActionWithGenericConstraints ( 7 , state ) ;
86+
87+ something . Received ( ) . SomeActionWithGenericConstraints ( Arg . Any < int > ( ) , Arg . Any < MyAnyType > ( ) ) ;
88+ something . Received ( ) . SomeActionWithGenericConstraints ( 7 , state ) ;
89+ }
90+
91+ [ Test ]
92+ public void When_Do_works_with_AnyType_and_constraints ( )
93+ {
94+ int [ ] whenDoResult = null ;
95+ bool whenDoCalled = false ;
96+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
97+ something
98+ . When ( substitute => substitute . SomeActionWithGenericConstraints ( Arg . Any < int > ( ) , Arg . Any < MyAnyType > ( ) ) )
99+ . Do ( info =>
100+ {
101+ whenDoResult = info . ArgAt < int [ ] > ( 1 ) ;
102+ whenDoCalled = true ;
103+ } ) ;
104+
105+ var expected = new [ ] { 3409 } ;
106+ something . SomeActionWithGenericConstraints ( 7 , expected ) ;
107+
108+ Assert . That ( whenDoCalled , Is . True ) ;
109+ Assert . That ( whenDoResult , Is . EqualTo ( expected ) ) ;
110+ }
111+
112+ [ Test ]
113+ public void ArgDo_works_with_AnyType_and_constraints ( )
114+ {
115+ string argDoResult = null ;
116+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
117+ something . SomeActionWithGenericConstraints ( Arg . Any < int > ( ) , Arg . Do < MyAnyType > ( a => argDoResult = ">>" + ( ( int [ ] ) a ) [ 0 ] . ToString ( "P" , CultureInfo . InvariantCulture ) ) ) ;
118+
119+ something . SomeActionWithGenericConstraints ( 7 , new [ ] { 3409 } ) ;
120+
121+ Assert . That ( argDoResult , Is . EqualTo ( ">>340,900.00 %" ) ) ;
122+ }
123+
124+ [ Test ]
125+ public void Is_matcher_works_with_AnyType_and_constraints ( )
126+ {
127+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
128+
129+ something . SomeFunctionWithGenericConstraints ( Arg . Any < int > ( ) , Arg . Is < MyAnyType > ( a => ( ( int [ ] ) a ) [ 0 ] == 3409 ) ) . Returns ( "matched" ) ;
130+
131+ var result = something . SomeFunctionWithGenericConstraints ( 7 , new [ ] { 3409 } ) ;
132+
133+ Assert . That ( result , Is . EqualTo ( "matched" ) ) ;
134+ }
41135}
0 commit comments