Пример #1
0
        private static async Task ParseCsharpFiles(string solutionFullPath, bool previewOnly, string tfsExePath)
        {
            foreach (var item in await GetUnitTestItems(solutionFullPath))
            {
                var stub2Moq    = new MsTestStub2Moq(item.Workspace, item.File, item.SemanticModel);
                var rewriter    = new MsTestStubRewriter(stub2Moq);
                var newTestFile = rewriter.Visit(item.Root);

                if (newTestFile != item.Root)
                {
                    var root = newTestFile.SyntaxTree.GetRoot();
                    var compilationUnitSyntax = root as CompilationUnitSyntax;

                    if (compilationUnitSyntax != null)
                    {
                        bool hasMoqUsing = compilationUnitSyntax.Usings
                                           .Select(u => u.Name)
                                           .OfType <IdentifierNameSyntax>()
                                           .Any(u => u.Identifier.ValueText.Equals("Moq", StringComparison.InvariantCulture));

                        if (!hasMoqUsing)
                        {
                            var moq      = SyntaxFactory.ParseName("Moq");
                            var moqUsing = SyntaxFactory.UsingDirective(moq).NormalizeWhitespace().WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
                            root = compilationUnitSyntax.AddUsings(moqUsing);
                        }
                    }

                    var newDoc           = item.File.WithSyntaxRoot(root);
                    var newSemanticModel = await newDoc.GetSemanticModelAsync().ConfigureAwait(false);

                    root = await newDoc.GetSyntaxRootAsync();

                    // remove useless usings (.Fakes and others)
                    root = UsingsHelper.RemoveUnusedImportDirectives(newSemanticModel, root, CancellationToken.None, (r, usingNode) =>
                    {
                        if (usingNode.ToString().Contains("Moq"))
                        {
                            return(!r.ToString().Contains("Mock"));
                        }
                        else
                        {
                            return(true);
                        }
                    });

                    if (previewOnly)
                    {
                        Console.WriteLine(newTestFile.ToFullString());
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(tfsExePath))
                        {
                            TfsCheckOut(tfsExePath, item.File.FilePath);
                        }

                        using (var writer = new StreamWriter(item.File.FilePath, false, System.Text.UTF8Encoding.UTF8))
                        {
                            root.WriteTo(writer);
                        }
                    }
                }
            }
        }
Пример #2
0
 public MsTestStubRewriter(MsTestStub2Moq stub2Moq)
 {
     this.stub2Moq = stub2Moq;
 }