Esempio n. 1
0
        /// <summary>
        /// Creates a test file named 'SecuredTests.cs' that contains all the test methods.
        /// </summary>
        /// <param name="p_methods"></param>
        public static void CreateTestFile(Dictionary <TestingMethodVM, List <TestConfiguration> > p_methods)
        {
            DTE2           dte         = (DTE2)Secure_TDDPackage.GetGlobalService(typeof(DTE));
            Solution2      soln        = (Solution2)dte.Solution;
            Project        currentProj = dte.ActiveDocument.ProjectItem.ContainingProject;
            Project        proj        = GetTestingProject(currentProj);
            ProjectItem    pi          = GetCSTestFile(proj, soln);
            FileCodeModel2 fcm         = (FileCodeModel2)pi.FileCodeModel;

            if (!IsContainImports(fcm))
            {
                fcm.AddImport("System");
                fcm.AddImport("System.Linq");
                fcm.AddImport("System.Text");
                fcm.AddImport("System.Collections.Generic");
                fcm.AddImport("Microsoft.VisualStudio.TestTools.UnitTesting");
            }

            string        namespaceName    = "Testing";
            CodeNamespace testingNameSpace = GetNamespace(fcm, namespaceName);

            if (testingNameSpace != null)
            {
                string     className    = "SecuredTestClass";
                CodeClass2 securedClass = GetClass(fcm, testingNameSpace, className);

                foreach (var methodKeyValue in p_methods)
                {
                    var startIndexOfMethodName = methodKeyValue.Key.FullName.LastIndexOf('.');
                    var methodBaseName         = methodKeyValue.Key.FullName.Substring(startIndexOfMethodName + 1) + "_{0}Test";

                    foreach (var testConfiguration in methodKeyValue.Value)
                    {
                        // Remove all white-spaces in the test name.
                        var fixedName    = testConfiguration.Name.Replace(" ", string.Empty);
                        var methodName   = string.Format(methodBaseName, fixedName);
                        var functionBody = GetMethodBody(methodKeyValue.Key, testConfiguration);
                        AddFunction(securedClass, methodName, functionBody);
                    }
                }
            }

            // if the file is not opened in the text editor, open and activate it.
            Window window = pi.Open(Constants.vsViewKindCode);

            if (window != null)
            {
                window.Activate();
                dte.ExecuteCommand("Edit.FormatDocument");
            }

            // Save the file.
            pi.Save();
        }
        public void AddImport_AddSystemXmlNamespace_NamespaceAndcompilationUnitPassedToNamespaceCreator()
        {
            CreateProjectWithOneFile();
            CreateCompilationUnitForFileProjectItem();
            CreateFileCodeModel();

            fileCodeModel.AddImport("System.Xml");

            namespaceCreator.AssertWasCalled(creator => creator.AddNamespace(compilationUnitHelper.CompilationUnit, "System.Xml"));
        }
        public void AddImport_AddSystemXmlNamespace_NamespaceAndcompilationUnitPassedToNamespaceCreator()
        {
            CreateProjectWithOneFile("", @"d:\project\MyProject\MyFile.cs");
            CreateFileCodeModel();

            fileCodeModel.AddImport("System.Xml");

            codeGenerator.AssertWasCalled(generator => generator.AddImport(
                                              new FileName(@"d:\project\MyProject\MyFile.cs"),
                                              "System.Xml"));
        }
        private object NewImport(NewCodeElementItemParams newItemParams, string path, object newItemValue)
        {
            FileCodeModel2 fileCodeModel2 = _codeModel as FileCodeModel2;

            if (null == fileCodeModel2)
            {
                return(null);
            }

            // apparantly required to prevent an exception in VS
            newItemParams.Position = Math.Max(newItemParams.Position, 0);

            return(fileCodeModel2.AddImport(path, newItemParams.Position, String.Empty));
        }
Esempio n. 5
0
        /// <summary>
        /// Adds new namespace import into the given document.
        /// </summary>
        public static CodeImport AddUsingBlock(this Document document, string newNamespace)
        {
            if (document == null || document.ProjectItem == null)
            {
                throw new Exception("No document or project item.");
            }
            if (string.IsNullOrEmpty(newNamespace))
            {
                throw new ArgumentNullException("newNamespace");
            }

            bool           fileOpened;
            FileCodeModel2 model = document.ProjectItem.GetCodeModel(true, false, out fileOpened);

            return(model.AddImport(newNamespace, 0, string.Empty));
        }
Esempio n. 6
0
        public static FileCodeModel2 AddNotExistingImports(this FileCodeModel2 fileCodeModel, IEnumerable <string> importsToAdd)
        {
            if (null == fileCodeModel)
            {
                throw new ArgumentNullException(nameof(fileCodeModel));
            }

            var currentImports = fileCodeModel.CodeElements
                                 .OfType <CodeImport>()
                                 .Select(x => x.Namespace)
                                 .ToList();

            foreach (var import in importsToAdd)
            {
                if (!currentImports.Any(x => x == import))
                {
                    fileCodeModel.AddImport(import, 0);
                }
            }

            return(fileCodeModel);
        }
        /// <summary>
        /// Adds the using statement.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="usingStatement">The using statement.</param>
        public static void AddUsingStatement(
            this ProjectItem instance,
            string usingStatement)
        {
            TraceService.WriteLine("ProjectItemExtensions::AddUsingStatement in file " + instance.Name + " statement " + usingStatement);

            CodeNamespace codeNamespace = instance.GetFirstNameSpace();

            if (codeNamespace != null)
            {
                FileCodeModel2 fileCodeModel2 = codeNamespace.ProjectItem.FileCodeModel as FileCodeModel2;

                if (fileCodeModel2 != null)
                {
                    fileCodeModel2.AddImport(usingStatement);
                }
            }
            else
            {
                TraceService.WriteError("ProjectItemExtensions::AddUsingStatement cannot find namespace");
            }
        }
Esempio n. 8
0
        public void BeginTraverse(FileCodeModel fcm)
        {
            FileCodeModel2 fcm2 = fcm as FileCodeModel2;

            if (fcm2 == null)
            {
                return;
            }
            String[] imports = _injector.OnGenerateUsing(_context);
            if (imports != null)
            {
                foreach (string import in imports)
                {
                    try
                    {
                        fcm2.AddImport(import, null, String.Empty);
                    }
                    catch
                    {
                    }
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Adds the using statement.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="usingStatement">The using statement.</param>
        public static void AddUsingStatement(
            this ProjectItem instance,
            string usingStatement)
        {
            FileCodeModel2 fileCodeModel2 = instance.GetFirstNameSpace().ProjectItem.FileCodeModel as FileCodeModel2;

            if (fileCodeModel2 != null)
            {
                foreach (CodeElement codeElement in fileCodeModel2.CodeElements)
                {
                    if (codeElement.Kind == vsCMElement.vsCMElementImportStmt)
                    {
                        CodeImport codeImport = codeElement as CodeImport;

                        if (codeImport.Namespace == usingStatement)
                        {
                            return;
                        }
                    }
                }

                fileCodeModel2.AddImport(usingStatement);
            }
        }
Esempio n. 10
0
        private static void EnsureUsingDirective(FileCodeModel2 fileCodeModel, string importName)
        {
            bool importNotFound = true;
            foreach (CodeElement ce in (fileCodeModel).CodeElements)
            {
                if (ce is CodeImport && ((CodeImport)ce).Namespace == importName)
                {
                    importNotFound = false;
                    break;
                }
            }

            if (importNotFound)
                fileCodeModel.AddImport(importName);
        }