Exemple #1
0
        public void CodeGeneratorDescriptor_Name_Uses_TypeName_When_Alias_Not_Present()
        {
            //Arrange
            var mockServiceProvider = new Mock <IServiceProvider>();
            var descriptor          = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithoutAlias).GetTypeInfo(),
                                                                  mockServiceProvider.Object);

            //Act
            var name = descriptor.Name;

            //Assert
            Assert.Equal("CodeGeneratorWithoutAlias", name);
        }
        public void CodeGeneratorDescriptor_CodeGeneratorAction_Throws_When_No_GenerateCode_Method()
        {
            //Arrange
            var mockServiceProvider = new Mock<IServiceProvider>();
            var descriptor = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithGenerateCodeNoParameters).GetTypeInfo(),
                mockServiceProvider.Object);

            //Act
            var ex = Assert.Throws<InvalidOperationException>(() => descriptor.CodeGeneratorAction);
            Assert.Equal("GenerateCode method with a model parameter is not found in class: " +
                "Microsoft.Framework.CodeGeneration.Core.Test.CodeGeneratorDescriptorTests+CodeGeneratorWithGenerateCodeNoParameters",
                ex.Message);
        }
        public void CodeGeneratorDescriptor_CodeGeneratorAction_Throws_When_Multiple_GenerateCode_Methods()
        {
            //Arrange
            var mockServiceProvider = new Mock<IServiceProvider>();
            var descriptor = new CodeGeneratorDescriptor(typeof(ClassWithMultipleGenerateCodeMethods).GetTypeInfo(),
                mockServiceProvider.Object);

            //Act
            var ex = Assert.Throws<InvalidOperationException>(() => descriptor.CodeGeneratorAction);
            Assert.Equal("Multiple GenerateCode methods with a model parameter are found in class: " +
                "Microsoft.Extensions.CodeGeneration.Core.Test.CodeGeneratorDescriptorTests+ClassWithMultipleGenerateCodeMethods",
                ex.Message);
        }
        public void CodeGeneratorDescriptor_Name_Uses_TypeName_When_Alias_Not_Present()
        {
            //Arrange
            var mockServiceProvider = new Mock<IServiceProvider>();
            var descriptor = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithoutAlias).GetTypeInfo(),
                mockServiceProvider.Object);

            //Act
            var name = descriptor.Name;

            //Assert
            Assert.Equal("CodeGeneratorWithoutAlias", name);
        }
        public void CodeGeneratorDescriptor_CodeGeneratorAction_Returns_Valid_GenerateCode_Method()
        {
            //Arrange
            var mockServiceProvider = new Mock<IServiceProvider>();
            var descriptor = new CodeGeneratorDescriptor(typeof(ClassWithGenerateCodeMethod).GetTypeInfo(),
                mockServiceProvider.Object);

            //Act
            var action = descriptor.CodeGeneratorAction;

            //Assert
            Assert.NotNull(action);
            Assert.Equal("GenerateCode", action.ActionMethod.Name);
        }
Exemple #6
0
        public void CodeGeneratorDescriptor_CodeGeneratorAction_Returns_Valid_GenerateCode_Method()
        {
            //Arrange
            var mockServiceProvider = new Mock <IServiceProvider>();
            var descriptor          = new CodeGeneratorDescriptor(typeof(ClassWithGenerateCodeMethod).GetTypeInfo(),
                                                                  mockServiceProvider.Object);

            //Act
            var action = descriptor.CodeGeneratorAction;

            //Assert
            Assert.NotNull(action);
            Assert.Equal("GenerateCode", action.ActionMethod.Name);
        }
Exemple #7
0
        public void CodeGeneratorDescriptor_CodeGeneratorAction_Throws_When_Multiple_GenerateCode_Methods()
        {
            //Arrange
            var mockServiceProvider = new Mock <IServiceProvider>();
            var descriptor          = new CodeGeneratorDescriptor(typeof(ClassWithMultipleGenerateCodeMethods).GetTypeInfo(),
                                                                  mockServiceProvider.Object);

            //Act
            var ex = Assert.Throws <InvalidOperationException>(() => descriptor.CodeGeneratorAction);

            Assert.Equal("Multiple GenerateCode methods with a model parameter are found in class: " +
                         "Microsoft.VisualStudio.Web.CodeGeneration.Core.Test.CodeGeneratorDescriptorTests+ClassWithMultipleGenerateCodeMethods",
                         ex.Message);
        }
