@@ -920,16 +920,26 @@ public AndConstraint<TAssertions> ContainInOrder(IEnumerable<T> expected, string
920
920
IList < T > expectedItems = expected . ConvertOrCastToList ( ) ;
921
921
IList < T > actualItems = Subject . ConvertOrCastToList ( ) ;
922
922
923
+ int subjectIndex = 0 ;
924
+
923
925
Func < T , T , bool > areSameOrEqual = ObjectExtensions . GetComparer < T > ( ) ;
924
926
for ( int index = 0 ; index < expectedItems . Count ; index ++ )
925
927
{
926
928
T expectedItem = expectedItems [ index ] ;
927
- actualItems = actualItems . SkipWhile ( actualItem => ! areSameOrEqual ( actualItem , expectedItem ) ) . ToArray ( ) ;
928
- if ( actualItems . Any ( ) )
929
+
930
+ bool foundExpectedItem = false ;
931
+ for ( ; subjectIndex < actualItems . Count ; subjectIndex ++ )
929
932
{
930
- actualItems = actualItems . Skip ( 1 ) . ToArray ( ) ;
933
+ T actualItem = actualItems [ subjectIndex ] ;
934
+ if ( areSameOrEqual ( actualItem , expectedItem ) )
935
+ {
936
+ foundExpectedItem = true ;
937
+ subjectIndex ++ ;
938
+ break ;
939
+ }
931
940
}
932
- else
941
+
942
+ if ( ! foundExpectedItem )
933
943
{
934
944
Execute . Assertion
935
945
. BecauseOf ( because , becauseArgs )
@@ -2261,43 +2271,38 @@ public AndConstraint<TAssertions> NotContainInOrder(IEnumerable<T> unexpected, s
2261
2271
}
2262
2272
2263
2273
IList < T > unexpectedItems = unexpected . ConvertOrCastToList ( ) ;
2264
- IList < T > actualItems = Subject . ConvertOrCastToList ( ) ;
2265
-
2266
- if ( unexpectedItems . Count > actualItems . Count )
2274
+ if ( unexpectedItems . Any ( ) )
2267
2275
{
2268
- return new AndConstraint < TAssertions > ( ( TAssertions ) this ) ;
2269
- }
2270
-
2271
- var actualItemsSkipped = 0 ;
2272
- Func < T , T , bool > areSameOrEqual = ObjectExtensions . GetComparer < T > ( ) ;
2273
- for ( int index = 0 ; index < unexpectedItems . Count ; index ++ )
2274
- {
2275
- T unexpectedItem = unexpectedItems [ index ] ;
2276
-
2277
- actualItems = actualItems . SkipWhile ( actualItem =>
2278
- {
2279
- actualItemsSkipped ++ ;
2280
- return ! areSameOrEqual ( actualItem , unexpectedItem ) ;
2281
- } ) . ToArray ( ) ;
2276
+ IList < T > actualItems = Subject . ConvertOrCastToList ( ) ;
2277
+ int subjectIndex = 0 ;
2282
2278
2283
- if ( actualItems . Any ( ) )
2279
+ Func < T , T , bool > areSameOrEqual = ObjectExtensions . GetComparer < T > ( ) ;
2280
+ foreach ( var unexpectedItem in unexpectedItems )
2284
2281
{
2285
- if ( index == unexpectedItems . Count - 1 )
2282
+ bool foundExpectedItem = false ;
2283
+ for ( ; subjectIndex < actualItems . Count ; subjectIndex ++ )
2286
2284
{
2287
- Execute . Assertion
2288
- . BecauseOf ( because , becauseArgs )
2289
- . FailWith (
2290
- "Expected {context:collection} {0} to not contain items {1} in order{reason}, " +
2291
- "but items appeared in order ending at index {2}." ,
2292
- Subject , unexpected , actualItemsSkipped - 1 ) ;
2285
+ T actualItem = actualItems [ subjectIndex ] ;
2286
+ if ( areSameOrEqual ( actualItem , unexpectedItem ) )
2287
+ {
2288
+ foundExpectedItem = true ;
2289
+ subjectIndex ++ ;
2290
+ break ;
2291
+ }
2293
2292
}
2294
2293
2295
- actualItems = actualItems . Skip ( 1 ) . ToArray ( ) ;
2296
- }
2297
- else
2298
- {
2299
- return new AndConstraint < TAssertions > ( ( TAssertions ) this ) ;
2294
+ if ( ! foundExpectedItem )
2295
+ {
2296
+ return new AndConstraint < TAssertions > ( ( TAssertions ) this ) ;
2297
+ }
2300
2298
}
2299
+
2300
+ Execute . Assertion
2301
+ . BecauseOf ( because , becauseArgs )
2302
+ . FailWith (
2303
+ "Expected {context:collection} {0} to not contain items {1} in order{reason}, " +
2304
+ "but items appeared in order ending at index {2}." ,
2305
+ Subject , unexpected , subjectIndex - 1 ) ;
2301
2306
}
2302
2307
2303
2308
return new AndConstraint < TAssertions > ( ( TAssertions ) this ) ;
0 commit comments