feat(core): naming templates with tokens, formats and sanitizing
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+346
No files matched your search
@@ -0,0 +1,89 @@
|
||||
using Titano.Core.Naming;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Naming;
|
||||
|
||||
public sealed class PathTemplateTests
|
||||
{
|
||||
private static readonly NamingContext Frame = new()
|
||||
{
|
||||
CapturedAt = new DateTime(2026, 9, 28, 18, 2, 11, 250),
|
||||
SessionStart = new DateTime(2026, 9, 28, 18, 2, 11),
|
||||
Session = 2,
|
||||
Number = 1,
|
||||
NumberInSession = 1,
|
||||
CameraMake = "Canon",
|
||||
CameraModel = "Canon EOS R5",
|
||||
Lens = "RF24-70mm F2.8 L IS USM",
|
||||
Iso = 400,
|
||||
ExposureTime = 1.0 / 8,
|
||||
FNumber = 8,
|
||||
OriginalName = "IMG_0001",
|
||||
Extension = "CR2",
|
||||
Title = "Harbour sunset",
|
||||
};
|
||||
|
||||
[Test]
|
||||
public void DefaultTemplatesMatchTheBriefExample()
|
||||
{
|
||||
Assert.Equal("2026/2026-09-28 Canon EOS R5", PathTemplate.Parse(TemplateDefaults.Folder).ExpandFolder(Frame));
|
||||
Assert.Equal("2026-09-28_18-02-11_0001.CR2", PathTemplate.Parse(TemplateDefaults.File).ExpandFile(Frame));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case("{n:000000}", "000001.CR2")]
|
||||
[Case("{date:yyyyMMdd}_{name}", "20260928_IMG_0001.CR2")]
|
||||
[Case("{iso}_{shutter}_{aperture}", "400_1-8_f8.CR2")]
|
||||
[Case("{name}.{ext}", "IMG_0001.CR2")]
|
||||
[Case("{{literal}}_{n}", "{literal}_0001.CR2")]
|
||||
[Case("{session}-{nsession:00}", "2-01.CR2")]
|
||||
public void FileTokens(string template, string expected) =>
|
||||
Assert.Equal(expected, PathTemplate.Parse(template).ExpandFile(Frame));
|
||||
|
||||
[Test]
|
||||
public void IllegalCharactersAreReplaced()
|
||||
{
|
||||
var context = Frame with { Title = "a:b*c?<d>|e\"f" };
|
||||
Assert.Equal("a_b_c__d__e_f.CR2", PathTemplate.Parse("{title}").ExpandFile(context));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TokenValuesNeverCreateFolders()
|
||||
{
|
||||
var context = Frame with { Lens = "18/55 mm" };
|
||||
Assert.Equal("2026/18-55 mm", PathTemplate.Parse("{year}/{lens}").ExpandFolder(context));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReservedWindowsNamesAreEscaped() =>
|
||||
Assert.Equal("_CON.CR2", PathTemplate.Parse("{title}").ExpandFile(Frame with { Title = "CON" }));
|
||||
|
||||
[Test]
|
||||
public void ErrorsCarryPositionAndToken()
|
||||
{
|
||||
var t = PathTemplate.Parse("{date}_{bogus}_{n");
|
||||
Assert.False(t.IsValid);
|
||||
Assert.Contains(new TemplateError(7, "UnknownToken", "bogus"), t.Errors);
|
||||
Assert.Contains(t.Errors, e => e.Code == "UnclosedBrace");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmptyExpansionFallsBackToOriginalName() =>
|
||||
Assert.Equal("IMG_0001.CR2", PathTemplate.Parse("{title}").ExpandFile(Frame with { Title = null }));
|
||||
|
||||
[Theory]
|
||||
[Case("NIKON CORPORATION", "NIKON Z 8", "Nikon Z 8")]
|
||||
[Case("SONY", "ILCE-7M4", "SONY ILCE-7M4")]
|
||||
[Case("Canon", "Canon EOS R5", "Canon EOS R5")]
|
||||
[Case(null, "HERO12 Black", "HERO12 Black")]
|
||||
public void CameraNameAvoidsDuplicatedBrand(string? make, string model, string expected) =>
|
||||
Assert.Equal(expected, PathTemplate.CameraName(make, model));
|
||||
|
||||
[Test]
|
||||
public void ShutterFormatting()
|
||||
{
|
||||
Assert.Equal("1-250", PathTemplate.Shutter(0.004));
|
||||
Assert.Equal("2s", PathTemplate.Shutter(2));
|
||||
Assert.Equal(string.Empty, PathTemplate.Shutter(null));
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,17 @@ public static class Assert
|
||||
if (!collection.Contains(expected)) Fail($"{expression}: does not contain {Format(expected)}");
|
||||
}
|
||||
|
||||
/// <summary>At least one element satisfies <paramref name="predicate"/>.</summary>
|
||||
public static T Contains<T>(IEnumerable<T> collection, Func<T, bool> predicate, [CallerArgumentExpression(nameof(predicate))] string? expression = null)
|
||||
{
|
||||
foreach (var item in collection)
|
||||
{
|
||||
if (predicate(item)) return item;
|
||||
}
|
||||
Fail($"No element matches {expression}");
|
||||
return default!;
|
||||
}
|
||||
|
||||
public static void DoesNotContain<T>(T unexpected, IEnumerable<T> collection, [CallerArgumentExpression(nameof(collection))] string? expression = null)
|
||||
{
|
||||
if (collection.Contains(unexpected)) Fail($"{expression}: unexpectedly contains {Format(unexpected)}");
|
||||
|
||||
Reference in new issue
Block a user