Add RazorSourceDocument.Create(string template)

Add document and fix casing of RazorSourceDocument.FileName
Fixes #1063
This commit is contained in:
Pranav K 2017-03-10 06:30:31 -08:00
parent 1330b7792d
commit 7d43bfc709
16 changed files with 232 additions and 83 deletions

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
Context.Writer Context.Writer
.Write("#pragma checksum \"") .Write("#pragma checksum \"")
.Write(node.Filename) .Write(node.FileName)
.Write("\" \"") .Write("\" \"")
.Write(node.Guid) .Write(node.Guid)
.Write("\" \"") .Write("\" \"")

View File

@ -51,14 +51,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
var import = imports[j]; var import = imports[j];
importsVisitor.Filename = import.Source.Filename; importsVisitor.FileName = import.Source.FileName;
importsVisitor.VisitBlock(import.Root); importsVisitor.VisitBlock(import.Root);
} }
} }
var visitor = new MainSourceVisitor(document, builder, namespaces) var visitor = new MainSourceVisitor(document, builder, namespaces)
{ {
Filename = syntaxTree.Source.Filename, FileName = syntaxTree.Source.FileName,
}; };
visitor.VisitBlock(syntaxTree.Root); visitor.VisitBlock(syntaxTree.Root);
@ -79,7 +79,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
_namespaces = namespaces; _namespaces = namespaces;
} }
public string Filename { get; set; } public string FileName { get; set; }
public override void VisitImportSpan(AddImportChunkGenerator chunkGenerator, Span span) public override void VisitImportSpan(AddImportChunkGenerator chunkGenerator, Span span)
{ {
@ -162,7 +162,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
} }
var span = new SourceSpan( var span = new SourceSpan(
node.Start.FilePath ?? Filename, node.Start.FilePath ?? FileName,
node.Start.AbsoluteIndex, node.Start.AbsoluteIndex,
node.Start.LineIndex, node.Start.LineIndex,
node.Start.CharacterIndex, node.Start.CharacterIndex,
@ -317,7 +317,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var contentLength = templateNode.Children.Sum(child => child.Source?.Length ?? 0); var contentLength = templateNode.Children.Sum(child => child.Source?.Length ?? 0);
templateNode.Source = new SourceSpan( templateNode.Source = new SourceSpan(
sourceRangeStart.Value.FilePath ?? Filename, sourceRangeStart.Value.FilePath ?? FileName,
sourceRangeStart.Value.AbsoluteIndex, sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex, sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex, sourceRangeStart.Value.CharacterIndex,
@ -353,7 +353,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var contentLength = expressionNode.Children.Sum(child => child.Source?.Length ?? 0); var contentLength = expressionNode.Children.Sum(child => child.Source?.Length ?? 0);
expressionNode.Source = new SourceSpan( expressionNode.Source = new SourceSpan(
sourceRangeStart.Value.FilePath ?? Filename, sourceRangeStart.Value.FilePath ?? FileName,
sourceRangeStart.Value.AbsoluteIndex, sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex, sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex, sourceRangeStart.Value.CharacterIndex,

View File

@ -2,10 +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;
using System.Diagnostics;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
namespace Microsoft.AspNetCore.Razor.Evolution namespace Microsoft.AspNetCore.Razor.Evolution
{ {
@ -14,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
private readonly string _content; private readonly string _content;
private readonly RazorSourceLineCollection _lines; private readonly RazorSourceLineCollection _lines;
public DefaultRazorSourceDocument(string content, Encoding encoding, string filename) public DefaultRazorSourceDocument(string content, Encoding encoding, string fileName)
{ {
if (content == null) if (content == null)
{ {
@ -28,7 +25,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
_content = content; _content = content;
Encoding = encoding; Encoding = encoding;
Filename = filename; FileName = fileName;
_lines = new DefaultRazorSourceLineCollection(this); _lines = new DefaultRazorSourceLineCollection(this);
} }
@ -37,7 +34,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public override Encoding Encoding { get; } public override Encoding Encoding { get; }
public override string Filename { get; } public override string FileName { get; }
public override int Length => _content.Length; public override int Length => _content.Length;

View File

@ -49,7 +49,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// We have an exact match for the start of a line. // We have an exact match for the start of a line.
Debug.Assert(_lineStarts[index] == position); Debug.Assert(_lineStarts[index] == position);
return new SourceLocation(_document.Filename, position, index, characterIndex: 0); return new SourceLocation(_document.FileName, position, index, characterIndex: 0);
} }
@ -59,12 +59,12 @@ namespace Microsoft.AspNetCore.Razor.Evolution
if (index == -1) if (index == -1)
{ {
// There's no preceding line, so it's based on the start of the string // There's no preceding line, so it's based on the start of the string
return new SourceLocation(_document.Filename, position, 0, position); return new SourceLocation(_document.FileName, position, 0, position);
} }
else else
{ {
var characterIndex = position - _lineStarts[index]; var characterIndex = position - _lineStarts[index];
return new SourceLocation(_document.Filename, position, index, characterIndex); return new SourceLocation(_document.FileName, position, index, characterIndex);
} }
} }

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public override bool Exists => File.Exists; public override bool Exists => File.Exists;
/// <inheritdoc /> /// <inheritdoc />
public override string Filename => File.Name; public override string FileName => File.Name;
/// <inheritdoc /> /// <inheritdoc />
public override string PhysicalPath => File.FullName; public override string PhysicalPath => File.FullName;

View File

@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
public string Bytes { get; set; } public string Bytes { get; set; }
public string Filename { get; set; } public string FileName { get; set; }
public string Guid { get; set; } public string Guid { get; set; }
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
var node = new ChecksumIRNode() var node = new ChecksumIRNode()
{ {
Filename = sourceDocument.Filename, FileName = sourceDocument.FileName,
Guid = Sha1AlgorithmId Guid = Sha1AlgorithmId
}; };

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
private readonly int _length; private readonly int _length;
public LargeTextRazorSourceDocument(StreamReader reader, int chunkMaxLength, Encoding encoding, string filename) public LargeTextRazorSourceDocument(StreamReader reader, int chunkMaxLength, Encoding encoding, string fileName)
{ {
if (reader == null) if (reader == null)
{ {
@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
_chunkMaxLength = chunkMaxLength; _chunkMaxLength = chunkMaxLength;
Encoding = encoding; Encoding = encoding;
Filename = filename; FileName = fileName;
ReadChunks(reader, _chunkMaxLength, out _length, out _chunks); ReadChunks(reader, _chunkMaxLength, out _length, out _chunks);
_lines = new DefaultRazorSourceLineCollection(this); _lines = new DefaultRazorSourceLineCollection(this);
@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public override Encoding Encoding { get; } public override Encoding Encoding { get; }
public override string Filename { get; } public override string FileName { get; }
public override int Length => _length; public override int Length => _length;

View File

@ -63,14 +63,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
get get
{ {
var index = Filename.LastIndexOf('.'); var index = FileName.LastIndexOf('.');
if (index == -1) if (index == -1)
{ {
return null; return null;
} }
else else
{ {
return Filename.Substring(index); return FileName.Substring(index);
} }
} }
} }
@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
/// <summary> /// <summary>
/// The name of the file including the extension. /// The name of the file including the extension.
/// </summary> /// </summary>
public virtual string Filename public virtual string FileName
{ {
get get
{ {

View File

@ -7,35 +7,74 @@ using System.Text;
namespace Microsoft.AspNetCore.Razor.Evolution namespace Microsoft.AspNetCore.Razor.Evolution
{ {
/// <summary>
/// The Razor template source.
/// </summary>
public abstract class RazorSourceDocument public abstract class RazorSourceDocument
{ {
private const int LargeObjectHeapLimitInChars = 40 * 1024; // 40K Unicode chars is 80KB which is less than the large object heap limit. internal const int LargeObjectHeapLimitInChars = 40 * 1024; // 40K Unicode chars is 80KB which is less than the large object heap limit.
internal static readonly RazorSourceDocument[] EmptyArray = new RazorSourceDocument[0]; internal static readonly RazorSourceDocument[] EmptyArray = new RazorSourceDocument[0];
/// <summary>
/// Encoding of the file that the text was read from.
/// </summary>
public abstract Encoding Encoding { get; } public abstract Encoding Encoding { get; }
public abstract string Filename { get; } /// <summary>
/// Path of the file the content was read from.
/// </summary>
public abstract string FileName { get; }
/// <summary>
/// Gets a character at given position.
/// </summary>
/// <param name="position">The position to get the character from.</param>
public abstract char this[int position] { get; } public abstract char this[int position] { get; }
/// <summary>
/// Gets the length of the text in characters.
/// </summary>
public abstract int Length { get; } public abstract int Length { get; }
/// <summary>
/// Gets the <see cref="RazorSourceLineCollection"/>.
/// </summary>
public abstract RazorSourceLineCollection Lines { get; } public abstract RazorSourceLineCollection Lines { get; }
/// <summary>
/// Copies a range of characters from the <see cref="RazorSourceDocument"/> to the specified <paramref name="destination"/>.
/// </summary>
/// <param name="sourceIndex">The index of the first character in this instance to copy.</param>
/// <param name="destination">The destination buffer.</param>
/// <param name="destinationIndex">The index in destination at which the copy operation begins.</param>
/// <param name="count">The number of characters in this instance to copy to destination.</param>
public abstract void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count); public abstract void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
public static RazorSourceDocument ReadFrom(Stream stream, string filename) /// <summary>
/// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to read from.</param>
/// <param name="fileName">The file name of the template.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument ReadFrom(Stream stream, string fileName)
{ {
if (stream == null) if (stream == null)
{ {
throw new ArgumentNullException(nameof(stream)); throw new ArgumentNullException(nameof(stream));
} }
return ReadFromInternal(stream, filename, encoding: null); return ReadFromInternal(stream, fileName, encoding: null);
} }
public static RazorSourceDocument ReadFrom(Stream stream, string filename, Encoding encoding) /// <summary>
/// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to read from.</param>
/// <param name="fileName">The file name of the template.</param>
/// <param name="encoding">The <see cref="System.Text.Encoding"/> to use to read the <paramref name="stream"/>.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument ReadFrom(Stream stream, string fileName, Encoding encoding)
{ {
if (stream == null) if (stream == null)
{ {
@ -47,9 +86,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution
throw new ArgumentNullException(nameof(encoding)); throw new ArgumentNullException(nameof(encoding));
} }
return ReadFromInternal(stream, filename, encoding); return ReadFromInternal(stream, fileName, encoding);
} }
/// <summary>
/// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="projectItem"/>.
/// </summary>
/// <param name="projectItem">The <see cref="RazorProjectItem"/> to read from.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument ReadFrom(RazorProjectItem projectItem) public static RazorSourceDocument ReadFrom(RazorProjectItem projectItem)
{ {
if (projectItem == null) if (projectItem == null)
@ -69,7 +113,39 @@ namespace Microsoft.AspNetCore.Razor.Evolution
} }
} }
private static RazorSourceDocument ReadFromInternal(Stream stream, string filename, Encoding encoding) /// <summary>
/// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
/// </summary>
/// <param name="content">The template content.</param>
/// <param name="fileName">The file name of the <see cref="RazorSourceDocument"/>.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
/// <remarks>Uses <see cref="System.Text.Encoding.UTF8" /></remarks>
public static RazorSourceDocument Create(string content, string fileName)
=> Create(content, fileName, Encoding.UTF8);
/// <summary>
/// Creates a <see cref="RazorSourceDocument"/> from the specified <paramref name="content"/>.
/// </summary>
/// <param name="content">The template content.</param>
/// <param name="fileName">The file name of the <see cref="RazorSourceDocument"/>.</param>
/// <param name="encoding">The <see cref="System.Text.Encoding"/> of the file <paramref name="content"/> was read from.</param>
/// <returns>The <see cref="RazorSourceDocument"/>.</returns>
public static RazorSourceDocument Create(string content, string fileName, Encoding encoding)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}
return new DefaultRazorSourceDocument(content, encoding, fileName);
}
private static RazorSourceDocument ReadFromInternal(Stream stream, string fileName, Encoding encoding)
{ {
var streamLength = (int)stream.Length; var streamLength = (int)stream.Length;
var content = string.Empty; var content = string.Empty;
@ -109,14 +185,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution
reader, reader,
LargeObjectHeapLimitInChars, LargeObjectHeapLimitInChars,
contentEncoding, contentEncoding,
filename); fileName);
} }
content = reader.ReadToEnd(); content = reader.ReadToEnd();
} }
} }
return new DefaultRazorSourceDocument(content, contentEncoding, filename); return new DefaultRazorSourceDocument(content, contentEncoding, fileName);
} }
} }
} }

