PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/Builders/SetUpFixtureBuilder.cs

#
C# | 70 lines | 51 code | 11 blank | 8 comment | 7 complexity | 2aeae0a0d4176d07c6d58c7b3cfa43d5 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. namespace NUnit.Core.Builders
  8. {
  9. /// <summary>
  10. /// SetUpFixtureBuilder knows how to build a SetUpFixture.
  11. /// </summary>
  12. public class SetUpFixtureBuilder : Extensibility.ISuiteBuilder
  13. {
  14. #region ISuiteBuilder Members
  15. public Test BuildFrom(Type type)
  16. {
  17. SetUpFixture fixture = new SetUpFixture( type );
  18. string reason = null;
  19. if (!IsValidFixtureType(type, ref reason))
  20. {
  21. fixture.RunState = RunState.NotRunnable;
  22. fixture.IgnoreReason = reason;
  23. }
  24. return fixture;
  25. }
  26. public bool CanBuildFrom(Type type)
  27. {
  28. return Reflect.HasAttribute( type, NUnitFramework.SetUpFixtureAttribute, false );
  29. }
  30. #endregion
  31. private bool IsValidFixtureType(Type type, ref string reason)
  32. {
  33. if (type.IsAbstract)
  34. {
  35. reason = string.Format("{0} is an abstract class", type.FullName);
  36. return false;
  37. }
  38. if (Reflect.GetConstructor(type) == null)
  39. {
  40. reason = string.Format("{0} does not have a valid constructor", type.FullName);
  41. return false;
  42. }
  43. if (!NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.SetUpAttribute, ref reason) ||
  44. !NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.TearDownAttribute, ref reason) )
  45. return false;
  46. if ( Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureSetUpAttribute, true) )
  47. {
  48. reason = "TestFixtureSetUp method not allowed on a SetUpFixture";
  49. return false;
  50. }
  51. if ( Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureTearDownAttribute, true) )
  52. {
  53. reason = "TestFixtureTearDown method not allowed on a SetUpFixture";
  54. return false;
  55. }
  56. return true;
  57. }
  58. }
  59. }