PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/NUnit/core/NamespaceTreeBuilder.cs

#
C# | 169 lines | 89 code | 27 blank | 53 comment | 15 complexity | f891df54cd0d47dfefa987b2ce58ea2c 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.Collections;
  7. namespace NUnit.Core
  8. {
  9. /// <summary>
  10. /// Class that can build a tree of automatic namespace
  11. /// suites from a group of fixtures.
  12. /// </summary>
  13. public class NamespaceTreeBuilder
  14. {
  15. #region Instance Variables
  16. /// <summary>
  17. /// Hashtable of all test suites we have created to represent namespaces.
  18. /// Used to locate namespace parent suites for fixtures.
  19. /// </summary>
  20. Hashtable namespaceSuites = new Hashtable();
  21. /// <summary>
  22. /// The root of the test suite being created by this builder.
  23. /// </summary>
  24. TestSuite rootSuite;
  25. #endregion
  26. #region Constructor
  27. public NamespaceTreeBuilder( TestSuite rootSuite )
  28. {
  29. this.rootSuite = rootSuite;
  30. }
  31. #endregion
  32. #region Properties
  33. public TestSuite RootSuite
  34. {
  35. get { return rootSuite; }
  36. }
  37. #endregion
  38. #region Public Methods
  39. public void Add( IList fixtures )
  40. {
  41. foreach (TestSuite fixture in fixtures)
  42. //if (fixture is SetUpFixture)
  43. // Add(fixture as SetUpFixture);
  44. //else
  45. Add( fixture );
  46. }
  47. public void Add( TestSuite fixture )
  48. {
  49. if (fixture != null)
  50. {
  51. string ns = fixture.TestName.FullName;
  52. int index = ns.IndexOf("[");
  53. if (index >= 0) ns = ns.Substring(0, index);
  54. index = ns.LastIndexOf('.');
  55. ns = index > 0 ? ns.Substring(0, index) : string.Empty;
  56. TestSuite containingSuite = BuildFromNameSpace(ns);
  57. if (fixture is SetUpFixture)
  58. {
  59. // The SetUpFixture must replace the namespace suite
  60. // in which it is "contained".
  61. //
  62. // First, add the old suite's children
  63. foreach (TestSuite child in containingSuite.Tests)
  64. fixture.Add(child);
  65. // Make the parent of the containing suite point to this
  66. // fixture instead
  67. // TODO: Get rid of this somehow?
  68. TestSuite parent = (TestSuite)containingSuite.Parent;
  69. if (parent == null)
  70. {
  71. fixture.TestName.Name = rootSuite.TestName.Name;
  72. rootSuite = fixture;
  73. }
  74. else
  75. {
  76. parent.Tests.Remove(containingSuite);
  77. parent.Add(fixture);
  78. }
  79. // Update the hashtable
  80. namespaceSuites[ns] = fixture;
  81. }
  82. else
  83. containingSuite.Add(fixture);
  84. }
  85. }
  86. //public void Add( SetUpFixture fixture )
  87. //{
  88. // string ns = fixture.FullName;
  89. // int index = ns.LastIndexOf( '.' );
  90. // ns = index > 0 ? ns.Substring( 0, index ) : string.Empty;
  91. // TestSuite suite = BuildFromNameSpace( ns );
  92. // // Make the parent point to this instead
  93. // // TODO: Get rid of this somehow?
  94. // TestSuite parent = suite.Parent;
  95. // if ( parent != null )
  96. // {
  97. // parent.Tests.Remove( suite );
  98. // parent.Add( fixture );
  99. // }
  100. // // Add the old suite's children
  101. // foreach( TestSuite child in suite.Tests )
  102. // fixture.Add( child );
  103. // if (parent == null && fixture is SetUpFixture)
  104. // {
  105. // suite.Tests.Clear();
  106. // suite.Add(fixture);
  107. // }
  108. // // Update the hashtable
  109. // namespaceSuites[ns] = fixture;
  110. //}
  111. #endregion
  112. #region Helper Method
  113. private TestSuite BuildFromNameSpace( string nameSpace )
  114. {
  115. if( nameSpace == null || nameSpace == "" ) return rootSuite;
  116. TestSuite suite = (TestSuite)namespaceSuites[nameSpace];
  117. if(suite!=null) return suite;
  118. int index = nameSpace.LastIndexOf(".");
  119. //string prefix = string.Format( "[{0}]" );
  120. if( index == -1 )
  121. {
  122. suite = new NamespaceSuite( nameSpace );
  123. if ( rootSuite == null )
  124. rootSuite = suite;
  125. else
  126. rootSuite.Add(suite);
  127. namespaceSuites[nameSpace]=suite;
  128. }
  129. else
  130. {
  131. string parentNameSpace = nameSpace.Substring( 0,index );
  132. TestSuite parent = BuildFromNameSpace( parentNameSpace );
  133. string suiteName = nameSpace.Substring( index+1 );
  134. suite = new NamespaceSuite( parentNameSpace, suiteName );
  135. parent.Add( suite );
  136. namespaceSuites[nameSpace] = suite;
  137. }
  138. return suite;
  139. }
  140. #endregion
  141. }
  142. }