Example #1
0
        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");
        }
        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");
        }
        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");
        }
Example #4
0
        protected override void InitializeCore()
        {
            IAssemblyProxy assembly = this.LoadAssembly();

            var builder = this.CreateASTBuilder(assembly);
            var astInfo = builder.Build();

            // Getting the AST node
            this.tree = astInfo.Tree as CSharpSyntaxTree;

            if (tree == null)
            {
                throw new InvalidOperationException("Invalid generated tree");
            }

            var node = this.tree.GetRoot();

            // Referencing the semantic model
            this.semanticModel = astInfo.SemanticModel;

            // Creating the walker
            this.walker = this.CreateASTWalker(node, this.semanticModel);

            // Translating
            this.output = this.walker.Walk().Translate();

            // Some info
            this.info = $"AST generation: {astInfo.ToString()}";
        }
        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");
        }
        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(@"
                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 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");
        }
        public LinearSearchTypeLookup(IAssemblyProxy assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            this.assembly = assembly;
        }
Example #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ASTBuilder"/> class.
        /// This class builds a plain tree from all the types in the assembly.
        /// </summary>
        /// <param name="assembly">The path to the assembly.</param>
        /// <param name="retrieveSemanticModel">
        /// A value indicating whether the Mscorlib assembly should be
        /// loaded and its semantic model employed.
        /// </param>
        public ASTBuilder(IAssemblyProxy assembly, bool retrieveSemanticModel = true)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            this.assembly = assembly;
            this.retrieveSemanticModel = retrieveSemanticModel;
        }
        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]);
            }
        }
Example #13
0
        /// <summary>
        /// Finds a <see cref="ITypeInfoProxy"/> into an <see cref="IAssemblyProxy"/> by looking at the name (partial match).
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static ITypeInfoProxy LocateType(this IAssemblyProxy assembly, string typeName)
        {
            foreach (var type in assembly.DefinedTypes)
            {
                if (type.Name.Contains(typeName))
                {
                    return type;
                }
            }

            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");
        }
