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.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Evolution
{
internal class DefaultRazorCodeDocument : RazorCodeDocument
{
public DefaultRazorCodeDocument(RazorSourceDocument source)
public DefaultRazorCodeDocument(
RazorSourceDocument source,
IEnumerable<RazorSourceDocument> imports,
IEnumerable<RazorSourceDocument> includes)
{
if (source == null)
{
@ -15,10 +20,16 @@ namespace Microsoft.AspNetCore.Razor.Evolution
}
Source = source;
Imports = imports?.ToArray() ?? RazorSourceDocument.EmptyArray;
Includes = includes?.ToArray() ?? RazorSourceDocument.EmptyArray;
Items = new DefaultItemCollection();
}
public override IReadOnlyList<RazorSourceDocument> Imports { get; }
public override IReadOnlyList<RazorSourceDocument> Includes { get; }
public override ItemCollection Items { 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.
using System;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Evolution
{
@ -14,11 +15,28 @@ namespace Microsoft.AspNetCore.Razor.Evolution
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 IReadOnlyList<RazorSourceDocument> Includes { get; }
public abstract RazorSourceDocument Source { get; }
}
}

View File

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

View File

@ -13,12 +13,44 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange
var source = TestRazorSourceDocument.Create();
var imports = new RazorSourceDocument[]
{
TestRazorSourceDocument.Create(),
};
var includes = new RazorSourceDocument[]
{
TestRazorSourceDocument.Create(),
};
// Act
var code = new DefaultRazorCodeDocument(source);
var code = new DefaultRazorCodeDocument(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 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.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.
// 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
{
internal class TestRazorCodeDocument : DefaultRazorCodeDocument
@ -8,17 +10,20 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public static TestRazorCodeDocument CreateEmpty()
{
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)
{
var source = TestRazorSourceDocument.Create(content);
return new TestRazorCodeDocument(source);
return new TestRazorCodeDocument(source, imports: null, includes: null);
}
private TestRazorCodeDocument(RazorSourceDocument source)
: base(source)
private TestRazorCodeDocument(
RazorSourceDocument source,
IEnumerable<RazorSourceDocument> imports,
IEnumerable<RazorSourceDocument> includes)
: base(source, imports, includes)
{
}
}