public void MethodName() { string[] methodExpectedNames = new string[] { TestSuite.Class11.Value["Method1Name"], TestSuite.Class11.Value["Method2Name"], TestSuite.Class11.Value["Method3Name"], TestSuite.Class11.Value["Method4Name"], TestSuite.Class11.Value["Method5Name"], TestSuite.Class11.Value["Method6Name"], TestSuite.Class11.Value["Method7Name"], }; IEnumerable<KeyValuePair<SyntaxNode, string>> items = new NodeLocator(Class11SyntaxTree).LocateAll(typeof(MethodDeclarationSyntax)) .Select((node, k) => new KeyValuePair<SyntaxNode, string>(node, methodExpectedNames[k])); foreach (KeyValuePair<SyntaxNode, string> item in items) { Assert.IsNotNull(item.Key, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); MethodDeclarationSyntax methodDeclarationNode = item.Key as MethodDeclarationSyntax; TestRetrieveMethodName(methodDeclarationNode, item.Value); } }
public void GetOnlyProperties() { string source = @" public interface MyInterface { int Property1 { get; } string Property2 { get; } } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(InterfaceDeclarationSyntax)); InterfaceDeclarationSyntax interfaceDeclarationNode = node as InterfaceDeclarationSyntax; // Creating the walker var astWalker = MockedInterfaceASTWalker.Create(interfaceDeclarationNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.InterfaceDeclaration); // Checking signatures Assert.IsNotNull(astWalker.InterfaceDeclaration.Signatures); Assert.IsTrue(astWalker.InterfaceDeclaration.Signatures.Count() > 0); Assert.AreEqual(2, astWalker.InterfaceDeclaration.Signatures.Count()); Assert.IsInstanceOfType(astWalker.InterfaceDeclaration.Signatures.ElementAt(0), typeof(MethodSignatureDeclarationTranslationUnit)); Assert.IsInstanceOfType(astWalker.InterfaceDeclaration.Signatures.ElementAt(1), typeof(MethodSignatureDeclarationTranslationUnit)); }
public void DetectVoidType() { var tree = CSharpSyntaxTree.ParseText(@" using System; public class MyClass { public void Method1() { } public int Method2() { } } "); // First method var node = new NodeLocator(tree).LocateFirst(typeof(MethodDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); var methodDeclarationNode = node as MethodDeclarationSyntax; var helper = new MethodDeclaration(methodDeclarationNode).ReturnType; Assert.IsTrue(helper.IsVoid, "Expected void type!"); // Second method node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); methodDeclarationNode = node as MethodDeclarationSyntax; helper = new MethodDeclaration(methodDeclarationNode).ReturnType; Assert.IsFalse(helper.IsVoid, "Void type not expected!"); }
public void OneClass() { string source = @" public class Class1 { } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)); CompilationUnitSyntax programNode = node as CompilationUnitSyntax; // Creating the walker var astWalker = MockedProgramASTWalker.Create(programNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.Program); // Checking members Assert.IsNotNull(astWalker.Program.Content); Assert.IsTrue(astWalker.Program.Content.Count() > 0); Assert.AreEqual(1, astWalker.Program.Content.Count()); Assert.IsInstanceOfType(astWalker.Program.Content.ElementAt(0), typeof(ClassDeclarationTranslationUnit)); }
private static void Test(string source) { // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(EnumDeclarationSyntax)); var enumDeclarationNode = node as EnumDeclarationSyntax; // Creating the walker var astWalker = MockedEnumASTWalker.Create(enumDeclarationNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.EnumDeclaration); // Checking signatures Assert.IsNotNull(astWalker.EnumDeclaration.Members); Assert.IsTrue(astWalker.EnumDeclaration.Members.Count() > 0); Assert.AreEqual(3, astWalker.EnumDeclaration.Members.Count()); Assert.IsInstanceOfType(astWalker.EnumDeclaration.Members.ElementAt(0), typeof(EnumMemberTranslationUnit)); Assert.IsInstanceOfType(astWalker.EnumDeclaration.Members.ElementAt(1), typeof(EnumMemberTranslationUnit)); Assert.IsInstanceOfType(astWalker.EnumDeclaration.Members.ElementAt(2), typeof(EnumMemberTranslationUnit)); }
public void ConstructorWithVariableDeclarations() { string source = @" class Class1 { public Class1() { string var1; int var2; } } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(ConstructorDeclarationSyntax)); ConstructorDeclarationSyntax constructorDeclarationNode = node as ConstructorDeclarationSyntax; // Creating the walker var astWalker = MockedConstructorASTWalker.Create(constructorDeclarationNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.ConstructorDeclaration); // Checking members Assert.IsNotNull(astWalker.ConstructorDeclaration.Statements); Assert.IsTrue(astWalker.ConstructorDeclaration.Statements.Count() > 0); Assert.AreEqual(2, astWalker.ConstructorDeclaration.Statements.Count()); Assert.IsInstanceOfType(astWalker.ConstructorDeclaration.Statements.ElementAt(0), typeof(StatementTranslationUnit)); Assert.IsInstanceOfType(astWalker.ConstructorDeclaration.Statements.ElementAt(1), typeof(StatementTranslationUnit)); }
public void FieldMembers() { string source = @" public class MyClass { private int myInt; public string MyString; } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(ClassDeclarationSyntax)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; // Creating the walker var astWalker = MockedClassASTWalker.Create(classDeclarationNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.ClassDeclaration); // Checking members Assert.IsNotNull(astWalker.ClassDeclaration.MemberDeclarations); Assert.IsTrue(astWalker.ClassDeclaration.MemberDeclarations.Count() > 0); Assert.AreEqual(2, astWalker.ClassDeclaration.MemberDeclarations.Count()); Assert.IsInstanceOfType(astWalker.ClassDeclaration.MemberDeclarations.ElementAt(0), typeof(FieldDeclarationTranslationUnit)); Assert.IsInstanceOfType(astWalker.ClassDeclaration.MemberDeclarations.ElementAt(1), typeof(FieldDeclarationTranslationUnit)); }
public void WalkASTDeep() { string source = @" namespace MyNamespaceA { public class MyClassA { } public class MyClassB { } namespace MyNamespaceB { public class MyClassC { } public class MyClassD { } } } "; int traversedClassesCount = 0; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); var node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)) as CSharpSyntaxNode; // Calling the walker var astWalker = new MultiPurposeASTWalker(node, syntaxNode => syntaxNode as ClassDeclarationSyntax != null, delegate { traversedClassesCount++; }, false); astWalker.Start(); // Checking Assert.AreEqual(4, traversedClassesCount, "Expected walker to deep traverse AST!"); }
public void EmptyMethodWith2Parameters() { string source = @" public void Method(string param1, int param2) { } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax)); MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax; // Creating the walker var astWalker = MockedMethodASTWalker.Create(methodDeclarationNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.MethodDeclaration); // Checking members Assert.IsNotNull(astWalker.MethodDeclaration.Arguments); Assert.IsTrue(astWalker.MethodDeclaration.Arguments.Count() > 0); Assert.AreEqual(2, astWalker.MethodDeclaration.Arguments.Count()); Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(0), typeof(ArgumentDefinitionTranslationUnit)); Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(1), typeof(ArgumentDefinitionTranslationUnit)); }
public void BaseClassNameFromClass() { SyntaxNode node = new NodeLocator(Class1SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(ClassDeclarationSyntax).Name)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; TestRetrieveBaseClassName(classDeclarationNode, Class1SemanticModel, null); }
public void BaseClassNameFromClassWithInheritance() { SyntaxNode node = new NodeLocator(Class2SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(ClassDeclarationSyntax).Name)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; TestRetrieveBaseClassName(classDeclarationNode, Class2SemanticModel, TestSuite.Class2.Value["BaseClassName"]); }
public void InterfaceTypeNameFromBaseList() { SyntaxNode node = new NodeLocator(Class3SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(ClassDeclarationSyntax).Name)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; BaseTypeSyntax baseTypeNode = classDeclarationNode.BaseList.Types.FirstOrDefault(); TestRetrieveTypeName(baseTypeNode, TestSuite.Class3.Value["Interface1Name"]); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <returns></returns> public static T GetNode(string source) { var syntaxTree = CSharpSyntaxTree.ParseText(source); var node = new NodeLocator(syntaxTree).LocateFirst(typeof(T)); if (node == null) { throw new InvalidOperationException($"Node of type `{typeof(T).Name}` should be found!"); } return(node as T); }
private static string GetTranslation(string source) { // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)); CompilationUnitSyntax compilationUnitNode = node as CompilationUnitSyntax; // Creating the walker var astWalker = ProgramDefinitionASTWalker.Create(compilationUnitNode); // Getting the translation unit ITranslationUnit translationUnit = astWalker.Walk(); return translationUnit.Translate(); }
public void Empty2IfsElseStatementWithBlocks() { string source = @" public class Class1 { public void Method1() { if (true) { } else if (false) { } else { } } } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(IfStatementSyntax)); IfStatementSyntax ifStatementNode = node as IfStatementSyntax; // Creating the walker var astWalker = MockedConditionalStatementASTWalker.Create(ifStatementNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.Statement); // Checking members Assert.IsNotNull(astWalker.Statement.Bodies); Assert.IsTrue(astWalker.Statement.Bodies.Count() > 0); Assert.AreEqual(2, astWalker.Statement.Bodies.Count()); Assert.IsInstanceOfType(astWalker.Statement.Bodies.ElementAt(0), typeof(StatementsGroupTranslationUnit)); Assert.IsInstanceOfType(astWalker.Statement.Bodies.ElementAt(1), typeof(StatementsGroupTranslationUnit)); Assert.IsNotNull(astWalker.Statement.TestExpressions); Assert.IsTrue(astWalker.Statement.TestExpressions.Count() > 0); Assert.AreEqual(2, astWalker.Statement.TestExpressions.Count()); Assert.IsInstanceOfType(astWalker.Statement.TestExpressions.ElementAt(0), typeof(LiteralTranslationUnit<bool>)); Assert.IsInstanceOfType(astWalker.Statement.TestExpressions.ElementAt(1), typeof(LiteralTranslationUnit<bool>)); Assert.IsNotNull(astWalker.Statement.LastBody); Assert.IsInstanceOfType(astWalker.Statement.LastBody, typeof(StatementsGroupTranslationUnit)); }
public string RenderSimpleEmptyClass() { var sourceInfo = SourceGenerator.Generate(SourceOptions.None); string source = sourceInfo.Key; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(ClassDeclarationSyntax)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; // Creating the walker var astWalker = ClassASTWalker.Create(classDeclarationNode); // Getting the translation unit ITranslationUnit translationUnit = astWalker.Walk(); return translationUnit.Translate(); }
public void DecoratingClass() { SyntaxTree tree = CSharpSyntaxTree.ParseText(@" using System; namespace Namespace1 { [Obsolete] public class Class1 { } public class Class2 { public Class1 Method1() { return null; } } }" ); // Second class var node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); // Loading MSCoreLib var compilation = CSharpCompilation.Create("TestAssembly") .AddReferences( MetadataReference.CreateFromFile( typeof(object).Assembly.Location)) .AddSyntaxTrees(tree); var semanticModel = compilation.GetSemanticModel(tree); var methodDeclarationNode = node as MethodDeclarationSyntax; var helper = new MethodDeclaration(methodDeclarationNode, semanticModel).ReturnType; var attributes = helper.Attributes; Assert.IsNotNull(attributes); Assert.AreNotEqual(0, attributes.Count(), "Expecting attributes!"); var attribute = attributes.First(); Assert.AreEqual("ObsoleteAttribute", attribute.AttributeClassName, "Wrong name!"); Assert.AreEqual("System.ObsoleteAttribute", attribute.AttributeClassFullName, "Wrong full name!"); Assert.IsNotNull(attribute.ConstructorArguments); Assert.AreEqual(0, attribute.ConstructorArguments.Count(), "Expected no constructor arguments"); }
public void ClassDeclarations() { string source = @" namespace MyNamespace { public class Class1 { } private class Class2 { } internal class Class3 { } } "; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(NamespaceDeclarationSyntax)); NamespaceDeclarationSyntax namespaceNode = node as NamespaceDeclarationSyntax; // Creating the walker var astWalker = MockedNamespaceASTWalker.Create(namespaceNode); // Getting the translation unit astWalker.Walk(); // Checking Assert.IsNotNull(astWalker.Module); // Checking members Assert.IsNotNull(astWalker.Module.Classes); Assert.IsTrue(astWalker.Module.Classes.Count() > 0); Assert.AreEqual(3, astWalker.Module.Classes.Count()); Assert.IsInstanceOfType(astWalker.Module.Classes.ElementAt(0), typeof(ClassDeclarationTranslationUnit)); Assert.IsInstanceOfType(astWalker.Module.Classes.ElementAt(1), typeof(ClassDeclarationTranslationUnit)); Assert.IsInstanceOfType(astWalker.Module.Classes.ElementAt(2), typeof(ClassDeclarationTranslationUnit)); }
private static void RetrieveNumberOfBodiesAndTest(SyntaxTree syntaxTree, int expectedNumberOfBodies) { SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(IfStatementSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(IfStatementSyntax).Name)); IfStatementSyntax conditionalNode = node as IfStatementSyntax; TestRetrievedNumberOfBodies(conditionalNode, expectedNumberOfBodies); }
private static void DetermineLastElseAndTest(SyntaxTree syntaxTree, bool expectedElse) { SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(IfStatementSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(IfStatementSyntax).Name)); IfStatementSyntax conditionalNode = node as IfStatementSyntax; TestDeterminedLastElseClause(conditionalNode, expectedElse); }
public void MethodParametersAreOfCorrectType() { var source = @" class Class1 { public void Method1(int param1) { } public void Method2(int param1, string param2) { } public void Method3(int param1, string param2, object param3) { } public void Method4(int param1, string param2, object param3, bool param4) { } } "; var syntaxTree = CSharpSyntaxTree.ParseText(source); var semanticModel = CSharpCompilation.Create("Class").AddReferences( MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees( syntaxTree).GetSemanticModel(syntaxTree); IEnumerable<SyntaxNode> nodes = new NodeLocator(syntaxTree).LocateAll( typeof(MethodDeclarationSyntax)); foreach (SyntaxNode node in nodes) { Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax; MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode); foreach (var param in methodDeclaration.Parameters) { Assert.IsNotNull(param, "Parameter should not be null!"); Assert.IsInstanceOfType(param, typeof(Parameter), "Wrong parameter type!"); } } }
public void WalkASTOnlyRootLevel() { string source = @" namespace MyNamespaceA { public class MyClassA { } public class MyClassB { } namespace MyNamespaceB { public class MyClassC { } public class MyClassD { } } } namespace MyNamespaceC { } "; int traversedClassesCount = 0; int traversedNamespacesCount = 0; // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); var node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)) as CSharpSyntaxNode; // Calling the walker var astClassWalker = new MultiPurposeASTWalker(node, syntaxNode => syntaxNode as ClassDeclarationSyntax != null, delegate { traversedClassesCount++; }, true); astClassWalker.Start(); //var astNamespaceWalker = new MultiPurposeASTWalker(node, // syntaxNode => syntaxNode as NamespaceDeclarationSyntax != null, // delegate // { // traversedNamespacesCount++; // }, // true); //astNamespaceWalker.Start(); // Checking Assert.AreEqual(0, traversedClassesCount, "Expected walker to root level traverse AST!"); //Assert.AreEqual(2, traversedNamespacesCount, "Expected walker to root level traverse AST!"); }
public void MethodReturnTypeVoid() { IEnumerable<SyntaxNode> nodes = new NodeLocator(Class11SyntaxTree).LocateAll( typeof(MethodDeclarationSyntax)).Take(3); // The first 3 have void return type foreach (SyntaxNode node in nodes) { Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax; TestRetrieveMethodReturnType(methodDeclarationNode); } }
public void InterfaceNameFromClassWithInterface() { SyntaxNode node = new NodeLocator(Class3SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(ClassDeclarationSyntax).Name)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; TestRetrieveInterfacesName(classDeclarationNode, Class3SemanticModel, new string[] { TestSuite.Class3.Value["Interface1Name"] }); }
public void ClassNameFromSimpleClass() { SyntaxNode node = new NodeLocator(Class1SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(ClassDeclarationSyntax).Name)); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; TestRetrieveClassName(classDeclarationNode, TestSuite.Class1.Value["ClassName"]); }
public void TypeFullNameRenderingSemanticModel() { var tree = CSharpSyntaxTree.ParseText(@" using System; public class MyClass : IDisposable { public void Dispose() { } } "); var node = new NodeLocator(tree).LocateLast(typeof(ClassDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(ClassDeclarationSyntax).Name)); // Loading MSCoreLib var compilation = CSharpCompilation.Create("TestAssembly") .AddReferences( MetadataReference.CreateFromFile( typeof(object).Assembly.Location)) .AddSyntaxTrees(tree); var semanticModel = compilation.GetSemanticModel(tree); ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax; BaseTypeSyntax baseTypeNode = classDeclarationNode.BaseList.Types.FirstOrDefault(); TestRetrieveTypeName(baseTypeNode, null, "IDisposable"); TestRetrieveTypeName(baseTypeNode, semanticModel, "IDisposable"); TestRetrieveTypeFullName(baseTypeNode, null, "IDisposable"); TestRetrieveTypeFullName(baseTypeNode, semanticModel, "System.IDisposable"); }
public void MethodWithNoParameters() { var source = @" class Class1 { public void Method1() { } } "; var syntaxTree = CSharpSyntaxTree.ParseText(source); var semanticModel = CSharpCompilation.Create("Class").AddReferences( MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees( syntaxTree).GetSemanticModel(syntaxTree); SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(MethodDeclarationSyntax)); Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax; MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode); Assert.IsNotNull(methodDeclaration.Parameters, "Expecting list of parameters not to be null!"); Assert.AreEqual(0, methodDeclaration.Parameters.Count(), "Expecting no parameters!"); }
private static string GetTranslation(string source) { // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); var node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)) as CSharpSyntaxNode; // Transforming new ScriptNamespaceBasedASTTransformer().Transform(ref tree); var programNode = node as CompilationUnitSyntax; // Creating the walker var astWalker = ProgramDefinitionASTWalker.Create(programNode); // Getting the translation unit ITranslationUnit translationUnit = astWalker.Walk(); return translationUnit.Translate(); }
public void MethodWithParameters() { var source = @" class Class1 { public void Method1(int param1) { } public void Method2(int param1, string param2) { } public void Method3(int param1, string param2, object param3) { } public void Method4(int param1, string param2, object param3, bool param4) { } } "; var syntaxTree = CSharpSyntaxTree.ParseText(source); var semanticModel = CSharpCompilation.Create("Class").AddReferences( MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees( syntaxTree).GetSemanticModel(syntaxTree); IEnumerable<SyntaxNode> nodes = new NodeLocator(syntaxTree).LocateAll( typeof(MethodDeclarationSyntax)); foreach (SyntaxNode node in nodes) { Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!", typeof(MethodDeclarationSyntax).Name)); MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax; MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode); Assert.IsNotNull(methodDeclaration.Parameters, "Expecting list of parameters not to be null!"); Assert.IsTrue(methodDeclaration.Parameters.Count() > 0, "Expecting parameters!"); } // 1 parameter MethodDeclaration method1Declaration = new MethodDeclaration(nodes.ElementAt(0) as MethodDeclarationSyntax); Assert.AreEqual(1, method1Declaration.Parameters.Count(), "Expecting different number of paramters!"); // 2 parameters MethodDeclaration method2Declaration = new MethodDeclaration(nodes.ElementAt(1) as MethodDeclarationSyntax); Assert.AreEqual(2, method2Declaration.Parameters.Count(), "Expecting different number of paramters!"); // 3 parameters MethodDeclaration method3Declaration = new MethodDeclaration(nodes.ElementAt(2) as MethodDeclarationSyntax); Assert.AreEqual(3, method3Declaration.Parameters.Count(), "Expecting different number of paramters!"); // 4 parameters MethodDeclaration method4Declaration = new MethodDeclaration(nodes.ElementAt(3) as MethodDeclarationSyntax); Assert.AreEqual(4, method4Declaration.Parameters.Count(), "Expecting different number of paramters!"); }
private static string GetTranslation(string source) { // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax)); var methodDeclarationNode = node as MethodDeclarationSyntax; // Creating the walker var astWalker = MethodASTWalker.Create(methodDeclarationNode); // Getting the translation unit ITranslationUnit translationUnit = astWalker.Walk(); return translationUnit.Translate(); }
private static MockedClassDefinitionASTWalker ParseClass(string source, bool generateTranslationUniOnProtectedMembers = false) { // Getting the AST node CSharpSyntaxTree tree = ASTExtractor.Extract(source); SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(ClassDeclarationSyntax)); var classDeclarationNode = node as ClassDeclarationSyntax; // Creating the walker var astWalker = MockedClassDefinitionASTWalker.Create(classDeclarationNode, generateTranslationUniOnProtectedMembers); // Getting the translation unit astWalker.Walk(); return astWalker; }