public void GenerateCodeWithConfigureClass() { // Arrange var project = new FileSystemRazorProject(TestProjectRoot); var razorEngine = RazorEngine.Create(engine => { engine.Features.Add(new SuppressChecksumOptionsFeature()); engine.ConfigureClass((document, @class) => { @class.ClassName = "MyClass"; @class.Modifiers.Clear(); @class.Modifiers.Add("protected"); @class.Modifiers.Add("internal"); }); engine.ConfigureClass((document, @class) => { @class.Interfaces = new[] { "global::System.IDisposable" }; @class.BaseType = "CustomBaseType"; }); }); var templateEngine = new RazorTemplateEngine(razorEngine, project); // Act var cSharpDocument = templateEngine.GenerateCode($"{FileName}.cshtml"); // Assert AssertCSharpDocumentMatchesBaseline(cSharpDocument); }
public void Ensure_RootProperty_AssignedOnConstructor() { string root = Path.Combine(DirectoryUtils.RootDirectory, "Assets", "Files"); var project = new FileSystemRazorProject(root); Assert.Equal(project.Root, root); }
public void Ensure_ExtensionProperty_IsDefaultIfNotProvided() { string root = Path.Combine(DirectoryUtils.RootDirectory, "Assets", "Files"); var project = new FileSystemRazorProject(root); Assert.Equal(project.Extension, FileSystemRazorProject.DefaultExtension); }
public void Ensure_ExtensionProperty_AssignedOnConstructor() { string root = Path.Combine(DirectoryUtils.RootDirectory, "Assets", "Files"); string extension = FileSystemRazorProject.DefaultExtension + "_test"; var project = new FileSystemRazorProject(root, extension); Assert.Equal(project.Extension, extension); }
public async Task Ensure_TemplateKey_IsNormalizedAsync() { var project = new FileSystemRazorProject(DirectoryUtils.RootDirectory); string templateKey = "Empty"; var item = await project.GetItemAsync(Path.Combine("Assets", "Embedded", templateKey)); Assert.NotNull(item); Assert.EndsWith(templateKey + project.Extension, item.Key); }
public void TemplateKey_Normalized_On_FilesystemProject() { string templateKey = "key"; var project = new FileSystemRazorProject("/"); var compiler = TestRazorTemplateCompiler.Create(project: project); string normalizedKey = compiler.GetNormalizedKey(templateKey); Assert.NotNull(normalizedKey); Assert.Equal($"/{templateKey}", normalizedKey); }
/// <summary> /// コンストラクタ /// </summary> public PlatypusServiceBase(Logic.Interfaces.ICommonDiLogic commonDiLogic, string templateDir = null) : base(commonDiLogic) { if (string.IsNullOrEmpty(templateDir) == false) { string path = Path.Combine(Directory.GetCurrentDirectory(), templateDir); var project = new FileSystemRazorProject(path); //自動で追加される拡張子を消す https://github.com/toddams/RazorLight/issues/94 project.Extension = ""; IRazorLightEngine engine = new RazorLightEngineBuilder() .UseProject(project) .UseMemoryCachingProvider() .Build(); RenderEngine = engine; } }
public void GenerateCodeWithDefaults() { // Arrange var project = new FileSystemRazorProject(TestProjectRoot); var razorEngine = RazorEngine.Create(engine => { engine.Features.Add(new SuppressChecksumOptionsFeature()); }); var templateEngine = new RazorTemplateEngine(razorEngine, project); // Act var cSharpDocument = templateEngine.GenerateCode($"{FileName}.cshtml"); // Assert AssertCSharpDocumentMatchesBaseline(cSharpDocument); }
public async Task Ensure_GetKnownKeysAsync_Returns_Existing_Keys() { var project = new FileSystemRazorProject(DirectoryUtils.RootDirectory.ToLowerInvariant()); var knownKeys = await project.GetKnownKeysAsync(); Assert.NotNull(knownKeys); Assert.NotEmpty(knownKeys); foreach (var key in knownKeys) { var projectItem = await project.GetItemAsync(key); Assert.True(projectItem.Exists); } }
public RazorLightDependencyBuilder UseFileSystemProject(string root, string extension = null) { _services.RemoveAll <RazorLightProject>(); RazorLightProject project; if (String.IsNullOrEmpty(extension)) { project = new FileSystemRazorProject(root); } else { project = new FileSystemRazorProject(root, extension); } _services.AddSingleton <RazorLightProject>(project); return(this); }
public RazorLightDependencyBuilder UseFileSystemProject(string root, string extension = null) { _services.RemoveAll <RazorLightProject>(); RazorLightProject project; if (string.IsNullOrEmpty(extension)) { project = new FileSystemRazorProject(root); } else { project = new FileSystemRazorProject(root, extension); } // ReSharper disable once RedundantTypeArgumentsOfMethod _services.AddSingleton <RazorLightProject>(project); return(this); }
public async Task Compiler_Searches_WithNormalizedKey_IfNotFound() { string templateKey = "key"; var descriptor = new CompiledTemplateDescriptor(); var descriptorTask = Task.FromResult(descriptor); var project = new FileSystemRazorProject("/"); var compiler = TestRazorTemplateCompiler.Create(project: project); string normalizedKey = compiler.GetNormalizedKey(templateKey); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed compiler.Cache.Set(normalizedKey, descriptorTask); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed CompiledTemplateDescriptor result = await compiler.CompileAsync(templateKey); Assert.NotNull(result); Assert.Same(descriptor, result); }
public async Task Ensure_GetKnownKeysAsync_Returns_Expected_Keys() { var subsetToCheck = new[] { "Assets/Files/Empty.cshtml", "Assets/Files/Layout.cshtml" }; var project = new FileSystemRazorProject(DirectoryUtils.RootDirectory.ToLowerInvariant()); var knownKeys = await project.GetKnownKeysAsync(); Assert.NotNull(knownKeys); Assert.NotEmpty(knownKeys); foreach (var key in subsetToCheck) { Assert.Contains(key, knownKeys); } }
public void Null_TemplateKey_ThrowsOn_GetItem() { var project = new FileSystemRazorProject(DirectoryUtils.RootDirectory); Assert.ThrowsAsync <ArgumentNullException>(async() => await project.GetItemAsync("not-existing-key")); }
/// <summary> /// Creates RazorLightEngine with a filesystem razor project /// </summary> /// <param name="root">Root folder where views are stored</param> /// <param name="options">Engine options</param> /// <returns>Instance of RazorLightEngine</returns> public virtual RazorLightEngine ForFileSystem(string root, RazorLightOptions options) { var project = new FileSystemRazorProject(root); return(Create(project, options)); }
public void NotExiting_RootDirectory_Throws() { void Action() => _ = new FileSystemRazorProject(@"C:/Not/Existing/Folder/Here"); Assert.Throws <DirectoryNotFoundException>(Action); }