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
44 changes: 44 additions & 0 deletions Frontmatter.Test/PropertyMergerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,48 @@
// Tags variants should be merged
Assert.IsTrue(extractedFrontmatter.ContainsKey("tags"), "Result should contain key 'tags'");
}

[TestMethod]
public void MergeSimilarProperties_SingleListWithRepeatedItems_KeepsEveryItem()
{
Dictionary<string, object> frontmatter = new()
{
{ "steps", new List<object> { "mix", "wait", "mix" } },
{ "scores", new List<object> { "1", "1", "2" } }
};

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

CollectionAssert.AreEqual(new object[] { "mix", "wait", "mix" }, (System.Collections.ICollection)result["steps"]);

Check warning on line 760 in Frontmatter.Test/PropertyMergerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Frontmatter&issues=AaDhyBRcz5PIa1mbhASV&open=AaDhyBRcz5PIa1mbhASV&pullRequest=144
CollectionAssert.AreEqual(new object[] { "1", "1", "2" }, (System.Collections.ICollection)result["scores"]);

Check warning on line 761 in Frontmatter.Test/PropertyMergerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Frontmatter&issues=AaDhyBRcz5PIa1mbhASW&open=AaDhyBRcz5PIa1mbhASW&pullRequest=144
}

[TestMethod]
public void MergeSimilarProperties_MergedListsRepeatWithinOneList_KeepsTheRepeatsAndDropsOnlyCrossListDuplicates()
{
Dictionary<string, object> frontmatter = new()
{
{ "tags", new List<object> { "a", "a", "c" } },
{ "tag", new List<object> { "b", "c", "b" } }
};

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

Assert.HasCount(1, result);
CollectionAssert.AreEqual(new object[] { "a", "a", "c", "b", "b" }, (System.Collections.ICollection)result["tags"]);

Check warning on line 776 in Frontmatter.Test/PropertyMergerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Frontmatter&issues=AaDhyBRcz5PIa1mbhASZ&open=AaDhyBRcz5PIa1mbhASZ&pullRequest=144
}

[TestMethod]
public void CombineFrontmatter_SingleBlockWithRepeatedListItems_RoundTripsTheLists()
{
string nl = Environment.NewLine;
string input = $"---{nl}steps: [mix, wait, mix]{nl}scores: [1, 1, 2]{nl}---{nl}Body{nl}";

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

Assert.IsNotNull(frontmatter);
CollectionAssert.AreEqual(new object[] { "mix", "wait", "mix" }, (System.Collections.ICollection)frontmatter["steps"]);

Check warning on line 789 in Frontmatter.Test/PropertyMergerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Frontmatter&issues=AaDhyBRcz5PIa1mbhASX&open=AaDhyBRcz5PIa1mbhASX&pullRequest=144
CollectionAssert.AreEqual(new object[] { "1", "1", "2" }, (System.Collections.ICollection)frontmatter["scores"]);

Check warning on line 790 in Frontmatter.Test/PropertyMergerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Frontmatter&issues=AaDhyBRcz5PIa1mbhASY&open=AaDhyBRcz5PIa1mbhASY&pullRequest=144
}
}
18 changes: 10 additions & 8 deletions Frontmatter/PropertyMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

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

Check warning on line 106 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 106 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 106 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 106 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 106 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 106 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.
}

private static bool IsInSameCategory(string key, string canonicalName)
Expand Down Expand Up @@ -172,18 +172,20 @@
string canonicalKey,
List<string> originalKeys)
{
HashSet<object> mergedList = [];
// Lists are appended in order. An item is dropped only when an earlier list being merged already
// supplied it, never because it repeats within its own list: a list such as recipe steps or
// scores can legitimately hold the same value more than once.
List<object> mergedList = [];
HashSet<object> fromEarlierLists = [];
foreach (string key in originalKeys)
{
object value = source[key];
if (value is IList<object> list)
if (source[key] is not IList<object> items)
{
mergedList.UnionWith(list);
}
else if (value is object[] array)
{
mergedList.UnionWith(array);
continue;
}

mergedList.AddRange(items.Where(item => !fromEarlierLists.Contains(item)));
fromEarlierLists.UnionWith(items);
}

target[canonicalKey] = mergedList.ToArray();
Expand All @@ -195,7 +197,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 200 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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

Check warning on line 200 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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

Check warning on line 200 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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

Check warning on line 200 in Frontmatter/PropertyMerger.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

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