From 72f299403037f35f3819acf702f067764ed9d1b3 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Mon, 5 Aug 2024 15:53:52 -0700 Subject: [PATCH] Test fonts --- NAPS2.Sdk.Tests/Asserts/PdfAsserts.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/NAPS2.Sdk.Tests/Asserts/PdfAsserts.cs b/NAPS2.Sdk.Tests/Asserts/PdfAsserts.cs index 17c5d67ce1..cd025df5a8 100644 --- a/NAPS2.Sdk.Tests/Asserts/PdfAsserts.cs +++ b/NAPS2.Sdk.Tests/Asserts/PdfAsserts.cs @@ -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; @@ -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; }