public async Task Ensure_PrerenderCallbacks_Are_Invoked() { //Assign var page = TemplatePageTest.CreatePage(t => t.Write("test")); bool triggered1 = false, triggered2 = false; var callbacks = new List <Action <ITemplatePage> > { t => triggered1 = true, t => triggered2 = true }; var options = new RazorLightOptions { PreRenderCallbacks = callbacks }; var engineMock = new Mock <IEngineHandler>(); engineMock.SetupGet(e => e.Options).Returns(options); //Act var templateRenderer = new TemplateRenderer(engineMock.Object, HtmlEncoder.Default, new MemoryPoolViewBufferScope()); await templateRenderer.RenderAsync(page); //Assert Assert.True(triggered1); Assert.True(triggered2); }
public RazorTemplateCompiler( RazorSourceGenerator sourceGenerator, RoslynCompilationService roslynCompilationService, RazorLightProject razorLightProject, RazorLightOptions razorLightOptions) { _razorSourceGenerator = sourceGenerator ?? throw new ArgumentNullException(nameof(sourceGenerator)); _compiler = roslynCompilationService ?? throw new ArgumentNullException(nameof(roslynCompilationService)); _razorProject = razorLightProject ?? throw new ArgumentNullException(nameof(razorLightProject)); _razorLightOptions = razorLightOptions ?? throw new ArgumentNullException(nameof(razorLightOptions)); // This is our L0 cache, and is a durable store. Views migrate into the cache as they are requested // from either the set of known precompiled views, or by being compiled. var cacheOptions = Options.Create(new MemoryCacheOptions()); _cache = new MemoryCache(cacheOptions); _normalizedKeysCache = new ConcurrentDictionary <string, string>(StringComparer.Ordinal); // We need to validate that the all of the precompiled views are unique by path (case-insensitive). // We do this because there's no good way to canonicalize paths on windows, and it will create // problems when deploying to linux. Rather than deal with these issues, we just don't support // views that differ only by case. _precompiledViews = new Dictionary <string, CompiledTemplateDescriptor>( 5, //Change capacity when precompiled views are arrived StringComparer.OrdinalIgnoreCase); }
public async Task Ensure_Content_Added_To_DynamicTemplates() { var options = new RazorLightOptions(); const string key = "key"; const string content = "content"; var project = new TestRazorProject { Value = new TextSourceRazorProjectItem(key, content) }; var engine = new RazorLightEngineBuilder() #if NETFRAMEWORK .SetOperatingAssembly(typeof(Root).Assembly) #endif .UseProject(project) .UseOptions(options) .AddDynamicTemplates(new Dictionary <string, string> { [key] = content, }) .Build(); var actual = await engine.CompileRenderStringAsync(key, content, new object(), new ExpandoObject()); Assert.NotEmpty(options.DynamicTemplates); Assert.Contains(options.DynamicTemplates, t => t.Key == key && t.Value == content); Assert.Equal(content, actual); }
public TestRazorTemplateCompiler( RazorSourceGenerator sourceGenerator, RoslynCompilationService roslynCompilationService, RazorLightProject razorLightProject, RazorLightOptions razorLightOptions) : base(sourceGenerator, roslynCompilationService, razorLightProject, razorLightOptions) { }
public TemplateFactoryProvider( RazorSourceGenerator generator, RoslynCompilationService compiler, RazorLightOptions razorOptions) { sourceGenerator = generator ?? throw new ArgumentNullException(nameof(generator)); templateCompiler = compiler ?? throw new ArgumentNullException(nameof(compiler)); options = razorOptions ?? throw new ArgumentNullException(nameof(razorOptions)); }
public static TestRazorTemplateCompiler Create(RazorLightOptions options = null, RazorLightProject project = null) { var razorOptions = options ?? new RazorLightOptions(); var metatadaManager = new DefaultMetadataReferenceManager(); var assembly = Assembly.GetCallingAssembly(); var razorProject = project ?? new EmbeddedRazorProject(assembly); var compilerService = new RoslynCompilationService(metatadaManager, assembly); var generator = new RazorSourceGenerator(DefaultRazorEngine.Instance, razorProject); return(new TestRazorTemplateCompiler(generator, compilerService, razorProject, razorOptions)); }
public void TemplateKey_NotNormalized_OnStringRendering() { string templateKey = "key"; var options = new RazorLightOptions(); options.DynamicTemplates.Add(templateKey, "Template content"); var compiler = TestRazorTemplateCompiler.Create(options); string normalizedKey = compiler.GetNormalizedKey(templateKey); Assert.NotNull(normalizedKey); Assert.Equal(templateKey, normalizedKey); }
public async Task Ensure_TemplateNotFoundException_KnownKeys_Null_When_EnableDebugMode_False() { var options = new RazorLightOptions { EnableDebugMode = false }; var project = new EmbeddedRazorProject(typeof(Root)); var compiler = TestRazorTemplateCompiler.Create(options, project); var item = new EmbeddedRazorProjectItem(typeof(Root), "Any.Key"); var exception = await compiler.CreateTemplateNotFoundException(item); Assert.Null(exception.KnownDynamicTemplateKeys); Assert.Null(exception.KnownProjectTemplateKeys); }
public async Task Ensure_Content_Added_To_DynamicTemplates() { var options = new RazorLightOptions(); string key = "key"; string content = "content"; var project = new TestRazorProject(); project.Value = new TextSourceRazorProjectItem(key, content); var engine = new EngineFactory().Create(project, options); await engine.CompileRenderAsync(key, content, new object(), typeof(object)); Assert.NotEmpty(options.DynamicTemplates); Assert.Contains(options.DynamicTemplates, t => t.Key == key && t.Value == content); }
public void Ensure_Throws_OnNull_Constructor_Dependencies() { var options = new RazorLightOptions(); var metadataManager = new DefaultMetadataReferenceManager(); var assembly = Assembly.GetCallingAssembly(); var project = new EmbeddedRazorProject(assembly); var compilerService = new RoslynCompilationService(metadataManager, assembly); var generator = new RazorSourceGenerator(DefaultRazorEngine.Instance, project); Action p1 = new Action(() => { new RazorTemplateCompiler(null, compilerService, project, options); }); Action p2 = new Action(() => { new RazorTemplateCompiler(generator, null, project, options); }); Action p3 = new Action(() => { new RazorTemplateCompiler(generator, compilerService, null, options); }); Action p4 = new Action(() => { new RazorTemplateCompiler(generator, compilerService, project, null); }); Assert.Throws <ArgumentNullException>(p1); Assert.Throws <ArgumentNullException>(p2); Assert.Throws <ArgumentNullException>(p3); Assert.Throws <ArgumentNullException>(p4); }
public async Task Ensure_TemplateNotFoundException_KnownDynamicTemplateKeys_Exist_When_EnableDebugMode_True() { var dynamicTemplateKeys = new[] { "dynamicKey1", "dynamicKey2" }; var project = new EmbeddedRazorProject(typeof(Root).Assembly, "RazorLight.Tests.Assets.Embedded"); var options = new RazorLightOptions { EnableDebugMode = true }; foreach (var dynamicKey in dynamicTemplateKeys) { options.DynamicTemplates.Add(dynamicKey, "Content"); } var compiler = TestRazorTemplateCompiler.Create(options, project); var item = new EmbeddedRazorProjectItem(typeof(Root), "Any.Key"); var exception = await compiler.CreateTemplateNotFoundException(item); Assert.NotNull(exception.KnownDynamicTemplateKeys); Assert.Equal(dynamicTemplateKeys.OrderBy(x => x), exception.KnownDynamicTemplateKeys.OrderBy(x => x)); }