1+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
2+
3+ using MSTest . Extensions . Core ;
4+ using MSTest . Extensions . Utils ;
5+
16using System ;
27using System . Collections . Generic ;
38using System . Diagnostics . CodeAnalysis ;
49using System . Diagnostics . Contracts ;
510using System . Linq ;
611using System . Reflection ;
12+ using System . Security . Cryptography ;
713using System . Threading . Tasks ;
8- using Microsoft . VisualStudio . TestTools . UnitTesting ;
9- using MSTest . Extensions . Core ;
10- using MSTest . Extensions . Utils ;
1114
1215// ## How it works?
1316//
@@ -80,12 +83,9 @@ public IEnumerable<object[]> GetData([NotNull] MethodInfo methodInfo)
8083 Contract . EndContractBlock ( ) ;
8184
8285 // Collect all test cases from the target unit test method.
83- Collect ( methodInfo ) ;
84-
85- var cases = ContractTest . Method [ methodInfo ] ;
86- VerifyContracts ( cases ) ;
86+ var cases = Collect ( methodInfo ) ;
8787
88- return Enumerable . Range ( 0 , cases . Count ) . Select ( x => ( object [ ] ) null ) ;
88+ return cases . Select ( t => new object [ ] { t } ) ;
8989 }
9090
9191 /// <summary>
@@ -96,9 +96,13 @@ public IEnumerable<object[]> GetData([NotNull] MethodInfo methodInfo)
9696 /// <param name="data">The parameter list which was returned by <see cref="GetData"/>.</param>
9797 /// <returns>The display name of this test case.</returns>
9898 [ NotNull ]
99- public string GetDisplayName ( [ NotNull ] MethodInfo methodInfo , [ NotNull ] object [ ] data )
99+ public string GetDisplayName ( [ NotNull ] MethodInfo methodInfo , [ CanBeNull ] object [ ] data )
100100 {
101- return ContractTest . Method [ methodInfo ] [ _testCaseIndex ++ ] . DisplayName ;
101+ Contract . Requires ( methodInfo != null , "The method must not be null." ) ;
102+
103+ return data is not null && data . Length > 0 && data [ 0 ] is ITestCase testCase
104+ ? testCase . DisplayName
105+ : methodInfo . Name ;
102106 }
103107
104108 #endregion
@@ -107,14 +111,18 @@ public string GetDisplayName([NotNull] MethodInfo methodInfo, [NotNull] object[]
107111
108112 [ SuppressMessage ( "ReSharper" , "AssignNullToNotNullAttribute" ) ]
109113 [ SuppressMessage ( "ReSharper" , "PossibleNullReferenceException" ) ]
110- private static void Collect ( [ NotNull ] MethodInfo methodInfo )
114+ [ NotNull ]
115+ private static IReadOnlyList < ITestCase > Collect ( [ NotNull ] MethodInfo methodInfo )
111116 {
112117 var type = methodInfo . DeclaringType ;
113118 Contract . Requires ( type != null ,
114119 "The method must be declared in a type. If this exception happened, there might be a bug in MSTest v2." ) ;
115120
116121 var testInstance = Activator . CreateInstance ( type ) ;
117- var testCaseList = ContractTest . Method [ methodInfo ] ;
122+
123+
124+ var testCaseList = new List < ITestCase > ( ) ;
125+ ContractTest . Method . SetCurrentCollection ( methodInfo , testCaseList ) ;
118126 try
119127 {
120128 // Invoke target test method to collect all test cases.
@@ -155,13 +163,16 @@ Try to call Test extension method to collect one.
155163
156164If you only need to write a normal test method, use `TestMethodAttribute` instead of `ContractTestCaseAttribute`." ) ) ;
157165 }
166+
167+ VerifyContracts ( testCaseList ) ;
168+ return testCaseList ;
158169 }
159170
160171 /// <summary>
161172 /// Find out the test cases which have the same contract string, add a special exception to it.
162173 /// </summary>
163174 /// <param name="cases">The test cases of a single test method.</param>
164- private void VerifyContracts ( [ NotNull ] IList < ITestCase > cases )
175+ private static void VerifyContracts ( [ NotNull ] List < ITestCase > cases )
165176 {
166177 var caseContractSet = new HashSet < string > ( ) ;
167178 var duplicatedCases = new HashSet < ITestCase > ( ) ;
@@ -200,13 +211,6 @@ 1. Please check whether you have created two test cases which have the same cont
200211
201212 #endregion
202213
203- /// <summary>
204- /// Gets or increment the current test case index.
205- /// <see cref="Execute"/> and <see cref="GetDisplayName"/> should increment it separately
206- /// because they are not in the same instance.
207- /// </summary>
208- private int _testCaseIndex ;
209-
210214 /// <summary>
211215 /// the proxy of ITestMethod(TestMethodInfo in fact)
212216 /// overwrite the invoke method
0 commit comments