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
66 changes: 66 additions & 0 deletions Frontmatter.Test/StandardOrderCompareTotalOrderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.Frontmatter.Test;

using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Names that differ only in case must not compare as equal, or a sorted collection
/// built on <see cref="StandardOrder.Compare"/> throws or drops keys (#197).
/// </summary>
[TestClass]
public class StandardOrderCompareTotalOrderTests
{
private static readonly Comparer<string> Comparer = Comparer<string>.Create(StandardOrder.Compare);

[TestMethod]
[DataRow("Title", "title")]
[DataRow("MyKey", "mykey")]
[DataRow("TAGS", "Tags")]
public void Compare_CaseVariants_AreNotEqualAndAntisymmetric(string a, string b)
{
int forward = StandardOrder.Compare(a, b);
int backward = StandardOrder.Compare(b, a);

Assert.AreNotEqual(0, forward, $"'{a}' and '{b}' are different strings and must not compare equal");
Assert.AreEqual(-Math.Sign(forward), Math.Sign(backward), "Compare must be antisymmetric");
}

[TestMethod]
public void Compare_SameString_IsZero()
{
Assert.AreEqual(0, StandardOrder.Compare("Title", "Title"));
Assert.AreEqual(0, StandardOrder.Compare("mykey", "mykey"));
}

[TestMethod]
public void SortedDictionary_KeepsCaseVariantKeys()
{
SortedDictionary<string, object> dictionary = new(Comparer)
{
{ "title", "a" },
{ "Title", "b" },
};

Assert.HasCount(2, dictionary);
Assert.AreEqual("a", dictionary["title"]);
Assert.AreEqual("b", dictionary["Title"]);
}

[TestMethod]
public void SortedSet_KeepsCaseVariantKeys()
{
SortedSet<string> set = new(["tags", "Tags", "myKey", "MyKey"], Comparer);

Assert.HasCount(4, set);
}

[TestMethod]
public void Compare_OrderAcrossPositionsIsUnchanged()
{
Assert.IsLessThan(0, StandardOrder.Compare("Title", "author"), "Known keys keep their standard order regardless of case");
Assert.IsLessThan(0, StandardOrder.Compare("Tags", "zzz_unknown"), "Known keys come before unknown keys");
Assert.IsLessThan(0, StandardOrder.Compare("Alpha", "beta"), "Unknown keys still sort case-insensitively");
Assert.IsGreaterThan(0, StandardOrder.Compare("zzz_unknown", "TITLE"), "Unknown keys come after known keys");
}
}
12 changes: 11 additions & 1 deletion Frontmatter/StandardOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,13 @@ public static class StandardOrder
/// <param name="b">The second property name.</param>
/// <returns>
/// A negative value if 'a' should appear before 'b',
/// zero if they are equal in order,
/// zero only if 'a' and 'b' are the same string,
/// a positive value if 'a' should appear after 'b'.
/// </returns>
/// <remarks>
/// Names that differ only in case share a position, so they are ordered against each other ordinally.
/// That keeps the comparison a total order, so it can back a sorted collection without losing keys.
/// </remarks>
public static int Compare(string a, string b)
{
Ensure.NotNull(a);
Expand All @@ -216,6 +220,12 @@ public static int Compare(string a, string b)
return 0;
}

int result = CompareByPosition(a, b);
return result != 0 ? result : string.CompareOrdinal(a, b);
}

private static int CompareByPosition(string a, string b)
{
// Get the indices from the standard property list
int indexA = Array.IndexOf(PropertyNames, a.ToLowerInvariant());
int indexB = Array.IndexOf(PropertyNames, b.ToLowerInvariant());
Expand Down
Loading