Repository navigation
[patch] Stop the merge-strategy cache letting one call decide a key for every later call #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright (c) 2023-2026 ktsu-dev contributors | ||
|
|
||
| namespace ktsu.Frontmatter.Test; | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for #130: the canonical name a key merges to depends on the strategy and on | ||
| /// the other keys in the same document, so one call must not decide it for a later one. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These tests deliberately clear nothing between calls. The bug lived in process-wide state, and a | ||
| /// test that resets that state cannot see it. | ||
| /// </remarks> | ||
| [TestClass] | ||
| public class MergeStrategyIsolationTests | ||
| { | ||
| [TestMethod] | ||
| public void ConservativeAfterAggressive_KeepsKeyThatOnlyAggressiveMerges() | ||
| { | ||
| static Dictionary<string, object> Document() => new() | ||
| { | ||
| ["title"] = "T", | ||
| ["custom_title"] = "C", | ||
| }; | ||
|
|
||
| Dictionary<string, object> aggressive = PropertyMerger.MergeSimilarProperties(Document(), FrontmatterMergeStrategy.Aggressive); | ||
| Assert.IsFalse(aggressive.ContainsKey("custom_title"), "Aggressive should merge custom_title into title"); | ||
|
|
||
| Dictionary<string, object> conservative = PropertyMerger.MergeSimilarProperties(Document(), FrontmatterMergeStrategy.Conservative); | ||
|
|
||
| Assert.AreEqual("T", conservative["title"]); | ||
| Assert.IsTrue(conservative.ContainsKey("custom_title"), "Conservative only applies predefined mappings, so custom_title must survive"); | ||
| Assert.AreEqual("C", conservative["custom_title"]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void AggressiveAfterConservative_StillMergesKeys() | ||
| { | ||
| static Dictionary<string, object> Document() => new() | ||
| { | ||
| ["summary"] = "S", | ||
| ["page_summary"] = "P", | ||
| }; | ||
|
|
||
| Dictionary<string, object> conservative = PropertyMerger.MergeSimilarProperties(Document(), FrontmatterMergeStrategy.Conservative); | ||
| Assert.HasCount(2, conservative); | ||
|
|
||
| Dictionary<string, object> aggressive = PropertyMerger.MergeSimilarProperties(Document(), FrontmatterMergeStrategy.Aggressive); | ||
|
|
||
| Assert.HasCount(1, aggressive); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void KeyAloneInALaterDocument_IsNotRenamedByAnEarlierDocument() | ||
| { | ||
| PropertyMerger.MergeSimilarProperties(new Dictionary<string, object> | ||
| { | ||
| ["blurb"] = "B", | ||
| ["page_blurb"] = "P", | ||
| }, FrontmatterMergeStrategy.Aggressive); | ||
|
|
||
| Dictionary<string, object> later = PropertyMerger.MergeSimilarProperties(new Dictionary<string, object> | ||
| { | ||
| ["page_blurb"] = "Only", | ||
| }, FrontmatterMergeStrategy.Aggressive); | ||
|
|
||
| Assert.IsTrue(later.ContainsKey("page_blurb"), "With no other key to merge with, page_blurb keeps its name"); | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
| Assert.AreEqual("Only", later["page_blurb"]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void CombineFrontmatter_ConservativeAfterAggressive_KeepsCustomTitle() | ||
| { | ||
| const string first = "---\ntitle: T\ncustom_title: C\n---\nBody1\n"; | ||
| const string second = "---\ntitle: T\ncustom_title: C\n---\nBody2\n"; | ||
|
|
||
| Frontmatter.CombineFrontmatter(first, FrontmatterNaming.AsIs, FrontmatterOrder.AsIs, FrontmatterMergeStrategy.Aggressive); | ||
| string result = Frontmatter.CombineFrontmatter(second, FrontmatterNaming.AsIs, FrontmatterOrder.AsIs, FrontmatterMergeStrategy.Conservative); | ||
|
|
||
| Dictionary<string, object>? frontmatter = Frontmatter.ExtractFrontmatter(result); | ||
| Assert.IsNotNull(frontmatter); | ||
| Assert.AreEqual("C", frontmatter["custom_title"]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.