public void List_chain_of_constructors_2()
        {
            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/ClassFamily.cs");

            var syntaxTree             = SyntaxOperations.Parse(codeAsText);
            var classDeclarationSyntax = SyntaxOperations.FindClass <BaseUser>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var zeroArgsCtor = SyntaxOperations
                               .FindConstructorsOf(classDeclarationSyntax, semanticModel, argsCount: 0)
                               .Single();


            var oneArgCtor = SyntaxOperations
                             .FindConstructorsOf(classDeclarationSyntax, semanticModel, argsCount: 1)
                             .Single();

            var(firstLink, isCompleted) = ChainOfConstructor.StartingFrom(oneArgCtor, classDeclarationSyntax, semanticModel);

            Assert.IsTrue(isCompleted);

            _Assert_IsIntermadiateLink(firstLink, classDeclarationSyntax, oneArgCtor);

            var secondLink = firstLink.NextLink.Value;

            _Assert_IsLastLink(secondLink, classDeclarationSyntax, zeroArgsCtor);
        }
예제 #2
0
        public void Partial_class_symbol_has_many_syntax_references_depending_on_source_syntax_tree_it_was_obtained()
        {
            var codeAsText1             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part1.cs");
            var syntaxTree1             = SyntaxOperations.Parse(codeAsText1);
            var classDeclarationSyntax1 = SyntaxOperations.FindClass <PartialClass>(syntaxTree1).A;


            var codeAsText2             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part2.cs");
            var syntaxTree2             = SyntaxOperations.Parse(codeAsText2);
            var classDeclarationSyntax2 = SyntaxOperations.FindClass <PartialClass>(syntaxTree2).A;



            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree1, new[] { syntaxTree1, syntaxTree2 });

            var classSymbol1 = semanticModel.GetDeclaredSymbol(classDeclarationSyntax1);
            var classSymbol2 = semanticModel.GetDeclaredSymbol(classDeclarationSyntax1);

            Assert.AreEqual(classSymbol1, classSymbol2);

            var declaringSyntaxRefs1 = classSymbol1.DeclaringSyntaxReferences;
            var declaringSyntaxRefs2 = classSymbol2.DeclaringSyntaxReferences;

            CollectionAssert.AreEquivalent(declaringSyntaxRefs1, declaringSyntaxRefs2);

            Assert.AreEqual(2, declaringSyntaxRefs1.Length);
        }
