public void Read_ReturnsResourceContent() { Type rootType = typeof(Root); string templateKey = "Assets.Embedded.Empty.cshtml"; string fullTemplateKey = rootType.Namespace + ".Assets.Embedded.Empty.cshtml"; string resourceContent = null; using (var reader = new StreamReader(rootType.Assembly.GetManifestResourceStream(fullTemplateKey))) { resourceContent = reader.ReadToEnd(); } var item = new EmbeddedRazorProjectItem(typeof(Root), templateKey); string projectContent = null; using (var reader = new StreamReader(item.Read())) { projectContent = reader.ReadToEnd(); } Assert.NotNull(projectContent); Assert.Equal(resourceContent, projectContent); }
public void ReturnsExistsTrue_OnExistingTemplate() { string templateKey = "Assets.Embedded.Empty.cshtml"; var item = new EmbeddedRazorProjectItem(typeof(Root), templateKey); Assert.NotNull(item); Assert.True(item.Exists); }
public void ReturnsExistFalse_OnNonExistingTemplate() { string templateKey = "Assets.Embedded.IDoNotExist.cshtml"; var item = new EmbeddedRazorProjectItem(typeof(Root), templateKey); Assert.NotNull(item); Assert.False(item.Exists); }
public void Ensure_ConstructorParameters_AreApplied() { var projectItem = new EmbeddedRazorProjectItem(typeof(Root), "ewffw"); var document = new Mock <RazorCSharpDocument>().Object; var template = new GeneratedRazorTemplate(projectItem, document); Assert.Same(projectItem, template.ProjectItem); Assert.Same(document, template.CSharpDocument); }
public void TemplateKey_IsProjectKey() { var projectItem = new EmbeddedRazorProjectItem(typeof(Root), "ewffw"); var document = new Mock <RazorCSharpDocument>().Object; var template = new GeneratedRazorTemplate(projectItem, document); Assert.NotNull(template.TemplateKey); Assert.Equal(template.TemplateKey, projectItem.Key); }
public void Ensure_ConstructorParams_AreApplied() { string templateKey = "Assets.Embedded.Empty"; Type rootType = typeof(Root); var item = new EmbeddedRazorProjectItem(rootType, templateKey); Assert.Equal(item.Key, templateKey); Assert.Equal(item.RootType, rootType); Assert.Equal(item.Assembly, rootType.GetTypeInfo().Assembly); }
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 void GeneratedCode_Taken_From_CSharpDocument() { var projectItem = new EmbeddedRazorProjectItem(typeof(Root), "ewffw"); string value = "test"; var moq = new Mock <RazorCSharpDocument>(); moq.Setup(t => t.GeneratedCode).Returns(value); var document = moq.Object; var template = new GeneratedRazorTemplate(projectItem, document); Assert.NotNull(template.GeneratedCode); Assert.Equal(template.GeneratedCode, document.GeneratedCode); }
public async Task CreateCodeDocumentAsync_Throws_On_ProjectItem_Not_Exists() { var generator = new RazorSourceGenerator(DefaultRazorEngine.Instance, project: null); string templateKey = "Assets.Embedded.IDoNotExist.cshtml"; var projectItem = new EmbeddedRazorProjectItem(typeof(Root), templateKey); Assert.False(projectItem.Exists); Func <Task> action = () => generator.CreateCodeDocumentAsync(projectItem); var exception = await Assert.ThrowsAsync <InvalidOperationException>(action); Assert.Equal($"{ nameof(RazorLightProjectItem)} of type {projectItem.GetType().FullName} with key {projectItem.Key} does not exist.", exception.Message); }
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)); }
public override Task <RazorLightProjectItem> GetItemAsync(string templateKey) { foreach (var expander in _expanders) { foreach (var location in expander.ExpandResource(templateKey)) { _logger?.LogInformation($"Trying to find MailTemplate {templateKey} in {location}."); var item = new EmbeddedRazorProjectItem(Assembly, RootNamespace, location); if (!item.Exists) { continue; } _logger?.LogInformation($"Found MailTemplate {templateKey} in {location}."); return(Task.FromResult((RazorLightProjectItem)item)); } } // let the base-class try it's best and throw... return(base.GetItemAsync(templateKey)); }
public void Null_Namespace_WillBe_Empty() { var item = new EmbeddedRazorProjectItem(typeof(Root).Assembly, null, "key"); Assert.Equal("", item.RootNamespace); }