test: cover p-rec-annot line-number list planning
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using PRecAnnot.Abstractions;
|
||||||
|
|
||||||
|
namespace PRecAnnot.Core;
|
||||||
|
|
||||||
|
public static class LineNumberListPlanner
|
||||||
|
{
|
||||||
|
public static LineNumberListApplyPreflight Build(AnnotationPoint position, double rotation, IEnumerable<LineNumberApplyPreflight> rows)
|
||||||
|
{
|
||||||
|
var ordered = rows.OrderBy(row => row.MarkerNumber).ToArray();
|
||||||
|
if (ordered.Length == 0) throw new ArgumentException("LINE_NUMBER_LIST_EMPTY", nameof(rows));
|
||||||
|
if (ordered.Any(row => string.IsNullOrWhiteSpace(row.StableId) || string.IsNullOrWhiteSpace(row.Text))) throw new ArgumentException("LINE_NUMBER_LIST_ROW_INVALID", nameof(rows));
|
||||||
|
if (ordered.Select(row => row.MarkerNumber).Distinct().Count() != ordered.Length) throw new ArgumentException("LINE_NUMBER_LIST_DUPLICATE_MARKER_NUMBER", nameof(rows));
|
||||||
|
var key = string.Join("|", ordered.Select(row => $"{row.MarkerNumber}:{row.StableId}"));
|
||||||
|
var digest = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key))).ToLowerInvariant();
|
||||||
|
return new($"ln-list:{digest}", position, rotation, ordered);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using PRecAnnot.Abstractions;
|
||||||
|
using PRecAnnot.Core;
|
||||||
|
|
||||||
|
namespace PRecAnnot.Core.Tests;
|
||||||
|
|
||||||
|
public sealed class LineNumberListPlannerTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ProducesStableOwnershipAndMarkerOrder()
|
||||||
|
{
|
||||||
|
var second = new LineNumberApplyPreflight("b", 2, "L-2", new(1, 1), 0, "L", "S");
|
||||||
|
var first = new LineNumberApplyPreflight("a", 1, "L-1", new(0, 0), 0, "L", "S");
|
||||||
|
var result = LineNumberListPlanner.Build(new(5, 6), 1.5, [second, first]);
|
||||||
|
Assert.Equal(new[] { 1, 2 }, result.Rows.Select(row => row.MarkerNumber));
|
||||||
|
Assert.Equal(result.StableId, LineNumberListPlanner.Build(new(99, 0), 0, [first, second]).StableId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RejectsDuplicateMarkerNumbers()
|
||||||
|
{
|
||||||
|
var one = new LineNumberApplyPreflight("a", 1, "L-1", new(), 0, "L", "S");
|
||||||
|
var duplicate = one with { StableId = "b" };
|
||||||
|
Assert.Throws<ArgumentException>(() => LineNumberListPlanner.Build(new(), 0, [one, duplicate]));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user