View File

@ -46,7 +46,7 @@ namespace RazorPageGenerator
.SetBaseType("Microsoft.Extensions.RazorViews.BaseView") .SetBaseType("Microsoft.Extensions.RazorViews.BaseView")
.ConfigureClass((document, @class) => .ConfigureClass((document, @class) =>
{ {
@class.Name = Path.GetFileNameWithoutExtension(document.Source.Filename); @class.Name = Path.GetFileNameWithoutExtension(document.Source.FileName);
@class.AccessModifier = "internal"; @class.AccessModifier = "internal";
}); });
@ -76,7 +76,7 @@ namespace RazorPageGenerator
foreach (var item in cshtmlFiles) foreach (var item in cshtmlFiles)
{ {
Console.WriteLine(" Generating code file for view {0}...", item.Filename); Console.WriteLine(" Generating code file for view {0}...", item.FileName);
results.Add(GenerateCodeFile(templateEngine, item)); results.Add(GenerateCodeFile(templateEngine, item));
Console.WriteLine(" Done!"); Console.WriteLine(" Done!");
fileCount++; fileCount++;
@ -118,7 +118,7 @@ namespace RazorPageGenerator
public override string Path => _source.Path; public override string Path => _source.Path;
// Mask the full name since we don't want a developer's local file paths to be commited. // Mask the full name since we don't want a developer's local file paths to be commited.
public override string PhysicalPath => _source.Filename; public override string PhysicalPath => _source.FileName;
public override bool Exists => _source.Exists; public override bool Exists => _source.Exists;

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var expectedContent = "Hello, World!"; var expectedContent = "Hello, World!";
var indexerBuffer = new char[expectedContent.Length]; var indexerBuffer = new char[expectedContent.Length];
var document = new DefaultRazorSourceDocument(expectedContent, Encoding.UTF8, filename: "file.cshtml"); var document = new DefaultRazorSourceDocument(expectedContent, Encoding.UTF8, fileName: "file.cshtml");
// Act // Act
for (var i = 0; i < document.Length; i++) for (var i = 0; i < document.Length; i++)
@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var expectedContent = "Hello, World!"; var expectedContent = "Hello, World!";
var document = new DefaultRazorSourceDocument(expectedContent, Encoding.UTF8, filename: "file.cshtml"); var document = new DefaultRazorSourceDocument(expectedContent, Encoding.UTF8, fileName: "file.cshtml");
// Act & Assert // Act & Assert
Assert.Equal(expectedContent.Length, document.Length); Assert.Equal(expectedContent.Length, document.Length);
@ -46,10 +46,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var content = "Hello, World!"; var content = "Hello, World!";
// Act // Act
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: "file.cshtml"); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: "file.cshtml");
// Assert // Assert
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
} }
[Fact] [Fact]
@ -59,10 +59,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var content = "Hello, World!"; var content = "Hello, World!";
// Act // Act
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Assert // Assert
Assert.Null(document.Filename); Assert.Null(document.FileName);
} }
[Fact] [Fact]
@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "Hello, World!"; var content = "Hello, World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
var expectedContent = "Hello"; var expectedContent = "Hello";
var charBuffer = new char[expectedContent.Length]; var charBuffer = new char[expectedContent.Length];
@ -87,7 +87,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "Hello, World!"; var content = "Hello, World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
var expectedContent = "$Hello"; var expectedContent = "$Hello";
var charBuffer = new char[expectedContent.Length]; var charBuffer = new char[expectedContent.Length];
charBuffer[0] = '$'; charBuffer[0] = '$';
@ -105,7 +105,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "Hello, World!"; var content = "Hello, World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
var expectedContent = "World"; var expectedContent = "World";
var charBuffer = new char[expectedContent.Length]; var charBuffer = new char[expectedContent.Length];
@ -122,7 +122,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "Hi"; var content = "Hi";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
var charBuffer = new char[2]; var charBuffer = new char[2];
// Act // Act
@ -138,7 +138,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "Hi"; var content = "Hi";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act & Assert // Act & Assert
// //
@ -157,7 +157,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = string.Empty; var content = string.Empty;
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.Count; var actual = document.Lines.Count;
@ -171,7 +171,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = string.Empty; var content = string.Empty;
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLineLength(0); var actual = document.Lines.GetLineLength(0);
@ -185,7 +185,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "hello\n"; var content = "hello\n";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLineLength(0); var actual = document.Lines.GetLineLength(0);
@ -199,7 +199,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
// Arrange // Arrange
var content = "hello\r\n"; var content = "hello\r\n";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLineLength(0); var actual = document.Lines.GetLineLength(0);
@ -218,7 +218,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
.Append("jumps over the lazy dog.") .Append("jumps over the lazy dog.")
.ToString(); .ToString();
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -233,7 +233,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\r\nWorld!"; var content = "Hello\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -248,7 +248,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\rWorld!"; var content = "Hello\rWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -264,7 +264,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\rBig\r\nWorld!"; var content = "Hello\rBig\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -279,7 +279,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\nWorld!"; var content = "Hello\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -294,7 +294,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\u0085World!"; var content = "Hello\u0085World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -309,7 +309,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\u2028World!"; var content = "Hello\u2028World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -324,7 +324,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\u2029World!"; var content = "Hello\u2029World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = GetAllLineMappings(document); var actual = GetAllLineMappings(document);
@ -339,7 +339,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello, World!"; var content = "Hello, World!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: "Hi.cshtml"); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: "Hi.cshtml");
// Act // Act
var actual = document.Lines.GetLocation(1); var actual = document.Lines.GetLocation(1);
@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\nBig\r\nWorld!"; var content = "Hello\nBig\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLocation(0); var actual = document.Lines.GetLocation(0);
@ -373,7 +373,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\nBig\r\nWorld!"; var content = "Hello\nBig\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLocation(5); var actual = document.Lines.GetLocation(5);
@ -389,7 +389,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\nBig\r\nWorld!"; var content = "Hello\nBig\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLocation(7); var actual = document.Lines.GetLocation(7);
@ -405,7 +405,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\nBig\r\nWorld!"; var content = "Hello\nBig\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLocation(11); var actual = document.Lines.GetLocation(11);
@ -421,7 +421,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Arrange // Arrange
var content = "Hello\nBig\r\nWorld!"; var content = "Hello\nBig\r\nWorld!";
var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, filename: null); var document = new DefaultRazorSourceDocument(content, Encoding.UTF8, fileName: null);
// Act // Act
var actual = document.Lines.GetLocation(16); var actual = document.Lines.GetLocation(16);

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
Assert.Equal("/Home.cshtml", projectItem.Path); Assert.Equal("/Home.cshtml", projectItem.Path);
Assert.Equal("/Views", projectItem.BasePath); Assert.Equal("/Views", projectItem.BasePath);
Assert.True(projectItem.Exists); Assert.True(projectItem.Exists);
Assert.Equal("Home.cshtml", projectItem.Filename); Assert.Equal("Home.cshtml", projectItem.FileName);
Assert.Equal(fileInfo.FullName, projectItem.PhysicalPath); Assert.Equal(fileInfo.FullName, projectItem.PhysicalPath);
} }

