From 94d64c3f6e84b16e77cf016ebf8fadfe46a3b5ff Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 14 Apr 2025 12:32:26 -0400 Subject: [PATCH] Add a couple of remaining Parser tests --- SabreTools.DatTools/Parser.cs | 6 ++-- SabreTools.Test/ParserTests.cs | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/SabreTools.DatTools/Parser.cs b/SabreTools.DatTools/Parser.cs index 4240c8dc..47ad68e8 100644 --- a/SabreTools.DatTools/Parser.cs +++ b/SabreTools.DatTools/Parser.cs @@ -532,7 +532,7 @@ namespace SabreTools.DatTools /// Format of the Statistics Report to be created /// List of statistics objects to set /// BaseReport of the specific internal type that corresponds to the inputs - public static BaseReport? CreateReport(StatReportFormat statReportFormat, List statsList) + public static BaseReport CreateReport(StatReportFormat statReportFormat, List statsList) { return statReportFormat switch { @@ -542,7 +542,9 @@ namespace SabreTools.DatTools StatReportFormat.HTML => new Reports.Formats.Html(statsList), StatReportFormat.SSV => new Reports.Formats.SemicolonSeparatedValue(statsList), StatReportFormat.TSV => new Reports.Formats.TabSeparatedValue(statsList), - _ => null, + + // We use console output as a backup for generic BaseReport + _ => new Reports.Formats.ConsoleOutput(statsList), }; } diff --git a/SabreTools.Test/ParserTests.cs b/SabreTools.Test/ParserTests.cs index 740ca120..c4aac729 100644 --- a/SabreTools.Test/ParserTests.cs +++ b/SabreTools.Test/ParserTests.cs @@ -2,6 +2,7 @@ using System; using System.IO; using SabreTools.DatFiles; using SabreTools.DatTools; +using SabreTools.Reports; using Xunit; namespace SabreTools.Test @@ -144,6 +145,53 @@ namespace SabreTools.Test Assert.Equal(0, datFile.ItemsDB.DatStatistics.TotalCount); } + [Theory] + [InlineData(null, (DatFormat)0x00, 0)] + [InlineData("test-logiqx.xml", DatFormat.Logiqx, 6)] + //[InlineData(null, DatFormat.LogiqxDeprecated, 0)] // Not parsed separately + [InlineData("test-softwarelist.xml", DatFormat.SoftwareList, 6)] + [InlineData("test-listxml.xml", DatFormat.Listxml, 19)] + [InlineData("test-offlinelist.xml", DatFormat.OfflineList, 1)] + //[InlineData(null, DatFormat.SabreXML, 0)] // TODO: Create good-enough test file for this + [InlineData("test-openmsx.xml", DatFormat.OpenMSX, 3)] + [InlineData("test-archivedotorg.xml", DatFormat.ArchiveDotOrg, 1)] + [InlineData("test-cmp.dat", DatFormat.ClrMamePro, 6)] + [InlineData("test-romcenter.dat", DatFormat.RomCenter, 1)] + [InlineData("test-doscenter.dat", DatFormat.DOSCenter, 1)] + [InlineData("test-attractmode.txt", DatFormat.AttractMode, 1)] + //[InlineData(null, DatFormat.MissFile, 0)] // Parsing is not supported + //[InlineData(null, DatFormat.CSV, 0)] // TODO: Create good-enough test file for this + //[InlineData(null, DatFormat.SSV, 0)] // TODO: Create good-enough test file for this + //[InlineData(null, DatFormat.TSV, 0)] // TODO: Create good-enough test file for this + [InlineData("test-listrom.txt", DatFormat.Listrom, 6)] + [InlineData("test-smdb.txt", DatFormat.EverdriveSMDB, 1)] + //[InlineData(null, DatFormat.SabreJSON, 0)] // TODO: Create good-enough test file for this + [InlineData("test-sfv.sfv", DatFormat.RedumpSFV, 1)] + [InlineData("test-md2.md2", DatFormat.RedumpMD2, 1)] + [InlineData("test-md4.md4", DatFormat.RedumpMD4, 1)] + [InlineData("test-md5.md5", DatFormat.RedumpMD5, 1)] + [InlineData("test-sha1.sha1", DatFormat.RedumpSHA1, 1)] + [InlineData("test-sha256.sha256", DatFormat.RedumpSHA256, 1)] + [InlineData("test-sha384.sha384", DatFormat.RedumpSHA384, 1)] + [InlineData("test-sha512.sha512", DatFormat.RedumpSHA512, 1)] + [InlineData("test-spamsum.spamsum", DatFormat.RedumpSpamSum, 1)] + public void ParseIntoTest(string? filename, DatFormat datFormat, int totalCount) + { + // For all filenames, add the local path for test data + if (filename != null) + filename = Path.Combine(Environment.CurrentDirectory, "TestData", filename); + else + filename = string.Empty; + + var datFile = Parser.CreateDatFile(); + datFile.Header.RemoveField(DatHeader.DatFormatKey); + + Parser.ParseInto(datFile, filename, throwOnError: true); + Assert.Equal(datFormat, datFile.Header.GetFieldValue(DatHeader.DatFormatKey)); + Assert.Equal(totalCount, datFile.Items.DatStatistics.TotalCount); + Assert.Equal(totalCount, datFile.ItemsDB.DatStatistics.TotalCount); + } + [Theory] [InlineData(null, (DatFormat)0x00, 0)] [InlineData("test-logiqx.xml", DatFormat.Logiqx, 6)] @@ -185,5 +233,19 @@ namespace SabreTools.Test Assert.Equal(totalCount, datFile.Items.DatStatistics.TotalCount); Assert.Equal(totalCount, datFile.ItemsDB.DatStatistics.TotalCount); } + + [Theory] + [InlineData(StatReportFormat.None, typeof(Reports.Formats.ConsoleOutput))] + [InlineData(StatReportFormat.Textfile, typeof(Reports.Formats.Textfile))] + [InlineData(StatReportFormat.CSV, typeof(Reports.Formats.CommaSeparatedValue))] + [InlineData(StatReportFormat.HTML, typeof(Reports.Formats.Html))] + [InlineData(StatReportFormat.SSV, typeof(Reports.Formats.SemicolonSeparatedValue))] + [InlineData(StatReportFormat.TSV, typeof(Reports.Formats.TabSeparatedValue))] + [InlineData((StatReportFormat)0xFF, typeof(Reports.Formats.ConsoleOutput))] + public void CreateReportTest(StatReportFormat reportFormat, Type expected) + { + var report = Parser.CreateReport(reportFormat, []); + Assert.Equal(expected, report.GetType()); + } } } \ No newline at end of file