Example #1
0
        private static void TestScriptNamespaceOnParameter(string source, bool withSemanticModel, string expectedFullName)
        {
            var tree = CSharpSyntaxTree.ParseText(source);

            // Loading MSCoreLib
            var semanticModel = withSemanticModel
                ? (CSharpCompilation.Create("TestAssembly")
                   .AddReferences(
                       MetadataReference.CreateFromFile(
                           typeof(object).Assembly.Location))
                   .AddSyntaxTrees(tree).AddScriptNamespaceReference().GetSemanticModel(tree))
                : null;

            var node = new NodeLocator(tree).LocateFirst(typeof(MethodDeclarationSyntax));

            Assert.IsNotNull(node);

            var methodDeclarationNode = node as MethodDeclarationSyntax;

            Assert.IsNotNull(methodDeclarationNode);

            var translationUnitFactory = new MethodDefinitionTranslationUnitFactory(methodDeclarationNode, semanticModel, true).Create();

            Assert.IsNotNull(translationUnitFactory, "Translation unit expected to be created!");

            var methodTranslationUnit = (translationUnitFactory as MethodDefinitionTranslationUnit);

            Assert.IsNotNull(methodTranslationUnit, $"Expecting a translation unit of type {typeof(MethodDefinitionTranslationUnit).Name}!");

            var translationUnit = MockedMethodDefinitionTranslationUnit.Create(methodTranslationUnit);

            Assert.IsNotNull(translationUnit.Arguments);
            Assert.AreEqual(1, translationUnit.Arguments.Count(), "Expecting 1 argument!");

            var argumentTranslationUnit = translationUnit.Arguments.ElementAt(0) as ArgumentDefinitionTranslationUnit;

            Assert.IsNotNull(argumentTranslationUnit, $"Expected argument to be of type {typeof(ArgumentDefinitionTranslationUnit).Name}");

            var mockedArgumentTranslationUnit = MockedArgumentDefinitionTranslationUnit.Create(argumentTranslationUnit);

            Assert.IsNotNull(mockedArgumentTranslationUnit.VariableDeclaration);

            var variableDeclarationTranslationUnit = mockedArgumentTranslationUnit.VariableDeclaration as VariableDeclarationTranslationUnit;

            Assert.IsNotNull(variableDeclarationTranslationUnit);

            var mockedVariableDeclarationTranslationUnit = MockedVariableDeclarationTranslationUnit.Create(variableDeclarationTranslationUnit);

            Assert.IsNotNull(mockedVariableDeclarationTranslationUnit.Type, "Expecting a type!");

            var typeIdentifierTranslationUnit = mockedVariableDeclarationTranslationUnit.Type as TypeIdentifierTranslationUnit;

            Assert.IsNotNull(typeIdentifierTranslationUnit, $"Expected argument to be of type {typeof(TypeIdentifierTranslationUnit).Name}");

            var typeFullName = typeIdentifierTranslationUnit.Translate();

            Assert.AreEqual(expectedFullName, typeFullName, "Expected ScriptNamespace overriden type to be used!");
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="node"></param>
        /// <remarks>
        /// This will cause an AST walker to be created, thus we don't need to go further deeper in the
        /// tree by visiting the node.
        /// </remarks>
        public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            var methodDefinitionTranslationUnit = new MethodDefinitionTranslationUnitFactory(node, this.semanticModel, this.generateTranslationUniOnProtectedMembers).Create();

            if (methodDefinitionTranslationUnit == null)
            {
                // When the factory returns null, then the member is not exposed, thus we do not generate it in the translation tree
                return;
            }

            this.classDeclaration.AddMethodDeclaration(methodDefinitionTranslationUnit);

            this.InvokeMethodDeclarationVisited(this, new WalkerEventArgs());
        }