private static MappingInformation GetInformation(string source)
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var references = AppDomain.CurrentDomain.GetAssemblies()
                             .Where(_ => !_.IsDynamic && !string.IsNullOrWhiteSpace(_.Location))
                             .Select(_ => MetadataReference.CreateFromFile(_.Location))
                             .Concat(new[] { MetadataReference.CreateFromFile(typeof(MapGenerator).Assembly.Location) });
            var compilation = CSharpCompilation.Create("generator", new SyntaxTree[] { syntaxTree },
                                                       references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
            var model = compilation.GetSemanticModel(syntaxTree);

            var receiver = new MapReceiver();

            foreach (var attributeSyntax in syntaxTree.GetRoot().DescendantNodes(_ => true).OfType <AttributeSyntax>())
            {
                var context = GeneratorSyntaxContextFactory.Create(attributeSyntax, model);
                receiver.OnVisitSyntaxNode(context);
            }

            foreach (var typeDeclarationSyntax in syntaxTree.GetRoot().DescendantNodes(_ => true).OfType <TypeDeclarationSyntax>())
            {
                var context = GeneratorSyntaxContextFactory.Create(typeDeclarationSyntax, model);
                receiver.OnVisitSyntaxNode(context);
            }

            return(new(receiver, compilation));
        }
        public static async Task FindCandidatesWithNoMatchesAsync(string code)
        {
            var classDeclaration = (await SyntaxFactory.ParseSyntaxTree(code)
                                    .GetRootAsync().ConfigureAwait(false)).DescendantNodes(_ => true).OfType <ClassDeclarationSyntax>().First();

            var receiver = new MapReceiver();
            var context  = MapReceiverTests.GetContext(classDeclaration);

            receiver.OnVisitSyntaxNode(context);

            Assert.That(receiver.Targets.Count, Is.EqualTo(0));
        }
        public static async Task FindCandidatesWhenAttributeIsTargetingAnAssemblyAsync(string code)
        {
            var attributeDeclaration = (await SyntaxFactory.ParseSyntaxTree(code)
                                        .GetRootAsync().ConfigureAwait(false)).DescendantNodes(_ => true).OfType <AttributeSyntax>().First();

            var receiver = new MapReceiver();
            var context  = MapReceiverTests.GetContext(attributeDeclaration);

            receiver.OnVisitSyntaxNode(context);

            Assert.That(receiver.Targets.Count, Is.EqualTo(1));
        }
예제 #4
0
        public static async Task FindCandidatesWhenAttributeIsTargetingATypeAsync(
            string code, int expectedFromCount, int expectedToCount)
        {
            var classDeclaration = (await SyntaxFactory.ParseSyntaxTree(code)
                                    .GetRootAsync().ConfigureAwait(false)).DescendantNodes(_ => true).OfType <ClassDeclarationSyntax>().First();

            var receiver = new MapReceiver();

            receiver.OnVisitSyntaxNode(classDeclaration);

            Assert.Multiple(() =>
            {
                Assert.That(receiver.MapFromCandidates.Count, Is.EqualTo(expectedFromCount));
                Assert.That(receiver.MapToCandidates.Count, Is.EqualTo(expectedToCount));
                Assert.That(receiver.MapCandidates.Count, Is.EqualTo(0));
            });
        }