@@ -63,6 +63,18 @@ private static void GatherTests()
6363 _suites . Add ( type ) ;
6464 }
6565
66+ private static bool TryGetAttribute < T > ( MemberInfo memberInfo , out T attribute ) where T : Attribute
67+ {
68+ attribute = memberInfo . GetCustomAttribute < T > ( ) ;
69+ return attribute != null ;
70+ }
71+
72+ private static bool TryGetAttributes < T > ( MemberInfo memberInfo , out List < T > attributes ) where T : Attribute
73+ {
74+ attributes = memberInfo . GetCustomAttributes < T > ( ) . ToList ( ) ;
75+ return attributes . Count > 0 ;
76+ }
77+
6678 public static void RunTests ( )
6779 {
6880 GatherTests ( ) ;
@@ -71,7 +83,6 @@ public static void RunTests()
7183 {
7284 var suiteMethods = suite . GetMethods ( ) ;
7385
74- var tests = suiteMethods . Where ( m => m . GetCustomAttributes < Test > ( ) . Count ( ) > 0 || m . GetCustomAttributes < TestCase > ( ) . Count ( ) > 0 ) . ToArray ( ) ;
7586 var setup = suiteMethods . Where ( m => m . GetCustomAttributes < OneTimeSetUp > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
7687 var disposer = suiteMethods . Where ( m => m . GetCustomAttributes < OneTimeTearDown > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
7788 var beforeEach = suiteMethods . Where ( m => m . GetCustomAttributes < SetUp > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
@@ -81,9 +92,12 @@ public static void RunTests()
8192
8293 setup ? . Invoke ( instance , null ) ;
8394
84- foreach ( var testMethod in tests )
95+ foreach ( var testMethod in suiteMethods )
8596 {
86- if ( testMethod . GetCustomAttributes < Test > ( ) . Count ( ) > 0 )
97+ bool hasTestCases = TryGetAttributes ( testMethod , out List < TestCase > testCasesAttribute ) ;
98+ bool hasTestCaseSource = TryGetAttribute ( testMethod , out TestCaseSource testCaseSourceAttribute ) ;
99+
100+ if ( TryGetAttribute ( testMethod , out Test testAttribute ) )
87101 {
88102 bool failed = false ;
89103 beforeEach ? . Invoke ( instance , null ) ;
@@ -102,20 +116,45 @@ public static void RunTests()
102116 finally
103117 {
104118 afterEach ? . Invoke ( instance , null ) ;
105- string message = $ "Test '{ suite . Name } { testMethod . Name } ' finished with " + ( failed ? "Error" : "Success" ) ;
106- if ( failed )
119+ OutputResults ( suite , testMethod , failed ) ;
120+ }
121+ }
122+ else if ( hasTestCases || hasTestCaseSource )
123+ {
124+ IEnumerable < TestCaseData > testCases = Enumerable . Empty < TestCaseData > ( ) ;
125+
126+ if ( hasTestCases )
127+ {
128+ testCases = testCases . Concat ( testCasesAttribute . Select ( a => a . TestCaseData ) ) ;
129+ }
130+ if ( hasTestCaseSource )
131+ {
132+ var sourceClassType = testCaseSourceAttribute . SourceClassType ?? suite ;
133+ object sourceObjectInstance = null ;
134+ if ( sourceClassType == suite || sourceClassType == null )
107135 {
108- Debug . LogError ( message ) ;
136+ sourceObjectInstance = instance ;
109137 }
110- else
138+ else if ( ! sourceClassType . IsAbstract )
111139 {
112- Debug . Log ( message ) ;
140+ sourceObjectInstance = Activator . CreateInstance ( sourceClassType ) ;
113141 }
142+
143+ var sourceTestCases = ( IEnumerable < TestCaseData > ) (
144+ sourceClassType
145+ . GetProperty ( testCaseSourceAttribute . MemberName , typeof ( IEnumerable < TestCaseData > ) )
146+ ? . GetValue ( sourceObjectInstance ) ??
147+ sourceClassType
148+ . GetField ( testCaseSourceAttribute . MemberName )
149+ ? . GetValue ( sourceObjectInstance ) ??
150+ sourceClassType
151+ . GetMethod ( testCaseSourceAttribute . MemberName )
152+ ? . Invoke ( sourceObjectInstance , null ) ) ;
153+
154+ testCases = testCases . Concat ( sourceTestCases ) ;
114155 }
115- }
116- else
117- {
118- var testCases = testMethod . GetCustomAttributes < TestCase > ( ) ;
156+
157+ int testCaseCount = 0 ;
119158 int successCount = 0 ;
120159 foreach ( var testCase in testCases )
121160 {
@@ -137,27 +176,46 @@ public static void RunTests()
137176 finally
138177 {
139178 afterEach ? . Invoke ( instance , null ) ;
179+ testCaseCount ++ ;
140180
141181 if ( ! failed )
142182 successCount ++ ;
143183 }
144184 }
145185
146- int testCount = testCases . Count ( ) ;
147- string message = $ "Test '{ suite . Name } { testMethod . Name } ' finished with { successCount } /{ testCount } successfull test cases.";
148- if ( successCount < testCount )
149- {
150- Debug . LogError ( message ) ;
151- }
152- else
153- {
154- Debug . Log ( message ) ;
155- }
186+ OutputResults ( suite , testMethod , successCount , testCaseCount ) ;
187+
156188 }
157189 }
158190
159191 disposer ? . Invoke ( instance , null ) ;
160192 }
161193 }
194+
195+ private static void OutputResults ( Type suite , MethodInfo testMethod , int successCount , int testCount )
196+ {
197+ string message = $ "Test '{ suite . Name } { testMethod . Name } ' finished with { successCount } /{ testCount } successfull test cases.";
198+ if ( successCount < testCount )
199+ {
200+ Debug . LogError ( message ) ;
201+ }
202+ else
203+ {
204+ Debug . Log ( message ) ;
205+ }
206+ }
207+
208+ private static void OutputResults ( Type suite , MethodInfo testMethod , bool failed )
209+ {
210+ string message = $ "Test '{ suite . Name } { testMethod . Name } ' finished with " + ( failed ? "Error" : "Success" ) ;
211+ if ( failed )
212+ {
213+ Debug . LogError ( message ) ;
214+ }
215+ else
216+ {
217+ Debug . Log ( message ) ;
218+ }
219+ }
162220 }
163221}
0 commit comments