예제 #3
0
        public void Finds_variable_origins_to_be_parameter_declaration()
        {
            var codeAsText    = File.ReadAllText(@"SyntaxAnalyserExamples/TestFunctions.cs");
            var syntaxTree    = SyntaxOperations.Parse(codeAsText);
            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var methodSyntax = SyntaxOperations
                               .FindMethodsOfClass(syntaxTree, nameof(TestFunctions), nameof(TestFunctions.ReturnSameValue))
                               .Value
                               .Single();


            var methodAnalysis = MethodBlockAnalysis.FromSemanticModel(methodSyntax, semanticModel);

            var returnSyntax = methodAnalysis.BodyBlock.DescendantNodes().OfType <ReturnStatementSyntax>().Single();

            var identSyntax = SyntaxOperations.GetVariable(returnSyntax);


            var origin = SemanticOperations.GetIdentifierDeclarationSyntax(identSyntax, methodAnalysis).Value.A;

            var paramDeclarSyntax = origin.D;

            var paramName = paramDeclarSyntax.Identifier.ToString();

            Assert.AreEqual("x", paramName);
        }
        public void List_chain_of_constructors_3()
        {
            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/ClassFamily.cs");

            var syntaxTree = SyntaxOperations.Parse(codeAsText);

            var classExtUserDeclarationSyntax    = SyntaxOperations.FindClass <ExtendedUser>(syntaxTree).A;
            var classNormalUserDeclarationSyntax = SyntaxOperations.FindClass <NormalUser>(syntaxTree).A;
            var classBaseUserDeclarationSyntax   = SyntaxOperations.FindClass <BaseUser>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);



            var threeArgsCtor = SyntaxOperations
                                .FindConstructorsOf(classExtUserDeclarationSyntax, semanticModel, argsCount: 3)
                                .Single();

            var(firstLink, isCompleted) = ChainOfConstructor.StartingFrom(threeArgsCtor, classExtUserDeclarationSyntax, semanticModel);

            var secondLink = firstLink.NextLink.Value;
            var thirdLink  = secondLink.NextLink.Value;
            var lastlink   = thirdLink.NextLink.Value;

            Assert.IsTrue(isCompleted);

            _Assert_IsIntermadiateLink(firstLink, classExtUserDeclarationSyntax, threeArgsCtor);

            _Assert_IsIntermadiateLink(secondLink, classNormalUserDeclarationSyntax, SyntaxOperations.FindConstructorsOf(classNormalUserDeclarationSyntax, semanticModel, 3).Single());

            _Assert_IsIntermadiateLink(thirdLink, classNormalUserDeclarationSyntax, SyntaxOperations.FindConstructorsOf(classNormalUserDeclarationSyntax, semanticModel, 2).Single());

            _Assert_IsLastLink(lastlink, classBaseUserDeclarationSyntax, SyntaxOperations.FindConstructorsOf(classBaseUserDeclarationSyntax, semanticModel, 0).Single());
        }
        private IXor2ComputationNodeReference _Assert_LocalDeclaration(ComputationGraphNode node, string variableName)
        {
            var localDecl = (LocalDeclarationStatementSyntax)node.Syntax;
            var varName   = SyntaxOperations.GetVariableTokenSingle(localDecl).ToString();

            Assert.AreEqual(variableName, varName);
            return(node.MainAntedecent.Value);
        }
        public void Creates_computation_chain()
        {
            var codeAsText    = File.ReadAllText(@"SyntaxAnalyserExamples/TestFunctions.cs");
            var syntaxTree    = SyntaxOperations.Parse(codeAsText);
            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var methodSyntax = SyntaxOperations
                               .FindMethodsOfClass(syntaxTree, nameof(TestFunctions), nameof(TestFunctions.ProcessInt32Parameter))
                               .Value
                               .Single();

            var methodAnalysis = MethodBlockAnalysis.FromSemanticModel(methodSyntax, semanticModel);

            var returnSyntax = methodAnalysis.BodyBlock.DescendantNodes().OfType <ReturnStatementSyntax>().Single();

            var identSyntax = SyntaxOperations.GetVariable(returnSyntax);


            var chainHead = ComputationGraph.FromReturnStatement(returnSyntax, methodAnalysis, methodAnalysis.MethodDeclarationSyntax.Span).Item1.A;

            var sqrtDoubledIncrementedInt           = _Assert_ReturnStatement(chainHead).A;
            var localDecl_sqrtDoubledIncrementedInt = _Assert_Identifier(sqrtDoubledIncrementedInt, "sqrtDoubledIncrementedInt").A;

            var castToInt = _Assert_LocalDeclaration(localDecl_sqrtDoubledIncrementedInt, "sqrtDoubledIncrementedInt").A;
            var sqrtDoubledIncremented           = _Assert_CastExpression(castToInt, typeof(int), methodAnalysis).A;
            var localDecl_sqrtDoubledIncremented = _Assert_Identifier(sqrtDoubledIncremented, "sqrtDoubledIncremented").A;
            var sqrt = _Assert_LocalDeclaration(localDecl_sqrtDoubledIncremented, "sqrtDoubledIncremented").A;

            var(mathClassSqrt, args_doubledIncremented) = _Assert_Invocation(sqrt, nameof(Math.Sqrt));



            var localDecl_doubledIncremented = _Assert_Identifier(args_doubledIncremented.Single().A, "doubledIncremented").A;
            var parenthesizedExprAdd         = _Assert_LocalDeclaration(localDecl_doubledIncremented, "doubledIncremented").A;

            var binary_add = _Assert_ParenthesizedExpr(parenthesizedExprAdd).A;

            var(doubled, oneLiteral) = _Assert_BinaryExpression(binary_add, operatorToken: SyntaxKind.PlusToken);

            _Assert_Literal(oneLiteral.A, 1);

            var localDecl_doubled = _Assert_Identifier(doubled.A, "doubled").A;


            var parenthesizedExprMult = _Assert_LocalDeclaration(localDecl_doubled, "doubled").A;

            var binary_mult = _Assert_ParenthesizedExpr(parenthesizedExprMult).A;

            var(twoLiteral, x) = _Assert_BinaryExpression(binary_mult, operatorToken: SyntaxKind.AsteriskToken);

            _Assert_Literal(twoLiteral.A, 2);

            var methodParam = _Assert_Identifier(x.A, "x").A;

            _Assert_MethodParam(methodParam, "x", typeof(int), methodAnalysis);
        }
