Esempio n. 1
0
        public void CreateCodeDocument_IncludesDefaultImportIfNotNull()
        {
            // Arrange
            var projectItem    = new TestRazorProjectItem("/Views/Home/Index.cshtml");
            var import1        = new TestRazorProjectItem("/MyImport.cshtml");
            var import2        = new TestRazorProjectItem("/Views/Home/MyImport.cshtml");
            var project        = new TestRazorProject(new[] { import1, import2, projectItem });
            var razorEngine    = RazorEngine.Create();
            var defaultImport  = RazorSourceDocument.ReadFrom(new MemoryStream(), "Default.cshtml");
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml",
                    DefaultImports  = defaultImport,
                }
            };

            // Act
            var codeDocument = templateEngine.CreateCodeDocument(projectItem);

            // Assert
            Assert.Collection(codeDocument.Imports,
                              import => Assert.Same(defaultImport, import),
                              import => Assert.Equal("/MyImport.cshtml", import.FilePath),
                              import => Assert.Equal("/Views/Home/MyImport.cshtml", import.FilePath));
        }
Esempio n. 2
0
        /// <summary>
        /// Gets <see cref="RazorSourceDocument"/> that are applicable to the specified <paramref name="projectItem"/>.
        /// </summary>
        /// <param name="projectItem">The <see cref="RazorProjectItem"/>.</param>
        /// <returns>The sequence of applicable <see cref="RazorSourceDocument"/>.</returns>
        public virtual IEnumerable <RazorSourceDocument> GetImports(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }
            var result = new List <RazorSourceDocument>();


            var importProjectItems = GetImportItems(projectItem);

            foreach (var importItem in importProjectItems)
            {
                if (importItem.Exists)
                {
                    // We want items in descending order. FindHierarchicalItems returns items in ascending order.
                    result.Insert(0, RazorSourceDocument.ReadFrom(importItem));
                }
            }

            if (Options.DefaultImports != null)
            {
                result.Insert(0, Options.DefaultImports);
            }

            return(result);
        }
        // Internal for testing
        internal static IReadOnlyList <RazorSourceDocument> GetImportSourceDocuments(
            IReadOnlyList <RazorProjectItem> importItems,
            bool suppressExceptions = false)
        {
            var imports = new List <RazorSourceDocument>();

            for (var i = 0; i < importItems.Count; i++)
            {
                var importItem = importItems[i];

                if (importItem.Exists)
                {
                    try
                    {
                        // Normal import, has file paths, content etc.
                        var sourceDocument = RazorSourceDocument.ReadFrom(importItem);
                        imports.Add(sourceDocument);
                    }
                    catch (IOException) when(suppressExceptions)
                    {
                        // Something happened when trying to read the item from disk.
                        // Catch the exception so we don't crash the editor.
                    }
                }
            }

            return(imports);
        }
        public void ReadFrom_ProjectItem()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("/test-path");

            // Act
            var document = RazorSourceDocument.ReadFrom(projectItem);

            // Assert
            Assert.Equal(projectItem.FilePath, document.FilePath);
            Assert.Equal(projectItem.Content, ReadContent(document));
        }
Esempio n. 5
0
        public void ReadFrom_EmptyStream_WithEncoding()
        {
            // Arrange
            var content = TestRazorSourceDocument.CreateStreamContent(content: string.Empty, encoding: Encoding.UTF32);

            // Act
            var document = RazorSourceDocument.ReadFrom(content, "file.cshtml", Encoding.UTF32);

            // Assert
            Assert.Equal("file.cshtml", document.FilePath);
            Assert.Same(Encoding.UTF32, Assert.IsType <StreamSourceDocument>(document).Encoding);
        }
Esempio n. 6
0
        public void ReadFrom()
        {
            // Arrange
            var content = TestRazorSourceDocument.CreateStreamContent();

            // Act
            var document = RazorSourceDocument.ReadFrom(content, "file.cshtml");

            // Assert
            Assert.IsType <StreamSourceDocument>(document);
            Assert.Equal("file.cshtml", document.FilePath);
            Assert.Same(Encoding.UTF8, document.Encoding);
        }
        public override RazorCodeDocument Process(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(filePath));
            }

            var projectItem    = FileSystem.GetItem(filePath);
            var sourceDocument = RazorSourceDocument.ReadFrom(projectItem);
            var codeDocument   = Process(sourceDocument);

            return(codeDocument);
        }
Esempio n. 8
0
        public void ReadFrom_ProjectItem_NoRelativePath()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("filePath.cshtml", "c:\\myapp\\filePath.cshtml", basePath: "c:\\myapp\\");

            // Act
            var document = RazorSourceDocument.ReadFrom(projectItem);

            // Assert
            Assert.Equal("c:\\myapp\\filePath.cshtml", document.FilePath);
            Assert.Equal("filePath.cshtml", document.RelativePath);
            Assert.Equal(projectItem.Content, ReadContent(document));
        }
