public void TryGetPageDirective_Directive_WithoutPathOrContent()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Test.cshtml", @"@page");

            // Act & Assert
            Assert.True(PageDirectiveFeature.TryGetPageDirective(NullLogger.Instance, projectItem, out var template));
            Assert.Null(template);
        }
        public void TryGetPageDirective_JunkBeforeDirective()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Test.cshtml", @"Not a directive @page ""Some/Path/{value}""");
            var sink        = new TestSink();
            var logger      = new TestLogger("logger", sink, enabled: true);

            // Act & Assert
            Assert.False(PageDirectiveFeature.TryGetPageDirective(logger, projectItem, out var template));
            Assert.Null(template);
            Assert.Empty(sink.Writes);
        }
        public void TryGetPageDirective_NoNewLine()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Test.cshtml", @"@page ""Some/Path/{value}""");
            var sink        = new TestSink();
            var logger      = new TestLogger("logger", sink, enabled: true);

            // Act & Assert
            Assert.True(PageDirectiveFeature.TryGetPageDirective(logger, projectItem, out var template));
            Assert.Equal("Some/Path/{value}", template);
            Assert.Empty(sink.Writes);
        }
        public void TryGetPageDirective_NoDirective()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Test.cshtml", @"This is junk
Nobody will use it");
            var sink        = new TestSink();
            var logger      = new TestLogger("logger", sink, enabled: true);

            // Act & Assert
            Assert.False(PageDirectiveFeature.TryGetPageDirective(logger, projectItem, out var template));
            Assert.Null(template);
            Assert.Empty(sink.Writes);
        }
        public void TryGetPageDirective_DirectiveWithContent_WithoutPath()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Test.cshtml", @"@page
Non-path things");
            var sink        = new TestSink();
            var logger      = new TestLogger("logger", sink, enabled: true);

            // Act & Assert
            Assert.True(PageDirectiveFeature.TryGetPageDirective(logger, projectItem, out var template));
            Assert.Null(template);
            Assert.Empty(sink.Writes);
        }
        public void TryGetPageDirective_WithoutBothQuotes_LogsWarning(string inTemplate)
        {
            // Arrange
            var expected    = "The page directive at 'Test.cshtml' is malformed. Please fix the following issues: The 'page' directive expects a string surrounded by double quotes.";
            var sink        = new TestSink();
            var logger      = new TestLogger("logger", sink, enabled: true);
            var projectItem = new TestRazorProjectItem("Test.cshtml", $@"@page {inTemplate}");

            // Act & Assert
            Assert.True(PageDirectiveFeature.TryGetPageDirective(logger, projectItem, out var template));
            Assert.Null(template);
            Assert.Collection(sink.Writes,
                              log =>
            {
                Assert.Equal(LogLevel.Warning, log.LogLevel);
                Assert.Equal(expected, log.State.ToString());
            });
        }
        private string GetRouteTemplate(RazorProjectItem item)
        {
            var source     = item.ToSourceDocument();
            var syntaxTree = RazorParser.Parse(source);

            var template = PageDirectiveFeature.GetRouteTemplate(syntaxTree);

            if (template != null && template.Length > 0 && template[0] == '/')
            {
                return(template.Substring(1));
            }

            if (template != null && template.Length > 1 && template[0] == '~' && template[1] == '/')
            {
                return(template.Substring(1));
            }

            var @base = item.PathWithoutExtension.Substring(1);

            if (string.Equals("Index", @base, StringComparison.OrdinalIgnoreCase))
            {
                @base = string.Empty;
            }

            if (@base == string.Empty && string.IsNullOrEmpty(template))
            {
                return(string.Empty);
            }
            else if (string.IsNullOrEmpty(template))
            {
                return(@base);
            }
            else if (@base == string.Empty)
            {
                return(template);
            }
            else
            {
                return(@base + "/" + template);
            }
        }