예제 #7
0
        public void Recognises_if_class_contains_aformentioned_ctor_attribute()
        {
            var syntaxTree = SyntaxOperations.Parse(CodeAsText);
            var syntax     = SyntaxOperations.FindClass <A>(syntaxTree).A;

            var semantics = SemanticOperations.GetSemanticModel(syntaxTree);

            var classSymbol = (INamedTypeSymbol)semantics.GetDeclaredSymbol(syntax);

            var isUnderInterest = ThisConstructorMustBeCalledAnalyzer.IsUnderInterest(classSymbol);
        }
예제 #8
0
        public void Creates_semantic_model_of_a_class()
        {
            var classToAnalyze = typeof(ClientDto);

            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/ClientDto.cs");

            var syntaxTree             = SyntaxOperations.Parse(codeAsText);
            var classDeclarationSyntax = SyntaxOperations.FindClass <ClientDto>(syntaxTree);

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <ClientDto>(syntaxTree, semanticModel).A;

            Assert.AreEqual(classToAnalyze.Name, typeSymbol.Name);
        }
예제 #9
0
        public void Computes_value_of_string_literal()
        {
            var codeAsText  = @"var x = ""konik"";";
            var syntaxTree  = SyntaxOperations.Parse(codeAsText);
            var fieldSyntax = syntaxTree.GetRoot().ChildNodes().OfType <FieldDeclarationSyntax>().Single();

            var semantics = SemanticOperations.GetSemanticModel(syntaxTree);


            var literalSyntax = fieldSyntax.DescendantNodes().OfType <LiteralExpressionSyntax>().Single();

            var literalVal = (string)semantics.GetConstantValue(literalSyntax).Value;

            Assert.AreEqual("konik", literalVal);
        }
        public void Semantic_model_can_be_obtained_from_partial_definition_of_a_class_1()
        {
            var codeAsText2             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part2.cs");
            var syntaxTree2             = SyntaxOperations.Parse(codeAsText2);
            var classDeclarationSyntax2 = SyntaxOperations.FindClass <PartialClass>(syntaxTree2).A;
            var semanticModel2          = SemanticOperations.GetSemanticModel(syntaxTree2);


            var twoArgsCtor = SyntaxOperations
                              .FindConstructorsOf(classDeclarationSyntax2, semanticModel2, argsCount: 2)
                              .Single();


            Assert.IsNotNull(twoArgsCtor);
        }
        public void Finds_implicit_constructor()
        {
            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/MyEmptyClass.cs");

            var syntaxTree = SyntaxOperations.Parse(codeAsText);

            var classDeclarationSyntax = SyntaxOperations.FindClass <MyEmptyClass>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <MyEmptyClass>(syntaxTree, semanticModel).A;


            var anonymousConstructor = typeSymbol.Constructors.Single();

            Assert.AreEqual(actual: anonymousConstructor.MethodKind, expected: MethodKind.Constructor);
            Assert.IsTrue(anonymousConstructor.IsImplicitlyDeclared);
        }
        public void Creates_computation_chain_ramifying_code()
        {
            var codeAsText    = File.ReadAllText(@"SyntaxAnalyserExamples/TestFunctions.cs");
            var syntaxTree    = SyntaxOperations.Parse(codeAsText);
            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var methodSyntax = SyntaxOperations
                               .FindMethodsOfClass(syntaxTree, nameof(TestFunctions), nameof(TestFunctions.ProcessInt32arameterWithRamification))
                               .Value
                               .Single();



            var methodAnalysis = MethodBlockAnalysis.FromSemanticModel(methodSyntax, semanticModel);

            var returnSyntax = methodAnalysis.BodyBlock.DescendantNodes().OfType <ReturnStatementSyntax>().Single();

            var chainHead = ComputationGraph.FromReturnStatement(returnSyntax, methodAnalysis, methodAnalysis.MethodDeclarationSyntax.Span).Item1.A;
        }
        public void Creates_computation_chain_from_member_access_expr()
        {
            var codeAsText    = File.ReadAllText(@"SyntaxAnalyserExamples/TestFunctions.cs");
            var syntaxTree    = SyntaxOperations.Parse(codeAsText);
            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var methodSyntax = SyntaxOperations
                               .FindMethodsOfClass(syntaxTree, nameof(TestFunctions), nameof(TestFunctions.ProcessStringParameter))
                               .Value
                               .Single();



            var methodAnalysis = MethodBlockAnalysis.FromSemanticModel(methodSyntax, semanticModel);

            var returnSyntax = methodAnalysis.BodyBlock.DescendantNodes().OfType <ReturnStatementSyntax>().Single();

            var chainHead = ComputationGraph.FromReturnStatement(returnSyntax, methodAnalysis, methodAnalysis.MethodDeclarationSyntax.Span).Item1.A;


            var propAccess_Name = _Assert_ReturnStatement(chainHead);
            var invoc_GetType   = _Assert_MemberAccess(propAccess_Name, nameof(Type.Name)).A;

            var(memberAccess_GetType, args) = _Assert_Invocation(invoc_GetType, nameof(object.GetType));

            CollectionAssert.That.IsEmpty(args);

            var invoc_ElementAt = _Assert_MemberAccess(memberAccess_GetType, nameof(object.GetType)).A;

            var(memberAccess_ElementAt, elementAt_Args) = _Assert_Invocation(invoc_ElementAt, "ElementAt");

            _Assert_Literal(elementAt_Args.Single().A, 2);


            var ident_x = _Assert_MemberAccess(memberAccess_ElementAt, "ElementAt").A;

            var methodParam_x = _Assert_Identifier(ident_x, "x").A;

            _Assert_MethodParam(methodParam_x, "x", typeof(string), methodAnalysis);
        }
