diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcRazorTemplateEngineTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcRazorTemplateEngineTest.cs index e14b83605e..072ee96976 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcRazorTemplateEngineTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/MvcRazorTemplateEngineTest.cs @@ -2,10 +2,10 @@ // 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; using Microsoft.AspNetCore.Mvc.Razor.Extensions.Internal; using Microsoft.AspNetCore.Razor.Evolution; -using Microsoft.Extensions.FileProviders; using Moq; using Xunit; @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions }; var mvcRazorTemplateEngine = new MvcRazorTemplateEngine( RazorEngine.Create(), - GetRazorProject(new TestFileProvider())); + new TestRazorProject()); // Act var imports = mvcRazorTemplateEngine.Options.DefaultImports; @@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions }; var mvcRazorTemplateEngine = new MvcRazorTemplateEngine( RazorEngine.Create(), - GetRazorProject(new TestFileProvider())); + new TestRazorProject()); // Act var imports = mvcRazorTemplateEngine.Options.DefaultImports; @@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions // Arrange var mvcRazorTemplateEngine = new MvcRazorTemplateEngine( RazorEngine.Create(), - GetRazorProject(new TestFileProvider())); + new TestRazorProject()); // Act var imports = mvcRazorTemplateEngine.Options.DefaultImports; @@ -91,11 +91,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { // Arrange var path = "/Views/Home/Index.cshtml"; - var fileProvider = new TestFileProvider(); - fileProvider.AddFile(path, "Hello world"); + var item = new TestRazorProjectItem(path) + { + Content = "Hello world", + }; + var project = new TestRazorProject(new List() { item, }); + var mvcRazorTemplateEngine = new MvcRazorTemplateEngine( RazorEngine.Create(), - GetRazorProject(fileProvider)); + project); // Act var codeDocument = mvcRazorTemplateEngine.CreateCodeDocument(path); @@ -110,17 +114,5 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions imports.CopyTo(0, contentChars, 0, imports.Length); return new string(contentChars); } - - private static RazorProject GetRazorProject(IFileProvider fileProvider) - { - var razorProject = new Mock(); - razorProject.Setup(s => s.GetItem(It.IsAny())) - .Returns((string path) => { - var fileInfo = fileProvider.GetFileInfo(path); - return new DefaultRazorProjectItem(fileInfo, null, path); - }); - - return razorProject.Object; - } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/RazorEngineTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/RazorEngineTest.cs index fd63a29c03..88582c0db7 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/RazorEngineTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/RazorEngineTest.cs @@ -12,7 +12,6 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis.Razor; using Microsoft.Extensions.DependencyModel; -using Microsoft.Extensions.FileProviders; using Moq; using Xunit; @@ -132,12 +131,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions }); var inputContent = ResourceFile.ReadResource(_assembly, inputFile, sourceFile: true); - var fileProvider = new TestFileProvider(); - fileProvider.AddFile(inputFile, inputContent); - var fileInfo = fileProvider.GetFileInfo(inputFile); - var razorTemplateEngine = new MvcRazorTemplateEngine(engine, GetRazorProject(fileProvider)); - var razorProjectItem = new DefaultRazorProjectItem(fileInfo, basePath: null, path: inputFile); - var codeDocument = razorTemplateEngine.CreateCodeDocument(razorProjectItem); + var item = new TestRazorProjectItem(inputFile) { Content = inputContent, }; + var project = new TestRazorProject(new List() + { + item, + }); + + var razorTemplateEngine = new MvcRazorTemplateEngine(engine, project); + var codeDocument = razorTemplateEngine.CreateCodeDocument(item); codeDocument.Items["SuppressUniqueIds"] = "test"; codeDocument.Items["NewLineString"] = "\r\n"; @@ -173,12 +174,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions }); var inputContent = ResourceFile.ReadResource(_assembly, inputFile, sourceFile: true); - var fileProvider = new TestFileProvider(); - fileProvider.AddFile(inputFile, inputContent); - var fileInfo = fileProvider.GetFileInfo(inputFile); - var razorTemplateEngine = new MvcRazorTemplateEngine(engine, GetRazorProject(fileProvider)); - var razorProjectItem = new DefaultRazorProjectItem(fileInfo, basePath: null, path: inputFile); - var codeDocument = razorTemplateEngine.CreateCodeDocument(razorProjectItem); + var item = new TestRazorProjectItem(inputFile) { Content = inputContent, }; + var project = new TestRazorProject(new List() + { + item, + }); + + var razorTemplateEngine = new MvcRazorTemplateEngine(engine, project); + var codeDocument = razorTemplateEngine.CreateCodeDocument(item); codeDocument.Items["SuppressUniqueIds"] = "test"; codeDocument.Items["NewLineString"] = "\r\n"; @@ -208,11 +211,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions .Select(assemblyPath => MetadataReference.CreateFromFile(assemblyPath)) .ToList(); - var syntaxTree = CreateTagHelperSyntaxTree(); + var syntaxTree = CreateTagHelperSyntaxTree(); var compilation = CSharpCompilation.Create("Microsoft.AspNetCore.Mvc.Razor", syntaxTree, references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var stream = new MemoryStream(); - var compilationResult = compilation.Emit(stream, options: new EmitOptions() ); + var compilationResult = compilation.Emit(stream, options: new EmitOptions()); stream.Position = 0; Assert.True(compilationResult.Success); @@ -237,37 +240,5 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions return new SyntaxTree[] { CSharpSyntaxTree.ParseText(text) }; } - - private static RazorProject GetRazorProject(IFileProvider fileProvider) - { - var razorProject = new Mock(); - - return razorProject.Object; - } - } - - public class DefaultRazorProjectItem : RazorProjectItem - { - public DefaultRazorProjectItem(IFileInfo fileInfo, string basePath, string path) - { - FileInfo = fileInfo; - BasePath = basePath; - Path = path; - } - - public IFileInfo FileInfo { get; } - - public override string BasePath { get; } - - public override string Path { get; } - - public override string PhysicalPath { get; } - - public override bool Exists => FileInfo.Exists; - - public override Stream Read() - { - return FileInfo.CreateReadStream(); - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFileProvider.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFileProvider.cs deleted file mode 100644 index 462508ef66..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFileProvider.cs +++ /dev/null @@ -1,186 +0,0 @@ -// 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; -using System.Collections.Generic; -using System.IO; -using System.Text; -using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.Primitives; - -namespace Microsoft.AspNetCore.Mvc.Razor.Extensions -{ - public class TestFileChangeToken : IChangeToken - { - public bool HasChanged => throw new NotImplementedException(); - - public bool ActiveChangeCallbacks => throw new NotImplementedException(); - - public IDisposable RegisterChangeCallback(Action callback, object state) - { - throw new NotImplementedException(); - } - } - - public class TestFileInfo : IFileInfo - { - private string _content; - - public bool IsDirectory { get; } = false; - - public DateTimeOffset LastModified { get; set; } - - public long Length { get; set; } - - public string Name { get; set; } - - public string PhysicalPath { get; set; } - - public string Content - { - get { return _content; } - set - { - _content = value; - Length = Encoding.UTF8.GetByteCount(Content); - } - } - - public bool Exists - { - get { return true; } - } - - public Stream CreateReadStream() - { - var bytes = Encoding.UTF8.GetBytes(Content); - return new MemoryStream(bytes); - } - } - - public class TestFileProvider : IFileProvider - { - private readonly Dictionary _lookup = - new Dictionary(StringComparer.Ordinal); - private readonly Dictionary _directoryContentsLookup = - new Dictionary(); - - private readonly Dictionary _fileTriggers = - new Dictionary(StringComparer.Ordinal); - - public virtual IDirectoryContents GetDirectoryContents(string subpath) - { - if (_directoryContentsLookup.TryGetValue(subpath, out var value)) - { - return value; - } - - return new NotFoundDirectoryContents(); - } - - public TestFileInfo AddFile(string path, string contents) - { - var fileInfo = new TestFileInfo - { - Content = contents, - PhysicalPath = path, - Name = Path.GetFileName(path), - LastModified = DateTime.UtcNow, - }; - - AddFile(path, fileInfo); - - return fileInfo; - } - - public void AddFile(string path, IFileInfo contents) - { - _lookup[path] = contents; - } - - public void DeleteFile(string path) - { - _lookup.Remove(path); - } - - public virtual IFileInfo GetFileInfo(string subpath) - { - if (_lookup.ContainsKey(subpath)) - { - return _lookup[subpath]; - } - else - { - return new NotFoundFileInfo(); - } - } - - public virtual IChangeToken Watch(string filter) - { - TestFileChangeToken changeToken; - if (!_fileTriggers.TryGetValue(filter, out changeToken) || changeToken.HasChanged) - { - changeToken = new TestFileChangeToken(); - _fileTriggers[filter] = changeToken; - } - - return changeToken; - } - - private class NotFoundFileInfo : IFileInfo - { - public bool Exists - { - get - { - return false; - } - } - - public bool IsDirectory - { - get - { - throw new NotImplementedException(); - } - } - - public DateTimeOffset LastModified - { - get - { - throw new NotImplementedException(); - } - } - - public long Length - { - get - { - throw new NotImplementedException(); - } - } - - public string Name - { - get - { - throw new NotImplementedException(); - } - } - - public string PhysicalPath - { - get - { - throw new NotImplementedException(); - } - } - - public Stream CreateReadStream() - { - throw new NotImplementedException(); - } - } - } -} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestRazorProject.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestRazorProject.cs new file mode 100644 index 0000000000..146c5b184f --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestRazorProject.cs @@ -0,0 +1,39 @@ +// 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; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.AspNetCore.Razor.Evolution +{ + public class TestRazorProject : RazorProject + { + private readonly Dictionary _lookup; + + public TestRazorProject() + : this(new RazorProjectItem[0]) + { + } + + public TestRazorProject(IList items) + { + _lookup = items.ToDictionary(item => item.Path); + } + + public override IEnumerable EnumerateItems(string basePath) + { + throw new NotImplementedException(); + } + + public override RazorProjectItem GetItem(string path) + { + if (!_lookup.TryGetValue(path, out var value)) + { + value = new NotFoundProjectItem("", path); + } + + return value; + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestRazorProjectItem.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestRazorProjectItem.cs new file mode 100644 index 0000000000..9ea4784a13 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestRazorProjectItem.cs @@ -0,0 +1,33 @@ +// 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.IO; +using System.Text; + +namespace Microsoft.AspNetCore.Razor.Evolution +{ + public class TestRazorProjectItem : RazorProjectItem + { + public TestRazorProjectItem( + string path, + string physicalPath = null, + string basePath = "/") + { + Path = path; + PhysicalPath = physicalPath; + BasePath = basePath; + } + + public override string BasePath { get; } + + public override string Path { get; } + + public override string PhysicalPath { get; } + + public override bool Exists => true; + + public string Content { get; set; } = "Default content"; + + public override Stream Read() => new MemoryStream(Encoding.UTF8.GetBytes(Content)); + } +}