Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Frontmatter.Test/ListKeyNamingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.Frontmatter.Test;

using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Regression tests for #147: merging must not rename a list property, since renaming is the job of
/// <see cref="FrontmatterNaming.Standard"/>, and <see cref="FrontmatterNaming.AsIs"/> turns it off.
/// </summary>
[TestClass]
public class ListKeyNamingTests
{
private static readonly string Nl = Environment.NewLine;

[TestMethod]
[DataRow(FrontmatterMergeStrategy.None)]
[DataRow(FrontmatterMergeStrategy.Conservative)]
[DataRow(FrontmatterMergeStrategy.Aggressive)]
[DataRow(FrontmatterMergeStrategy.Maximum)]
public void CombineFrontmatter_AsIsNaming_LoneListPropertyKeepsItsKey(FrontmatterMergeStrategy strategy)
{
string input = $"---{Nl}section:{Nl} - news{Nl}summary: hi{Nl}---{Nl}Body{Nl}";

string result = Frontmatter.CombineFrontmatter(input, FrontmatterNaming.AsIs, FrontmatterOrder.AsIs, strategy);
Dictionary<string, object>? frontmatter = Frontmatter.ExtractFrontmatter(result);

Assert.IsNotNull(frontmatter);
Assert.IsTrue(frontmatter.ContainsKey("section"), $"section was renamed: {result}");
Assert.IsFalse(frontmatter.ContainsKey("categories"), $"section was renamed: {result}");
}

[TestMethod]
[DataRow("keywords")]
[DataRow("category")]
public void MergeSimilarProperties_LoneListProperty_KeepsItsKey(string key)
{
Dictionary<string, object> source = new()
{
[key] = new List<object> { "a", "b" },
};

Dictionary<string, object> result = PropertyMerger.MergeSimilarProperties(source, FrontmatterMergeStrategy.Conservative);

Assert.IsTrue(result.ContainsKey(key), $"{key} was renamed to {string.Join(", ", result.Keys)}");
Assert.AreEqual(1, result.Count);

Check warning on line 48 in Frontmatter.Test/ListKeyNamingTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Frontmatter&issues=AaDwKDZCwIxFzkbHVDXO&open=AaDwKDZCwIxFzkbHVDXO&pullRequest=173
}
}
5 changes: 3 additions & 2 deletions Frontmatter/PropertyMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

return !isKnownMapping && !hasExactMatch && !isInSameCategory
? key
: PropertyMappings.All.TryGetValue(canonicalName, out string? knownName) ? knownName : canonicalName;

Check warning on line 96 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 96 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 96 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 96 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 96 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 96 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.
}

private static bool IsInSameCategory(string key, string canonicalName)
Expand Down Expand Up @@ -144,8 +144,9 @@
return;
}

// Handle array/list types specially
if (firstValue is IList<object> || firstValue is object[])
// Handle array/list types specially. A lone list has nothing to merge with, so it keeps its own
// key like a scalar does: renaming is NameStandardizer's job, which FrontmatterNaming.AsIs turns off.
if (originalKeys.Count > 1 && (firstValue is IList<object> || firstValue is object[]))
{
MergeArrayValues(source, target, canonicalKey, originalKeys);
}
Expand Down Expand Up @@ -187,7 +188,7 @@
/// <param name="key">The key to analyze.</param>
/// <param name="existingKeys">All existing keys in the frontmatter.</param>
/// <returns>The canonical name for the key.</returns>
private static string FindBasicCanonicalName(string key, string[] existingKeys)

Check warning on line 191 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 191 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 191 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 191 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
{
// First check if it's a known property
if (PropertyMappings.All.TryGetValue(key, out string? canonicalName))
Expand Down
Loading