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 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); }
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); }
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); }
private void _Assert_MethodParam(ComputationGraphNode node, string paramName, Type paramType, MethodBlockAnalysis methodAnalysis) { var syntax = (ParameterSyntax)node.Syntax; var expectedType = $"{paramType.Namespace}.{paramType.Name}"; var typeInfo = methodAnalysis.SemanticModel.GetTypeInfo(syntax.Type); var actualType = $"{typeInfo.Type.ContainingNamespace}.{typeInfo.Type.Name}"; Assert.AreEqual(paramName, syntax.Identifier.ToString()); Assert.AreEqual(expectedType, actualType); Assert.IsFalse(node.MainAntedecent.Exists); }
private IXor2ComputationNodeReference _Assert_CastExpression(ComputationGraphNode node, Type type, MethodBlockAnalysis methodAnalysis) { var cast = (CastExpressionSyntax)node.Syntax; var typeActual = methodAnalysis.SemanticModel.GetTypeInfo(cast.Type); var typeActualFullName = $"{typeActual.Type.ContainingNamespace}.{typeActual.Type.Name}"; Assert.AreEqual(type.FullName, typeActualFullName); return(node.MainAntedecent.Value); }