PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/AutoFixture/SpecimenFactory.cs

http://autofixture.codeplex.com
C# | 295 lines | 130 code | 27 blank | 138 comment | 20 complexity | 0eac10ef3748df262049cde5a5e4239c MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Ploeh.AutoFixture.Dsl;
  5. using Ploeh.AutoFixture.Kernel;
  6. namespace Ploeh.AutoFixture
  7. {
  8. /// <summary>
  9. /// Creates anonymous variables from <see cref="ISpecimenContext"/> or
  10. /// <see cref="ISpecimenBuilderComposer"/> instances.
  11. /// </summary>
  12. public static class SpecimenFactory
  13. {
  14. /// <summary>
  15. /// Creates an anonymous variable of the requested type.
  16. /// </summary>
  17. /// <typeparam name="T">The type of object to create.</typeparam>
  18. /// <param name="context">The context used to resolve the type request.</param>
  19. /// <returns>An anonymous object of type <typeparamref name="T"/>.</returns>
  20. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Although this CA warning should never be suppressed, this particular usage scenario has been discussed and accepted on the FxCop DL.")]
  21. public static T CreateAnonymous<T>(this ISpecimenContext context)
  22. {
  23. if (context == null)
  24. {
  25. throw new ArgumentNullException("context");
  26. }
  27. return (T)context.CreateAnonymous(default(T));
  28. }
  29. /// <summary>
  30. /// Creates an anonymous variable of the requested type.
  31. /// </summary>
  32. /// <typeparam name="T">The type of object to create.</typeparam>
  33. /// <param name="composer">The composer used to resolve the type request.</param>
  34. /// <returns>An anonymous object of type <typeparamref name="T"/>.</returns>
  35. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Although this CA warning should never be suppressed, this particular usage scenario has been discussed and accepted on the FxCop DL.")]
  36. public static T CreateAnonymous<T>(this ISpecimenBuilderComposer composer)
  37. {
  38. if (composer == null)
  39. {
  40. throw new ArgumentNullException("composer");
  41. }
  42. return composer.Compose().CreateContext().CreateAnonymous<T>();
  43. }
  44. /// <summary>
  45. /// Creates an anonymous variable of the requested type.
  46. /// </summary>
  47. /// <typeparam name="T">The type of object to create.</typeparam>
  48. /// <param name="composer">The composer used to resolve the type request.</param>
  49. /// <returns>An anonymous object of type <typeparamref name="T"/>.</returns>
  50. /// <remarks>
  51. /// <para>
  52. /// The only purpose of this explicit overload is to support type inferencing.
  53. /// </para>
  54. /// </remarks>
  55. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Supports type inferencing.")]
  56. public static T CreateAnonymous<T>(this IPostprocessComposer<T> composer)
  57. {
  58. return ((ISpecimenBuilderComposer)composer).CreateAnonymous<T>();
  59. }
  60. /// <summary>
  61. /// Creates an anonymous object, potentially using the supplied seed as additional
  62. /// information when creating the object.
  63. /// </summary>
  64. /// <typeparam name="T">The type of object to create.</typeparam>
  65. /// <param name="seed">
  66. /// Any data that adds additional information when creating the anonymous object.
  67. /// </param>
  68. /// <param name="context">The context used to resolve the type request.</param>
  69. /// <returns>An anonymous object.</returns>
  70. public static T CreateAnonymous<T>(this ISpecimenContext context, T seed)
  71. {
  72. if (context == null)
  73. {
  74. throw new ArgumentNullException("context");
  75. }
  76. return (T)context.Resolve(new SeededRequest(typeof(T), seed));
  77. }
  78. /// <summary>
  79. /// Creates an anonymous object, potentially using the supplied seed as additional
  80. /// information when creating the object.
  81. /// </summary>
  82. /// <typeparam name="T">The type of object to create.</typeparam>
  83. /// <param name="seed">
  84. /// Any data that adds additional information when creating the anonymous object.
  85. /// </param>
  86. /// <param name="composer">The composer used to resolve the type request.</param>
  87. /// <returns>An anonymous object.</returns>
  88. public static T CreateAnonymous<T>(this ISpecimenBuilderComposer composer, T seed)
  89. {
  90. if (composer == null)
  91. {
  92. throw new ArgumentNullException("composer");
  93. }
  94. return composer.Compose().CreateContext().CreateAnonymous(seed);
  95. }
  96. /// <summary>
  97. /// Creates many anonymous objects.
  98. /// </summary>
  99. /// <typeparam name="T">The type of objects to create.</typeparam>
  100. /// <param name="context">The context used to resolve the type request.</param>
  101. /// <returns>A sequence of anonymous object of type <typeparamref name="T"/>.</returns>
  102. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Although this CA warning should never be suppressed, this particular usage scenario has been discussed and accepted on the FxCop DL.")]
  103. public static IEnumerable<T> CreateMany<T>(this ISpecimenContext context)
  104. {
  105. return context.CreateMany(default(T));
  106. }
  107. /// <summary>
  108. /// Creates many anonymous objects.
  109. /// </summary>
  110. /// <typeparam name="T">The type of objects to create.</typeparam>
  111. /// <param name="composer">The composer used to resolve the type request.</param>
  112. /// <returns>A sequence of anonymous object of type <typeparamref name="T"/>.</returns>
  113. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Although this CA warning should never be suppressed, this particular usage scenario has been discussed and accepted on the FxCop DL.")]
  114. public static IEnumerable<T> CreateMany<T>(this ISpecimenBuilderComposer composer)
  115. {
  116. if (composer == null)
  117. {
  118. throw new ArgumentNullException("composer");
  119. }
  120. return composer.Compose().CreateContext().CreateMany<T>();
  121. }
  122. /// <summary>
  123. /// Creates many anonymous objects.
  124. /// </summary>
  125. /// <typeparam name="T">The type of objects to create.</typeparam>
  126. /// <param name="composer">The composer used to resolve the type request.</param>
  127. /// <returns>A sequence of anonymous object of type <typeparamref name="T"/>.</returns>
  128. /// <remarks>
  129. /// <para>
  130. /// The only purpose of this explicit overload is to support type inferencing.
  131. /// </para>
  132. /// </remarks>
  133. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Supports type inferencing.")]
  134. public static IEnumerable<T> CreateMany<T>(this IPostprocessComposer<T> composer)
  135. {
  136. return ((ISpecimenBuilderComposer)composer).CreateMany<T>();
  137. }
  138. /// <summary>
  139. /// Creates many anonymous objects.
  140. /// </summary>
  141. /// <typeparam name="T">The type of objects to create.</typeparam>
  142. /// <param name="context">The context used to resolve the type request.</param>
  143. /// <param name="seed">
  144. /// An initial value that may or may not be used as input for the algorithm creating the
  145. /// return value.
  146. /// </param>
  147. /// <returns>A sequence of anonymous object of type <typeparamref name="T"/>.</returns>
  148. public static IEnumerable<T> CreateMany<T>(this ISpecimenContext context, T seed)
  149. {
  150. if (context == null)
  151. {
  152. throw new ArgumentNullException("context");
  153. }
  154. return from s in (IEnumerable<object>)context.Resolve(new MultipleRequest(new SeededRequest(typeof(T), seed)))
  155. select (T)s;
  156. }
  157. /// <summary>
  158. /// Creates many anonymous objects.
  159. /// </summary>
  160. /// <typeparam name="T">The type of objects to create.</typeparam>
  161. /// <param name="composer">The composer used to resolve the type request.</param>
  162. /// <param name="seed">
  163. /// An initial value that may or may not be used as input for the algorithm creating the
  164. /// return value.
  165. /// </param>
  166. /// <returns>A sequence of anonymous object of type <typeparamref name="T"/>.</returns>
  167. public static IEnumerable<T> CreateMany<T>(this ISpecimenBuilderComposer composer, T seed)
  168. {
  169. if (composer == null)
  170. {
  171. throw new ArgumentNullException("composer");
  172. }
  173. return composer.Compose().CreateContext().CreateMany(seed);
  174. }
  175. /// <summary>
  176. /// Creates many anonymous objects.
  177. /// </summary>
  178. /// <typeparam name="T">The type of objects to create.</typeparam>
  179. /// <param name="context">The context used to resolve the type request.</param>
  180. /// <param name="count">The number of objects to create.</param>
  181. /// <returns>A sequence of anonymous objects of type <typeparamref name="T"/>.</returns>
  182. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Although this CA warning should never be suppressed, this particular usage scenario has been discussed and accepted on the FxCop DL.")]
  183. public static IEnumerable<T> CreateMany<T>(this ISpecimenContext context, int count)
  184. {
  185. return context.CreateMany(default(T), count);
  186. }
  187. /// <summary>
  188. /// Creates many anonymous objects.
  189. /// </summary>
  190. /// <typeparam name="T">The type of objects to create.</typeparam>
  191. /// <param name="composer">The composer used to resolve the type request.</param>
  192. /// <param name="count">The number of objects to create.</param>
  193. /// <returns>A sequence of anonymous objects of type <typeparamref name="T"/>.</returns>
  194. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Although this CA warning should never be suppressed, this particular usage scenario has been discussed and accepted on the FxCop DL.")]
  195. public static IEnumerable<T> CreateMany<T>(this ISpecimenBuilderComposer composer, int count)
  196. {
  197. if (composer == null)
  198. {
  199. throw new ArgumentNullException("composer");
  200. }
  201. return composer.Compose().CreateContext().CreateMany<T>(count);
  202. }
  203. /// <summary>
  204. /// Creates many anonymous objects.
  205. /// </summary>
  206. /// <typeparam name="T">The type of objects to create.</typeparam>
  207. /// <param name="composer">The composer used to resolve the type request.</param>
  208. /// <param name="count">The number of objects to create.</param>
  209. /// <returns>A sequence of anonymous objects of type <typeparamref name="T"/>.</returns>
  210. /// <remarks>
  211. /// <para>
  212. /// The only purpose of this explicit overload is to support type inferencing.
  213. /// </para>
  214. /// </remarks>
  215. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Supports type inferencing.")]
  216. public static IEnumerable<T> CreateMany<T>(this IPostprocessComposer<T> composer, int count)
  217. {
  218. return ((ISpecimenBuilderComposer)composer).CreateMany<T>(count);
  219. }
  220. /// <summary>
  221. /// Creates many anonymous objects.
  222. /// </summary>
  223. /// <typeparam name="T">The type of objects to create.</typeparam>
  224. /// <param name="context">The context used to resolve the type request.</param>
  225. /// <param name="seed">
  226. /// An initial value that may or may not be used as input for the algorithm creating the
  227. /// return value.
  228. /// </param>
  229. /// <param name="count">The number of objects to create.</param>
  230. /// <returns>A sequence of anonymous objects of type <typeparamref name="T"/>.</returns>
  231. public static IEnumerable<T> CreateMany<T>(this ISpecimenContext context, T seed, int count)
  232. {
  233. if (context == null)
  234. {
  235. throw new ArgumentNullException("context");
  236. }
  237. return from s in (IEnumerable<object>)context.Resolve(new FiniteSequenceRequest(new SeededRequest(typeof(T), seed), count))
  238. select (T)s;
  239. }
  240. /// <summary>
  241. /// Creates many anonymous objects.
  242. /// </summary>
  243. /// <typeparam name="T">The type of objects to create.</typeparam>
  244. /// <param name="composer">The composer used to resolve the type request.</param>
  245. /// <param name="seed">
  246. /// An initial value that may or may not be used as input for the algorithm creating the
  247. /// return value.
  248. /// </param>
  249. /// <param name="count">The number of objects to create.</param>
  250. /// <returns>A sequence of anonymous objects of type <typeparamref name="T"/>.</returns>
  251. public static IEnumerable<T> CreateMany<T>(this ISpecimenBuilderComposer composer, T seed, int count)
  252. {
  253. if (composer == null)
  254. {
  255. throw new ArgumentNullException("composer");
  256. }
  257. return composer.Compose().CreateContext().CreateMany(seed, count);
  258. }
  259. internal static object CreateAnonymous(this ISpecimenBuilderComposer composer, Type type)
  260. {
  261. return composer.Compose().CreateContext().Resolve(type);
  262. }
  263. private static ISpecimenContext CreateContext(this ISpecimenBuilder builder)
  264. {
  265. return new SpecimenContext(builder);
  266. }
  267. }
  268. }