NStub.Core.Utility.GetNamespaceFromFullyQualifiedTypeName C# (CSharp) Méthode

GetNamespaceFromFullyQualifiedTypeName() public static méthode

Gets the name of the namespace from fully the given qualified type. For example, if System.IO.Stream is provided, System.IO is returned.
name is null. name is an empty string.
public static GetNamespaceFromFullyQualifiedTypeName ( string name ) : string
name string The fully qualified name.
Résultat string
        public static string GetNamespaceFromFullyQualifiedTypeName(string name)
        {
            #region Validation

            // Ensure our fully qualified name is indeed a valid name
            if (name == null)
            {
                throw new ArgumentNullException("name", Exceptions.ParameterCannotBeNull);
            }
            if (name.Length == 0)
            {
                throw new ArgumentException(Exceptions.StringCannotBeEmpty, "name");
            }

            #endregion Validation

            // Each level represents an object in the qualified name
            string[] levels =
                name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (levels.Length == 1)
            {
                // We've encountered a class which has no namespace.
                return "";
            }

            // Now build the namespace back up again
            StringBuilder namespaceBuilder = new StringBuilder();
            for (int i = 0; i < (levels.Length - 1); i++)
            {
                namespaceBuilder.Append(levels[i]);
                namespaceBuilder.Append(".");
            }
            // Remove the final trailing '.'
            namespaceBuilder.Remove((namespaceBuilder.Length - 1), 1);

            return namespaceBuilder.ToString();
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Creates a list of the methods
        /// for which the user wishes to generate test cases for and instantiates an
        /// instance of <c>NStub</c> around these methods.  The sources are files are then
        /// generated.
        /// </summary>
        /// <param name="outputFolder">The output folder.</param>
        /// <param name="generatorType">Type of the code generator to use.</param>
        /// <param name="inputAssemblyPath">The input assembly path.</param>
        /// <param name="mainNodes">The main nodes.</param>
        /// <param name="referencedAssemblies">The list of referenced assemblies.</param>
        public void GenerateTests(
            string outputFolder,
            Type generatorType,
            string inputAssemblyPath,
            IList <TestNode> mainNodes,
            IList <AssemblyName> referencedAssemblies)
        {
            // string outputFolder = this._outputDirectoryTextBox.Text;
            // Type generatorType = (Type)cbGenerators.SelectedItem;
            // string inputAssemblyPath = this._inputAssemblyTextBox.Text;
            // //TreeNodeCollection mainNodes = this._assemblyGraphTreeView.Nodes;
            // IList<TreeNode> mainNodes = this._assemblyGraphTreeView.Nodes.Cast<TreeNode>().ToList();
            // IList<AssemblyName> referencedAssemblies = this._referencedAssemblies;
            if (this.logger != null)
            {
                this.logger("-----------------------------------------------------------------------------");
                this.logger("Generating Test Project '" + this.csharpProjectGenerator.ProjectName + "'");
                this.logger("-----------------------------------------------------------------------------");
            }

            // Create a new directory for each assembly
            foreach (var assemblyNode in mainNodes)
            {
                string calculatedOutputDirectory = outputFolder +
                                                   this.sbs.DirectorySeparatorChar +
                                                   this.sbs.GetFileNameWithoutExtension(assemblyNode.Text) +
                                                   ".Tests";
                this.sbs.CreateDirectory(calculatedOutputDirectory);

                // Create our project generator

                // Add our referenced assemblies to the project generator so we
                // can build the resulting project
                foreach (AssemblyName assemblyName in referencedAssemblies)
                {
                    this.csharpProjectGenerator.ReferencedAssemblies.Add(assemblyName);
                }

                // Generate the new test namespace
                // At the assembly level
                foreach (var moduleNode in assemblyNode.Nodes)
                {
                    if (!moduleNode.Checked)
                    {
                        // continue;
                    }

                    // Create the namespace and initial inputs
                    var codeNamespace = new CodeNamespace();

                    // At the type level
                    foreach (var classNode in moduleNode.Nodes)
                    {
                        // TODO: This namespace isn't being set correctly.
                        // Also one namespace per run probably won't work, we may
                        // need to break this up more.
                        codeNamespace.Name =
                            Utility.GetNamespaceFromFullyQualifiedTypeName(classNode.Text);

                        if (!classNode.Checked)
                        {
                            continue;
                        }

                        Thread.Sleep(1);
                        if (this.logger != null)
                        {
                            this.logger("Building '" + classNode.Text + "'");
                        }

                        // Create the class
                        var testClass = new CodeTypeDeclaration(classNode.Text);
                        codeNamespace.Types.Add(testClass);
                        var testObjectClassType = classNode.ClrType;
                        testClass.UserData.Add(NStubConstants.UserDataClassTypeKey, testObjectClassType);

                        // At the method level
                        // Create a test method for each method in this type
                        foreach (var classmemberNode in classNode.Nodes)
                        {
                            if (!classmemberNode.Checked)
                            {
                                continue;
                            }

                            try
                            {
                                // Retrieve the MethodInfo object from this TreeNode's tag
                                // var memberInfo = (MethodInfo)rootsubsubsubnode.Tag;
                                var memberInfo = classmemberNode.MethodInfo;
                                CodeMemberMethod codeMemberMethod =
                                    this.CreateMethod(classmemberNode.Text, memberInfo);
                                codeMemberMethod.UserData.Add(NStubConstants.TestMemberMethodInfoKey, memberInfo);
                                testClass.Members.Add(codeMemberMethod);
                            }
                            catch (Exception ex)
                            {
                                // MessageBox.Show(ex.Message + Environment.NewLine + ex.ToString());
                                if (this.logger != null)
                                {
                                    this.logger(
                                        ex.Message + Environment.NewLine + ex + Environment.NewLine);
                                }
                            }
                        }
                    }

                    this.WriteTestFile(calculatedOutputDirectory, codeNamespace);
                }

                // Now write the project file and add all of the test files to it
                this.csharpProjectGenerator.GenerateProjectFile();
            }

            if (this.logger != null)
            {
                this.logger("-----------------------------------------------------------------------------");
                this.logger("Finished generating Project '" + this.csharpProjectGenerator.ProjectName + "'");
                this.logger("-----------------------------------------------------------------------------");
            }
        }