Helper for accessing class in AST
Inheritance: InheritableDeclaration
Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClassASTWalker"/> class.
        /// </summary>
        protected ClassASTWalker(CSharpSyntaxNode node)
        {
            var classDeclarationSyntaxNode = node as ClassDeclarationSyntax;
            if (classDeclarationSyntaxNode == null)
            {
                throw new ArgumentException(
                    string.Format("Specified node is not of type {0}",
                    typeof(ClassDeclarationSyntax).Name));
            }

            this.node = node;
            ClassDeclaration classHelper = new ClassDeclaration(classDeclarationSyntaxNode);

            this.classDeclaration = ClassDeclarationTranslationUnit.Create(
                classHelper.Visibility,
                IdentifierTranslationUnit.Create(classHelper.Name),
                classHelper.BaseClass == null ? null : IdentifierTranslationUnit.Create(classHelper.BaseClass.Name));

            foreach (BaseTypeReference implementedInterface in classHelper.ImplementedInterfaces)
            {
                this.classDeclaration.AddImplementedInterface(IdentifierTranslationUnit.Create(implementedInterface.Name));
            }
        }
        private static void TestRetrieveClassName(ClassDeclarationSyntax classDeclarationNode, string expected)
        {
            Assert.IsNotNull(classDeclarationNode, "Found node should be of type `{0}`!",
                typeof(ClassDeclarationSyntax).Name);

            ClassDeclaration classDeclaration = new ClassDeclaration(classDeclarationNode);
            string name = classDeclaration.Name;

            Assert.IsNotNull(name, "Class name should not be null!");
            Assert.AreNotEqual(string.Empty, name, "Class name should not be empty!");
            Assert.AreEqual(expected, name, "Class name is not the one in source!");
        }
        private static void TestRetrieveInterfacesName(ClassDeclarationSyntax classDeclarationNode, SemanticModel semanticModel, string[] expected)
        {
            Assert.IsNotNull(classDeclarationNode, "Found node should be of type `{0}`!",
                typeof(ClassDeclarationSyntax).Name);

            ClassDeclaration classDeclaration = new ClassDeclaration(classDeclarationNode, semanticModel);
            IEnumerable<BaseTypeReference> implementedInterfaces = classDeclaration.ImplementedInterfaces;
            Assert.IsNotNull(implementedInterfaces, "Implemented interfaces should not be null!");

            if (expected == null)
            {
                Assert.AreEqual(0, implementedInterfaces.Count(),
                    "Number of implemented interfaces does not match expected!");
                return;
            }

            Assert.AreEqual(expected.Length, implementedInterfaces.Count(),
                "Number of implemented interfaces does not match expected!");
            string[] names = classDeclaration.ImplementedInterfaces.Select(interfaceType => interfaceType.Name).ToArray();

            Assert.IsNotNull(names, "Interface names should not be null!");
            Assert.AreEqual(names.Length, implementedInterfaces.Count(),
                "Number of implemented interfaces does not match expected!");

            foreach (string name in names)
            {
                Assert.AreNotEqual(string.Empty, name, "Interface name should not be empty!");
                Assert.IsTrue(expected.Where(expectedName => name == expectedName).Count() == 1,
                    string.Format("Expecting an interface of type {0}, but not found!", name));
            }
        }
        private static void TestRetrieveBaseClassName(ClassDeclarationSyntax classDeclarationNode, SemanticModel semanticModel, string expected)
        {
            Assert.IsNotNull(classDeclarationNode, "Found node should be of type `{0}`!",
                typeof(ClassDeclarationSyntax).Name);

            ClassDeclaration classDeclaration = new ClassDeclaration(classDeclarationNode, semanticModel);
            BaseTypeReference baseClass = classDeclaration.BaseClass;

            if (expected == null)
            {
                Assert.IsNull(baseClass, "Class name should be null!");
                return;
            }

            string name = classDeclaration.BaseClass.Name;

            Assert.IsNotNull(name, "Class name should not be null!");
            Assert.AreNotEqual(string.Empty, name, "Class name should not be empty!");
            Assert.AreEqual(expected, name, "Base class name is not the one in source!");
        }