Ejemplo n.º 1
0
        public void Creates_semantic_model_of_a_class()
        {
            var classToAnalyze = typeof(ClientDto);

            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/ClientDto.cs");

            var syntaxTree             = SyntaxOperations.Parse(codeAsText);
            var classDeclarationSyntax = SyntaxOperations.FindClass <ClientDto>(syntaxTree);

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <ClientDto>(syntaxTree, semanticModel).A;

            Assert.AreEqual(classToAnalyze.Name, typeSymbol.Name);
        }
        public void Finds_implicit_constructor()
        {
            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/MyEmptyClass.cs");

            var syntaxTree = SyntaxOperations.Parse(codeAsText);

            var classDeclarationSyntax = SyntaxOperations.FindClass <MyEmptyClass>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <MyEmptyClass>(syntaxTree, semanticModel).A;


            var anonymousConstructor = typeSymbol.Constructors.Single();

            Assert.AreEqual(actual: anonymousConstructor.MethodKind, expected: MethodKind.Constructor);
            Assert.IsTrue(anonymousConstructor.IsImplicitlyDeclared);
        }
Ejemplo n.º 3
0
        public void Finds_all_instance_methods()
        {
            var classToAnalyze = typeof(PersonObject);

            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/PersonObject.cs");

            var syntaxTree             = SyntaxOperations.Parse(codeAsText);
            var classDeclarationSyntax = SyntaxOperations.FindClass <PersonObject>(syntaxTree);

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <PersonObject>(syntaxTree, semanticModel).A;


            var members = typeSymbol.GetMembers();

            var methods = members.Where(x => x.Kind == SymbolKind.Method).Cast <IMethodSymbol>().ToList();

            var staticMethod = methods.Where(x => !x.IsStatic && x.MethodKind == MethodKind.Ordinary).Single();

            Assert.AreEqual(nameof(PersonObject.AddChild), staticMethod.Name);
        }
Ejemplo n.º 4
0
        public void Finds_all_readonly_properties()
        {
            var classToAnalyze = typeof(PersonObject);

            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/PersonObject.cs");

            var syntaxTree = SyntaxOperations.Parse(codeAsText);

            var classDeclarationSyntax = SyntaxOperations.FindClass <PersonObject>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);

            var typeSymbol = SemanticOperations.FindClassSingle <PersonObject>(syntaxTree, semanticModel).A;


            var members = typeSymbol.GetMembers();

            var props = members.Where(x => x.Kind == SymbolKind.Property).Cast <IPropertySymbol>().ToList();

            var readOnlyProps = props.Where(x => x.IsReadOnly).ToList();

            CollectionAssert.AreEquivalent(new[] { nameof(PersonObject.Id), nameof(PersonObject.Name) }, readOnlyProps.Select(x => x.Name).ToArray());
        }