Beispiel #1
0
        public async Task RewritesDefaultConstructor(string filePath)
        {
            // Arrange
            var filePathClassUnderTest = "files.ClassUnderTest.txt";
            var filePaths = new[] { filePath, filePathClassUnderTest };
            var documents = DocumentProvider.CreateCompilationAndReturnDocuments(filePaths);

            var documentWithTestClass = documents[filePath];
            var root = await documentWithTestClass.GetSyntaxRootAsync();

            var testClass = root.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();

            var documentWithClassUnderTest = documents[filePathClassUnderTest];
            var classUnderTest             = SyntaxNodeProvider.GetSyntaxNodeFromDocument <ClassDeclarationSyntax>(documentWithClassUnderTest, "ClassUnderTest");
            var semanticModel = await documentWithClassUnderTest.GetSemanticModelAsync();

            _classUnderTestFinder.Setup(_ => _.GetAsync(It.IsAny <Solution>(), It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new ClassUnderTest(classUnderTest, semanticModel));

            // Act
            var actual = await _target.RegenerateSetup(documentWithTestClass, testClass, new CancellationToken());

            // Assert
            var actualText = await actual.GetTextAsync();

            _testOutput.WriteLine(actualText.ToString().Replace(";", ";\n"));

            var expected = await DocumentProvider.CreateDocumentFromFile("files.TestClass_WithFinalSetupConstructor.txt").GetTextAsync();

            Assert.Equal(expected.ToString().RemoveWhitespace(), actualText.ToString().RemoveWhitespace());
        }
        public async Task Adds_Usings_For_All_Parameters(string filePath, string parameterName, string expectedNamespaces)
        {
            var source   = TextFileReader.ReadFile(filePath);
            var document = DocumentProvider.CreateDocument(source);

            var parameterSyntax = SyntaxNodeProvider.GetSyntaxNodeFromDocument <ParameterSyntax>(document, parameterName);
            var syntaxGenerator = new SyntaxGeneratorProvider().GetSyntaxGenerator();

            var semanticModel = await document.GetSemanticModelAsync();

            var usingDirectives = _target.UsingDirectives(semanticModel.Compilation, new[] { parameterSyntax }, new List <string>(), syntaxGenerator);

            Assert.Equal(expectedNamespaces, usingDirectives.First().Name.ToString());
        }