public void GenerateCodeTest() { try { _nStubCore.GenerateCode(); } catch (Exception exception) { Assert.Fail("This operation should be able to complete with no errors: {0}", exception.Message); } }
/// <summary> /// Handles the Click event of the btnGo control. Creates a list of the methods /// for which the user wishes to generate test cases for and instantiates an /// instance of NStub around these methods. The sources are files are then /// generated. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the /// event data.</param> private void btnGo_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; _browseInputAssemblyButton.Enabled = false; _browseOutputDirectoryButton.Enabled = false; // Create a new directory for each assembly for (int h = 0; h < _assemblyGraphTreeView.Nodes.Count; h++) { string outputDirectory = _outputDirectoryTextBox.Text + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(_assemblyGraphTreeView.Nodes[h].Text) + "Test"; Directory.CreateDirectory(outputDirectory); // Create our project generator CSharpProjectGenerator cSharpProjectGenerator = new CSharpProjectGenerator( Path.GetFileNameWithoutExtension(_inputAssemblyTextBox.Text) + "Test", outputDirectory); // Add our referenced assemblies to the project generator so we // can build the resulting project foreach (AssemblyName assemblyName in _referencedAssemblies) { cSharpProjectGenerator.ReferencedAssemblies.Add(assemblyName); } // Generate the new test namespace // At the assembly level for (int i = 0; i < _assemblyGraphTreeView.Nodes[h].Nodes.Count; i++) { if (_assemblyGraphTreeView.Nodes[h].Nodes[i].Checked) { // Create the namespace and initial inputs CodeNamespace codeNamespace = new CodeNamespace(); // At the type level for (int j = 0; j < _assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes.Count; j++) { //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( _assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Text); if (_assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Checked) { // Create the class CodeTypeDeclaration testClass = new CodeTypeDeclaration(_assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Text); codeNamespace.Types.Add(testClass); // At the method level // Create a test method for each method in this type for (int k = 0; k < _assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Nodes.Count; k++) { try { if (_assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Nodes[k].Checked) { // Retrieve the MethodInfo object from this TreeNode's tag CodeMemberMethod codeMemberMethod = CreateMethod( _assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Nodes[k].Text, (MethodInfo)_assemblyGraphTreeView.Nodes[h].Nodes[i].Nodes[j].Nodes[k].Tag); testClass.Members.Add(codeMemberMethod); } } catch (Exception) { return; } } } } // Now write the test file NStubCore nStub = new NStubCore(codeNamespace, outputDirectory, new CSharpCodeGenerator(codeNamespace, outputDirectory)); nStub.GenerateCode(); // Add all of our classes to the project foreach (CodeTypeDeclaration codeType in nStub.CodeNamespace.Types) { string fileName = codeType.Name; fileName = fileName.Remove(0, (fileName.LastIndexOf(".") + 1)); fileName += ".cs"; cSharpProjectGenerator.ClassFiles.Add(fileName); } } } // Now write the project file and add all of the test files to it cSharpProjectGenerator.GenerateProjectFile(); } _browseInputAssemblyButton.Enabled = true; _browseOutputDirectoryButton.Enabled = true; Cursor.Current = Cursors.Arrow; }