Esempio n. 9
0
        public void ProcessDesignTime_WithNullImports_SetsEmptyListOnCodeDocument()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Index.cshtml");

            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty);

            // Act
            var codeDocument = projectEngine.ProcessDesignTime(RazorSourceDocument.ReadFrom(projectItem), importSources: null, tagHelpers: null);

            // Assert
            Assert.Empty(codeDocument.Imports);
        }
Esempio n. 10
0
        // Internal for testing
        internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem)
        {
            if (importItem.Exists)
            {
                // Normal import, has file paths, content etc.
                return(RazorSourceDocument.ReadFrom(importItem));
            }

            // Marker import, doesn't exist, used as an identifier for "there could be something here".
            var sourceDocumentProperties = new RazorSourceDocumentProperties(importItem.FilePath, importItem.RelativePhysicalPath);

            return(RazorSourceDocument.Create(string.Empty, sourceDocumentProperties));
        }
Esempio n. 11
0
        public void ReadFrom_WithProjectItem_FallbackToFilePath_WhenRelativePhysicalPathIsNull()
        {
            // Arrange
            var filePath    = "filePath.cshtml";
            var projectItem = new TestRazorProjectItem(filePath, relativePhysicalPath: null);

            // Act
            var document = RazorSourceDocument.ReadFrom(projectItem);

            // Assert
            Assert.Equal(filePath, document.FilePath);
            Assert.Equal(filePath, document.RelativePath);
        }
Esempio n. 12
0
        public void ReadFrom_WithProperties()
        {
            // Arrange
            var content    = TestRazorSourceDocument.CreateStreamContent(encoding: Encoding.UTF32);
            var properties = new RazorSourceDocumentProperties("c:\\myapp\\filePath.cshtml", "filePath.cshtml");

            // Act
            var document = RazorSourceDocument.ReadFrom(content, Encoding.UTF32, properties);

            // Assert
            Assert.Equal("c:\\myapp\\filePath.cshtml", document.FilePath);
            Assert.Equal("filePath.cshtml", document.RelativePath);
            Assert.Same(Encoding.UTF32, Assert.IsType <StreamSourceDocument>(document).Encoding);
        }
Esempio n. 13
0
        public void ReadFrom_WithProjectItem_UsesRelativePhysicalPath()
        {
            // Arrange
            var filePath             = "filePath.cshtml";
            var relativePhysicalPath = "relative-path.cshtml";
            var projectItem          = new TestRazorProjectItem(filePath, relativePhysicalPath: relativePhysicalPath);

            // Act
            var document = RazorSourceDocument.ReadFrom(projectItem);

            // Assert
            Assert.Equal(relativePhysicalPath, document.FilePath);
            Assert.Equal(relativePhysicalPath, document.RelativePath);
        }
        public void Process_WithFileKind_SetsOnCodeDocument()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Index.cshtml");

            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty);

            // Act
            var codeDocument = projectEngine.Process(RazorSourceDocument.ReadFrom(projectItem), "test", Array.Empty <RazorSourceDocument>(), tagHelpers: null);

            // Assert
            var actual = codeDocument.GetFileKind();

            Assert.Equal("test", actual);
        }
Esempio n. 15
0
        protected override RazorCodeDocument CreateCodeDocumentDesignTimeCore(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }

            var sourceDocument = RazorSourceDocument.ReadFrom(projectItem);

            var importFeature         = GetRequiredFeature <IImportProjectFeature>();
            var importItems           = importFeature.GetImports(projectItem);
            var importSourceDocuments = GetImportSourceDocuments(importItems, suppressExceptions: true);

            return(CreateCodeDocumentDesignTimeCore(sourceDocument, importSourceDocuments, tagHelpers: null));
        }
Esempio n. 16
0
        public void ProcessDesignTime_WithNullTagHelpers_SetsOnCodeDocument()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Index.cshtml");

            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty);

            // Act
            var codeDocument = projectEngine.ProcessDesignTime(RazorSourceDocument.ReadFrom(projectItem), Array.Empty <RazorSourceDocument>(), tagHelpers: null);

            // Assert
            var tagHelpers = codeDocument.GetTagHelpers();

            Assert.Null(tagHelpers);
        }
Esempio n. 17
0
        // Internal for testing
        internal static IReadOnlyList <RazorSourceDocument> GetImportSourceDocuments(IReadOnlyList <RazorProjectItem> importItems)
        {
            var imports = new List <RazorSourceDocument>();

            for (var i = 0; i < importItems.Count; i++)
            {
                var importItem = importItems[i];

                if (importItem.Exists)
                {
                    var sourceDocument = RazorSourceDocument.ReadFrom(importItem);
                    imports.Add(sourceDocument);
                }
            }

            return(imports);
        }
