public void List_chain_of_constructors_3()
        {
            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/ClassFamily.cs");

            var syntaxTree = SyntaxOperations.Parse(codeAsText);

            var classExtUserDeclarationSyntax    = SyntaxOperations.FindClass <ExtendedUser>(syntaxTree).A;
            var classNormalUserDeclarationSyntax = SyntaxOperations.FindClass <NormalUser>(syntaxTree).A;
            var classBaseUserDeclarationSyntax   = SyntaxOperations.FindClass <BaseUser>(syntaxTree).A;

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);



            var threeArgsCtor = SyntaxOperations
                                .FindConstructorsOf(classExtUserDeclarationSyntax, semanticModel, argsCount: 3)
                                .Single();

            var(firstLink, isCompleted) = ChainOfConstructor.StartingFrom(threeArgsCtor, classExtUserDeclarationSyntax, semanticModel);

            var secondLink = firstLink.NextLink.Value;
            var thirdLink  = secondLink.NextLink.Value;
            var lastlink   = thirdLink.NextLink.Value;

            Assert.IsTrue(isCompleted);

            _Assert_IsIntermadiateLink(firstLink, classExtUserDeclarationSyntax, threeArgsCtor);

            _Assert_IsIntermadiateLink(secondLink, classNormalUserDeclarationSyntax, SyntaxOperations.FindConstructorsOf(classNormalUserDeclarationSyntax, semanticModel, 3).Single());

            _Assert_IsIntermadiateLink(thirdLink, classNormalUserDeclarationSyntax, SyntaxOperations.FindConstructorsOf(classNormalUserDeclarationSyntax, semanticModel, 2).Single());

            _Assert_IsLastLink(lastlink, classBaseUserDeclarationSyntax, SyntaxOperations.FindConstructorsOf(classBaseUserDeclarationSyntax, semanticModel, 0).Single());
        }
        public void Partial_class_symbol_has_many_syntax_references_depending_on_source_syntax_tree_it_was_obtained()
        {
            var codeAsText1             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part1.cs");
            var syntaxTree1             = SyntaxOperations.Parse(codeAsText1);
            var classDeclarationSyntax1 = SyntaxOperations.FindClass <PartialClass>(syntaxTree1).A;


            var codeAsText2             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part2.cs");
            var syntaxTree2             = SyntaxOperations.Parse(codeAsText2);
            var classDeclarationSyntax2 = SyntaxOperations.FindClass <PartialClass>(syntaxTree2).A;



            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree1, new[] { syntaxTree1, syntaxTree2 });

            var classSymbol1 = semanticModel.GetDeclaredSymbol(classDeclarationSyntax1);
            var classSymbol2 = semanticModel.GetDeclaredSymbol(classDeclarationSyntax1);

            Assert.AreEqual(classSymbol1, classSymbol2);

            var declaringSyntaxRefs1 = classSymbol1.DeclaringSyntaxReferences;
            var declaringSyntaxRefs2 = classSymbol2.DeclaringSyntaxReferences;

            CollectionAssert.AreEquivalent(declaringSyntaxRefs1, declaringSyntaxRefs2);

            Assert.AreEqual(2, declaringSyntaxRefs1.Length);
        }
        public void List_chain_of_constructors_2()
        {
            var codeAsText = File.ReadAllText(@"SyntaxAnalyserExamples/ClassFamily.cs");

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

            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree);


            var zeroArgsCtor = SyntaxOperations
                               .FindConstructorsOf(classDeclarationSyntax, semanticModel, argsCount: 0)
                               .Single();


            var oneArgCtor = SyntaxOperations
                             .FindConstructorsOf(classDeclarationSyntax, semanticModel, argsCount: 1)
                             .Single();

            var(firstLink, isCompleted) = ChainOfConstructor.StartingFrom(oneArgCtor, classDeclarationSyntax, semanticModel);

            Assert.IsTrue(isCompleted);

            _Assert_IsIntermadiateLink(firstLink, classDeclarationSyntax, oneArgCtor);

            var secondLink = firstLink.NextLink.Value;

            _Assert_IsLastLink(secondLink, classDeclarationSyntax, zeroArgsCtor);
        }
Beispiel #4
0
        public void Recognises_if_class_contains_aformentioned_ctor_attribute()
        {
            var syntaxTree = SyntaxOperations.Parse(CodeAsText);
            var syntax     = SyntaxOperations.FindClass <A>(syntaxTree).A;

            var semantics = SemanticOperations.GetSemanticModel(syntaxTree);

            var classSymbol = (INamedTypeSymbol)semantics.GetDeclaredSymbol(syntax);

            var isUnderInterest = ThisConstructorMustBeCalledAnalyzer.IsUnderInterest(classSymbol);
        }
        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 Semantic_model_can_be_obtained_from_partial_definition_of_a_class_1()
        {
            var codeAsText2             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part2.cs");
            var syntaxTree2             = SyntaxOperations.Parse(codeAsText2);
            var classDeclarationSyntax2 = SyntaxOperations.FindClass <PartialClass>(syntaxTree2).A;
            var semanticModel2          = SemanticOperations.GetSemanticModel(syntaxTree2);


            var twoArgsCtor = SyntaxOperations
                              .FindConstructorsOf(classDeclarationSyntax2, semanticModel2, argsCount: 2)
                              .Single();


            Assert.IsNotNull(twoArgsCtor);
        }
        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);
        }
        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);
        }
        public void Partial_chain_of_constructors_can_be_obtained_from_partial_code_2()
        {
            var codeAsText2             = File.ReadAllText(@"SyntaxAnalyserExamples/PartialClass.part2.cs");
            var syntaxTree2             = SyntaxOperations.Parse(codeAsText2);
            var classDeclarationSyntax2 = SyntaxOperations.FindClass <PartialClass>(syntaxTree2).A;



            var semanticModel = SemanticOperations.GetSemanticModel(syntaxTree2, new[] { syntaxTree2 });


            var twoArgsCtor = SyntaxOperations
                              .FindConstructorsOf(classDeclarationSyntax2, semanticModel, argsCount: 2)
                              .Single();


            var(firstLink, isCompleted) = ChainOfConstructor.StartingFrom(twoArgsCtor, classDeclarationSyntax2, semanticModel);

            Assert.IsFalse(isCompleted);

            _Assert_IsIntermadiateLink(firstLink, classDeclarationSyntax2, twoArgsCtor);
        }
        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());
        }