11namespace TestStack . ConventionTests . ConventionData
22{
33 using System ;
4+ using System . Collections ;
45 using System . Collections . Generic ;
6+ using System . ComponentModel ;
57 using System . Linq ;
6- using System . Runtime . CompilerServices ;
8+ using System . Reflection ;
79
810 /// <summary>
911 /// This is where we set what our convention is all about.
1012 /// </summary>
11- public class Types : IConventionData
13+ public class Types : IConventionData , IEnumerable < Type >
1214 {
13- public Types ( string descriptionOfTypes )
15+ public Types ( string descriptionOfTypes ) : this ( Enumerable . Empty < Type > ( ) , descriptionOfTypes )
1416 {
17+ }
18+
19+ public Types ( IEnumerable < Type > types , string descriptionOfTypes )
20+ {
21+ TypesToVerify = types . ToArray ( ) ;
1522 Description = descriptionOfTypes ;
1623 }
1724
18- public Type [ ] TypesToVerify { get ; set ; }
25+ public IEnumerable < Type > TypesToVerify { get ; private set ; }
1926
2027 public string Description { get ; private set ; }
2128
22- public bool HasData { get { return TypesToVerify . Any ( ) ; } }
29+ public bool HasData
30+ {
31+ get { return TypesToVerify . Any ( ) ; }
32+ }
33+
34+ /// <summary>
35+ /// Gets a filtered list of types from the assembly of the specified
36+ /// type, <typeparam name="T" />, using the specified <param name="predicate" />.
37+ /// </summary>
38+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
39+ /// <param name="predicate">A function to test each type for a condition.</param>
40+ public static Types InAssemblyOf < T > ( Func < Type , bool > predicate )
41+ {
42+ return InAssemblyOf ( typeof ( T ) , predicate ) ;
43+ }
44+
45+ /// <summary>
46+ /// Gets a filtered list of types from the assembly of the specified
47+ /// type, <param name="type" />, using the specified <param name="predicate" />.
48+ /// </summary>
49+ /// <param name="type">A type residing in the assembly to get types from.</param>
50+ /// <param name="predicate">A function to test each type for a condition.</param>
51+ public static Types InAssemblyOf ( Type type , Func < Type , bool > predicate )
52+ {
53+ return InAssembly ( type . Assembly , predicate ) ;
54+ }
55+
56+ /// <summary>
57+ /// Gets a filtered list of types from the specified <param name="assembly" /> using the specified <param name="predicate" />.
58+ /// </summary>
59+ /// <param name="assembly">The assembly to get types from.</param>
60+ /// <param name="predicate">A function to test each type for a condition.</param>
61+ public static Types InAssembly ( Assembly assembly , Func < Type , bool > predicate )
62+ {
63+ return InAssembly ( assembly , GetAssemblyName ( assembly ) , predicate ) ;
64+ }
65+
66+ /// <summary>
67+ /// Gets a filtered list of types from the assembly of the specified
68+ /// type, <typeparam name="T" />, using the specified <param name="predicate" />.
69+ /// </summary>
70+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
71+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
72+ /// <param name="predicate">A function to test each type for a condition.</param>
73+ public static Types InAssemblyOf < T > ( string descriptionOfTypes , Func < Type , bool > predicate )
74+ {
75+ return InAssemblyOf ( typeof ( T ) , descriptionOfTypes , predicate ) ;
76+ }
77+
78+ /// <summary>
79+ /// Gets a filtered list of types from the assembly of the specified
80+ /// type, <param name="type" />, using the specified <param name="predicate" />.
81+ /// </summary>
82+ /// <param name="type">A type residing in the assembly to get types from.</param>
83+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
84+ /// <param name="predicate">A function to test each type for a condition.</param>
85+ public static Types InAssemblyOf ( Type type , string descriptionOfTypes , Func < Type , bool > predicate )
86+ {
87+ return InAssembly ( type . Assembly , descriptionOfTypes , predicate ) ;
88+ }
89+
90+ /// <summary>
91+ /// Gets a filtered list of types from the specified <param name="assembly" /> using the specified <param name="predicate" />.
92+ /// </summary>
93+ /// <param name="assembly">The assembly to get types from.</param>
94+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
95+ /// <param name="predicate">A function to test each type for a condition.</param>
96+ public static Types InAssembly ( Assembly assembly , string descriptionOfTypes , Func < Type , bool > predicate )
97+ {
98+ return InAssemblies ( new [ ] { assembly } , descriptionOfTypes , predicate ) ;
99+ }
23100
101+ /// <summary>
102+ /// Gets an optionally filtered list of types from the specified <param name="assemblies" /> using the specified <param name="predicate" />.
103+ /// </summary>
104+ /// <param name="assemblies">A list of assemblies to get types from.</param>
105+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
106+ /// <param name="predicate">A function to test each type for a condition.</param>
107+ public static Types InAssemblies ( IEnumerable < Assembly > assemblies , string descriptionOfTypes , Func < Type , bool > predicate )
108+ {
109+ return InCollection ( assemblies . SelectMany ( x => x . GetTypes ( ) ) . Where ( predicate ) , descriptionOfTypes ) ;
110+ }
111+
112+ /// <summary>
113+ /// Gets an optionally filtered list of types from the assembly of the specified type, <typeparam name="T" />.
114+ /// </summary>
115+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
116+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
24117 public static Types InAssemblyOf < T > ( bool excludeCompilerGeneratedTypes = true )
25118 {
26- var assembly = typeof ( T ) . Assembly ;
27- var typesToVerify = assembly . GetTypes ( ) ;
28- if ( excludeCompilerGeneratedTypes )
29- {
30- typesToVerify = typesToVerify
31- . Where ( t => ! t . GetCustomAttributes ( typeof ( CompilerGeneratedAttribute ) , true ) . Any ( ) )
32- . ToArray ( ) ;
33- }
34- return new Types ( assembly . GetName ( ) . Name )
35- {
36- TypesToVerify = typesToVerify
37- } ;
119+ return InAssemblyOf ( typeof ( T ) , excludeCompilerGeneratedTypes ) ;
120+ }
121+
122+ /// <summary>
123+ /// Gets an optionally filtered list of types from the assembly of the specified type, <param name="type" />.
124+ /// </summary>
125+ /// <param name="type">A type residing in the assembly to get types from.</param>
126+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
127+ public static Types InAssemblyOf ( Type type , bool excludeCompilerGeneratedTypes = true )
128+ {
129+ return InAssembly ( type . Assembly , excludeCompilerGeneratedTypes ) ;
130+ }
131+
132+ /// <summary>
133+ /// Gets an optionally filtered list of types from the specified <param name="assembly" />.
134+ /// </summary>
135+ /// <param name="assembly">The assembly to get types from.</param>
136+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
137+ public static Types InAssembly ( Assembly assembly , bool excludeCompilerGeneratedTypes = true )
138+ {
139+ return InAssembly ( assembly , GetAssemblyName ( assembly ) , excludeCompilerGeneratedTypes ) ;
38140 }
39141
142+ /// <summary>
143+ /// Gets an optionally filtered list of types from the assembly of the specified
144+ /// type, <typeparam name="T" />, using the specified <param name="types" /> filter.
145+ /// </summary>
146+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
147+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
148+ /// <param name="types">A function to filter or add matched types.</param>
149+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
150+ [ Obsolete ( "This method is obsolete and should not be used. Use the overload with a predicate instead." ) ]
40151 public static Types InAssemblyOf < T > ( string descriptionOfTypes , Func < IEnumerable < Type > , IEnumerable < Type > > types )
41152 {
42- var assembly = typeof ( T ) . Assembly ;
43- return new Types ( descriptionOfTypes )
44- {
45- TypesToVerify = types ( assembly . GetTypes ( ) ) . ToArray ( )
46- } ;
153+ return InCollection ( types ( typeof ( T ) . Assembly . GetTypes ( ) ) , descriptionOfTypes ) ;
154+ }
155+
156+ /// <summary>
157+ /// Gets an optionally filtered list of types from the assembly of the specified type, <typeparam name="T" />.
158+ /// </summary>
159+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
160+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
161+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
162+ public static Types InAssemblyOf < T > ( string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
163+ {
164+ return InAssemblyOf ( typeof ( T ) , descriptionOfTypes , excludeCompilerGeneratedTypes ) ;
165+ }
166+
167+ /// <summary>
168+ /// Gets an optionally filtered list of types from the assembly of the specified type, <param name="type" />.
169+ /// </summary>
170+ /// <param name="type">A type residing in the assembly to get types from.</param>
171+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
172+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
173+ public static Types InAssemblyOf ( Type type , string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
174+ {
175+ return InAssembly ( type . Assembly , descriptionOfTypes , excludeCompilerGeneratedTypes ) ;
176+ }
177+
178+ /// <summary>
179+ /// Gets an optionally filtered list of types from the specified <param name="assembly" />.
180+ /// </summary>
181+ /// <param name="assembly">The assembly to get types from.</param>
182+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
183+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
184+ public static Types InAssembly ( Assembly assembly , string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
185+ {
186+ return InAssemblies ( new [ ] { assembly } , descriptionOfTypes , excludeCompilerGeneratedTypes ) ;
187+ }
188+
189+ /// <summary>
190+ /// Gets an optionally filtered list of types from the specified <param name="assemblies" />.
191+ /// </summary>
192+ /// <param name="assemblies">A list of assemblies to get types from.</param>
193+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
194+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
195+ public static Types InAssemblies ( IEnumerable < Assembly > assemblies , string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
196+ {
197+ return InAssemblies ( assemblies , descriptionOfTypes , type => ! ( excludeCompilerGeneratedTypes && type . IsCompilerGenerated ( ) ) ) ;
198+ }
199+
200+ /// <summary>
201+ /// Gets a list of types from the specified <param name="types" /> collection.
202+ /// </summary>
203+ /// <param name="types">The types.</param>
204+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
205+ public static Types InCollection ( IEnumerable < Type > types , string descriptionOfTypes )
206+ {
207+ return new Types ( types , descriptionOfTypes ) ;
208+ }
209+
210+ private static string GetAssemblyName ( Assembly assembly )
211+ {
212+ return assembly . GetName ( ) . Name ;
213+ }
214+
215+ public IEnumerator < Type > GetEnumerator ( )
216+ {
217+ return TypesToVerify . GetEnumerator ( ) ;
218+ }
219+
220+ IEnumerator IEnumerable . GetEnumerator ( )
221+ {
222+ return GetEnumerator ( ) ;
47223 }
48224 }
49225}
0 commit comments