diff --git a/.build.Nuke/Build.GitLab.cs b/.build.Nuke/Build.GitLab.cs index be8bec9679e144d07a11010b8800055b91a068ab..aa3ac4dd415bf83fe5ba4d65a73832a5ecda5137 100644 --- a/.build.Nuke/Build.GitLab.cs +++ b/.build.Nuke/Build.GitLab.cs @@ -83,7 +83,7 @@ sealed partial class Build : NukeBuild packageLink, new StreamContent(fileStream)).ConfigureAwait(false); - _ = response.EnsureSuccessStatusCode(); + _ = response.EnsureSuccessStatusCode(); } catch (Exception ex) { @@ -116,7 +116,7 @@ sealed partial class Build : NukeBuild description = $"Created {Date}" }).ConfigureAwait(false); - _ = response.EnsureSuccessStatusCode(); + _ = response.EnsureSuccessStatusCode(); } catch (Exception ex) { @@ -148,7 +148,7 @@ sealed partial class Build : NukeBuild message = $"Automatic tag creation: {isScheduled} at {Date}" }).ConfigureAwait(false); - _ = response.EnsureSuccessStatusCode(); + _ = response.EnsureSuccessStatusCode(); } catch (Exception ex) { @@ -229,7 +229,7 @@ sealed partial class Build : NukeBuild url = itemLink }).ConfigureAwait(false); - _ = response.EnsureSuccessStatusCode(); + _ = response.EnsureSuccessStatusCode(); } catch (Exception ex) { diff --git a/.build.Nuke/Build.Test.cs b/.build.Nuke/Build.Test.cs index 76ec9e937a11896cc5d0fd669c84a637cf718a69..42e83d0ea781558f546980cca963d757d17494c4 100644 --- a/.build.Nuke/Build.Test.cs +++ b/.build.Nuke/Build.Test.cs @@ -30,26 +30,26 @@ sealed partial class Build : NukeBuild .DependsOn(Compile) .Executes(() => { - _ = coverageResultDirectory.CreateDirectory(); - _ = Coverlet(s => s - .SetTarget("dotnet") - .SetTargetArgs("test --no-build --no-restore") - .SetAssembly(testAssembly) - // .SetThreshold(75) - .SetOutput(coverageResultFile) - .SetFormat(CoverletOutputFormat.cobertura)); + _ = coverageResultDirectory.CreateDirectory(); + _ = Coverlet(s => s + .SetTarget("dotnet") + .SetTargetArgs("test --no-build --no-restore") + .SetAssembly(testAssembly) + // .SetThreshold(75) + .SetOutput(coverageResultFile) + .SetFormat(CoverletOutputFormat.cobertura)); }); public Target TestReport => td => td .DependsOn(Test) .Executes(() => { - _ = coverageReportDirectory.CreateDirectory(); - _ = ReportGenerator(s => s - .SetTargetDirectory(coverageReportDirectory) - .SetReportTypes([ReportTypes.Html, ReportTypes.TextSummary]) - .SetReports(coverageResultFile) - ); + _ = coverageReportDirectory.CreateDirectory(); + _ = ReportGenerator(s => s + .SetTargetDirectory(coverageReportDirectory) + .SetReportTypes([ReportTypes.Html, ReportTypes.TextSummary]) + .SetReports(coverageResultFile) + ); var summaryText = coverageReportSummaryDirectory.ReadAllLines(); Log.Information(string.Join(Environment.NewLine, summaryText)); }); diff --git a/.editorconfig b/.editorconfig index a02ffc7a6d7a73b06a3f3d3d1ef497e5c7a5e2de..080a294fcb13bf2c8836c30f5e1ceade5fd77584 100644 --- a/.editorconfig +++ b/.editorconfig @@ -67,6 +67,7 @@ dotnet_diagnostic.CA2201.severity = error dotnet_diagnostic.CA2208.severity = error dotnet_diagnostic.CA2214.severity = error dotnet_diagnostic.CS8019.severity = warning +dotnet_diagnostic.IDE0055.severity = warning [**/obj/**/*.cs] dotnet_diagnostic.CS8019.severity = none # disable on debug genereated files diff --git a/source/BuildCommand.cs b/source/BuildCommand.cs index ae0f4d132b1f182b2a1486d63c932ba18e80157e..e023d280762e6c1945a0d8af8b7114ffb3c64d9f 100644 --- a/source/BuildCommand.cs +++ b/source/BuildCommand.cs @@ -51,7 +51,7 @@ public class BuildCommand : BaseGeneratorCommand if (output is IPage page) { - var path = (url + (site.UglyURLs ? "" : "/index.html")).TrimStart('/'); + var path = (url + (site.UglyURLs ? string.Empty : "/index.html")).TrimStart('/'); // Generate the output path var outputAbsolutePath = Path.Combine(options.Output, path); @@ -87,7 +87,7 @@ public class BuildCommand : BaseGeneratorCommand /// /// Copy a folder content from source into the output folder. - /// + /// /// The source folder to copy from. /// The output folder to copy to. private static void CopyFolder(string source, string output) diff --git a/source/Helpers/SourceFileWatcher.cs b/source/Helpers/SourceFileWatcher.cs index bcacae6c145ccbad688b97f87c1c225668f9dd52..2a3bcce13e6c68393a16bbb37362acb58b71f006 100644 --- a/source/Helpers/SourceFileWatcher.cs +++ b/source/Helpers/SourceFileWatcher.cs @@ -1,3 +1,5 @@ +using System; + namespace SuCoS.Helpers; /// @@ -7,8 +9,8 @@ public sealed class SourceFileWatcher : IFileWatcher, IDisposable { /// /// The FileSystemWatcher object that monitors the source directory for file changes. - /// When a change is detected, this triggers a server restart to ensure the served content - /// remains up-to-date. The FileSystemWatcher is configured with the source directory + /// When a change is detected, this triggers a server restart to ensure the served content + /// remains up-to-date. The FileSystemWatcher is configured with the source directory /// at construction and starts watching immediately. /// private FileSystemWatcher? fileWatcher; @@ -17,7 +19,7 @@ public sealed class SourceFileWatcher : IFileWatcher, IDisposable public void Start(string SourceAbsolutePath, Action OnSourceFileChanged) { ArgumentNullException.ThrowIfNull(OnSourceFileChanged); - + fileWatcher = new FileSystemWatcher { Path = SourceAbsolutePath, diff --git a/source/Helpers/StopwatchReporter.cs b/source/Helpers/StopwatchReporter.cs index 34be0c06b2cfbdbe7e50ab284265a98b4dc7be54..8ec271b24bb52078118506cb3c890f8abaa5c2b9 100644 --- a/source/Helpers/StopwatchReporter.cs +++ b/source/Helpers/StopwatchReporter.cs @@ -73,7 +73,7 @@ public class StopwatchReporter _ = itemCounts.TryGetValue(stepName, out var itemCount); var duration = stopwatch.ElapsedMilliseconds; var durationString = $"{duration} ms"; - var status = itemCount > 0 ? itemCount.ToString(CultureInfo.InvariantCulture) : ""; + var status = itemCount > 0 ? itemCount.ToString(CultureInfo.InvariantCulture) : string.Empty; reportData.Add((Step: stepName, Status: status, DurationString: durationString, Duration: duration)); } diff --git a/source/Helpers/Urlizer.cs b/source/Helpers/Urlizer.cs index a3976f322df5b4630cee05a4a37cff098de93dff..1e783a5164bb607ce78f731cf14211884e6abaf1 100644 --- a/source/Helpers/Urlizer.cs +++ b/source/Helpers/Urlizer.cs @@ -24,13 +24,13 @@ public static partial class Urlizer /// public static string Urlize(string? title, UrlizerOptions? options = null) { - title ??= ""; + title ??= string.Empty; options ??= new UrlizerOptions(); // Use default options if not provided var cleanedTitle = !options.LowerCase ? title : title.ToLower(CultureInfo.CurrentCulture); var replacementChar = options.ReplacementChar ?? '\0'; - var replacementCharString = options.ReplacementChar.ToString() ?? ""; + var replacementCharString = options.ReplacementChar.ToString() ?? string.Empty; // Remove non-alphanumeric characters and replace spaces with the replacement character cleanedTitle = (options.ReplaceDot ? UrlizeRegexAlpha() : UrlizeRegexAlphaDot()) diff --git a/source/Parser/IMetadataParser.cs b/source/Parsers/IMetadataParser.cs similarity index 100% rename from source/Parser/IMetadataParser.cs rename to source/Parsers/IMetadataParser.cs diff --git a/source/Parser/ParamsConverter.cs b/source/Parsers/ParamsConverter.cs similarity index 100% rename from source/Parser/ParamsConverter.cs rename to source/Parsers/ParamsConverter.cs diff --git a/source/Parser/StaticContext.cs b/source/Parsers/StaticContext.cs similarity index 100% rename from source/Parser/StaticContext.cs rename to source/Parsers/StaticContext.cs diff --git a/source/Parser/YAMLParser.cs b/source/Parsers/YAMLParser.cs similarity index 100% rename from source/Parser/YAMLParser.cs rename to source/Parsers/YAMLParser.cs diff --git a/test/BaseGeneratorCommandTests.cs b/test/BaseGeneratorCommandTests.cs index ee68c57dc34292338b632e822c1e6aa63184a6e0..de99b7aa479cde381438e4ccbd73dc1cec06471b 100644 --- a/test/BaseGeneratorCommandTests.cs +++ b/test/BaseGeneratorCommandTests.cs @@ -24,13 +24,13 @@ public class BaseGeneratorCommandTests [Fact] public void Constructor_ShouldThrowArgumentNullException_WhenOptionsIsNull() { - _ = Assert.Throws(() => new BaseGeneratorCommandStub(null!, testLogger)); + _ = Assert.Throws(() => new BaseGeneratorCommandStub(null!, testLogger)); } [Fact] public void Constructor_ShouldThrowArgumentNullException_WhenLoggerIsNull() { - _ = Assert.Throws(() => new BaseGeneratorCommandStub(testOptions, null!)); + _ = Assert.Throws(() => new BaseGeneratorCommandStub(testOptions, null!)); } [Fact] diff --git a/test/Helpers/UrlizerTests.cs b/test/Helpers/UrlizerTests.cs index ad6d42f0ffabe07b62ce2f1783719ebb827ea303..02b44ff09beb83b329c3a3692293212dbc0640ac 100644 --- a/test/Helpers/UrlizerTests.cs +++ b/test/Helpers/UrlizerTests.cs @@ -11,7 +11,7 @@ public class UrlizerTests public void Urlize_NullOrEmptyText_ThrowsArgumentNullException(string? text) { var result = Urlizer.Urlize(text); - Assert.Equal("", result); + Assert.Equal(string.Empty, result); } [Theory] @@ -21,7 +21,7 @@ public class UrlizerTests { var result = Urlizer.UrlizePath(path); - Assert.Equal("", result); + Assert.Equal(string.Empty, result); } [Theory] @@ -77,7 +77,7 @@ public class UrlizerTests const string text = "!@#$%^&*()"; var result = Urlizer.Urlize(text); - Assert.Equal("", result); + Assert.Equal(string.Empty, result); } [Fact] diff --git a/test/Models/PageTests.cs b/test/Models/PageTests.cs index f254da7b249a0544edee8fed043091ff1098bcf3..8dbb6c869742c7e28122e88510985c458ac01fc1 100644 --- a/test/Models/PageTests.cs +++ b/test/Models/PageTests.cs @@ -89,7 +89,7 @@ word03 word04 word05 6 7 eight // Assert Assert.Equal(3, site.OutputReferences.Count); - _ = site.OutputReferences.TryGetValue(url, out var pageOther); + _ = site.OutputReferences.TryGetValue(url, out var pageOther); Assert.NotNull(pageOther); Assert.Same(page, pageOther); } @@ -179,7 +179,7 @@ word03 word04 word05 6 7 eight }, site); var options = Substitute.For(); - _ = options.Draft.Returns(draftOption); + _ = options.Draft.Returns(draftOption); // Assert Assert.Equal(expectedValue, site.IsValidPage(page, options)); @@ -199,7 +199,7 @@ word03 word04 word05 6 7 eight // Act var options = Substitute.For(); - _ = options.Future.Returns(futureOption); + _ = options.Future.Returns(futureOption); // Assert Assert.Equal(expected, site.IsValidDate(page, options)); diff --git a/test/Parser/YAMLParserTests.cs b/test/Parser/YAMLParserTests.cs index dda4fa4cd32541505291fb3a0e1fc45f2062eacf..4f8c8ffb9e4eee7dfa7ba298fc4698170d3f7df3 100644 --- a/test/Parser/YAMLParserTests.cs +++ b/test/Parser/YAMLParserTests.cs @@ -7,9 +7,9 @@ namespace Tests.YAMLParser; public class YAMLParserTests : TestSetup { - private readonly SuCoS.Parser.YAMLParser parser; + private readonly SuCoS.Parser.YAMLParser parser; - private const string pageFrontmaterCONST = @"Title: Test Title + private const string pageFrontmaterCONST = @"Title: Test Title Type: post Date: 2023-07-01 LastMod: 2023-06-01 @@ -33,12 +33,12 @@ Params: - Test - Real Data "; - private const string pageMarkdownCONST = @" + private const string pageMarkdownCONST = @" # Real Data Test This is a test using real data. Real Data Test "; - private const string siteContentCONST = @" + private const string siteContentCONST = @" Title: My Site BaseURL: https://www.example.com/ Description: Tastiest C# Static Site Generator of the World @@ -55,237 +55,237 @@ Params: - Test - Real Data "; - private const string fileFullPathCONST = "test.md"; - private const string fileRelativePathCONST = "test.md"; - private readonly string pageContent; + private const string fileFullPathCONST = "test.md"; + private const string fileRelativePathCONST = "test.md"; + private readonly string pageContent; - public YAMLParserTests() - { + public YAMLParserTests() + { parser = new SuCoS.Parser.YAMLParser(); - pageContent = @$"--- + pageContent = @$"--- {pageFrontmaterCONST} --- {pageMarkdownCONST}"; - } + } - [Fact] - public void GetSection_ShouldReturnFirstFolderName() - { - // Arrange - var filePath = Path.Combine("folder1", "folder2", "file.md"); + [Fact] + public void GetSection_ShouldReturnFirstFolderName() + { + // Arrange + var filePath = Path.Combine("folder1", "folder2", "file.md"); - // Act - var section = SiteHelper.GetSection(filePath); + // Act + var section = SiteHelper.GetSection(filePath); - // Assert - Assert.Equal("folder1", section); - } + // Assert + Assert.Equal("folder1", section); + } - [Theory] - [InlineData(@"--- + [Theory] + [InlineData(@"--- Title: Test Title --- ", "Test Title")] - [InlineData(@"--- + [InlineData(@"--- Date: 2023-04-01 --- ", "")] - public void ParseFrontmatter_ShouldParseTitleCorrectly(string fileContent, string expectedTitle) - { - // Arrange - var frontMatter = parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, fileContent); + public void ParseFrontmatter_ShouldParseTitleCorrectly(string fileContent, string expectedTitle) + { + // Arrange + var frontMatter = parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, fileContent); - // Assert - Assert.Equal(expectedTitle, frontMatter.Title); - } + // Assert + Assert.Equal(expectedTitle, frontMatter.Title); + } - [Theory] - [InlineData(@"--- + [Theory] + [InlineData(@"--- Date: 2023-01-01 --- ", "2023-01-01")] - [InlineData(@"--- + [InlineData(@"--- Date: 2023/01/01 --- ", "2023-01-01")] - public void ParseFrontmatter_ShouldParseDateCorrectly(string fileContent, string expectedDateString) - { - // Arrange - var expectedDate = DateTime.Parse(expectedDateString, CultureInfo.InvariantCulture); - - // Act - var frontMatter = parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, fileContent); - - // Assert - Assert.Equal(expectedDate, frontMatter.Date); - } - - [Fact] - public void ParseFrontmatter_ShouldParseOtherFieldsCorrectly() - { - // Arrange - var expectedDate = DateTime.Parse("2023-07-01", CultureInfo.InvariantCulture); - var expectedLastMod = DateTime.Parse("2023-06-01", CultureInfo.InvariantCulture); - var expectedPublishDate = DateTime.Parse("2023-06-01", CultureInfo.InvariantCulture); - var expectedExpiryDate = DateTime.Parse("2024-06-01", CultureInfo.InvariantCulture); - - // Act - var frontMatter = parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, pageContent); - - // Assert - Assert.Equal("Test Title", frontMatter.Title); - Assert.Equal("post", frontMatter.Type); - Assert.Equal(expectedDate, frontMatter.Date); - Assert.Equal(expectedLastMod, frontMatter.LastMod); - Assert.Equal(expectedPublishDate, frontMatter.PublishDate); - Assert.Equal(expectedExpiryDate, frontMatter.ExpiryDate); - } - - [Fact] - public void ParseFrontmatter_ShouldThrowException_WhenInvalidYAMLSyntax() - { - // Arrange - const string fileContent = @"--- + public void ParseFrontmatter_ShouldParseDateCorrectly(string fileContent, string expectedDateString) + { + // Arrange + var expectedDate = DateTime.Parse(expectedDateString, CultureInfo.InvariantCulture); + + // Act + var frontMatter = parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, fileContent); + + // Assert + Assert.Equal(expectedDate, frontMatter.Date); + } + + [Fact] + public void ParseFrontmatter_ShouldParseOtherFieldsCorrectly() + { + // Arrange + var expectedDate = DateTime.Parse("2023-07-01", CultureInfo.InvariantCulture); + var expectedLastMod = DateTime.Parse("2023-06-01", CultureInfo.InvariantCulture); + var expectedPublishDate = DateTime.Parse("2023-06-01", CultureInfo.InvariantCulture); + var expectedExpiryDate = DateTime.Parse("2024-06-01", CultureInfo.InvariantCulture); + + // Act + var frontMatter = parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, pageContent); + + // Assert + Assert.Equal("Test Title", frontMatter.Title); + Assert.Equal("post", frontMatter.Type); + Assert.Equal(expectedDate, frontMatter.Date); + Assert.Equal(expectedLastMod, frontMatter.LastMod); + Assert.Equal(expectedPublishDate, frontMatter.PublishDate); + Assert.Equal(expectedExpiryDate, frontMatter.ExpiryDate); + } + + [Fact] + public void ParseFrontmatter_ShouldThrowException_WhenInvalidYAMLSyntax() + { + // Arrange + const string fileContent = @"--- Title --- "; - // Assert - Assert.Throws(() => parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, fileContent)); - } - - [Fact] - public void ParseSiteSettings_ShouldReturnSiteWithCorrectSettings() - { - // Act - var siteSettings = parser.Parse(siteContentCONST); - - - // Assert - Assert.Equal("My Site", siteSettings.Title); - Assert.Equal("https://www.example.com/", siteSettings.BaseURL); - Assert.Equal("Tastiest C# Static Site Generator of the World", siteSettings.Description); - Assert.Equal("Copyright message", siteSettings.Copyright); - } - - [Fact] - public void ParseParams_ShouldFillParamsWithNonMatchingFields() - { - // Arrange - var page = new Page(parser.ParseFrontmatterAndMarkdown(string.Empty, string.Empty, pageContent), site); - - // Assert - Assert.False(page.Params.ContainsKey("customParam")); - Assert.Equal("Custom Value", page.Params["ParamsCustomParam"]); - } - - [Fact] - public void ParseFrontmatter_ShouldParseContentInSiteFolder() - { - // Arrange - var date = DateTime.Parse("2023-07-01", CultureInfo.InvariantCulture); - var frontMatter = parser.ParseFrontmatterAndMarkdown("", "", pageContent); - Page page = new(frontMatter, site); - - // Act - site.PostProcessPage(page); - - // Assert - Assert.Equal(date, frontMatter.Date); - } - - [Fact] - public void ParseFrontmatter_ShouldCreateTags() - { - // Arrange - var frontMatter = parser.ParseFrontmatterAndMarkdown("", "", pageContent); - Page page = new(frontMatter, site); - - // Act - site.PostProcessPage(page); - - // Assert - Assert.Equal(2, page.TagsReference.Count); - } - - [Fact] - public void ParseFrontmatter_ShouldThrowExceptionWhenSiteIsNull() - { - _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdownFromFile(null!, "fakeFilePath")); - } - - [Fact] - public void ParseFrontmatter_ShouldThrowExceptionWhenFilePathIsNull() - { - _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdownFromFile(null!)); - } - - [Fact] - public void ParseFrontmatter_ShouldThrowExceptionWhenFilePathDoesNotExist() - { - _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdownFromFile("fakePath")); - } - - [Fact] - public void ParseFrontmatter_ShouldThrowExceptionWhenFilePathDoesNotExist2() - { - _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdown(null!, null!, "fakeContent")); - } - - [Fact] - public void ParseFrontmatter_ShouldHandleEmptyFileContent() - { - _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdown("fakeFilePath", "/fakeFilePath", "")); - } - - [Fact] - public void ParseYAML_ShouldThrowExceptionWhenFrontmatterIsInvalid() - { - _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdown("fakeFilePath", "/fakeFilePath", "invalidFrontmatter")); - } - - [Fact] - public void ParseSiteSettings_ShouldReturnSiteSettings() - { - // Arrange - var siteSettings = parser.Parse(siteContentCONST); - - // Assert - Assert.NotNull(siteSettings); - Assert.Equal("My Site", siteSettings.Title); - Assert.Equal("https://www.example.com/", siteSettings.BaseURL); - } - - [Fact] - public void ParseSiteSettings_ShouldReturnContent() - { - // Arrange - var frontMatter = parser.ParseFrontmatterAndMarkdown("fakeFilePath", "/fakeFilePath", pageContent); - - // Assert - Assert.Equal(pageMarkdownCONST, frontMatter.RawContent); - } - - - [Fact] - public void SiteParams_ShouldHandleEmptyContent() - { - Assert.Empty(site.Params); - } - - [Fact] - public void SiteParams_ShouldPopulateParamsWithExtraFields() - { - // Arrange - var siteSettings = parser.Parse(siteContentCONST); - site = new Site(generateOptionsMock, siteSettings, frontMatterParser, loggerMock, systemClockMock); - - // Assert - Assert.NotEmpty(siteSettings.Params); - Assert.DoesNotContain("customParam", site.Params); - Assert.Contains("ParamsCustomParam", site.Params); - Assert.Equal("Custom Value", site.Params["ParamsCustomParam"]); - Assert.Equal(new[] { "Test", "Real Data" }, ((Dictionary)siteSettings.Params["ParamsNestedData"])["Level2"]); - Assert.Equal("Test", ((siteSettings.Params["ParamsNestedData"] as Dictionary)?["Level2"] as List)?[0]); - } + // Assert + Assert.Throws(() => parser.ParseFrontmatterAndMarkdown(fileRelativePathCONST, fileFullPathCONST, fileContent)); + } + + [Fact] + public void ParseSiteSettings_ShouldReturnSiteWithCorrectSettings() + { + // Act + var siteSettings = parser.Parse(siteContentCONST); + + + // Assert + Assert.Equal("My Site", siteSettings.Title); + Assert.Equal("https://www.example.com/", siteSettings.BaseURL); + Assert.Equal("Tastiest C# Static Site Generator of the World", siteSettings.Description); + Assert.Equal("Copyright message", siteSettings.Copyright); + } + + [Fact] + public void ParseParams_ShouldFillParamsWithNonMatchingFields() + { + // Arrange + var page = new Page(parser.ParseFrontmatterAndMarkdown(string.Empty, string.Empty, pageContent), site); + + // Assert + Assert.False(page.Params.ContainsKey("customParam")); + Assert.Equal("Custom Value", page.Params["ParamsCustomParam"]); + } + + [Fact] + public void ParseFrontmatter_ShouldParseContentInSiteFolder() + { + // Arrange + var date = DateTime.Parse("2023-07-01", CultureInfo.InvariantCulture); + var frontMatter = parser.ParseFrontmatterAndMarkdown(string.Empty, string.Empty, pageContent); + Page page = new(frontMatter, site); + + // Act + site.PostProcessPage(page); + + // Assert + Assert.Equal(date, frontMatter.Date); + } + + [Fact] + public void ParseFrontmatter_ShouldCreateTags() + { + // Arrange + var frontMatter = parser.ParseFrontmatterAndMarkdown(string.Empty, string.Empty, pageContent); + Page page = new(frontMatter, site); + + // Act + site.PostProcessPage(page); + + // Assert + Assert.Equal(2, page.TagsReference.Count); + } + + [Fact] + public void ParseFrontmatter_ShouldThrowExceptionWhenSiteIsNull() + { + _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdownFromFile(null!, "fakeFilePath")); + } + + [Fact] + public void ParseFrontmatter_ShouldThrowExceptionWhenFilePathIsNull() + { + _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdownFromFile(null!)); + } + + [Fact] + public void ParseFrontmatter_ShouldThrowExceptionWhenFilePathDoesNotExist() + { + _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdownFromFile("fakePath")); + } + + [Fact] + public void ParseFrontmatter_ShouldThrowExceptionWhenFilePathDoesNotExist2() + { + _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdown(null!, null!, "fakeContent")); + } + + [Fact] + public void ParseFrontmatter_ShouldHandleEmptyFileContent() + { + _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdown("fakeFilePath", "/fakeFilePath", string.Empty)); + } + + [Fact] + public void ParseYAML_ShouldThrowExceptionWhenFrontmatterIsInvalid() + { + _ = Assert.Throws(() => parser.ParseFrontmatterAndMarkdown("fakeFilePath", "/fakeFilePath", "invalidFrontmatter")); + } + + [Fact] + public void ParseSiteSettings_ShouldReturnSiteSettings() + { + // Arrange + var siteSettings = parser.Parse(siteContentCONST); + + // Assert + Assert.NotNull(siteSettings); + Assert.Equal("My Site", siteSettings.Title); + Assert.Equal("https://www.example.com/", siteSettings.BaseURL); + } + + [Fact] + public void ParseSiteSettings_ShouldReturnContent() + { + // Arrange + var frontMatter = parser.ParseFrontmatterAndMarkdown("fakeFilePath", "/fakeFilePath", pageContent); + + // Assert + Assert.Equal(pageMarkdownCONST, frontMatter.RawContent); + } + + + [Fact] + public void SiteParams_ShouldHandleEmptyContent() + { + Assert.Empty(site.Params); + } + + [Fact] + public void SiteParams_ShouldPopulateParamsWithExtraFields() + { + // Arrange + var siteSettings = parser.Parse(siteContentCONST); + site = new Site(generateOptionsMock, siteSettings, frontMatterParser, loggerMock, systemClockMock); + + // Assert + Assert.NotEmpty(siteSettings.Params); + Assert.DoesNotContain("customParam", site.Params); + Assert.Contains("ParamsCustomParam", site.Params); + Assert.Equal("Custom Value", site.Params["ParamsCustomParam"]); + Assert.Equal(new[] { "Test", "Real Data" }, ((Dictionary)siteSettings.Params["ParamsNestedData"])["Level2"]); + Assert.Equal("Test", ((siteSettings.Params["ParamsNestedData"] as Dictionary)?["Level2"] as List)?[0]); + } } diff --git a/test/ServerHandlers/PingRequestHandlerTests.cs b/test/ServerHandlers/PingRequestHandlerTests.cs index d4501856398accbbb8e1ad400f113a2c86ac4d3b..d707d6908d5e494712782004375397550d346e86 100644 --- a/test/ServerHandlers/PingRequestHandlerTests.cs +++ b/test/ServerHandlers/PingRequestHandlerTests.cs @@ -12,15 +12,15 @@ public class PingRequestHandlerTests : TestSetup // Arrange var response = Substitute.For(); var stream = new MemoryStream(); - _ = response.OutputStream.Returns(stream); + _ = response.OutputStream.Returns(stream); var pingRequests = new PingRequests(); // Act var code = await pingRequests.Handle(response, "ping", todayDate).ConfigureAwait(true); - // Assert - _ = stream.Seek(0, SeekOrigin.Begin); + // Assert + _ = stream.Seek(0, SeekOrigin.Begin); using var reader = new StreamReader(stream); var content = await reader.ReadToEndAsync().ConfigureAwait(true); diff --git a/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs b/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs index 267153e71fd3ebf1fb26df6d266587b868fd15f5..a0125e89ca4455f886627751b2a1dcbf26ba6d3e 100644 --- a/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs +++ b/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs @@ -48,15 +48,15 @@ public class RegisteredPageRequestHandlerTests : TestSetup var response = Substitute.For(); var stream = new MemoryStream(); - _ = response.OutputStream.Returns(stream); + _ = response.OutputStream.Returns(stream); // Act site.ParseAndScanSourceFiles(Path.Combine(siteFullPath, "content")); - _ = registeredPageRequest.Check(requestPath); + _ = registeredPageRequest.Check(requestPath); var code = await registeredPageRequest.Handle(response, requestPath, DateTime.Now).ConfigureAwait(true); - // Assert - _ = stream.Seek(0, SeekOrigin.Begin); + // Assert + _ = stream.Seek(0, SeekOrigin.Begin); using var reader = new StreamReader(stream); var content = await reader.ReadToEndAsync().ConfigureAwait(true); diff --git a/test/ServerHandlers/StaticFileRequestHandlerTests.cs b/test/ServerHandlers/StaticFileRequestHandlerTests.cs index 92378e70d30be23aa990515ee0733e6dcaf01506..c196a92c6c0d5a4d387687a7206144909b9fcd30 100644 --- a/test/ServerHandlers/StaticFileRequestHandlerTests.cs +++ b/test/ServerHandlers/StaticFileRequestHandlerTests.cs @@ -44,14 +44,14 @@ public class StaticFileRequestHandlerTests : TestSetup, IDisposable var response = Substitute.For(); var stream = new MemoryStream(); - _ = response.OutputStream.Returns(stream); + _ = response.OutputStream.Returns(stream); - // Act - _ = staticFileRequest.Check(requestPath); + // Act + _ = staticFileRequest.Check(requestPath); var code = await staticFileRequest.Handle(response, requestPath, DateTime.Now).ConfigureAwait(true); - // Assert - _ = stream.Seek(0, SeekOrigin.Begin); + // Assert + _ = stream.Seek(0, SeekOrigin.Begin); using var reader = new StreamReader(stream); var content = await reader.ReadToEndAsync().ConfigureAwait(true); diff --git a/test/TestSetup.cs b/test/TestSetup.cs index ce5d9b9d837e0e315a4adfdadac91dd8d5821af1..c43fa1dfc1535e57d7c4500c18c6e485b70b3126 100644 --- a/test/TestSetup.cs +++ b/test/TestSetup.cs @@ -42,13 +42,13 @@ public class TestSetup public TestSetup() { - _ = systemClockMock.Now.Returns(todayDate); + _ = systemClockMock.Now.Returns(todayDate); site = new Site(generateOptionsMock, siteSettingsMock, frontMatterParser, loggerMock, systemClockMock); } public TestSetup(SiteSettings siteSettings) { - _ = systemClockMock.Now.Returns(todayDate); + _ = systemClockMock.Now.Returns(todayDate); site = new Site(generateOptionsMock, siteSettings, frontMatterParser, loggerMock, systemClockMock); } }