Beispiel #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");
        }
Beispiel #2
0
        private static void TestVisibility(string source, string className, 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
            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 modifiers = constructorDeclarationSyntaxNode.Modifiers;

            Assert.IsTrue(Utils.CheckModifier(modifiers, expectedVisibility), "Constructor does not have correct visibility");
        }
Beispiel #3
0
        public void ArgumentsCorrectlyAcquired()
        {
            // Assembling some code
            IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@"
                namespace MyNamespace {
                    public class MyClass {
                        public MyClass(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
            IConstructorInfoProxy ctorDeclaration = classDefinition.LocateConstructor(3);

            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");

            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");
        }
Beispiel #4
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;
        }
Beispiel #5
0
        private static void TestArgumentsCorrectlyAcquired(string source, string className, 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 ctor
            IConstructorInfoProxy ctorDeclaration = classDefinition.LocateConstructor(3);

            Assert.IsNotNull(ctorDeclaration);

            // Generating the AST
            var factory    = new ConstructorDeclarationSyntaxFactory(ctorDeclaration, classDefinition, lookup);
            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");

            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>
        /// 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;
        }
Beispiel #7
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");
        }
 protected virtual ISyntaxFactory CreateConstructorDeclarationSyntaxFactory(IConstructorInfoProxy ctorInfo)
 => new ConstructorDeclarationSyntaxFactory(ctorInfo, this.ClassInfo);
 protected override ISyntaxFactory CreateConstructorDeclarationSyntaxFactory(IConstructorInfoProxy ctorInfo)
 => new ConstructorDeclarationSyntaxFactory(ctorInfo, this.ClassInfo, this.typeLookup);