View File

@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Test
var document = new LargeTextRazorSourceDocument(reader, ChunkTestLength, Encoding.UTF8, fileName); var document = new LargeTextRazorSourceDocument(reader, ChunkTestLength, Encoding.UTF8, fileName);
// Assert // Assert
Assert.Equal(fileName, document.Filename); Assert.Equal(fileName, document.FileName);
} }
[Fact] [Fact]

View File

@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var projectItem = new TestRazorProjectItem(path, basePath: "/"); var projectItem = new TestRazorProjectItem(path, basePath: "/");
// Act // Act
var fileName = projectItem.Filename; var fileName = projectItem.FileName;
// Assert // Assert
Assert.Equal(expected, fileName); Assert.Equal(expected, fileName);

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert // Assert
Assert.IsType<DefaultRazorSourceDocument>(document); Assert.IsType<DefaultRazorSourceDocument>(document);
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
Assert.Same(Encoding.UTF8, document.Encoding); Assert.Same(Encoding.UTF8, document.Encoding);
} }
@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var document = RazorSourceDocument.ReadFrom(content, "file.cshtml", Encoding.UTF32); var document = RazorSourceDocument.ReadFrom(content, "file.cshtml", Encoding.UTF32);
// Assert // Assert
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
Assert.Same(Encoding.UTF32, Assert.IsType<DefaultRazorSourceDocument>(document).Encoding); Assert.Same(Encoding.UTF32, Assert.IsType<DefaultRazorSourceDocument>(document).Encoding);
} }
@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var document = RazorSourceDocument.ReadFrom(content, "file.cshtml", Encoding.UTF32); var document = RazorSourceDocument.ReadFrom(content, "file.cshtml", Encoding.UTF32);
// Assert // Assert
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
Assert.Same(Encoding.UTF32, Assert.IsType<DefaultRazorSourceDocument>(document).Encoding); Assert.Same(Encoding.UTF32, Assert.IsType<DefaultRazorSourceDocument>(document).Encoding);
} }
@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert // Assert
Assert.IsType<DefaultRazorSourceDocument>(document); Assert.IsType<DefaultRazorSourceDocument>(document);
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
Assert.Equal(Encoding.UTF32, document.Encoding); Assert.Equal(Encoding.UTF32, document.Encoding);
} }
@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert // Assert
Assert.IsType<DefaultRazorSourceDocument>(document); Assert.IsType<DefaultRazorSourceDocument>(document);
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
Assert.Equal(Encoding.UTF32, document.Encoding); Assert.Equal(Encoding.UTF32, document.Encoding);
} }
@ -95,19 +95,95 @@ namespace Microsoft.AspNetCore.Razor.Evolution
Assert.Equal(expectedMessage, exception.Message); Assert.Equal(expectedMessage, exception.Message);
} }
[Fact] [Theory]
public void ReadFrom_LargeContent() [InlineData(100000)]
[InlineData(RazorSourceDocument.LargeObjectHeapLimitInChars)]
[InlineData(RazorSourceDocument.LargeObjectHeapLimitInChars + 2)]
[InlineData(RazorSourceDocument.LargeObjectHeapLimitInChars * 2 - 1)]
[InlineData(RazorSourceDocument.LargeObjectHeapLimitInChars * 2)]
public void ReadFrom_LargeContent(int contentLength)
{ {
// Arrange // Arrange
var content = TestRazorSourceDocument.CreateStreamContent(new string('a', 100000)); var content = new string('a', contentLength);
var stream = TestRazorSourceDocument.CreateStreamContent(content);
// Act // Act
var document = RazorSourceDocument.ReadFrom(content, "file.cshtml"); var document = RazorSourceDocument.ReadFrom(stream, "file.cshtml");
// Assert // Assert
Assert.IsType<LargeTextRazorSourceDocument>(document); Assert.IsType<LargeTextRazorSourceDocument>(document);
Assert.Equal("file.cshtml", document.Filename); Assert.Equal("file.cshtml", document.FileName);
Assert.Same(Encoding.UTF8, document.Encoding); Assert.Same(Encoding.UTF8, document.Encoding);
Assert.Equal(content, ReadContent(document));
}
[Fact]
public void ReadFrom_ProjectItem()
{
// Arrange
var projectItem = new TestRazorProjectItem("/test-path");
// Act
var document = RazorSourceDocument.ReadFrom(projectItem);
// Assert
Assert.Equal(projectItem.Path, document.FileName);
Assert.Equal(projectItem.Content, ReadContent(document));
}
[Fact]
public void ReadFrom_UsesProjectItemPhysicalPath()
{
// Arrange
var projectItem = new TestRazorProjectItem("/test-path", "some-physical-path");
// Act
var document = RazorSourceDocument.ReadFrom(projectItem);
// Assert
Assert.Equal(projectItem.PhysicalPath, document.FileName);
Assert.Equal(projectItem.Content, ReadContent(document));
}
[Fact]
public void Create_WithoutEncoding()
{
// Arrange
var content = "Hello world";
var fileName = "some-file-name";
// Act
var document = RazorSourceDocument.Create(content, fileName);
// Assert
Assert.Equal(fileName, document.FileName);
Assert.Equal(content, ReadContent(document));
Assert.Same(Encoding.UTF8, document.Encoding);
}
[Fact]
public void Create_WithEncoding()
{
// Arrange
var content = "Hello world";
var fileName = "some-file-name";
var encoding = Encoding.UTF32;
// Act
var document = RazorSourceDocument.Create(content, fileName, encoding);
// Assert
Assert.Equal(fileName, document.FileName);
Assert.Equal(content, ReadContent(document));
Assert.Same(encoding, document.Encoding);
}
private static string ReadContent(RazorSourceDocument razorSourceDocument)
{
var buffer = new char[razorSourceDocument.Length];
razorSourceDocument.CopyTo(0, buffer, 0, buffer.Length);
return new string(buffer);
} }
} }
} }

View File

@ -159,8 +159,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert // Assert
Assert.Collection(codeDocument.Imports, Assert.Collection(codeDocument.Imports,
import => Assert.Equal("/MyImport.cshtml", import.Filename), import => Assert.Equal("/MyImport.cshtml", import.FileName),
import => Assert.Equal("/Views/Home/MyImport.cshtml", import.Filename)); import => Assert.Equal("/Views/Home/MyImport.cshtml", import.FileName));
} }
[Fact] [Fact]
@ -188,8 +188,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert // Assert
Assert.Collection(codeDocument.Imports, Assert.Collection(codeDocument.Imports,
import => Assert.Same(defaultImport, import), import => Assert.Same(defaultImport, import),
import => Assert.Equal("/MyImport.cshtml", import.Filename), import => Assert.Equal("/MyImport.cshtml", import.FileName),
import => Assert.Equal("/Views/Home/MyImport.cshtml", import.Filename)); import => Assert.Equal("/Views/Home/MyImport.cshtml", import.FileName));
} }
} }
} }