Example #16
0
        public void DummyBody()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        MyClass() {
                        }
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            // Locating the method
            IConstructorInfoProxy ctorDeclaration = classDefinition.LocateConstructor(0);

            Assert.IsNotNull(ctorDeclaration);

            // Generating the AST
            var factory    = new ConstructorDeclarationSyntaxFactory(ctorDeclaration, classDefinition);
            var syntaxNode = factory.Create();

            Assert.IsNotNull(syntaxNode, "A node was expected to be built");
            Assert.IsInstanceOfType(syntaxNode, typeof(ConstructorDeclarationSyntax), "Expected a constructor declaration node to be built");

            var constructorDeclarationSyntaxNode = syntaxNode as ConstructorDeclarationSyntax;

            var body = constructorDeclarationSyntaxNode.Body;

            Assert.IsNotNull(body, "Expected a body");

            var statements = body.Statements;

            Assert.IsNotNull(statements, "Expected a non empty body");
            Assert.AreEqual(1, statements.Count(), "Expected a body with 1 statement only");

            var statement = statements.First();

            Assert.IsNotNull(statement, "Expected one single statement");
            Assert.IsInstanceOfType(statement, typeof(ThrowStatementSyntax), "Expected a throw statement");
        }
        public void NameCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        public int myField;
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            // Locating the field
            IFieldInfoProxy fieldDeclaration = classDefinition.LocateField("myField");

            Assert.IsNotNull(fieldDeclaration);

            // Generating the AST
            var factory    = new FieldDeclarationSyntaxFactory(fieldDeclaration);
            var syntaxNode = factory.Create();

            Assert.IsNotNull(syntaxNode, "A node was expected to be built");
            Assert.IsInstanceOfType(syntaxNode, typeof(FieldDeclarationSyntax), "Expected a field declaration node to be built");

            var fieldDeclarationSyntaxNode = syntaxNode as FieldDeclarationSyntax;

            var variables = fieldDeclarationSyntaxNode.Declaration.Variables;

            Assert.IsNotNull(variables);
            Assert.IsTrue(variables.Any());

            var variable = fieldDeclarationSyntaxNode.Declaration.Variables.First();

            Assert.IsNotNull(variable);

            var name = variable.Identifier.Text;

            Assert.AreEqual("myField", name, "Field name not correctly acquired");
        }
        public void NoBody()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public interface MyInterface {
                        int MyProperty { get; set; }
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy interfaceDefinition = assembly.LocateType("MyInterface");

            Assert.IsNotNull(interfaceDefinition);

            // Locating the property
            IPropertyInfoProxy propertyDeclaration = interfaceDefinition.LocateProperty("MyProperty");

            Assert.IsNotNull(propertyDeclaration);

            // Generating the AST
            var factory    = new PropertyDeclarationSyntaxFactory(propertyDeclaration, false);
            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;

            Assert.IsNotNull(propertyDeclarationSyntaxNode.AccessorList);
            var accessors = propertyDeclarationSyntaxNode.AccessorList.Accessors;

            Assert.IsNotNull(accessors);
            Assert.AreEqual(2, accessors.Count, "2 accessors expected");

            var body1 = accessors.First().Body;
            var body2 = accessors.Last().Body;

            Assert.IsNull(body1, "Expected a null body");
            Assert.IsNull(body2, "Expected a null body");
        }
        public void ReturnTypeCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        public int MyProperty {
                            get { return 0; }
                            set { }
                        }
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            // Locating the property
            IPropertyInfoProxy propertyDeclaration = classDefinition.LocateProperty("MyProperty");

            Assert.IsNotNull(propertyDeclaration);

            // Generating the AST
            var factory    = new PropertyDeclarationSyntaxFactory(propertyDeclaration);
            var syntaxNode = factory.Create() as PropertyDeclarationSyntax;

            Assert.IsNotNull(syntaxNode, "A node was expected to be built");
            Assert.IsInstanceOfType(syntaxNode, typeof(PropertyDeclarationSyntax), "Expected a property declaration node to be built");

            var returnType = syntaxNode.Type;

            Assert.IsNotNull(returnType);

            var typeIdentifier = returnType as QualifiedNameSyntax;

            Assert.IsNotNull(typeIdentifier, "Type expected to be qualified name");
            Assert.AreEqual("System.Int32", typeIdentifier.ToString(), "Parameter name does not match");
        }
        protected virtual IAssemblyProxy LoadCore()
        {
            if (this.assemblyProxy == null)
            {
                ModuleDefinition module = null;
                try
                {
                    module = ModuleDefinition.ReadModule(this.AssemblyStream);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"An error occurred while loading module stream", ex);
                }

                this.assemblyProxy = new MonoAssemblyProxy(module);
            }

            return(this.assemblyProxy);
        }
        private static void TestInterfaceFullNames(string source, string className, string[] expectedInterfaceFullNames)
        {
            // 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);

            // Generating the AST
            var factory    = new ClassDeclarationSyntaxFactory(classDefinition, lookup);
            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(expectedInterfaceFullNames.Length, baseList.Count, $"Expected {expectedInterfaceFullNames.Length} interfaces");

            Action <int, string> NameChecker = (index, expectedName) =>
            {
                var baseType           = classDeclarationSyntaxNode.BaseList.Types.ElementAt(index);
                var baseTypeIdentifier = baseType.Type as QualifiedNameSyntax;

                Assert.IsNotNull(baseTypeIdentifier, "Qualified name expected");

                var baseTypeName = baseTypeIdentifier.ToString();
                Assert.AreEqual(expectedName, baseTypeName, "Base type full name not correct");
            };

            for (int i = 0; i < expectedInterfaceFullNames.Length; i++)
            {
                NameChecker(i, expectedInterfaceFullNames[i]);
            }
        }
        public void TypeCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        public int myField;
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            // Locating the field
            IFieldInfoProxy fieldDeclaration = classDefinition.LocateField("myField");

            Assert.IsNotNull(fieldDeclaration);

            // Generating the AST
            var factory    = new FieldDeclarationSyntaxFactory(fieldDeclaration);
            var syntaxNode = factory.Create();

            Assert.IsNotNull(syntaxNode, "A node was expected to be built");
            Assert.IsInstanceOfType(syntaxNode, typeof(FieldDeclarationSyntax), "Expected a field declaration node to be built");

            var fieldDeclarationSyntaxNode = syntaxNode as FieldDeclarationSyntax;

            var type = fieldDeclarationSyntaxNode.Declaration.Type;

            Assert.IsNotNull(type);

            var typeIdentifier = type as QualifiedNameSyntax;

            Assert.IsNotNull(typeIdentifier, "Type expected to be qualified name");
            Assert.AreEqual("System.Int32", typeIdentifier.ToString(), "Parameter name does not match");
        }
        private static void TestDummyBody(string source, string className, string methodName)
        {
            // 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 body = methodDeclarationSyntaxNode.Body;

            Assert.IsNotNull(body, "Expected a body");

            var statements = body.Statements;

            Assert.IsNotNull(statements, "Expected a non empty body");
            Assert.AreEqual(1, statements.Count(), "Expected a body with 1 statement only");

            var statement = statements.First();

            Assert.IsNotNull(statement, "Expected one single statement");
            Assert.IsInstanceOfType(statement, typeof(ThrowStatementSyntax), "Expected a throw statement");
        }
