Example #1
0
        public static void CanCallCreate()
        {
            var sourceNameSpaceRoot = "TestValue1446777119";
            var targetNameSpaceRoot = "TestValue70491992";
            var result = NamespaceTransform.Create(sourceNameSpaceRoot, targetNameSpaceRoot);

            Assert.That(result("TestValue1446777119.Some.Other.Bit"), Is.EqualTo("TestValue70491992.Some.Other.Bit"));
            Assert.That(result("TestValue1446777119"), Is.EqualTo("TestValue70491992"));
            Assert.That(result(string.Empty), Is.EqualTo("TestValue70491992"));
        }
#pragma warning restore VSTHRD010

        private void Execute(bool withRegeneration)
        {
            GenerationItem generationItem;

            Attempt.Action(
                () =>
            {
                ThreadHelper.ThrowIfNotOnUIThread();

                var textView = GetTextView();
                if (textView == null)
                {
                    throw new InvalidOperationException("Could not find the text view");
                }

                var caretPosition = textView.Caret.Position.BufferPosition;
                var document      = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

                var messageLogger = new AggregateLogger();
                messageLogger.Initialize();

                var item = VsProjectHelper.GetProjectItem(document.FilePath);

                var options = _package.Options;
                var source  = new ProjectItemModel(item, options.GenerationOptions);

                var projectItem = source.Item;

                var nameParts = VsProjectHelper.GetNameParts(projectItem);

                if (source.TargetProject == null && options.GenerationOptions.CreateProjectAutomatically)
                {
                    var testProject = SolutionUtilities.CreateTestProjectInCurrentSolution(_dte, source.Project, options.GenerationOptions);
                    ReferencesHelper.AddNugetPackagesToProject(testProject, StandardReferenceHelper.GetReferencedNugetPackages(options), messageLogger.LogMessage, _package);
                }

                var targetProject = source.TargetProject;
                if (targetProject == null && !options.GenerationOptions.AllowGenerationWithoutTargetProject)
                {
                    throw new InvalidOperationException("Cannot create tests for '" + Path.GetFileName(source.FilePath) + "' because there is no project '" + source.TargetProjectName + "'");
                }

                var projectDictionary = new Dictionary <EnvDTE.Project, Tuple <HashSet <TargetAsset>, HashSet <IReferencedAssembly> > >();
                var set = new HashSet <TargetAsset>();
                var assemblyReferences = new HashSet <IReferencedAssembly>();
                if (targetProject != null)
                {
                    projectDictionary[targetProject] = Tuple.Create(set, assemblyReferences);
                }

                var targetProjectItems = TargetFinder.FindTargetFolder(targetProject, nameParts, true, out var targetPath);
                if (targetProjectItems == null && !options.GenerationOptions.AllowGenerationWithoutTargetProject)
                {
                    // we asked to create targetProjectItems - so if it's null we effectively had a problem getting to the target project
                    throw new InvalidOperationException("Cannot create tests for '" + Path.GetFileName(source.FilePath) + "' because there is no project '" + source.TargetProjectName + "'");
                }

                _package.JoinableTaskFactory.RunAsync(
                    () => Attempt.ActionAsync(
                        async() =>
                {
                    var methodSymbol = await GetTargetSymbolAsync(textView).ConfigureAwait(true);

                    if (methodSymbol != null)
                    {
                        var sourceNameSpaceRoot = VsProjectHelper.GetProjectRootNamespace(source.Project);

                        Func <string, string> namespaceTransform;
                        if (source.TargetProject != null)
                        {
                            var targetNameSpaceRoot = VsProjectHelper.GetProjectRootNamespace(source.TargetProject);
                            namespaceTransform      = NamespaceTransform.Create(sourceNameSpaceRoot, targetNameSpaceRoot);
                        }
                        else
                        {
                            namespaceTransform = x => x + ".Tests";
                        }

                        generationItem = new GenerationItem(source, methodSymbol.Item1, targetProjectItems, targetPath, set, assemblyReferences, namespaceTransform, options.GenerationOptions);

                        await CodeGenerator.GenerateCodeAsync(new[] { generationItem }, withRegeneration, _package, projectDictionary, messageLogger).ConfigureAwait(true);
                    }
                }, _package));
            }, _package);
        }
Example #3
0
 public static void CannotCallCreateWithInvalidTargetNameSpaceRoot(string value)
 {
     Assert.Throws <ArgumentNullException>(() => NamespaceTransform.Create("TestValue2131007467", value));
 }
Example #4
0
 public static void CannotCallCreateWithInvalidSourceNameSpaceRoot(string value)
 {
     Assert.Throws <ArgumentNullException>(() => NamespaceTransform.Create(value, "TestValue1220339710"));
 }
