コード例 #1
0
        public void RegistersAllInterfacesAndTheClassIfTheClassIsPublicIfOpenServiceTypeIsNotDefined()
        {
            var compilation = GetCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"namespace Temp.Providers
                {
                    public interface IProviderA<T>
                    {
                        decimal GetValue();
                    }

                    public interface IProviderB<T>
                    {
                        decimal GetValue2();
                    }

                    [ServiceDescriptor( Lifecycle = LifecycleKind.Singleton )]
                    public class ProviderA<T> : IProviderB<T>, IProviderA<T>
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }

                        public decimal GetValue2()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
            services.AddSingleton(typeof (Temp.Providers.IProviderB<>), typeof (Temp.Providers.ProviderA<>));
            services.AddSingleton(typeof (Temp.Providers.IProviderA<>), typeof (Temp.Providers.ProviderA<>));
            services.AddSingleton(typeof (Temp.Providers.ProviderA<>), typeof (Temp.Providers.ProviderA<>));
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
コード例 #2
0
        public void AddAssemblyIsReplacedByDefault()
        {
            var compilation = GetCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"
                    namespace Temp {
                        public class Startup2 {
                            void Configure(IServiceCollection collection) {
                                collection.AddAssembly(typeof(Startup2));
                            }
                        }
                    }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));


            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.Last().GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup2
    {
        void Configure(IServiceCollection collection)
        {
        }
    }
}", context.CSharpCompilation.SyntaxTrees.Last().GetText().ToString());
        }
コード例 #3
0
        public void Compiles()
        {
            var compilation = GetCompilation();

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };



            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
        public void Compiles()
        {
            var compilation = GetCompilation();

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
コード例 #5
0
        public void UnderstandsSingleton()
        {
            var compilation = GetCSharpCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"using Microsoft.Framework.DependencyInjection;
                namespace Temp.Providers
                {
                    public interface IProviderA
                    {
                        decimal GetValue();
                    }

                    [ServiceDescriptor(typeof(IProviderA), Lifetime = ServiceLifetime.Singleton)]
                    public class ProviderA : IProviderA
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6)));

            var context = new BeforeCompileContext()
            {
                Compilation = compilation,
                Diagnostics = new List <Diagnostic>()
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile(context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.Compilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"using Microsoft.Framework.DependencyInjection;

namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
            services.AddSingleton(typeof (Temp.Providers.IProviderA), typeof (Temp.Providers.ProviderA));
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.Compilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
コード例 #6
0
        public void DefaultsToTransient()
        {
            var compilation = GetCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"namespace Temp.Providers
                {
                    public interface IProviderA
                    {
                        decimal GetValue();
                    }

                    [ServiceDescriptor(typeof(IProviderA))]
                    public class ProviderA : IProviderA
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
            services.AddTransient(typeof (Temp.Providers.IProviderA), typeof (Temp.Providers.ProviderA));
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
コード例 #7
0
        public void AddAssemblyWorksWithAssemblyExpression()
        {
            var compilation = GetCSharpCompilation();

            compilation.AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"
                        using Microsoft.Framework.DependencyInjection;
namespace Temp {
                            public class Startup {
                                void Configure(IServiceCollection services) {
                                    services.AddAssembly(typeof(Startup).GetTypeInfo().Assembly);
                                }

                                public static int Main() { return 0; }
                            }
                        }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6)));

            var context = new BeforeCompileContext()
            {
                Compilation = compilation,
                Diagnostics = new List <Diagnostic>()
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile(context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.Compilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"using Microsoft.Framework.DependencyInjection;

namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.Compilation.SyntaxTrees.Last().GetText().ToString());
        }
コード例 #8
0
        public void AddAssemblyIsLeftAloneForOtherTypes()
        {
            var compilation = GetCSharpCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"
                    using Microsoft.Framework.DependencyInjection;
namespace Temp {
                        public class NotStartup {
                        }

                        public class Startup2 {
                            void Configure(IServiceCollection collection) {
                                collection.AddAssembly(typeof(NotStartup));
                            }
                        }
                    }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6)));


            var context = new BeforeCompileContext()
            {
                Compilation = compilation,
                Diagnostics = new List <Diagnostic>()
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile(context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.Compilation.SyntaxTrees.Last().GetText().ToString());

            Assert.Equal(@"
                    using Microsoft.Framework.DependencyInjection;
namespace Temp {
                        public class NotStartup {
                        }

                        public class Startup2 {
                            void Configure(IServiceCollection collection) {
                                collection.AddAssembly(typeof(NotStartup));
                            }
                        }
                    }", context.Compilation.SyntaxTrees.Last().GetText().ToString());
        }
コード例 #9
0
        public void OpenDiagnosticsStillFunctionEvenIfReplacementNeverHappens()
        {
            var compilation = GetCSharpCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"using Microsoft.Framework.DependencyInjection;
                namespace Temp.Providers
                {
                    public interface IProviderA<T>
                    {
                        decimal GetValue();
                    }

                    public interface IProviderB<T>
                    {
                        decimal GetValue();
                    }

                    [ServiceDescriptor(typeof(IProviderA<>), Lifetime = ServiceLifetime.Singleton)]
                    public class ProviderA<T> : IProviderB<T>
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6)));

            var context = new BeforeCompileContext()
            {
                Compilation = compilation,
                Diagnostics = new List <Diagnostic>()
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile(context);

            //Console.WriteLine(context.Compilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.True(context.Diagnostics.Any());
        }
