Skip to content

Commit

Permalink
Merge pull request #391 from maxbanas/fix/378-1
Browse files Browse the repository at this point in the history
Change exception type/message when project item does not exist
  • Loading branch information
jzabroski authored Nov 17, 2020
2 parents f36d8b7 + 608dc20 commit 8d41e69
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/RazorLight/Generation/RazorSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public async Task<IGeneratedRazorTemplate> GenerateCodeAsync(RazorLightProjectIt

if (!projectItem.Exists)
{
throw new TemplateNotFoundException($"Project can not find template with key {projectItem.Key}");
throw new InvalidOperationException($"{nameof(RazorLightProjectItem)} of type " +
$"{projectItem.GetType().FullName} with key {projectItem.Key} does not exist.");
}

RazorCodeDocument codeDocument = await CreateCodeDocumentAsync(projectItem);
Expand Down Expand Up @@ -108,7 +109,8 @@ public virtual async Task<RazorCodeDocument> CreateCodeDocumentAsync(RazorLightP

if (!projectItem.Exists)
{
throw new InvalidOperationException($"Project can not find template with key {projectItem.Key}");
throw new InvalidOperationException($"{nameof(RazorLightProjectItem)} of type " +
$"{projectItem.GetType().FullName} with key {projectItem.Key} does not exist.");
}

using (var stream = projectItem.Read())
Expand Down
57 changes: 56 additions & 1 deletion tests/RazorLight.Tests/Generation/RazorSourceGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public async Task GetImports_Returns_EmptyCollection_On_Empty_Project_WhenResolv
Assert.Empty(result);
}


[Fact]
public async Task GenerateCode_ByKey_Throws_OnEmpty_Project()
{
Expand All @@ -129,5 +128,61 @@ public async Task GenerateCode_ByKey_Throws_OnEmpty_Project()
var exception = await Assert.ThrowsAsync<InvalidOperationException>(action);
Assert.Equal("Can not resolve a content for the template \"key\" as there is no project set. You can only render a template by passing it's content directly via string using corresponding function overload", exception.Message);
}

[Fact]
public async Task GenerateCode_ByProjectItem_Throws_On_Null_ProjectItem()
{
var generator = new RazorSourceGenerator(DefaultRazorEngine.Instance, project: null);

Func<Task> action = () => generator.GenerateCodeAsync((RazorLightProjectItem)null);

var exception = await Assert.ThrowsAsync<ArgumentNullException>(action);
Assert.Equal("projectItem", exception.ParamName);
}

[Fact]
public async Task GenerateCode_ByProjectItem_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.GenerateCodeAsync(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);
}

[Fact]
public async Task CreateCodeDocumentAsync_Throws_On_Null_ProjectItem()
{
var generator = new RazorSourceGenerator(DefaultRazorEngine.Instance, project: null);

Func<Task> action = () => generator.CreateCodeDocumentAsync(null);

var exception = await Assert.ThrowsAsync<ArgumentNullException>(action);
Assert.Equal("projectItem", exception.ParamName);
}

[Fact]
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);
}
}
}

0 comments on commit 8d41e69

Please sign in to comment.