#pragma warning restore VSTHRD010

        private void Execute(bool withRegeneration)
        {
            var generationItems   = new List <GenerationItem>();
            var projectDictionary = new Dictionary <Project, Tuple <HashSet <TargetAsset>, HashSet <IReferencedAssembly> > >();

            var messageLogger = new AggregateLogger();

            messageLogger.Initialize();

            Attempt.Action(
                () =>
            {
                ThreadHelper.ThrowIfNotOnUIThread();
#pragma warning disable VSTHRD010

                if (!IsAvailable)
                {
                    throw new InvalidOperationException("Cannot generate unit tests for this item because no supported files were found");
                }

                var options = _package.Options;
                var sources = SolutionUtilities.GetSelectedFiles(_dte, true, options.GenerationOptions).Where(ProjectItemModel.IsSupported).ToList();

                var targetProjects = new Dictionary <Project, Project>();

                foreach (var source in sources)
                {
                    if (targetProjects.ContainsKey(source.Project))
                    {
                        continue;
                    }

                    if (source.TargetProject == null && options.GenerationOptions.CreateProjectAutomatically)
                    {
                        var testProject = SolutionUtilities.CreateTestProjectInCurrentSolution(_dte, source.Project, options.GenerationOptions);
                        ReferencesHelper.AddNugetPackagesToProject(testProject, StandardReferenceHelper.GetReferencedNugetPackages(options), messageLogger.LogMessage, _package);
                    }

                    var targetProject = source.TargetProject;

                    if (targetProject == null && !options.GenerationOptions.AllowGenerationWithoutTargetProject)
                    {
                        throw new InvalidOperationException("Cannot create tests for '" + Path.GetFileName(source.FilePath) + "' because there is no project '" + source.TargetProjectName + "'");
                    }

                    if (targetProject != null)
                    {
                        targetProjects[source.Project]   = targetProject;
                        projectDictionary[targetProject] = Tuple.Create(new HashSet <TargetAsset>(), new HashSet <IReferencedAssembly>());
                    }
                }

                foreach (var source in sources)
                {
                    var projectItem = source.Item;

                    if (!withRegeneration && TargetFinder.FindExistingTargetItem(source, options.GenerationOptions, out _) == FindTargetStatus.Found)
                    {
                        if (sources.Count == 1)
                        {
                            throw new InvalidOperationException("Cannot create tests for '" + Path.GetFileName(source.FilePath) + "' because tests already exist. If you want to re-generate tests for this item, hold down the left Shift key and right-click the item.");
                        }

                        continue;
                    }

                    var nameParts = VsProjectHelper.GetNameParts(projectItem);

                    targetProjects.TryGetValue(source.Project, out var targetProject);
                    var targetProjectItems = TargetFinder.FindTargetFolder(targetProject, nameParts, true, out var targetPath);

                    if (targetProjectItems == null && !options.GenerationOptions.AllowGenerationWithoutTargetProject)
                    {
                        // we asked to create targetProjectItems - so if it's null we effectively had a problem getting to the target project
                        throw new InvalidOperationException("Cannot create tests for '" + Path.GetFileName(source.FilePath) + "' because there is no project '" + source.TargetProjectName + "'");
                    }

                    var sourceNameSpaceRoot = VsProjectHelper.GetProjectRootNamespace(source.Project);
                    HashSet <TargetAsset> requiredAssets;
                    HashSet <IReferencedAssembly> referencedAssemblies;

                    if (targetProject != null && projectDictionary.TryGetValue(targetProject, out var targetProjectEntry))
                    {
                        requiredAssets       = targetProjectEntry.Item1;
                        referencedAssemblies = targetProjectEntry.Item2;
                    }
                    else
                    {
                        requiredAssets       = new HashSet <TargetAsset>();
                        referencedAssemblies = new HashSet <IReferencedAssembly>();
                    }

                    Func <string, string> namespaceTransform;
                    if (source.TargetProject != null)
                    {
                        var targetNameSpaceRoot = VsProjectHelper.GetProjectRootNamespace(source.TargetProject);
                        namespaceTransform      = NamespaceTransform.Create(sourceNameSpaceRoot, targetNameSpaceRoot);
                    }
                    else
                    {
                        namespaceTransform = x => x + ".Tests";
                    }

                    generationItems.Add(new GenerationItem(source, null, targetProjectItems, targetPath, requiredAssets, referencedAssemblies, namespaceTransform, options.GenerationOptions));
                }
#pragma warning restore VSTHRD010
            }, _package);

            if (generationItems.Any())
            {
                _package.JoinableTaskFactory.RunAsync(() => Attempt.ActionAsync(() => CodeGenerator.GenerateCodeAsync(generationItems, withRegeneration, _package, projectDictionary, messageLogger), _package));
            }
        }