Skip to content

Commit

Permalink
Test fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 5, 2024
1 parent 869a09a commit 52ccdf3
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions NAPS2.Sdk.Tests/Asserts/PdfAsserts.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Text.RegularExpressions;
using System.Text;
using System.Text.RegularExpressions;
using Codeuctivity;
using NAPS2.Pdf;
using NAPS2.Pdf.Pdfium;
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Pdf.Security;
using PdfSharpCore.Utils;
using Xunit;
using Xunit.Sdk;

Expand Down Expand Up @@ -42,37 +44,42 @@ public static async Task AssertCompliant(string profile, string filePath)

public static void AssertContainsTextOnce(string text, string filePath)
{
var value = CountText(text, filePath);
var value = CountText(text, filePath, out var actualText);
if (value != 1)
{
throw new XunitException($"Unexpected count for \"{text}\": expected {1}, got {value}");
throw new XunitException(
$"Unexpected count for \"{text}\": expected {1}, got {value}; installed fonts {string.Join(",", FontResolver.InstalledFonts.Where(x => x.Key.ContainsInvariantIgnoreCase("Sinhala")).OrderBy(x => x.Key).Select(x => x.Key))}");
}
}

public static void AssertDoesNotContainText(string text, string filePath)
{
var value = CountText(text, filePath);
var value = CountText(text, filePath, out var actualText);
if (value != 0)
{
throw new XunitException($"Unexpected count for \"{text}\": expected {0}, got {value}");
throw new XunitException(
$"Unexpected count for \"{text}\": expected {0}, got {value}; actual text {actualText}");
}
}

private static int CountText(string text, string filePath)
private static int CountText(string text, string filePath, out string actualText)
{
Assert.True(File.Exists(filePath));
int count = 0;
var fullText = new StringBuilder();
foreach (var pageText in new PdfiumPdfReader().ReadTextByPage(filePath))
{
int startIndex = 0;
int index;
var normalized = Regex.Replace(pageText, "\\s+", " ");
fullText.Append(normalized);
while ((index = normalized.IndexOf(text, startIndex, StringComparison.InvariantCulture)) != -1)
{
count++;
startIndex = index + 1;
}
}
actualText = fullText.ToString();
return count;
}

Expand Down

0 comments on commit 52ccdf3

Please sign in to comment.