Example #24
0
        private static void Test(string source, bool expected = true)
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(source);

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            ITypeProxy baseClass = classDefinition.BaseType;

            Assert.IsNotNull(baseClass);

            var helper = new ObjectClass(baseClass);

            Assert.AreEqual(expected, helper.Is, "Object class check failed");
        }
        public void NameCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        public int MyProperty {
                            get { return 0; }
                            set { }
                        }
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            // Locating the property
            IPropertyInfoProxy propertyDeclaration = classDefinition.LocateProperty("MyProperty");

            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 name = propertyDeclarationSyntaxNode.Identifier.Text;

            Assert.AreEqual("MyProperty", name, "Property name not correctly acquired");
        }
        public void BaseTypeNameCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                class MyBaseClass {
                }
                class MyClass : MyBaseClass {
                }
            ");

            // 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(1, baseList.Count, "Expected one base class only");

            var baseType           = classDeclarationSyntaxNode.BaseList.Types.First();
            var baseTypeIdentifier = baseType.Type as IdentifierNameSyntax;

            Assert.IsNotNull(baseTypeIdentifier, "Identifier expected");

            var baseTypeName = baseTypeIdentifier.ToString();

            Assert.AreEqual("MyBaseClass", baseTypeName, "Base type full name not correct");
        }
        public void NameCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        public void MyMethod() {
                        }
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the class
            ITypeInfoProxy classDefinition = assembly.LocateType("MyClass");

            Assert.IsNotNull(classDefinition);

            // Locating the method
            IMethodInfoProxy methodDeclaration = classDefinition.LocateMethod("MyMethod");

            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 name = methodDeclarationSyntaxNode.Identifier.Text;

            Assert.AreEqual("MyMethod", name, "Method name not correctly acquired");
        }
        public void NoBody()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public interface IMyInterface {
                        void MyMethod();
                    }
                }
            ");

            // Loading the assembly
            IAssemblyProxy assembly = assemblyLoader.Load();

            // Locating the interface
            ITypeInfoProxy interfaceDefinition = assembly.LocateType("IMyInterface");

            Assert.IsNotNull(interfaceDefinition);

            // Locating the method
            IMethodInfoProxy methodDeclaration = interfaceDefinition.LocateMethod("MyMethod");

            Assert.IsNotNull(methodDeclaration);

            // Generating the AST
            var factory    = new MethodDeclarationSyntaxFactory(methodDeclaration, false);
            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 body = methodDeclarationSyntaxNode.Body;

            Assert.IsNull(body, "Expected a null body");
        }
Example #29
0
        private static void TestReturnTypeCorrectlyAcquired(string source, string className, string propertyName, string expectedTypeFullName)
        {
            // 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 property
            IPropertyInfoProxy propertyDeclaration = classDefinition.LocateProperty(propertyName);

            Assert.IsNotNull(propertyDeclaration);

            // Generating the AST
            var factory    = new PropertyDeclarationSyntaxFactory(propertyDeclaration, lookup);
            var syntaxNode = factory.Create() as PropertyDeclarationSyntax;

            Assert.IsNotNull(syntaxNode, "A node was expected to be built");
            Assert.IsInstanceOfType(syntaxNode, typeof(PropertyDeclarationSyntax), "Expected a property declaration node to be built");

            var returnType = syntaxNode.Type;

            Assert.IsNotNull(returnType);

            var typeIdentifier = returnType as QualifiedNameSyntax;

            Assert.IsNotNull(typeIdentifier, "Type expected to be qualified name");
            Assert.AreEqual(expectedTypeFullName, typeIdentifier.ToString(), "Parameter name does not match");
        }
Example #30
0
        private IAssemblyProxy LoadAssembly()
        {
            IAssemblyProxy assembly = null;

            try
            {
                var loader = this.CreateAssemblyLoader(this.assemblyPath);
                assembly = loader.Load();
            }
            catch (FileNotFoundException ex)
            {
                throw new InvalidOperationException("Invalid assembly path", ex);
            }
            catch (FileLoadException ex)
            {
                throw new InvalidOperationException("Invalid assembly path", ex);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"An error occurred while loading assembly at {this.assemblyPath}", ex);
            }

            return(assembly);
        }