Esempio n. 18
0
        /// <summary>
        /// Generates a <see cref="T:RazorCodeDocument" /> for the specified <paramref name="projectItem" />.
        /// </summary>
        /// <param name="projectItem">The <see cref="T:RazorProjectItem" />.</param>
        /// <returns>The created <see cref="T:RazorCodeDocument" />.</returns>
        public virtual RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException("projectItem");
            }

            if (!projectItem.Exists)
            {
                throw new InvalidOperationException($"FormatRazorTemplateEngine_ItemCouldNotBeFound {projectItem.FilePath}");
            }

            var source  = RazorSourceDocument.ReadFrom(projectItem);
            var imports = this.GetImports(projectItem);

            return(RazorCodeDocument.Create(source, imports));
        }
Esempio n. 19
0
        protected override RazorCodeDocument CreateCodeDocumentDesignTimeCore(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }

            var sourceDocument = RazorSourceDocument.ReadFrom(projectItem);

            var importFeature         = GetRequiredFeature <IImportProjectFeature>();
            var importItems           = importFeature.GetImports(projectItem);
            var importSourceDocuments = GetImportSourceDocuments(importItems, suppressExceptions: true);

            var parserOptions         = GetRequiredFeature <IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
            var codeGenerationOptions = GetRequiredFeature <IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);

            return(RazorCodeDocument.Create(sourceDocument, importSourceDocuments, parserOptions, codeGenerationOptions));
        }
        public void CreateCodeDocument_NullImportFileName_IncludesDefaultImportIfNotNull()
        {
            // Arrange
            var projectItem    = new TestRazorProjectItem("/Views/Home/Index.cshtml");
            var project        = new TestRazorProjectFileSystem(new[] { projectItem });
            var razorEngine    = CreateProjectEngine().Engine;
            var defaultImport  = RazorSourceDocument.ReadFrom(new MemoryStream(), "Default.cshtml");
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    DefaultImports = defaultImport,
                }
            };

            // Act
            var codeDocument = templateEngine.CreateCodeDocument(projectItem);

            // Assert
            Assert.Collection(codeDocument.Imports,
                              import => Assert.Same(defaultImport, import));
        }
Esempio n. 21
0
        public void ProcessDesignTime_WithImportsAndTagHelpers_SetsOnCodeDocument()
        {
            // Arrange
            var projectItem        = new TestRazorProjectItem("Index.cshtml");
            var importItem         = new TestRazorProjectItem("_import.cshtml");
            var expectedImports    = new[] { RazorSourceDocument.ReadFrom(importItem) };
            var expectedTagHelpers = new[]
            {
                TagHelperDescriptorBuilder.Create("TestTagHelper", "TestAssembly").Build(),
                TagHelperDescriptorBuilder.Create("Test2TagHelper", "TestAssembly").Build(),
            };

            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty);

            // Act
            var codeDocument = projectEngine.ProcessDesignTime(RazorSourceDocument.ReadFrom(projectItem), expectedImports, expectedTagHelpers);

            // Assert
            var tagHelpers = codeDocument.GetTagHelpers();

            Assert.Same(expectedTagHelpers, tagHelpers);
            Assert.Equal(expectedImports, codeDocument.Imports);
        }
        protected RazorCodeDocument CreateCodeDocumentCore(
            RazorProjectItem projectItem,
            Action <RazorParserOptionsBuilder> configureParser,
            Action <RazorCodeGenerationOptionsBuilder> configureCodeGeneration)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }

            var sourceDocument = RazorSourceDocument.ReadFrom(projectItem);

            var importItems = new List <RazorProjectItem>();
            var features    = ProjectFeatures.OfType <IImportProjectFeature>();

            foreach (var feature in features)
            {
                importItems.AddRange(feature.GetImports(projectItem));
            }

            var importSourceDocuments = GetImportSourceDocuments(importItems);

            return(CreateCodeDocumentCore(sourceDocument, projectItem.FileKind, importSourceDocuments, tagHelpers: null, configureParser, configureCodeGeneration, cssScope: projectItem.CssScope));
        }
Esempio n. 23
0
        /// <summary>
        /// Gets <see cref="T:RazorSourceDocument" /> that are applicable to the specified <paramref name="projectItem" />.
        /// </summary>
        /// <param name="projectItem">The <see cref="T:RazorProjectItem" />.</param>
        /// <returns>The sequence of applicable <see cref="T:RazorSourceDocument" />.</returns>
        public virtual IEnumerable <RazorSourceDocument> GetImports(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException("projectItem");
            }

            var list = new List <RazorSourceDocument>();

            foreach (var razorProjectItem in this.GetImportItems(projectItem))
            {
                if (razorProjectItem.Exists)
                {
                    list.Insert(0, RazorSourceDocument.ReadFrom(razorProjectItem));
                }
            }

            if (this.Options.DefaultImports != null)
            {
                list.Insert(0, this.Options.DefaultImports);
            }

            return(list);
        }