예제 #14
0
        public void Finds_all_instance_methods()
        {
            var classToAnalyze = typeof(PersonObject);

            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/PersonObject.cs");

            var syntaxTree             = SyntaxOperations.Parse(codeAsText);
            var classDeclarationSyntax = SyntaxOperations.FindClass <PersonObject>(syntaxTree);

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <PersonObject>(syntaxTree, semanticModel).A;


            var members = typeSymbol.GetMembers();

            var methods = members.Where(x => x.Kind == SymbolKind.Method).Cast <IMethodSymbol>().ToList();

            var staticMethod = methods.Where(x => !x.IsStatic && x.MethodKind == MethodKind.Ordinary).Single();

            Assert.AreEqual(nameof(PersonObject.AddChild), staticMethod.Name);
        }
        public void Partial_chain_of_constructors_can_be_obtained_from_partial_code_2()
        {
            var codeAsText2             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part2.cs");
            var syntaxTree2             = SyntaxOperations.Parse(codeAsText2);
            var classDeclarationSyntax2 = SyntaxOperations.FindClass <PartialClass>(syntaxTree2).A;



            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree2, new[] { syntaxTree2 });


            var twoArgsCtor = SyntaxOperations
                              .FindConstructorsOf(classDeclarationSyntax2, semanticModel, argsCount: 2)
                              .Single();


            var(firstLink, isCompleted) = ChainOfConstructor.StartingFrom(twoArgsCtor, classDeclarationSyntax2, semanticModel);

            Assert.IsFalse(isCompleted);

            _Assert_IsIntermadiateLink(firstLink, classDeclarationSyntax2, twoArgsCtor);
        }
예제 #16
0
        public void Finds_all_readonly_properties()
        {
            var classToAnalyze = typeof(PersonObject);

            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/PersonObject.cs");

            var syntaxTree = SyntaxOperations.Parse(codeAsText);

            var classDeclarationSyntax = SyntaxOperations.FindClass <PersonObject>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <PersonObject>(syntaxTree, semanticModel).A;


            var members = typeSymbol.GetMembers();

            var props = members.Where(x => x.Kind == SymbolKind.Property).Cast <IPropertySymbol>().ToList();

            var readOnlyProps = props.Where(x => x.IsReadOnly).ToList();

            CollectionAssert.AreEquivalent(new[] { nameof(PersonObject.Id), nameof(PersonObject.Name) }, readOnlyProps.Select(x => x.Name).ToArray());
        }
예제 #17
0
        public void Throws_when_not_a_simple_data_flow()
        {
            var codeAsText    = File.ReadAllText(@"SyntaxAnalyserExamples/TestFunctions.cs");
            var syntaxTree    = SyntaxOperations.Parse(codeAsText);
            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var methodSyntax = SyntaxOperations
                               .FindMethodsOfClass(syntaxTree, nameof(TestFunctions), nameof(TestFunctions.IncrementUntil10))
                               .Value
                               .Single();


            var methodAnalysis = MethodBlockAnalysis.FromSemanticModel(methodSyntax, semanticModel);

            var returnSyntax = methodAnalysis.BodyBlock.DescendantNodes().OfType <ReturnStatementSyntax>().Single();

            var identSyntax = SyntaxOperations.GetVariable(returnSyntax);


            var res = SemanticOperations.GetIdentifierDeclarationSyntax(identSyntax, methodAnalysis).Value;

            Assert.IsTrue(res.IsB);
        }