Exemple #8
0
        public void CodeGeneratorDescriptor_CodeGeneratorInstance_Throws_When_Not_Able_To_Create_Instance()
        {
            //Arrange
            var mockServiceProvider = new Mock <IServiceProvider>();

            var descriptor = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithDependencies).GetTypeInfo(),
                                                         mockServiceProvider.Object);

            //Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => descriptor.CodeGeneratorInstance);

            Assert.True(ex.Message.StartsWith("There was an error creating the code generator instance: " +
                                              "Microsoft.VisualStudio.Web.CodeGeneration.Core.Test.CodeGeneratorDescriptorTests+CodeGeneratorWithDependencies"));
        }
        public void CodeGeneratorDescriptor_CodeGeneratorAction_Throws_When_No_GenerateCode_Method()
        {
            //Arrange
            var mockServiceProvider = new Mock <IServiceProvider>();
            var descriptor          = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithGenerateCodeNoParameters).GetTypeInfo(),
                                                                  mockServiceProvider.Object);

            //Act
            var ex = Assert.Throws <InvalidOperationException>(() => descriptor.CodeGeneratorAction);

            Assert.Equal("GenerateCode method with a model parameter is not found in class: " +
                         "Microsoft.Framework.CodeGeneration.Core.Test.CodeGeneratorDescriptorTests+CodeGeneratorWithGenerateCodeNoParameters",
                         ex.Message);
        }
        public void CodeGeneratorDescriptor_CodeGeneratorInstance_Injects_Dependencies()
        {
            //Arrange
            var mockServiceProvider = new Mock<IServiceProvider>();
            mockServiceProvider
                .Setup(sp => sp.GetService(typeof(IServiceProvider)))
                .Returns(mockServiceProvider.Object);

            var descriptor = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithDependencies).GetTypeInfo(),
                mockServiceProvider.Object);

            //Act
            var instance = descriptor.CodeGeneratorInstance as CodeGeneratorWithDependencies;

            //Assert
            Assert.NotNull(instance);
            Assert.Equal(mockServiceProvider.Object, instance.ServiceProvider);
        }
Exemple #11
0
        public void CodeGeneratorDescriptor_CodeGeneratorInstance_Injects_Dependencies()
        {
            //Arrange
            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider
            .Setup(sp => sp.GetService(typeof(IServiceProvider)))
            .Returns(mockServiceProvider.Object);

            var descriptor = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithDependencies).GetTypeInfo(),
                                                         mockServiceProvider.Object);

            //Act
            var instance = descriptor.CodeGeneratorInstance as CodeGeneratorWithDependencies;

            //Assert
            Assert.NotNull(instance);
            Assert.Equal(mockServiceProvider.Object, instance.ServiceProvider);
        }
        /// <inheritdoc />
        public override Task GenerateAsync(AssemblyTransformationContext context, IProgress <Diagnostic> progress, CancellationToken cancellationToken)
        {
            var s = Service;

            void GenerateCallback()
            {
                // TODO: TBD: sprinkles in a bit of Validation as well...
                // TODO: TBD: would be better if 'Validation' actually supported a more Fluent style...
                Assumes.Present(s);

                // A lot goes in behind the Descriptor parsing the Protocol Buffer file into a usable form.
                var pd      = s.Descriptor;
                var visitor = s.CodeGenerationVisitor;

                Assumes.Present(pd);
                Assumes.Present(visitor);

                // Then we want to Visit the Descriptor to glean the critical details for CG.
                visitor.Visit(pd);

                // ReSharper disable once RedundantEmptyObjectOrCollectionInitializer
                var cgd = new CodeGeneratorDescriptor {
                };

                Assumes.NotNullOrEmpty(visitor.CompilationUnits);
                Assumes.NotNullOrEmpty(cgd.CompilationUnits);

                foreach (var(k, x) in visitor.CompilationUnits)
                {
                    // TODO: TBD: potential future direction for CGR, we may work by adding a dictionary of CompilationUnitSyntax instances...
                    Verify.Operation(k != Guid.Empty, $"Dictionary key '{nameof(k)}' should not be Empty.");
                    Assumes.NotNull(x);

                    cgd.CompilationUnits.Add(x);
                }

                Assumes.Present(Descriptors);

                Descriptors.Add(cgd);
            }

            return(Task.Run(GenerateCallback, cancellationToken));
        }
        public void CodeGeneratorDescriptor_CodeGeneratorInstance_Throws_When_Not_Able_To_Create_Instance()
        {
            //Arrange
            var mockServiceProvider = new Mock<IServiceProvider>();

            var descriptor = new CodeGeneratorDescriptor(typeof(CodeGeneratorWithDependencies).GetTypeInfo(),
                mockServiceProvider.Object);

            //Act & Assert
            var ex = Assert.Throws<InvalidOperationException>(() => descriptor.CodeGeneratorInstance);
            Assert.True(ex.Message.StartsWith("There was an error creating the code generator instance: " +
                "Microsoft.Framework.CodeGeneration.Core.Test.CodeGeneratorDescriptorTests+CodeGeneratorWithDependencies"));
        }