public async Task SetsFields(string testClassFilePath)
        {
            // Arrange
            var document  = DocumentProvider.CreateDocumentFromFile(testClassFilePath);
            var root      = document.GetSyntaxRootAsync().Result;
            var testClass = root.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();

            _target = new DocumentBuilder(_memberFinder.Object, _fieldFinder, document, testClass);

            var newFieldDeclarations =
                SyntaxNodeProvider.GetAllSyntaxNodesFromFile <FieldDeclarationSyntax>(FieldDeclarations).ToList();

            var expectedFieldDeclarations =
                SyntaxNodeProvider.GetAllSyntaxNodesFromFile <FieldDeclarationSyntax>(TestClassWithAllFields).ToList();
            var expected = GetCollectionTextAsString(expectedFieldDeclarations);

            // Act
            var actual = await _target.WithFields(newFieldDeclarations)
                         .BuildAsync(new CancellationToken());

            // Assert
            var actualRoot = await actual.GetSyntaxRootAsync();

            var actualFields = GetCollectionTextAsString(actualRoot.DescendantNodes().OfType <FieldDeclarationSyntax>());

            _testOutput.WriteLine(actualFields);

            Assert.Equal(expected, actualFields);
        }
        public void ReturnsConstructorWithBody()
        {
            var filePath        = "files.ConstructorGenerator_DocumentWithOneClassAndVariables.txt";
            var source          = TextFileReader.ReadFile(filePath);
            var methodBody      = SyntaxNodeProvider.GetAllSyntaxNodesFromFile <LocalDeclarationStatementSyntax>(filePath);
            var syntaxGenerator = new SyntaxGeneratorProvider().GetSyntaxGenerator(source);
            var className       = "ClassA";

            var actual = _target.Constructor(className, methodBody, syntaxGenerator);

            Assert.Single(actual.DescendantNodes().OfType <LocalDeclarationStatementSyntax>());
        }
예제 #3
0
        public async Task SetsConstructorAndFields(string testClassFilePath)
        {
            // Arrange
            var document  = DocumentProvider.CreateDocumentFromFile(testClassFilePath);
            var root      = document.GetSyntaxRootAsync().Result;
            var testClass = root.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();

            _target = new DocumentBuilder(_memberFinder, _fieldFinder, document, testClass);

            var newFieldDeclarations =
                SyntaxNodeProvider.GetAllSyntaxNodesFromFile <FieldDeclarationSyntax>(FieldDeclarations).ToList();

            var expectedFieldDeclarations =
                SyntaxNodeProvider.GetAllSyntaxNodesFromFile <FieldDeclarationSyntax>(TestClassWithConstructorAndFields)
                .ToList();
            var expectedFields = GetCollectionTextAsString(expectedFieldDeclarations);

            var newSetupMethod =
                SyntaxNodeProvider.GetSyntaxNodeFromFile <ConstructorDeclarationSyntax>(Constructor, "ClassUnderTestTests");

            // Act
            var actual = await _target.WithSetupMethod(newSetupMethod)
                         .WithFields(newFieldDeclarations)
                         .BuildAsync(new CancellationToken());

            // Assert
            var actualRoot = await actual.GetSyntaxRootAsync();

            _testOutput.WriteLine(actualRoot.GetText().ToString());

            var actualFields = GetCollectionTextAsString(actualRoot.DescendantNodes().OfType <FieldDeclarationSyntax>());

            Assert.Equal(expectedFields, actualFields);

            var actualMethods = actualRoot.DescendantNodes().OfType <ConstructorDeclarationSyntax>().ToList();

            Assert.Single((IEnumerable)actualMethods);
            Assert.Equal(newSetupMethod.GetText().ToString(), actualMethods.First().GetText().ToString());
        }