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
39 changes: 39 additions & 0 deletions ImageDescriber.Test/ScanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,45 @@ public void SanitizeFileNameStripsUpperCaseImageExtension()
Assert.AreEqual("photo.png", result.WeakString);
}

[TestMethod]
[DataRow("sunset-over-the-lake.", "sunset-over-the-lake.jpg")]
[DataRow("Sunset over the lake.", "Sunset over the lake.jpg")]
[DataRow("sunset-over-the-lake. ", "sunset-over-the-lake.jpg")]
[DataRow("\"sunset-over-the-lake.\"", "sunset-over-the-lake.jpg")]
public void SanitizeFileNameDropsTrailingPeriod(string suggestion, string expected)
{
FileName result = Scan.SanitizeFileName(suggestion, ".jpg".As<FileExtension>());

Assert.AreEqual(expected, result.WeakString);
}

[TestMethod]
public void SanitizeFileNameStripsImageExtensionFollowedByPeriod()
{
FileName result = Scan.SanitizeFileName("photo.jpg.", ".png".As<FileExtension>());

Assert.AreEqual("photo.png", result.WeakString);
}

[TestMethod]
[DataRow("...")]
[DataRow(". . .")]
[DataRow("-.-")]
public void SanitizeFileNameReturnsUnnamedForOnlyPeriods(string suggestion)
{
FileName result = Scan.SanitizeFileName(suggestion, ".jpg".As<FileExtension>());

Assert.AreEqual("unnamed.jpg", result.WeakString);
}

[TestMethod]
public void SanitizeFileNameDropsPeriodLeftBeforeTrimmedHyphen()
{
FileName result = Scan.SanitizeFileName("sunset.-", ".jpg".As<FileExtension>());

Assert.AreEqual("sunset.jpg", result.WeakString);
}

[TestMethod]
public void HashFilesSkipsFilesThatCannotBeRead()
{
Expand Down
6 changes: 4 additions & 2 deletions ImageDescriber/Verbs/Scan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ internal static FileName SanitizeFileName(string rawSuggestion, FileExtension ex
.Split('\n')
.Select(line => line.Trim())
.FirstOrDefault(line => line.Length > 0 && !IsCodeFence(line)) ?? string.Empty;
name = name.Trim('"', '\'', '`').Trim();
// Models often end the answer with a full stop. Drop it before the extension check, so
// "photo.jpg." still loses its extension and "sunset." does not become "sunset..jpg"
name = name.Trim('"', '\'', '`').Trim().TrimEnd('.', ' ');

// Strip an image extension the model may have included. A suggestion normally has no
// extension, so any other dot is part of the name ("st.-louis-arch", "sunset-at-5.30pm")
Expand All @@ -283,7 +285,7 @@ internal static FileName SanitizeFileName(string rawSuggestion, FileExtension ex
name = name.Replace("--", "-", StringComparison.Ordinal);
}

name = name.Trim('-', ' ');
name = name.Trim('-', ' ', '.');

if (string.IsNullOrEmpty(name))
{
Expand Down
Loading