コード例 #10
0
        public void ReportsOpenDiagnostics()
        {
            var compilation = GetCompilation()
                              .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"namespace Temp.Providers
                {
                    public interface IProviderA<T>
                    {
                        decimal GetValue();
                    }

                    public interface IProviderB<T>
                    {
                        decimal GetValue();
                    }

                    [ServiceDescriptor(typeof(IProviderA<>), Lifecycle = LifecycleKind.Singleton)]
                    public class ProviderA<T> : IProviderB<T>
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));


            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.True(context.Diagnostics.Any());
        }
        public void RegistersAllInterfacesAndTheClassIfTheClassIsPublicIfOpenServiceTypeIsNotDefined()
        {
            var compilation = GetCompilation()
                .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"namespace Temp.Providers
                {
                    public interface IProviderA<T>
                    {
                        decimal GetValue();
                    }

                    public interface IProviderB<T>
                    {
                        decimal GetValue2();
                    }

                    [ServiceDescriptor( Lifecycle = LifecycleKind.Singleton )]
                    public class ProviderA<T> : IProviderB<T>, IProviderA<T>
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }

                        public decimal GetValue2()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
            services.AddSingleton(typeof (Temp.Providers.IProviderB<>), typeof (Temp.Providers.ProviderA<>));
            services.AddSingleton(typeof (Temp.Providers.IProviderA<>), typeof (Temp.Providers.ProviderA<>));
            services.AddSingleton(typeof (Temp.Providers.ProviderA<>), typeof (Temp.Providers.ProviderA<>));
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
        public void DefaultsToTransient()
        {
            var compilation = GetCompilation()
                .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"namespace Temp.Providers
                {
                    public interface IProviderA
                    {
                        decimal GetValue();
                    }

                    [ServiceDescriptor(typeof(IProviderA))]
                    public class ProviderA : IProviderA
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup
    {
        void Configure(IServiceCollection services)
        {
            services.AddTransient(typeof (Temp.Providers.IProviderA), typeof (Temp.Providers.ProviderA));
        }

        public static int Main()
        {
            return 0;
        }
    }
}", context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());
        }
        public void OpenDiagnosticsStillFunctionEvenIfReplacementNeverHappens()
        {
            var compilation = GetCompilation()
                .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"namespace Temp.Providers
                {
                    public interface IProviderA<T>
                    {
                        decimal GetValue();
                    }

                    public interface IProviderB<T>
                    {
                        decimal GetValue();
                    }

                    [ServiceDescriptor(typeof(IProviderA<>), Lifecycle = LifecycleKind.Singleton)]
                    public class ProviderA<T> : IProviderB<T>
                    {
                        public decimal GetValue()
                        {
                            return 9000.99M;
                        }
                    }
                }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));

            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.First(x => x.GetText().ToString().Contains("class Startup")).GetText().ToString());

            Assert.True(context.Diagnostics.Any());
        }
        public void AddAssemblyIsReplacedByDefault()
        {
            var compilation = GetCompilation()
                .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(SourceText.From(@"
                    namespace Temp {
                        public class Startup2 {
                            void Configure(IServiceCollection collection) {
                                collection.AddAssembly(typeof(Startup2));
                            }
                        }
                    }", Encoding.UTF8), CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)));


            var context = new BeforeCompileContext()
            {
                CSharpCompilation = compilation
            };

            var unit = new PreprocessAnnotation();

            unit.BeforeCompile((IBeforeCompileContext)context);
            Assert.False(context.Diagnostics.Any());

            //Console.WriteLine(context.CSharpCompilation.SyntaxTrees.Last().GetText().ToString());

            Assert.Equal(@"namespace Temp
{
    public class Startup2
    {
        void Configure(IServiceCollection collection)
        {
        }
    }
}", context.CSharpCompilation.SyntaxTrees.Last().GetText().ToString());
        }