private static void TestVisibility(string source, string enumName, SyntaxKind?expectedVisibility) { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(source); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the enum ITypeInfoProxy enumDefinition = assembly.LocateType(enumName); Assert.IsNotNull(enumDefinition); // Generating the AST var factory = new EnumDeclarationSyntaxFactory(enumDefinition); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(EnumDeclarationSyntax), "Expected an enum declaration node to be built"); var enumDeclarationSyntaxNode = syntaxNode as EnumDeclarationSyntax; var modifiers = enumDeclarationSyntaxNode.Modifiers; if (expectedVisibility.HasValue) { Assert.IsTrue(Utils.CheckModifier(modifiers, expectedVisibility.Value), "Enum does not have correct visibility"); return; } Assert.AreEqual(0, modifiers.Count(), "Expected no modifier"); }
public void ConstructorCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public class MyClass { public MyClass() { } } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Locating the ctor IConstructorInfoProxy ctorDeclaration = classDefinition.LocateConstructor(0); Assert.IsNotNull(ctorDeclaration); // Generating the AST var factory = new ConstructorDeclarationSyntaxFactory(ctorDeclaration, classDefinition); var syntaxNode = factory.Create() as ConstructorDeclarationSyntax; Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(ConstructorDeclarationSyntax), "Expected a constructor declaration node to be built"); }
private static void TestVisibility(string source, string className, string methodName, SyntaxKind expectedVisibility) { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(source); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType(className); Assert.IsNotNull(classDefinition); // Locating the method IMethodInfoProxy methodDeclaration = classDefinition.LocateMethod(methodName); Assert.IsNotNull(methodDeclaration); // Generating the AST var factory = new MethodDeclarationSyntaxFactory(methodDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(MethodDeclarationSyntax), "Expected a method declaration node to be built"); var methodDeclarationSyntaxNode = syntaxNode as MethodDeclarationSyntax; var modifiers = methodDeclarationSyntaxNode.Modifiers; Assert.IsTrue(Utils.CheckModifier(modifiers, expectedVisibility), "Method does not have correct visibility"); }
public void ImplicitObjectClassInheritanceIsNotGenerated() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" class MyClass { } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Generating the AST var factory = new ClassDeclarationSyntaxFactory(classDefinition); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(ClassDeclarationSyntax), "Expected a class declaration node to be built"); var classDeclarationSyntaxNode = syntaxNode as ClassDeclarationSyntax; var baseList = classDeclarationSyntaxNode.BaseList; Assert.IsNull(baseList, "No base class should have been generated"); }
public void NameCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" class MyClass { } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Generating the AST var factory = new ClassDeclarationSyntaxFactory(classDefinition); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(ClassDeclarationSyntax), "Expected a class declaration node to be built"); var classDeclarationSyntaxNode = syntaxNode as ClassDeclarationSyntax; var name = classDeclarationSyntaxNode.Identifier.Text; Assert.AreEqual("MyClass", name, "Class name not correctly acquired"); }
public void NameCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public enum MyEnum { } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the enum ITypeInfoProxy enumDeclaration = assembly.LocateType("MyEnum"); Assert.IsNotNull(enumDeclaration); // Generating the AST var factory = new EnumDeclarationSyntaxFactory(enumDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(EnumDeclarationSyntax), "Expected an enum declaration node to be built"); var enumDeclarationSyntaxNode = syntaxNode as EnumDeclarationSyntax; var name = enumDeclarationSyntaxNode.Identifier.Text; Assert.AreEqual("MyEnum", name, "Enum name not correctly acquired"); }
public void NameCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" interface IMyInterface { } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the interface ITypeInfoProxy interfaceDefinition = assembly.LocateType("IMyInterface"); Assert.IsNotNull(interfaceDefinition); // Generating the AST var factory = new InterfaceDeclarationSyntaxFactory(interfaceDefinition); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(InterfaceDeclarationSyntax), "Expected an interface declaration node to be built"); var interfaceDeclarationSyntaxNode = syntaxNode as InterfaceDeclarationSyntax; var name = interfaceDeclarationSyntaxNode.Identifier.Text; Assert.AreEqual("IMyInterface", name, "Interface name not correctly acquired"); }
public void ArgumentsCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public class MyClass { public void MyMethod(string param1, int param2, double param3) { } } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Locating the ctor IMethodInfoProxy methodDeclaration = classDefinition.LocateMethod("MyMethod"); Assert.IsNotNull(methodDeclaration); // Generating the AST var factory = new MethodDeclarationSyntaxFactory(methodDeclaration); var syntaxNode = factory.Create() as MethodDeclarationSyntax; Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(MethodDeclarationSyntax), "Expected a method declaration node to be built"); var args = syntaxNode.ParameterList.Parameters; Assert.AreEqual(3, args.Count, "Expected 3 parameters"); Action <int, string, string> ParamChecker = (index, expectedName, expectedTypeFullName) => { var @param = args[index]; Assert.IsNotNull(@param, "Parameter expected"); var identifier = @param.Identifier; Assert.IsNotNull(identifier, "Identifier expected"); Assert.AreEqual(identifier.ToString(), expectedName, "Parameter name does not match"); var type = @param.Type; Assert.IsNotNull(type, "Type expected"); var typeIdentifier = type as QualifiedNameSyntax; Assert.IsNotNull(typeIdentifier, "Type expected to be qualified name"); Assert.AreEqual(type.ToString(), expectedTypeFullName, "Parameter name does not match"); }; ParamChecker(0, "param1", "System.String"); ParamChecker(1, "param2", "System.Int32"); ParamChecker(2, "param3", "System.Double"); }
/// <summary> /// Initializes a new instance of the <see cref="ClassDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="classInfo"></param> public ClassDeclarationSyntaxFactory(ITypeInfoProxy classInfo) { if (classInfo == null) { throw new ArgumentNullException(nameof(classInfo)); } this.classInfo = classInfo; }
private MemberDeclarationSyntax BuildInterfaceNode(ITypeInfoProxy type) { if (this.IsLoggingEnabled) { new TypeInfoLogger(type, this.Logger).LogSyntaxNodeCreation("Interface"); } return(this.BuildNode(type, this.BuildInterfaceNodeCore(type))); }
/// <summary> /// Initializes a new instance of the <see cref="InterfaceDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="classInfo"></param> public InterfaceDeclarationSyntaxFactory(ITypeInfoProxy interfaceInfo) { if (interfaceInfo == null) { throw new ArgumentNullException(nameof(interfaceInfo)); } this.interfaceInfo = interfaceInfo; }
/// <summary> /// Initializes a new instance of the <see cref="Visibility"/> class. /// </summary> /// <param name="type">The <see cref="ITypeInfoProxy"/> to analyze.</param> public Visibility(ITypeInfoProxy type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } this.type = type; }
/// <summary> /// Initializes a new instance of the <see cref="NativeClass"/> class. /// </summary> /// <param name="typeInfo">The <see cref="ITypeInfoProxy"/> to analyze.</param> public NativeClass(ITypeInfoProxy typeInfo) { if (typeInfo == null) { throw new ArgumentNullException(nameof(typeInfo)); } this.typeInfo = typeInfo; }
/// <summary> /// Initializes a new instance of the <see cref="EnumDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="classInfo"></param> public EnumDeclarationSyntaxFactory(ITypeInfoProxy enumInfo) { if (enumInfo == null) { throw new ArgumentNullException(nameof(enumInfo)); } this.enumInfo = enumInfo; }
public void NoBlackListed() { var types = new ITypeInfoProxy[] { new MockedTypeInfoProxy("TypeA"), new MockedTypeInfoProxy("TypeB") }; Test(types, types, new string[] { "TypeC", "TypeD" }); }
public void EmptyBlackList() { var types = new ITypeInfoProxy[] { new MockedTypeInfoProxy("TypeA"), new MockedTypeInfoProxy("TypeB") }; Test(types, types, new string[0]); }
/// <summary> /// Initializes a new instance of the <see cref="ConstructorDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="ctorInfo"></param> /// <param name="classInfo"></param> /// <param name="typeLookup"></param> public ConstructorDeclarationSyntaxFactory(IConstructorInfoProxy ctorInfo, ITypeInfoProxy classInfo, ITypeLookup typeLookup) : base(ctorInfo, classInfo) { if (typeLookup == null) { throw new ArgumentNullException(nameof(typeLookup)); } this.typeLookup = typeLookup; }
/// <summary> /// Initializes a new instance of the <see cref="InterfaceDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="interfaceInfo"></param> /// <param name="typeLookup"></param> public InterfaceDeclarationSyntaxFactory(ITypeInfoProxy interfaceInfo, ITypeLookup typeLookup) : base(interfaceInfo) { if (typeLookup == null) { throw new ArgumentNullException(nameof(typeLookup)); } this.typeLookup = typeLookup; }
private MemberDeclarationSyntax BuildStructNode(ITypeInfoProxy type) { if (this.IsLoggingEnabled) { new TypeInfoLogger(type, this.Logger).LogSyntaxNodeCreation("Struct"); } // TODO return(this.BuildNode(type, SyntaxFactory.StructDeclaration)); }
/// <summary> /// Gets a value indicating whether the type is an actual enum or not. /// </summary> /// <param name="typeInfo">The <see cref="ITypeInfoProxy"/> to check.</param> /// <returns><code>true</code> if the type is an actual enum, <code>false</code> otherwise.</returns> public static bool IsNativeEnumType(this ITypeInfoProxy typeInfo) { if (typeInfo.IsClass && typeInfo.BaseType != null) { var isEnum = new EnumClass(typeInfo.BaseType).Is; return(isEnum); } return(typeInfo.IsEnum); }
/// <summary> /// Gets a value indicating whether the type is an actual struct or not. /// </summary> /// <param name="typeInfo">The <see cref="ITypeInfoProxy"/> to check.</param> /// <returns><code>true</code> if the type is an actual struct, <code>false</code> otherwise.</returns> public static bool IsNativeStructType(this ITypeInfoProxy typeInfo) { if (typeInfo.IsClass && typeInfo.BaseType != null) { var isValueType = new ValueTypeClass(typeInfo.BaseType).Is; return(isValueType); } return(typeInfo.IsValueType && !typeInfo.IsEnum); }
private static void TestArgumentsCorrectlyAcquired(string source, string className, string methodName, string[] expectedParamNames, string[] expectedParamTypeFullNames) { // Assembling some code IAssemblyLoader assemblyLoader = new ScriptSharpReflectionUtils.AsmlDasmlAssemblyLoader(source, ScriptNamespaceAttributeHelper.AttributeSourceCode); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); ITypeLookup lookup = new LinearSearchTypeLookup(assembly); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType(className); Assert.IsNotNull(classDefinition); // Locating the method IMethodInfoProxy methodDeclaration = classDefinition.LocateMethod(methodName); Assert.IsNotNull(methodDeclaration); // Generating the AST var factory = new MethodDeclarationSyntaxFactory(methodDeclaration, lookup); var syntaxNode = factory.Create() as MethodDeclarationSyntax; Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(MethodDeclarationSyntax), "Expected a method declaration node to be built"); var args = syntaxNode.ParameterList.Parameters; Assert.AreEqual(expectedParamNames.Length, args.Count, $"Expected {expectedParamNames.Length} parameters"); Action <int, string, string> ParamChecker = (index, expectedName, expectedTypeFullName) => { var @param = args[index]; Assert.IsNotNull(@param, "Parameter expected"); var identifier = @param.Identifier; Assert.IsNotNull(identifier, "Identifier expected"); Assert.AreEqual(identifier.ToString(), expectedName, "Parameter name does not match"); var type = @param.Type; Assert.IsNotNull(type, "Type expected"); var typeIdentifier = type as QualifiedNameSyntax; Assert.IsNotNull(typeIdentifier, "Type expected to be qualified name"); Assert.AreEqual(type.ToString(), expectedTypeFullName, "Parameter name does not match"); }; for (int i = 0; i < expectedParamNames.Length; i++) { ParamChecker(i, expectedParamNames[i], expectedParamTypeFullNames[i]); } }
/// <summary> /// Finds a <see cref="IMethodInfoProxy"/> into an <see cref="ITypeInfoProxy"/> by looking at the name (partial match). /// </summary> /// <param name="type"></param> /// <param name="methodName"></param> /// <returns></returns> public static IMethodInfoProxy LocateMethod(this ITypeInfoProxy type, string methodName) { foreach (var method in type.DeclaredMethods) { if (method.Name.Contains(methodName)) { return method; } } return null; }
/// <summary> /// Finds a <see cref="IConstructorInfoProxy"/> into an <see cref="ITypeInfoProxy"/>. /// </summary> /// <param name="type"></param> /// <param name="argsNum"></param> /// <returns></returns> public static IConstructorInfoProxy LocateConstructor(this ITypeInfoProxy type, int argsNum) { foreach (var ctor in type.DeclaredConstructors) { if ((argsNum == 0 && ctor.Parameters == null) || ctor.Parameters.Count() == argsNum) { return ctor; } } return null; }
/// <summary> /// Finds a <see cref="IPropertyInfoProxy"/> into an <see cref="ITypeInfoProxy"/> by looking at the name (partial match). /// </summary> /// <param name="type"></param> /// <param name="propertyName"></param> /// <returns></returns> public static IPropertyInfoProxy LocateProperty(this ITypeInfoProxy type, string propertyName) { foreach (var property in type.DeclaredProperties) { if (property.Name.Contains(propertyName)) { return property; } } return null; }
/// <summary> /// Initializes a new instance of the <see cref="Namespace"/> class. /// </summary> /// <param name="type"></param> public Namespace(ITypeInfoProxy type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } // Supported input types CheckType(type); this.type = type; }
/// <summary> /// Finds a <see cref="IFieldInfoProxy"/> into an <see cref="ITypeInfoProxy"/> by looking at the name (partial match). /// </summary> /// <param name="type"></param> /// <param name="fieldName"></param> /// <returns></returns> public static IFieldInfoProxy LocateField(this ITypeInfoProxy type, string fieldName) { foreach (var field in type.DeclaredFields) { if (field.Name.Contains(fieldName)) { return field; } } return null; }
public void StaticModifierCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public class MyClass { public static int MyProperty1 { get { return 0; } set { } } public int MyProperty2 { get { return 0; } set { } } } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); Action <string, bool> CheckStatic = (propertyName, expected) => { // Locating the property IPropertyInfoProxy propertyDeclaration = classDefinition.LocateProperty(propertyName); Assert.IsNotNull(propertyDeclaration); // Generating the AST var factory = new PropertyDeclarationSyntaxFactory(propertyDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(PropertyDeclarationSyntax), "Expected a property declaration node to be built"); var propertyDeclarationSyntaxNode = syntaxNode as PropertyDeclarationSyntax; var modifiers = propertyDeclarationSyntaxNode.Modifiers; Assert.IsNotNull(modifiers); var staticModifier = modifiers.Where(modifier => modifier.Kind() == SyntaxKind.StaticKeyword); Assert.AreEqual(expected ? 1 : 0, staticModifier.Count(), expected ? "Expected one static modifier" : "No static modifier expected"); }; CheckStatic("MyProperty1", true); CheckStatic("MyProperty2", false); }
public void InterfaceNamesCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" interface MyInterface1 { } interface MyInterface2 { } interface MyInterface3 { } class MyClass : MyInterface1, MyInterface2, MyInterface3 { } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Generating the AST var factory = new ClassDeclarationSyntaxFactory(classDefinition); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(ClassDeclarationSyntax), "Expected a class declaration node to be built"); var classDeclarationSyntaxNode = syntaxNode as ClassDeclarationSyntax; var baseList = classDeclarationSyntaxNode.BaseList.Types; Assert.AreEqual(3, baseList.Count, "Expected 3 interfaces"); Action <int, string> NameChecker = (index, expectedName) => { var baseType = classDeclarationSyntaxNode.BaseList.Types.ElementAt(index); var baseTypeIdentifier = baseType.Type as IdentifierNameSyntax; Assert.IsNotNull(baseTypeIdentifier, "Identifier expected"); var baseTypeName = baseTypeIdentifier.ToString(); Assert.AreEqual(expectedName, baseTypeName, "Base type full name not correct"); }; NameChecker(0, "MyInterface1"); NameChecker(1, "MyInterface2"); NameChecker(2, "MyInterface3"); }
/// <summary> /// Initializes a new instance of the <see cref="ConstructorDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="ctorInfo"></param> /// <param name="classInfo"></param> public ConstructorDeclarationSyntaxFactory(IConstructorInfoProxy ctorInfo, ITypeInfoProxy classInfo) { if (ctorInfo == null) { throw new ArgumentNullException(nameof(ctorInfo)); } if (classInfo == null) { throw new ArgumentNullException(nameof(classInfo)); } this.ctorInfo = ctorInfo; this.classInfo = classInfo; }