Merge branch 'preview-1/stabilization' into dev

# Conflicts:
#	build/dependencies.props
This commit is contained in:
N. Taylor Mullen 2017-04-28 17:20:20 -07:00
commit f0d0c3d0b8
4 changed files with 97 additions and 7 deletions

View File

@ -85,7 +85,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
var directiveSource = NormalizeDirectory(directive.Source?.FilePath);
var baseNamespace = directive.Tokens.First().Content;
var baseNamespace = directive.Tokens.FirstOrDefault()?.Content;
if (string.IsNullOrEmpty(baseNamespace))
{
// The namespace directive was incomplete.
@namespace = string.Empty;
return false;
}
if (string.IsNullOrEmpty(source) || directiveSource == null)
{
// No sources, can't compute a suffix.

View File

@ -182,12 +182,6 @@ namespace Microsoft.AspNetCore.Razor.Language
{
throw new ArgumentNullException(nameof(projectItem));
}
if (!projectItem.Exists)
{
throw new InvalidOperationException(Resources.FormatRazorTemplateEngine_ItemCouldNotBeFound(projectItem.Path));
}
var result = new List<RazorSourceDocument>();

View File

@ -9,6 +9,48 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
public class NamespaceDirectiveTest
{
[Fact]
public void TryComputeNamespace_IncompleteDirective_UsesEmptyNamespace()
{
// Arrange
var source = "c:\\foo\\bar\\bleh.cshtml";
var imports = "c:\\foo\\baz\\bleh.cshtml";
var node = new DirectiveIRNode()
{
Descriptor = NamespaceDirective.Directive,
Source = new SourceSpan(imports, 0, 0, 0, 0),
};
// Act
var computed = NamespaceDirective.TryComputeNamespace(source, node, out var @namespace);
// Assert
Assert.False(computed);
Assert.Equal(string.Empty, @namespace);
}
[Fact]
public void TryComputeNamespace_EmptyDirective_UsesEmptyNamespace()
{
// Arrange
var source = "c:\\foo\\bar\\bleh.cshtml";
var imports = "c:\\foo\\baz\\bleh.cshtml";
var node = new DirectiveIRNode()
{
Descriptor = NamespaceDirective.Directive,
Source = new SourceSpan(imports, 0, 0, 0, 0),
};
node.Children.Add(new DirectiveTokenIRNode() { Content = string.Empty });
node.Children[0].Parent = node;
// Act
var computed = NamespaceDirective.TryComputeNamespace(source, node, out var @namespace);
// Assert
Assert.False(computed);
Assert.Equal(string.Empty, @namespace);
}
// When we don't have a relationship between the source file and the imports file
// we will just use the namespace on the node directly.
[Theory]

View File

@ -11,6 +11,53 @@ namespace Microsoft.AspNetCore.Razor.Language
{
public class RazorTemplateEngineTest
{
[Fact]
public void GetImports_CanQueryInformationOnNonExistentFileWithoutImports()
{
// Arrange
var project = new TestRazorProject();
var razorEngine = RazorEngine.Create();
var templateEngine = new RazorTemplateEngine(razorEngine, project)
{
Options =
{
ImportsFileName = "MyImport.cshtml"
}
};
var projectItemToQuery = project.GetItem("/Views/Home/Index.cshtml");
// Act
var imports = templateEngine.GetImports(projectItemToQuery);
// Assert
Assert.Empty(imports);
}
[Fact]
public void GetImports_CanQueryInformationOnNonExistentFileWithImports()
{
// Arrange
var path = "/Views/Home/MyImport.cshtml";
var projectItem = new TestRazorProjectItem(path);
var project = new TestRazorProject(new[] { projectItem });
var razorEngine = RazorEngine.Create();
var templateEngine = new RazorTemplateEngine(razorEngine, project)
{
Options =
{
ImportsFileName = "MyImport.cshtml"
}
};
var projectItemToQuery = project.GetItem("/Views/Home/Index.cshtml");
// Act
var imports = templateEngine.GetImports(projectItemToQuery);
// Assert
var import = Assert.Single(imports);
Assert.Equal(projectItem.Path, import.FileName);
}
[Fact]
public void GenerateCode_ThrowsIfItemCannotBeFound()
{