Add plumbing for imports and includes

Groundwork for support of _ViewImports _ViewStart and others.
This commit is contained in:
Ryan Nowak 2017-01-12 09:25:19 -08:00
parent ffd455b77e
commit 208da8ca12
6 changed files with 121 additions and 7 deletions

View File

@ -2,12 +2,17 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Evolution namespace Microsoft.AspNetCore.Razor.Evolution
{ {
internal class DefaultRazorCodeDocument : RazorCodeDocument internal class DefaultRazorCodeDocument : RazorCodeDocument
{ {
public DefaultRazorCodeDocument(RazorSourceDocument source) public DefaultRazorCodeDocument(
RazorSourceDocument source,
IEnumerable<RazorSourceDocument> imports,
IEnumerable<RazorSourceDocument> includes)
{ {
if (source == null) if (source == null)
{ {
@ -15,10 +20,16 @@ namespace Microsoft.AspNetCore.Razor.Evolution
} }
Source = source; Source = source;
Imports = imports?.ToArray() ?? RazorSourceDocument.EmptyArray;
Includes = includes?.ToArray() ?? RazorSourceDocument.EmptyArray;
Items = new DefaultItemCollection(); Items = new DefaultItemCollection();
} }
public override IReadOnlyList<RazorSourceDocument> Imports { get; }
public override IReadOnlyList<RazorSourceDocument> Includes { get; }
public override ItemCollection Items { get; } public override ItemCollection Items { get; }
public override RazorSourceDocument Source { get; } public override RazorSourceDocument Source { get; }

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Evolution namespace Microsoft.AspNetCore.Razor.Evolution
{ {
@ -14,11 +15,28 @@ namespace Microsoft.AspNetCore.Razor.Evolution
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
} }
return new DefaultRazorCodeDocument(source); return Create(source, imports: null, includes: null);
} }
public static RazorCodeDocument Create(
RazorSourceDocument source,
IEnumerable<RazorSourceDocument> imports,
IEnumerable<RazorSourceDocument> includes)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
return new DefaultRazorCodeDocument(source, imports, includes);
}
public abstract IReadOnlyList<RazorSourceDocument> Imports { get; }
public abstract ItemCollection Items { get; } public abstract ItemCollection Items { get; }
public abstract IReadOnlyList<RazorSourceDocument> Includes { get; }
public abstract RazorSourceDocument Source { get; } public abstract RazorSourceDocument Source { get; }
} }
} }

View File

@ -9,6 +9,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
public abstract class RazorSourceDocument public abstract class RazorSourceDocument
{ {
internal static readonly RazorSourceDocument[] EmptyArray = new RazorSourceDocument[0];
public abstract Encoding Encoding { get; } public abstract Encoding Encoding { get; }
public abstract string Filename { get; } public abstract string Filename { get; }

View File

@ -13,12 +13,44 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var source = TestRazorSourceDocument.Create(); var source = TestRazorSourceDocument.Create();
var imports = new RazorSourceDocument[]
{
TestRazorSourceDocument.Create(),
};
var includes = new RazorSourceDocument[]
{
TestRazorSourceDocument.Create(),
};
// Act // Act
var code = new DefaultRazorCodeDocument(source); var code = new DefaultRazorCodeDocument(source, imports, includes);
// Assert // Assert
Assert.Same(source, code.Source); Assert.Same(source, code.Source);
Assert.NotNull(code.Items); Assert.NotNull(code.Items);
Assert.NotSame(imports, code.Imports);
Assert.Collection(imports, d => Assert.Same(imports[0], d));
Assert.NotSame(includes, code.Includes);
Assert.Collection(includes, d => Assert.Same(includes[0], d));
}
[Fact]
public void Ctor_AllowsNullForImportsAndIncludes()
{
// Arrange
var source = TestRazorSourceDocument.Create();
// Act
var code = new DefaultRazorCodeDocument(source, imports: null, includes: null);
// Assert
Assert.Same(source, code.Source);
Assert.NotNull(code.Items);
Assert.Empty(code.Imports);
Assert.Empty(code.Includes);
} }
} }
} }

View File

@ -20,5 +20,51 @@ namespace Microsoft.AspNetCore.Razor.Evolution
Assert.Same(source, code.Source); Assert.Same(source, code.Source);
Assert.NotNull(code.Items); Assert.NotNull(code.Items);
} }
[Fact]
public void Create_WithImportsAndIncludes()
{
// Arrange
var source = TestRazorSourceDocument.Create();
var imports = new RazorSourceDocument[]
{
TestRazorSourceDocument.Create(),
};
var includes = new RazorSourceDocument[]
{
TestRazorSourceDocument.Create(),
};
// Act
var code = RazorCodeDocument.Create(source, imports, includes);
// Assert
Assert.Same(source, code.Source);
Assert.NotNull(code.Items);
Assert.NotSame(imports, code.Imports);
Assert.Collection(imports, d => Assert.Same(imports[0], d));
Assert.NotSame(includes, code.Includes);
Assert.Collection(includes, d => Assert.Same(includes[0], d));
}
[Fact]
public void Create_WithImportsAndIncludes_AllsNull()
{
// Arrange
var source = TestRazorSourceDocument.Create();
// Act
var code = RazorCodeDocument.Create(source, imports: null, includes: null);
// Assert
Assert.Same(source, code.Source);
Assert.NotNull(code.Items);
Assert.Empty(code.Imports);
Assert.Empty(code.Includes);
}
} }
} }

View File

@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Evolution namespace Microsoft.AspNetCore.Razor.Evolution
{ {
internal class TestRazorCodeDocument : DefaultRazorCodeDocument internal class TestRazorCodeDocument : DefaultRazorCodeDocument
@ -8,17 +10,20 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public static TestRazorCodeDocument CreateEmpty() public static TestRazorCodeDocument CreateEmpty()
{ {
var source = TestRazorSourceDocument.Create(content: string.Empty); var source = TestRazorSourceDocument.Create(content: string.Empty);
return new TestRazorCodeDocument(source); return new TestRazorCodeDocument(source, imports: null, includes: null);
} }
public static TestRazorCodeDocument Create(string content) public static TestRazorCodeDocument Create(string content)
{ {
var source = TestRazorSourceDocument.Create(content); var source = TestRazorSourceDocument.Create(content);
return new TestRazorCodeDocument(source); return new TestRazorCodeDocument(source, imports: null, includes: null);
} }
private TestRazorCodeDocument(RazorSourceDocument source) private TestRazorCodeDocument(
: base(source) RazorSourceDocument source,
IEnumerable<RazorSourceDocument> imports,
IEnumerable<RazorSourceDocument> includes)
: base(source, imports, includes)
{ {
} }
} }