Exemplo n.º 1
0
        public async Task ReplacesSetupMethodInClass()
        {
            var document  = DocumentProvider.CreateDocumentFromFile(TestClassWithEmptyConstructor);
            var root      = document.GetSyntaxRootAsync().Result;
            var testClass = root.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();

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

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

            var existingSetupMethod = root.DescendantNodes().OfType <ConstructorDeclarationSyntax>().First();

            _memberFinder.Setup(_ =>
                                _.FindSimilarNode(It.IsAny <SyntaxList <MemberDeclarationSyntax> >(),
                                                  It.Is <SyntaxNode>(s => s == newSetupMethod)))
            .Returns(existingSetupMethod);

            var actual = await _target.WithSetupMethod(newSetupMethod)
                         .BuildAsync(new CancellationToken());

            var actualRoot = await actual.GetSyntaxRootAsync();

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

            Assert.Single((IEnumerable)actualMethods);
            Assert.Equal(newSetupMethod.GetText().ToString(), actualMethods.First().GetText().ToString());
        }
Exemplo n.º 2
0
        public void Returns_CorrectName_For_Parameter(string filePath, string parameterName, string expected)
        {
            var parameterSyntax = SyntaxNodeProvider.GetSyntaxNodeFromFile <ParameterSyntax>(filePath, parameterName);

            var actual = _target.GetFromParameter(parameterSyntax);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 3
0
        public void GeneratesFieldNameWithUnderscoreAndLowerCase(string filePath, string parameterType, string expected)
        {
            var parameterSyntax = SyntaxNodeProvider.GetSyntaxNodeFromFile <ParameterSyntax>(filePath, parameterType);

            var actual = _target.GetFromParameter(parameterSyntax);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        public void Returns_SimilarClassSyntaxNode_IfExists(string filePath, string filePathCompared, string constructorOrMethodName, bool expected)
        {
            var memberSyntax = SyntaxNodeProvider.GetSyntaxNodeFromFile <MemberDeclarationSyntax>(filePath, constructorOrMethodName);
            var classSyntax  = SyntaxNodeProvider.GetSyntaxNodeFromFile <ClassDeclarationSyntax>(filePathCompared, "TestClass");

            var actual = _target.FindSimilarNode(classSyntax.Members, memberSyntax);

            Assert.Equal(expected, actual != null);
        }
Exemplo n.º 5
0
        public void CanFindSimilarField_When_FieldExistsWithSameType(string searchInFile)
        {
            var fieldSyntax = FieldDeclarationProvider.GetFieldDeclarationFromFile(FileContainingFieldToUse, FieldNameToUse);
            var classSyntax = SyntaxNodeProvider.GetSyntaxNodeFromFile <ClassDeclarationSyntax>(searchInFile, ClassNameToUse);

            var actual = _target.FindSimilarNode(classSyntax.Members, fieldSyntax);

            Assert.NotNull(actual);
        }
        public void Returns_FieldDeclarationsWithCorrectName_ForEach_Parameter(string filePath, string parameterName, string expected)
        {
            var parameterSyntax = SyntaxNodeProvider.GetSyntaxNodeFromFile <ParameterSyntax>(filePath, parameterName);
            var syntaxGenerator = new SyntaxGeneratorProvider().GetSyntaxGenerator();

            var actual = _target.GetGenericFieldDeclaration(parameterSyntax, "Mock", syntaxGenerator);

            var actualTextWithoutSpaces   = actual.GetText().ToString().Replace(" ", "");
            var expectedTextWithoutSpaces = expected.Replace(" ", "");

            Assert.Equal(expectedTextWithoutSpaces, actualTextWithoutSpaces);
        }
        public void ReturnsAllConstructorParameters()
        {
            var textFilePath           = "files.Class_WithConstructorWithTwoParameters.txt";
            var classDeclarationSyntax = SyntaxNodeProvider.GetSyntaxNodeFromFile <ClassDeclarationSyntax>(textFilePath, "TestClass");

            var actual = _target.GetParametersOfConstructor(classDeclarationSyntax).ToList();

            Assert.Equal(2, actual.Count);

            var firstParameter = actual.First();

            Assert.Equal("someType", firstParameter.Identifier.Text);
            Assert.Equal("ISomeType", firstParameter.Type.ToString());

            var secondParameter = actual.Skip(1).First();

            Assert.Equal("someOtherType", secondParameter.Identifier.Text);
            Assert.Equal("ISomeOtherType", secondParameter.Type.ToString());
        }
Exemplo n.º 8
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());
        }