コード例 #1
0
        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]);
            }
        }
コード例 #2
0
        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]);
            }
        }
コード例 #3
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");
        }
コード例 #4
0
        private static void TestBaseClassFullName(string source, string className, string expectedBaseTypeName)
        {
            // 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(1, baseList.Count, "Expected one base class only");

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

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

            var baseTypeName = baseTypeIdentifier.ToString();

            Assert.AreEqual(expectedBaseTypeName, baseTypeName, "Base type full name not correct");
        }