Example #1
0
        public void Configure_SetsDefines()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextOptions(
                new[] { "MyDefine" },
                languageVersion: "csharp4",
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: null,
                optimize: true,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var setup   = new TestableDependencyContextOptionsSetup(dependencyContextOptions);
            var options = new RazorViewEngineOptions();

            // Act
            setup.Configure(options);

            // Assert
            Assert.Equal(new[] { "MyDefine" }, options.ParseOptions.PreprocessorSymbolNames);
        }
Example #2
0
        public void Constructor_ConfiguresAllowUnsafe()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: null,
                platform: null,
                allowUnsafe: true,
                warningsAsErrors: null,
                optimize: null,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var referenceManager   = Mock.Of <RazorReferenceManager>();
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();

            var compiler = new TestCSharpCompiler(referenceManager, hostingEnvironment, dependencyContextOptions);

            // Act & Assert
            var compilationOptions = compiler.CSharpCompilationOptions;

            Assert.True(compilationOptions.AllowUnsafe);
        }
Example #3
0
        public void Compile_UsesApplicationsCompilationSettings_ForParsingAndCompilation()
        {
            // Arrange
            var content = "public class Test {}";
            var define  = "MY_CUSTOM_DEFINE";
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { define },
                languageVersion: null,
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: null,
                optimize: true,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();
            var compiler           = new TestCSharpCompiler(ReferenceManager, hostingEnvironment, dependencyContextOptions);

            // Act
            var syntaxTree = compiler.CreateSyntaxTree(SourceText.From(content));

            // Assert
            Assert.Contains(define, syntaxTree.Options.PreprocessorSymbolNames);
        }
Example #4
0
        public void Configure_SetsOptimizationLevel()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextOptions(
                new[] { "MyDefine" },
                languageVersion: null,
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: null,
                optimize: true,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var setup   = new TestableDependencyContextOptionsSetup(dependencyContextOptions);
            var options = new RazorViewEngineOptions();

            // Act
            setup.Configure(options);

            // Assert
            Assert.False(options.CompilationOptions.AllowUnsafe);
            Assert.Equal(ReportDiagnostic.Default, options.CompilationOptions.GeneralDiagnosticOption);
            Assert.Equal(OptimizationLevel.Release, options.CompilationOptions.OptimizationLevel);
        }
Example #5
0
        public void Constructor_SetsDefines()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: null,
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: null,
                optimize: true,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();

            var compiler = new TestCSharpCompiler(ReferenceManager, hostingEnvironment, dependencyContextOptions);

            // Act & Assert
            var parseOptions = compiler.ParseOptions;

            Assert.Equal(new[] { "MyDefine", "RELEASE" }, parseOptions.PreprocessorSymbolNames);
        }
Example #6
0
        public void Constructor_SetsDiagnosticOption()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: null,
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: true,
                optimize: null,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();

            var compiler = new TestCSharpCompiler(ReferenceManager, hostingEnvironment, dependencyContextOptions);

            // Act & Assert
            var compilationOptions = compiler.CSharpCompilationOptions;

            Assert.Equal(ReportDiagnostic.Error, compilationOptions.GeneralDiagnosticOption);
        }
Example #7
0
        public void EmitOptions_SetsDebugInformationFormatToPortable_WhenDebugTypeIsEmbedded()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: "7.1",
                platform: null,
                allowUnsafe: true,
                warningsAsErrors: null,
                optimize: null,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: "embedded",
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();

            var compiler = new TestCSharpCompiler(ReferenceManager, hostingEnvironment, dependencyContextOptions);

            // Act & Assert
            var emitOptions = compiler.EmitOptions;

            Assert.Equal(DebugInformationFormat.PortablePdb, emitOptions.DebugInformationFormat);
            Assert.True(compiler.EmitPdb);
        }
Example #8
0
        public void Constructor_ConfiguresLanguageVersion()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: "7.1",
                platform: null,
                allowUnsafe: true,
                warningsAsErrors: null,
                optimize: null,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);

            var compiler = new TestCSharpCompiler(new DefaultMetadataReferenceManager(), dependencyContextOptions
#if NETFRAMEWORK
                                                  , typeof(Root).Assembly
#endif
                                                  );

            // Act & Assert
            var compilationOptions = compiler.ParseOptions;

            Assert.Equal(LanguageVersion.CSharp7_1, compilationOptions.LanguageVersion);
        }
