예제 #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);
        }
예제 #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,
                new RazorTemplateEngine(
                    RazorEngine.Create(),
                    new FileProviderRazorProject(accessor)),
                accessor,
                new CSharpCompiler(referenceManager, Mock.Of <IHostingEnvironment>()),
                options,
                NullLoggerFactory.Instance);

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

            Assert.Equal(expected, exception.Message);
        }
예제 #3
0
        private static TestRazorViewCompiler GetViewCompiler(
            TestFileProvider fileProvider = null,
            Action <RoslynCompilationContext> compilationCallback = null,
            RazorReferenceManager referenceManager          = null,
            IList <CompiledViewDescriptor> precompiledViews = null)
        {
            fileProvider = fileProvider ?? new TestFileProvider();
            var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);

            compilationCallback = compilationCallback ?? (_ => { });
            var options = Options.Create(new RazorViewEngineOptions());

            if (referenceManager == null)
            {
                var applicationPartManager = new ApplicationPartManager();
                var assembly = typeof(RazorViewCompilerTest).Assembly;
                applicationPartManager.ApplicationParts.Add(new AssemblyPart(assembly));
                applicationPartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider());

                referenceManager = new DefaultRazorReferenceManager(applicationPartManager, options);
            }

            precompiledViews = precompiledViews ?? Array.Empty <CompiledViewDescriptor>();

            var hostingEnvironment = Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath");
            var projectSystem      = new FileProviderRazorProject(accessor, hostingEnvironment);
            var templateEngine     = new RazorTemplateEngine(RazorEngine.Create(), projectSystem)
            {
                Options =
                {
                    ImportsFileName = "_ViewImports.cshtml",
                }
            };
            var viewCompiler = new TestRazorViewCompiler(
                fileProvider,
                templateEngine,
                new CSharpCompiler(referenceManager, hostingEnvironment),
                compilationCallback,
                precompiledViews);

            return(viewCompiler);
        }