Esempio n. 1
0
        public void Compile_SucceedsIfReferencesAreAddedInCallback()
        {
            // Arrange
            Action <RoslynCompilationContext> compilationCallback = context =>
            {
                var assemblyLocation = typeof(object).Assembly.Location;

                context.Compilation = context
                                      .Compilation
                                      .AddReferences(MetadataReference.CreateFromFile(assemblyLocation));
            };

            var applicationPartManager = new ApplicationPartManager();
            var referenceManager       = new DefaultRazorReferenceManager(
                applicationPartManager,
                Options.Create(new RazorViewEngineOptions()));
            var compiler = GetViewCompiler(
                compilationCallback: compilationCallback,
                referenceManager: referenceManager);

            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "some-relative-path.cshtml"));

            // Act
            var result = compiler.CompileAndEmit(codeDocument, "public class Test {}");

            // Assert
            Assert.NotNull(result);
        }
Esempio n. 2
0
        public void GetCompiler_ThrowsIfNullFileProvider()
        {
            // Arrange
            var expected =
                $"'{typeof(RazorViewEngineOptions).FullName}.{nameof(RazorViewEngineOptions.FileProviders)}' must " +
                $"not be empty. At least one '{typeof(IFileProvider).FullName}' is required to locate a view for " +
                "rendering.";
            var fileProvider = new NullFileProvider();
            var accessor     = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);

            var partManager = new ApplicationPartManager();
            var options     = Options.Create(new RazorViewEngineOptions());

            var referenceManager = new DefaultRazorReferenceManager(partManager, options);

            var provider = new RazorViewCompilerProvider(
                partManager,
                RazorProjectEngine.Create(
                    RazorConfiguration.Default,
                    new FileProviderRazorProjectFileSystem(accessor, Mock.Of <IHostingEnvironment>())),
                accessor,
                new CSharpCompiler(referenceManager, Mock.Of <IHostingEnvironment>()),
                options,
                new RazorViewCompilationMemoryCacheProvider(),
                NullLoggerFactory.Instance);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(
                () => provider.GetCompiler());

            Assert.Equal(expected, exception.Message);
        }
Esempio n. 3
0
        public void GetCompilationReferences_CombinesApplicationPartAndOptionMetadataReferences()
        {
            // Arrange
            var options = new RazorViewEngineOptions();
            var objectAssemblyLocation          = typeof(object).GetTypeInfo().Assembly.Location;
            var objectAssemblyMetadataReference = MetadataReference.CreateFromFile(objectAssemblyLocation);

            options.AdditionalCompilationReferences.Add(objectAssemblyMetadataReference);

            var applicationPartManager = GetApplicationPartManager();
            var feature = new MetadataReferenceFeature();

            applicationPartManager.PopulateFeature(feature);
            var partReferences            = feature.MetadataReferences;
            var expectedReferenceDisplays = partReferences
                                            .Concat(new[] { objectAssemblyMetadataReference })
                                            .Select(r => r.Display);
            var referenceManager = new DefaultRazorReferenceManager(
                applicationPartManager,
                Options.Create(options));

            // Act
            var references        = referenceManager.CompilationReferences;
            var referenceDisplays = references.Select(reference => reference.Display);

            // Assert
            Assert.Equal(expectedReferenceDisplays, referenceDisplays);
        }