Example #9
0
        public void ConfigureAfterRazorViewEngineOptionsSetupIsExecuted_CorrectlySetsUpOptimizationLevel()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextOptions(
                new[] { "MyDefine" },
                languageVersion: null,
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: null,
                optimize: true,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var dependencyContextSetup = new TestableDependencyContextOptionsSetup(dependencyContextOptions);
            var options            = new RazorViewEngineOptions();
            var hostingEnvironment = new Mock <IHostingEnvironment>();

            hostingEnvironment.SetupGet(e => e.EnvironmentName)
            .Returns("Development");
            var viewEngineSetup = new RazorViewEngineOptionsSetup(hostingEnvironment.Object);

            // Act
            viewEngineSetup.Configure(options);
            dependencyContextSetup.Configure(options);

            // Assert
            Assert.Equal(OptimizationLevel.Release, options.CompilationOptions.OptimizationLevel);
        }
Example #10
0
        public void EmitOptions_ReadsDebugTypeFromDependencyContext(string debugType, DebugInformationFormat expected)
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: "7.1",
                platform: null,
                allowUnsafe: true,
                warningsAsErrors: null,
                optimize: null,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: debugType,
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var referenceManager   = Mock.Of <RazorReferenceManager>();
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();

            var compiler = new TestCSharpCompiler(referenceManager, hostingEnvironment, dependencyContextOptions);

            // Act & Assert
            var emitOptions = compiler.EmitOptions;

            Assert.Equal(expected, emitOptions.DebugInformationFormat);
        }
Example #11
0
 public TestCSharpCompiler(
     RazorReferenceManager referenceManager,
     IWebHostEnvironment hostingEnvironment,
     DependencyContextCompilationOptions options)
     : base(referenceManager, hostingEnvironment)
 {
     _options = options;
 }
            public TestCSharpCompiler(
#pragma warning disable CS0618 // Type or member is obsolete
                RazorReferenceManager referenceManager,
#pragma warning restore CS0618 // Type or member is obsolete
                IHostingEnvironment hostingEnvironment,
                DependencyContextCompilationOptions options)
                : base(referenceManager, hostingEnvironment)
            {
                _options = options;
            }
        public void EmitOptions_DoesNotSetEmitPdb_IfDebugTypeIsNone()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: "7.1",
                platform: null,
                allowUnsafe: true,
                warningsAsErrors: null,
                optimize: null,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: "none",
                emitEntryPoint: null,
                generateXmlDocumentation: null);
            var hostingEnvironment = Mock.Of <IHostingEnvironment>();

            var compiler = new TestCSharpCompiler(ReferenceManager, hostingEnvironment, dependencyContextOptions);

            // Act & Assert
            Assert.False(compiler.EmitPdb);
        }
        public void Constructor_SetsOptimizationLevel()
        {
            // Arrange
            var dependencyContextOptions = new DependencyContextCompilationOptions(
                new[] { "MyDefine" },
                languageVersion: null,
                platform: null,
                allowUnsafe: null,
                warningsAsErrors: null,
                optimize: true,
                keyFile: null,
                delaySign: null,
                publicSign: null,
                debugType: null,
                emitEntryPoint: null,
                generateXmlDocumentation: null);

            var compiler = new TestCSharpCompiler(new DefaultMetadataReferenceManager(), dependencyContextOptions);

            // Act & Assert
            var compilationOptions = compiler.CSharpCompilationOptions;

            Assert.Equal(OptimizationLevel.Release, compilationOptions.OptimizationLevel);
        }
 public DependencyContext(Microsoft.Extensions.DependencyModel.TargetInfo target, Microsoft.Extensions.DependencyModel.CompilationOptions compilationOptions, System.Collections.Generic.IEnumerable <Microsoft.Extensions.DependencyModel.CompilationLibrary> compileLibraries, System.Collections.Generic.IEnumerable <Microsoft.Extensions.DependencyModel.RuntimeLibrary> runtimeLibraries, System.Collections.Generic.IEnumerable <Microsoft.Extensions.DependencyModel.RuntimeFallbacks> runtimeGraph)
 {
 }
 public TestCSharpCompiler(IMetadataReferenceManager referenceManager, DependencyContextCompilationOptions options) : base(referenceManager, Assembly.GetEntryAssembly())
 {
     _options = options;
 }
 public TestableDependencyContextOptionsSetup(DependencyContextOptions options)
     : base(Mock.Of <IHostingEnvironment>())
 {
     _options = options;
 }