<commit_before>// Copyright (c) <NAME>, <NAME> and Contributors. MIT License - see LICENSE.txt

#nullable enable

using System.Collections.Generic;

namespace NUnit.Framework.Internal.Builders
{
    /// <summary>
    /// Class that can build a tree of automatic namespace
    /// suites from a group of fixtures.
    /// </summary>
    public class NamespaceTreeBuilder
    {
        #region Instance Variables

        /// <summary>
        /// NamespaceDictionary of all test suites we have created to represent 
        /// namespaces. Used to locate namespace parent suites for fixtures.
        /// </summary>
        readonly Dictionary<string, TestSuite> _namespaceIndex  = new Dictionary<string, TestSuite>();

        /// <summary>
        /// Point in the tree where items in the global namespace are added
        /// </summary>
        private TestSuite _globalInsertionPoint;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="NamespaceTreeBuilder"/> class.
        /// </summary>
        /// <param name="rootSuite">The root suite.</param>
        public NamespaceTreeBuilder( TestSuite rootSuite )
        {
            Guard.ArgumentNotNull(rootSuite, nameof(rootSuite));
            RootSuite = _globalInsertionPoint = rootSuite;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the root entry in the tree created by the NamespaceTreeBuilder.
        /// </summary>
        /// <value>The root suite.</value>
        public TestSuite RootSuite { get; private set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Adds the specified fixtures to the tree.
        /// </summary>
        /// <param name="fixtures">The fixtures to be added.</param>
        public void Add( IList<Test> fixtures )
        {
            foreach (TestSuite fixture in fixtures)
                Add( fixture );
        }

        /// <summary>
        /// Adds the specified fixture to the tree.
        /// </summary>
        /// <param name="fixture">The fixture to be added.</param>
        public void Add( TestSuite fixture )
        {
            string ns = GetNamespaceForFixture(fixture);
            TestSuite containingSuite = GetNamespaceSuite( ns );

            if (fixture is SetUpFixture)
                AddSetUpFixture(fixture, containingSuite, ns);
            else
                containingSuite.Add( fixture );
        }

        #endregion

        #region Helper Method

        private static string GetNamespaceForFixture(TestSuite fixture)
        {
            string ns = fixture.FullName;
            int index = ns.IndexOfAny(new char[] { '[', '(' });
            if (index >= 0) ns = ns.Substring(0, index);
            index = ns.LastIndexOf('.');
            ns = index > 0 ? ns.Substring(0, index) : string.Empty;
            return ns;
        }

        private TestSuite GetNamespaceSuite( string ns )
        {
            Guard.ArgumentNotNull(ns, nameof(ns));

            if( ns  == "" ) return _globalInsertionPoint;

            if (_namespaceIndex.ContainsKey(ns))
                return _namespaceIndex[ns];

            TestSuite suite;
            int index = ns.LastIndexOf('.');

            if( index == -1 )
            {
                suite = new TestSuite( ns );
                _globalInsertionPoint.Add(suite);
            }
            else
            {<commit_msg>Use TryGetValue() in NamespaceTreeBuilder.GetNamespaceSuite() method<commit_after>// Copyright (c) <NAME>, <NAME> and Contributors. MIT License - see LICENSE.txt

#nullable enable

using System.Collections.Generic;

namespace NUnit.Framework.Internal.Builders
{
    /// <summary>
    /// Class that can build a tree of automatic namespace
    /// suites from a group of fixtures.
    /// </summary>
    public class NamespaceTreeBuilder
    {
        #region Instance Variables

        /// <summary>
        /// NamespaceDictionary of all test suites we have created to represent 
        /// namespaces. Used to locate namespace parent suites for fixtures.
        /// </summary>
        readonly Dictionary<string, TestSuite> _namespaceIndex  = new Dictionary<string, TestSuite>();

        /// <summary>
        /// Point in the tree where items in the global namespace are added
        /// </summary>
        private TestSuite _globalInsertionPoint;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="NamespaceTreeBuilder"/> class.
        /// </summary>
        /// <param name="rootSuite">The root suite.</param>
        public NamespaceTreeBuilder( TestSuite rootSuite )
        {
            Guard.ArgumentNotNull(rootSuite, nameof(rootSuite));
            RootSuite = _globalInsertionPoint = rootSuite;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the root entry in the tree created by the NamespaceTreeBuilder.
        /// </summary>
        /// <value>The root suite.</value>
        public TestSuite RootSuite { get; private set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Adds the specified fixtures to the tree.
        /// </summary>
        /// <param name="fixtures">The fixtures to be added.</param>
        public void Add( IList<Test> fixtures )
        {
            foreach (TestSuite fixture in fixtures)
                Add( fixture );
        }

        /// <summary>
        /// Adds the specified fixture to the tree.
        /// </summary>
        /// <param name="fixture">The fixture to be added.</param>
        public void Add( TestSuite fixture )
        {
            string ns = GetNamespaceForFixture(fixture);
            TestSuite containingSuite = GetNamespaceSuite( ns );

            if (fixture is SetUpFixture)
                AddSetUpFixture(fixture, containingSuite, ns);
            else
                containingSuite.Add( fixture );
        }

        #endregion

        #region Helper Method

        private static string GetNamespaceForFixture(TestSuite fixture)
        {
            string ns = fixture.FullName;
            int index = ns.IndexOfAny(new char[] { '[', '(' });
            if (index >= 0) ns = ns.Substring(0, index);
            index = ns.LastIndexOf('.');
            ns = index > 0 ? ns.Substring(0, index) : string.Empty;
            return ns;
        }

        private TestSuite GetNamespaceSuite( string ns )
        {
            Guard.ArgumentNotNull(ns, nameof(ns));

            if( ns  == "" ) return _globalInsertionPoint;

            _namespaceIndex.TryGetValue(ns, out TestSuite suiteToReturn);
            if (suiteToReturn != null)
                return suiteToReturn;

            TestSuite suite;
            int index = ns.LastIndexOf('.');

            if( index == -1 )
            {
                suite = new TestSuite( ns );
                _globalInsertionPoint.Add(suite);
            }
            else
            {
{'fraction_non_alphanumeric': 0.10261115031757234, 'fraction_numerical': 0.0014114326040931546, 'mean_word_length': 1.7295839753466873, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7488816', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1730, 'n_words': 541}