From 38db9538cc4a8fdfdab5f4932aec3e56b5b77b90 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 9 Sep 2024 02:01:05 +0100 Subject: [PATCH 1/2] chore: refactored tests/neatened --- RTWLibPlus/dataWrappers/baseWrapper.cs | 18 +-- RTWLibPlus/helpers/FileHelper.cs | 13 +++ RTWLibPlus/parsers/DepthParse.cs | 2 +- RTWLibPlus/parsers/objects/creator.cs | 23 +++- RTWLib_Tests/TestHelper.cs | 45 ++----- RTWLib_Tests/map/Tests_citymap.cs | 4 +- RTWLib_Tests/randomised/Tests_RandDS.cs | 19 ++- RTWLib_Tests/wrappers/Tests_SMF.cs | 35 ++---- RTWLib_Tests/wrappers/Tests_dmb.cs | 36 +----- RTWLib_Tests/wrappers/Tests_dr.cs | 23 +--- RTWLib_Tests/wrappers/Tests_ds.cs | 148 ++++++++---------------- RTWLib_Tests/wrappers/Tests_edb.cs | 64 +++------- RTWLib_Tests/wrappers/Tests_edu.cs | 56 ++++----- 13 files changed, 167 insertions(+), 319 deletions(-) diff --git a/RTWLibPlus/dataWrappers/baseWrapper.cs b/RTWLibPlus/dataWrappers/baseWrapper.cs index 4490cb6..ae0d69c 100644 --- a/RTWLibPlus/dataWrappers/baseWrapper.cs +++ b/RTWLibPlus/dataWrappers/baseWrapper.cs @@ -1,4 +1,6 @@ namespace RTWLibPlus.dataWrappers; + +using RTWLibPlus.data; using RTWLibPlus.interfaces; using RTWLibPlus.parsers.objects; using System; @@ -7,23 +9,9 @@ public abstract class BaseWrapper { - + public delegate BaseWrapper WrapperCreator(List data, TWConfig config); public string OutputPath { get; set; } - // { - // get - // { - // string path = ExString.CrossPlatPath(this.outputPath); - // string dir = Path.GetDirectoryName(path); - // if (Directory.Exists(dir)) - // { return path; } - - // return path.Split('/').Last(); - // } - - // set => this.outputPath = value; - // } public string LoadPath { get; set; } - public List Data { get; set; } = []; /// /// Location is a collection of strings that represent the tags. Once the final string is found it will return the corresponding kv diff --git a/RTWLibPlus/helpers/FileHelper.cs b/RTWLibPlus/helpers/FileHelper.cs index edb2b1e..06aea1c 100644 --- a/RTWLibPlus/helpers/FileHelper.cs +++ b/RTWLibPlus/helpers/FileHelper.cs @@ -1,8 +1,12 @@ namespace RTWLibPlus.helpers; + +using RTWLibPlus.data; +using RTWLibPlus.dataWrappers; using RTWLibPlus.interfaces; using RTWLibPlus.parsers; using System.Collections.Generic; using System.IO; +using static RTWLibPlus.dataWrappers.BaseWrapper; using static RTWLibPlus.parsers.DepthParse; public static class RFH @@ -44,6 +48,15 @@ public static List ParseFile(ObjectCreator creator, char splitter = ' return parsed; } + public static BaseWrapper CreateWrapper( + ObjectCreator creator, WrapperCreator wrapper, TWConfig config, + char splitter = ' ', bool removeEmptyLines = false, params string[] path) + { + List parsed = ParseFile(creator, splitter, removeEmptyLines, path); + return wrapper(parsed, config); + + } + public static string GetPartOfPath(string path, string from) { if (path == null) diff --git a/RTWLibPlus/parsers/DepthParse.cs b/RTWLibPlus/parsers/DepthParse.cs index cd480b1..ab9382e 100644 --- a/RTWLibPlus/parsers/DepthParse.cs +++ b/RTWLibPlus/parsers/DepthParse.cs @@ -10,8 +10,8 @@ public class DepthParse { private readonly List list = []; - public delegate IBaseObj ObjectCreator(string value, string tag, int depth); + public List Parse(string[] lines, ObjectCreator creator, char splitter = ' ') { this.list.Clear(); diff --git a/RTWLibPlus/parsers/objects/creator.cs b/RTWLibPlus/parsers/objects/creator.cs index d1a97f6..b247130 100644 --- a/RTWLibPlus/parsers/objects/creator.cs +++ b/RTWLibPlus/parsers/objects/creator.cs @@ -1,4 +1,9 @@ namespace RTWLibPlus.parsers.objects; + +using RTWLibPlus.data; +using RTWLibPlus.dataWrappers; +using RTWLibPlus.helpers; +using static RTWLibPlus.dataWrappers.BaseWrapper; using static RTWLibPlus.parsers.DepthParse; public static class Creator { @@ -7,6 +12,22 @@ public static class Creator public static readonly ObjectCreator DScreator = (value, tag, depth) => new DSObj(tag, value, depth); public static readonly ObjectCreator EDBcreator = (value, tag, depth) => new EDBObj(tag, value, depth); public static readonly ObjectCreator SMFcreator = (value, tag, depth) => new SMFObj(tag, value, depth); - public static readonly ObjectCreator DMBcreator = (value, tag, depth) => new DMBObj(tag, value, depth); + public static readonly WrapperCreator DSWrapper = (data, config) => new DS(data, config); + public static readonly WrapperCreator DRWrapper = (data, config) => new DR(data, config); + public static readonly WrapperCreator DMBWrapper = (data, config) => new DMB(data, config); + public static readonly WrapperCreator SMFWrapper = (data, config) => new SMF(data, config); + public static readonly WrapperCreator EDBWrapper = (data, config) => new EDB(data, config); + public static readonly WrapperCreator EDUWrapper = (data, config) => new EDU(data, config); +} + +public static class Instance +{ + public static DS InstanceDS(TWConfig config, params string[] path) => (DS)RFH.CreateWrapper(Creator.DScreator, Creator.DSWrapper, config, ' ', false, path); + public static DR InstanceDR(TWConfig config, params string[] path) => (DR)RFH.CreateWrapper(Creator.DRcreator, Creator.DRWrapper, config, '\t', false, path); + public static DMB InstanceDMB(TWConfig config, params string[] path) => (DMB)RFH.CreateWrapper(Creator.DMBcreator, Creator.DMBWrapper, config, ' ', false, path); + public static SMF InstanceSMF(TWConfig config, params string[] path) => (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, config, ':', false, path); + public static EDB InstanceEDB(TWConfig config, params string[] path) => (EDB)RFH.CreateWrapper(Creator.EDBcreator, Creator.EDBWrapper, config, ' ', false, path); + public static EDU InstanceEDU(TWConfig config, params string[] path) => (EDU)RFH.CreateWrapper(Creator.EDUcreator, Creator.EDUWrapper, config, ' ', false, path); + } diff --git a/RTWLib_Tests/TestHelper.cs b/RTWLib_Tests/TestHelper.cs index ba782f9..0cb444a 100644 --- a/RTWLib_Tests/TestHelper.cs +++ b/RTWLib_Tests/TestHelper.cs @@ -1,43 +1,14 @@ namespace RTWLib_Tests; -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Collections.Generic; -using System.Linq; -using RTWLibPlus.interfaces; -using System.Reflection; +using RTWLibPlus.data; public static class TestHelper { - public static string[] GetValues(this KeyValuePair dict) => dict.Value; - - public static void LoopCollectionAssert(Dictionary expected, Dictionary result) - { - List lstKeysExpected = expected.Keys.ToList(); - List lstKeysResult = result.Keys.ToList(); - List lstValuesExpected = expected.Values.ToList(); - List lstValuesResult = result.Values.ToList(); - - for (int i = 0; i < expected.Count; i++) - { - CollectionAssert.AreEqual(lstValuesExpected[i], lstValuesResult[i]); - } - - CollectionAssert.AreEqual(lstKeysExpected.ToArray(), lstKeysResult.ToArray()); - } - - public static void LoopListAssert(List expected, List result) - { - Type t = result[0].GetType(); - - for (int i = 0; i < result.Count; i++) - { - foreach (PropertyInfo property in t.GetProperties()) - { - object expVal = property.GetValue(expected[i]); - object resVal = property.GetValue(result[i]); - Assert.AreEqual(property.GetValue(expected[i]), property.GetValue(result[i])); - } - } - } + public static readonly TWConfig Config = TWConfig.LoadConfig(@"resources/remaster.json"); + public static string[] EDB => ["resources", "export_descr_buildings.txt"]; + public static string[] EDU => ["resources", "export_descr_unit.txt"]; + public static string[] SMF => ["resources", "descr_sm_factions.txt"]; + public static string[] DS => ["resources", "descr_strat.txt"]; + public static string[] DMB => ["resources", "descr_model_battle.txt"]; + public static string[] DR => ["resources", "descr_regions.txt"]; } diff --git a/RTWLib_Tests/map/Tests_citymap.cs b/RTWLib_Tests/map/Tests_citymap.cs index f6c604f..0d4a95d 100644 --- a/RTWLib_Tests/map/Tests_citymap.cs +++ b/RTWLib_Tests/map/Tests_citymap.cs @@ -20,15 +20,13 @@ public void CityCoordinatesFetchedCorrectly() TGA image = new("tgafile", RFH.CurrDirPath("resources", "map_regions.tga"), ""); string[] drread = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_regions.txt"), false); - System.Collections.Generic.List drparse = this.dp.Parse(drread, Creator.DRcreator, '\t'); - DR dr = new(drparse, this.config); + DR dr = (DR)RFH.CreateWrapper(Creator.DRcreator, Creator.DRWrapper, this.config, '\t', false, "resources", "descr_regions.txt"); CityMap cm = new(image, dr); Assert.IsTrue(cm.CityCoordinates.ContainsKey("Thebais")); Assert.IsTrue(cm.CityCoordinates.ContainsKey("Celtiberia")); Assert.IsTrue(cm.CityCoordinates.ContainsKey("Britannia_Inferior")); - } diff --git a/RTWLib_Tests/randomised/Tests_RandDS.cs b/RTWLib_Tests/randomised/Tests_RandDS.cs index 1fbc9de..a9e503d 100644 --- a/RTWLib_Tests/randomised/Tests_RandDS.cs +++ b/RTWLib_Tests/randomised/Tests_RandDS.cs @@ -19,13 +19,11 @@ public class Tests_RandDS [TestMethod] public void UnitsAreRecruitable() { - List dsParse = RFH.ParseFile(Creator.DScreator, ' ', false, "resources", "descr_strat.txt"); - DS ds = new(dsParse, this.config); - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU edu = new(parse, this.config); + DS ds = (DS)RFH.CreateWrapper(Creator.DScreator, Creator.DSWrapper, this.config, ' ', false, "resources", "descr_strat.txt"); + EDU edu = (EDU)RFH.CreateWrapper(Creator.EDUcreator, Creator.EDUWrapper, this.config, ' ', false, "resources", "export_descr_unit.txt"); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, this.config, ':', false, "resources", "descr_sm_factions.txt"); edu.PrepareEDU(); - List smfParse = RFH.ParseFile(Creator.SMFcreator, ':', false, "resources", "descr_sm_factions.txt"); - SMF smf = new(smfParse, this.config); + List beforeUnits = edu.GetUnitsFromFaction("romans_julii", []); RandEDU.RandomiseOwnership(edu, this.rand, smf); RandDS.SwitchUnitsToRecruitable(edu, ds, this.rand); @@ -44,13 +42,10 @@ public void UnitsAreRecruitable() [TestMethod] public void RelationsAreRandom() { - List dsParse = RFH.ParseFile(Creator.DScreator, ' ', false, "resources", "descr_strat.txt"); - DS ds = new(dsParse, this.config); - List smfParse = RFH.ParseFile(Creator.SMFcreator, ':', false, "resources", "descr_sm_factions.txt"); - SMF smf = new(smfParse, this.config); + DS ds = (DS)RFH.CreateWrapper(Creator.DScreator, Creator.DSWrapper, this.config, ' ', false, "resources", "descr_strat.txt"); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, this.config, ':', false, "resources", "descr_sm_factions.txt"); TGA image = new("tgafile", RFH.CurrDirPath("resources", "map_regions.tga"), ""); - List drparse = RFH.ParseFile(Creator.DRcreator, '\t', false, "resources", "descr_regions.txt"); - DR dr = new(drparse, this.config); + DR dr = (DR)RFH.CreateWrapper(Creator.DRcreator, Creator.DRWrapper, this.config, '\t', false, "resources", "descr_regions.txt"); CityMap cm = new(image, dr); List before = ds.GetItemsByIdent("core_attitudes").DeepCopy(); diff --git a/RTWLib_Tests/wrappers/Tests_SMF.cs b/RTWLib_Tests/wrappers/Tests_SMF.cs index a10e750..8f9a9df 100644 --- a/RTWLib_Tests/wrappers/Tests_SMF.cs +++ b/RTWLib_Tests/wrappers/Tests_SMF.cs @@ -1,26 +1,20 @@ namespace RTWLib_Tests.wrappers; using Microsoft.VisualStudio.TestTools.UnitTesting; -using RTWLibPlus.data; using RTWLibPlus.dataWrappers; using RTWLibPlus.helpers; -using RTWLibPlus.parsers; using RTWLibPlus.parsers.objects; using System.Collections.Generic; [TestClass] public class Tests_smf { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); [TestMethod] public void SMFCheckDataIsParsedDepth1() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_sm_factions.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.SMFcreator, ':'); - SMF parsedsmf = new(smfParse, this.config); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "romans_julii", "culture"); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, TestHelper.Config, ':', false, TestHelper.SMF); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(smf.Data, 0, "romans_julii", "culture"); KeyValuePair expected = new("culture", "roman"); Assert.AreEqual(expected, result); } @@ -28,20 +22,16 @@ public void SMFCheckDataIsParsedDepth1() [TestMethod] public void SMFCheckDataIsParsedDepth2() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_sm_factions.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.SMFcreator, ':'); - SMF parsedsmf = new(smfParse, this.config); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "germans", "colours", "primary"); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, TestHelper.Config, ':', false, TestHelper.SMF); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(smf.Data, 0, "germans", "colours", "primary"); KeyValuePair expected = new("primary", "[88, 21, 38, ]"); Assert.AreEqual(expected, result); } [TestMethod] public void SMFCheckDataIsParsedDepth3() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_sm_factions.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.SMFcreator, ':'); - SMF parsedsmf = new(smfParse, this.config); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "britons", "colours", "family tree", "selected line"); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, TestHelper.Config, ':', false, TestHelper.SMF); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(smf.Data, 0, "britons", "colours", "family tree", "selected line"); KeyValuePair expected = new("selected line", "[255, 255, 255, ]"); Assert.AreEqual(expected, result); } @@ -49,11 +39,8 @@ public void SMFCheckDataIsParsedDepth3() [TestMethod] public void SMFCheckDataIsParsedDepth4() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_sm_factions.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.SMFcreator, ':'); - SMF parsedsmf = new(smfParse, this.config); - - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "thrace", "prefer naval invasions"); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, TestHelper.Config, ':', false, TestHelper.SMF); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(smf.Data, 0, "thrace", "prefer naval invasions"); KeyValuePair expected = new("prefer naval invasions", "false"); Assert.AreEqual(expected, result); @@ -62,11 +49,9 @@ public void SMFCheckDataIsParsedDepth4() [TestMethod] public void SMFGetFactions() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_sm_factions.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.SMFcreator, ':'); - SMF parsedsmf = new(smfParse, this.config); + SMF smf = (SMF)RFH.CreateWrapper(Creator.SMFcreator, Creator.SMFWrapper, TestHelper.Config, ':', false, TestHelper.SMF); - int result = parsedsmf.GetFactions().Count; + int result = smf.GetFactions().Count; int expected = 21; Assert.AreEqual(expected, result); diff --git a/RTWLib_Tests/wrappers/Tests_dmb.cs b/RTWLib_Tests/wrappers/Tests_dmb.cs index ae517e2..e716411 100644 --- a/RTWLib_Tests/wrappers/Tests_dmb.cs +++ b/RTWLib_Tests/wrappers/Tests_dmb.cs @@ -1,48 +1,24 @@ namespace RTWLib_Tests.wrappers; using Microsoft.VisualStudio.TestTools.UnitTesting; -using RTWLibPlus.parsers; -using RTWLibPlus.data; -using RTWLibPlus.helpers; using RTWLibPlus.parsers.objects; using RTWLibPlus.dataWrappers; [TestClass] public class Tests_dmb { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); - - // [TestMethod] - // public void ReadDMB() - // { - // string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_model_battle.txt"), false); - // System.Collections.Generic.List smfParse = this.dp.Parse(smf, Creator.DMBcreator, ' '); - // DMB dmb = new(smfParse, this.config); - - // // measure by line not characters. DMB is too annoying to get the exact same formatting on - // string[] result = dmb.Output().Split('\n'); - // string[] expected = this.dp.ReadFileAsString(RFH.CurrDirPath("resources", "descr_model_battle.txt")).Split('\n'); - // string elast = expected[^3]; - // string eresult = result[^3]; - // Assert.AreEqual(expected[^3], result[^3]); - // } - [TestMethod] public void FallbackForAllTextures() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_model_battle.txt"), false); - System.Collections.Generic.List smfParse = this.dp.Parse(smf, Creator.DMBcreator, ' '); - DMB dmb = new(smfParse, this.config); - int textureCountOrig = dmb.GetItemsByIdent("texture").Count; - int typeCountOrig = dmb.GetItemsByIdent("type").Count; - dmb.AddFallBacksForAllTypes(); - int textureCountNew = dmb.GetItemsByIdent("texture").Count; + DMB descr_model_battle = Instance.InstanceDMB(TestHelper.Config, TestHelper.DMB); + int textureCountOrig = descr_model_battle.GetItemsByIdent("texture").Count; + int typeCountOrig = descr_model_battle.GetItemsByIdent("type").Count; + descr_model_battle.AddFallBacksForAllTypes(); + int textureCountNew = descr_model_battle.GetItemsByIdent("texture").Count; int diff = textureCountNew - textureCountOrig; - RFH.Write("test_dmb_result.txt", dmb.Output()); - Assert.AreEqual(typeCountOrig - dmb.NoDefaultNeeded, diff, 1); + Assert.AreEqual(typeCountOrig - descr_model_battle.NoDefaultNeeded, diff, 1); } diff --git a/RTWLib_Tests/wrappers/Tests_dr.cs b/RTWLib_Tests/wrappers/Tests_dr.cs index 7706485..c8f8983 100644 --- a/RTWLib_Tests/wrappers/Tests_dr.cs +++ b/RTWLib_Tests/wrappers/Tests_dr.cs @@ -2,23 +2,16 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using RTWLibPlus.dataWrappers; -using RTWLibPlus.helpers; using RTWLibPlus.parsers.objects; -using RTWLibPlus.parsers; -using RTWLibPlus.data; [TestClass] public class Tests_dr { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); [TestMethod] public void DRGetRegionDataLocusGepidae() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_regions.txt"), false); - System.Collections.Generic.List smfParse = this.dp.Parse(smf, Creator.DRcreator, '\t'); - DR parsedsmf = new(smfParse, this.config); - System.Collections.Generic.List result = parsedsmf.GetNumberOfItems(8, "Locus_Gepidae"); + DR descr_regions = Instance.InstanceDR(TestHelper.Config, TestHelper.DR); + System.Collections.Generic.List result = descr_regions.GetNumberOfItems(8, "Locus_Gepidae"); int expected = 8; Assert.AreEqual("Locus_Gepidae", ((BaseObj)result[0]).Value); Assert.AreEqual(expected, result.Count); @@ -27,10 +20,8 @@ public void DRGetRegionDataLocusGepidae() [TestMethod] public void DRGetRegionDataHibernia() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_regions.txt"), false); - System.Collections.Generic.List smfParse = this.dp.Parse(smf, Creator.DRcreator, '\t'); - DR parsedsmf = new(smfParse, this.config); - System.Collections.Generic.List result = parsedsmf.GetNumberOfItems(8, "Hibernia"); + DR descr_regions = Instance.InstanceDR(TestHelper.Config, TestHelper.DR); + System.Collections.Generic.List result = descr_regions.GetNumberOfItems(8, "Hibernia"); int expected = 8; Assert.AreEqual("Hibernia", ((BaseObj)result[0]).Value); Assert.AreEqual(expected, result.Count); @@ -38,10 +29,8 @@ public void DRGetRegionDataHibernia() [TestMethod] public void DRGetRegionDataThebais() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_regions.txt"), false); - System.Collections.Generic.List smfParse = this.dp.Parse(smf, Creator.DRcreator, '\t'); - DR parsedsmf = new(smfParse, this.config); - System.Collections.Generic.List result = parsedsmf.GetNumberOfItems(8, "Thebais"); + DR descr_regions = Instance.InstanceDR(TestHelper.Config, TestHelper.DR); + System.Collections.Generic.List result = descr_regions.GetNumberOfItems(8, "Thebais"); int expected = 8; Assert.AreEqual("Thebais", ((BaseObj)result[0]).Value); Assert.AreEqual(expected, result.Count); diff --git a/RTWLib_Tests/wrappers/Tests_ds.cs b/RTWLib_Tests/wrappers/Tests_ds.cs index de78f86..02856ce 100644 --- a/RTWLib_Tests/wrappers/Tests_ds.cs +++ b/RTWLib_Tests/wrappers/Tests_ds.cs @@ -1,7 +1,6 @@ namespace RTWLib_Tests.wrappers; using Microsoft.VisualStudio.TestTools.UnitTesting; -using RTWLibPlus.data; using RTWLibPlus.dataWrappers; using RTWLibPlus.dataWrappers.TGA; using RTWLibPlus.helpers; @@ -14,23 +13,16 @@ using System.Linq; using System.Numerics; + [TestClass] public class Tests_ds { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); [TestMethod] public void DsWholeFile() { - - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - string result = parsedds.Output(); - - string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath("resources", "descr_strat.txt")); - + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + string result = descr_strat.Output(); + string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath(TestHelper.DS)); int rl = result.Length; //123502 int el = expected.Length; //127957 @@ -41,23 +33,17 @@ public void DsWholeFile() [TestMethod] public void DsGetItemsByIdentSettlements() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - List result = parsedds.GetItemsByIdent("settlement"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List result = descr_strat.GetItemsByIdent("settlement"); int expected = 96; //number of settlements - Assert.AreEqual(expected, result.Count); //check number of returned settlements } [TestMethod] public void DsGetCharacterChangeCoords() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List characters = parsedds.GetItemsByIdent("character"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List characters = descr_strat.GetItemsByIdent("character"); string result = StratModifier.ChangeCharacterCoordinates(((BaseObj)characters[0]).Value, new Vector2(1, 1)); string expected = "Julius, named character, leader, age 47, , x 1, y 1"; //number of settlements @@ -67,11 +53,8 @@ public void DsGetCharacterChangeCoords() [TestMethod] public void DsGetItemsByIdentResource() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - List result = parsedds.GetItemsByIdent("resource"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List result = descr_strat.GetItemsByIdent("resource"); int expected = 300; //number of resources Assert.AreEqual(expected, result.Count); //check number of returned resources @@ -79,11 +62,8 @@ public void DsGetItemsByIdentResource() [TestMethod] public void DsGetItemsByIdentFaction() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - List result = parsedds.GetItemsByIdent("faction"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List result = descr_strat.GetItemsByIdent("faction"); int expected = 21; //number of factions Assert.AreEqual(expected, result.Count); //check number of returned factions @@ -91,11 +71,8 @@ public void DsGetItemsByIdentFaction() [TestMethod] public void DsGetItemsByIdentCoreAttitudes() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - List result = parsedds.GetItemsByIdent("core_attitudes"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List result = descr_strat.GetItemsByIdent("core_attitudes"); int expected = 47; //number of ca Assert.AreEqual(expected, result.Count); //check number of returned ca @@ -103,13 +80,10 @@ public void DsGetItemsByIdentCoreAttitudes() [TestMethod] public void DsDeleteByIdent() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - List settlements = parsedds.GetItemsByIdent("settlement"); - BaseWrapper.DeleteValue(parsedds.Data, "settlement"); - List result = parsedds.GetItemsByIdent("settlement"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByIdent("settlement"); + BaseWrapper.DeleteValue(descr_strat.Data, "settlement"); + List result = descr_strat.GetItemsByIdent("settlement"); int expected = 0; //number of ca Assert.AreEqual(expected, result.Count); //check number of returned ca @@ -117,13 +91,10 @@ public void DsDeleteByIdent() [TestMethod] public void DsAddSettlementToRomansBrutii() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - - List settlements = parsedds.GetItemsByIdent("settlement"); - bool add = BaseWrapper.InsertNewObjectByCriteria(parsedds.Data, settlements[30], "faction\tromans_brutii,", "denari"); - List result = parsedds.GetItemsByCriteria("character", "settlement", "faction\tromans_brutii,"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByIdent("settlement"); + bool add = BaseWrapper.InsertNewObjectByCriteria(descr_strat.Data, settlements[30], "faction\tromans_brutii,", "denari"); + List result = descr_strat.GetItemsByCriteria("character", "settlement", "faction\tromans_brutii,"); int expected = 3; //number of ca Assert.AreEqual(expected, result.Count); //check number of returned ca @@ -131,27 +102,23 @@ public void DsAddSettlementToRomansBrutii() [TestMethod] public void DsAddSettlementToScythia() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); - List settlements = parsedds.GetItemsByIdent("settlement"); - bool add = BaseWrapper.InsertNewObjectByCriteria(parsedds.Data, settlements[30], "faction\tscythia,", "denari"); - List result = parsedds.GetItemsByCriteria("character", "settlement", "faction\tscythia,"); + List settlements = descr_strat.GetItemsByIdent("settlement"); + bool add = BaseWrapper.InsertNewObjectByCriteria(descr_strat.Data, settlements[30], "faction\tscythia,", "denari"); + List result = descr_strat.GetItemsByCriteria("character", "settlement", "faction\tscythia,"); int expected = 5; //number of ca Assert.AreEqual(expected, result.Count); //check number of returned ca } [TestMethod] public void DsAddUnitToFlaviusAbstracted() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List units = parsedds.GetItemsByCriteria("character", "unit", "faction\tromans_julii,", "character", "army"); - List character = parsedds.GetItemsByCriteria("army", "character", "faction\tromans_julii,"); - List faction = parsedds.GetItemsByIdent("faction"); - parsedds.AddUnitToArmy(faction[0], character[0], units[0]); - List result = parsedds.GetItemsByCriteria("character", "unit", "character\tFlavius", "army"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List units = descr_strat.GetItemsByCriteria("character", "unit", "faction\tromans_julii,", "character", "army"); + List character = descr_strat.GetItemsByCriteria("army", "character", "faction\tromans_julii,"); + List faction = descr_strat.GetItemsByIdent("faction"); + descr_strat.AddUnitToArmy(faction[0], character[0], units[0]); + List result = descr_strat.GetItemsByCriteria("character", "unit", "character\tFlavius", "army"); int expected = 6; //number of ca Assert.AreEqual(expected, result.Count); //check number of returned ca } @@ -159,10 +126,8 @@ public void DsAddUnitToFlaviusAbstracted() [TestMethod] public void GetCharactersOfFaction() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List characters = parsedds.GetItemsByCriteria("character_record", "character", "faction\tromans_julii,"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List characters = descr_strat.GetItemsByCriteria("character_record", "character", "faction\tromans_julii,"); int expected = 7; //number of ca Assert.AreEqual(expected, characters.Count); //check number of returned ca } @@ -170,12 +135,10 @@ public void GetCharactersOfFaction() [TestMethod] public void DsAddUnitToFlavius() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List units = parsedds.GetItemsByCriteria("character", "unit", "faction\tromans_julii,", "character", "army"); - bool add = BaseWrapper.InsertNewObjectByCriteria(parsedds.Data, units[1], "faction\tromans_julii,", "character\tFlavius", "unit"); - List result = parsedds.GetItemsByCriteria("character", "unit", "character\tFlavius", "army"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List units = descr_strat.GetItemsByCriteria("character", "unit", "faction\tromans_julii,", "character", "army"); + bool add = BaseWrapper.InsertNewObjectByCriteria(descr_strat.Data, units[1], "faction\tromans_julii,", "character\tFlavius", "unit"); + List result = descr_strat.GetItemsByCriteria("character", "unit", "character\tFlavius", "army"); int expected = 6; //number of ca Assert.AreEqual(expected, result.Count); //check number of returned ca } @@ -183,10 +146,8 @@ public void DsAddUnitToFlavius() [TestMethod] public void GetRegions() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List regions = parsedds.GetItemsByCriteriaDepth(parsedds.Data, "core_attitudes", "region", "settlement"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List regions = descr_strat.GetItemsByCriteriaDepth(descr_strat.Data, "core_attitudes", "region", "settlement"); int result = regions.Count; int expected = 96; //number of ca @@ -196,11 +157,9 @@ public void GetRegions() [TestMethod] public void GetFactionByRegion() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); string region = "Paionia"; - string faction = parsedds.GetFactionByRegion(region); + string faction = descr_strat.GetFactionByRegion(region); string result = faction; string expected = "macedon"; //number of ca @@ -210,10 +169,8 @@ public void GetFactionByRegion() [TestMethod] public void GetSettlementsForFaction() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List faction_cities = parsedds.GetItemsByCriteriaSimpleDepth(parsedds.Data, "character", "region", [], "faction\tromans_julii,"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List faction_cities = descr_strat.GetItemsByCriteriaSimpleDepth(descr_strat.Data, "character", "region", [], "faction\tromans_julii,"); int expected = 2; int result = faction_cities.Count; Assert.AreEqual(expected, result); //check number of returned ca @@ -221,15 +178,12 @@ public void GetSettlementsForFaction() [TestMethod] public void GetFactionRegionsDict() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); TGA image = new("tgafile", RFH.CurrDirPath("resources", "map_regions.tga"), ""); - List drparse = RFH.ParseFile(Creator.DRcreator, '\t', false, "resources", "descr_regions.txt"); - DR dr = new(drparse, this.config); + DR dr = (DR)RFH.CreateWrapper(Creator.DRcreator, Creator.DRWrapper, TestHelper.Config, '\t', false, "resources", "descr_regions.txt"); CityMap cm = new(image, dr); - Dictionary fr = parsedds.GetFactionRegionDict(); + Dictionary fr = descr_strat.GetFactionRegionDict(); string[] expectedFirst = ["Etruria", "Umbria"]; string[] expectedSecond = ["Gallaecia", "Lusitania", "Hispania", "Taraconenis"]; @@ -247,11 +201,9 @@ public void GetFactionRegionsDict() [TestMethod] public void RemoveSuperFaction() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - parsedds.RemoveSuperFaction(); - List results = parsedds.GetItemsByIdent("superfaction"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + descr_strat.RemoveSuperFaction(); + List results = descr_strat.GetItemsByIdent("superfaction"); Assert.AreEqual(0, results.Count); } } diff --git a/RTWLib_Tests/wrappers/Tests_edb.cs b/RTWLib_Tests/wrappers/Tests_edb.cs index 58fa1d4..080ad60 100644 --- a/RTWLib_Tests/wrappers/Tests_edb.cs +++ b/RTWLib_Tests/wrappers/Tests_edb.cs @@ -4,27 +4,18 @@ using RTWLibPlus.helpers; using RTWLibPlus.parsers; using RTWLibPlus.parsers.objects; -using System.IO; using RTWLibPlus.dataWrappers; using System.Collections.Generic; using RTWLibPlus.interfaces; -using RTWLibPlus.data; -using System.Runtime.InteropServices; - [TestClass] public class Tests_edb { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); [TestMethod] public void EdbParse() { - string[] edb = this.dp.ReadFile(Path.Combine("resources", "edbExample.txt"), false); - List edbParse = this.dp.Parse(edb, Creator.EDBcreator); - EDB parsedEdb = new(edbParse, this.config); - - string result = parsedEdb.Output(); + EDB edb = Instance.InstanceEDB(TestHelper.Config, "resources", "edbExample.txt"); + string result = edb.Output(); string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath("resources", "edbExample.txt")); int rl = result.Length; @@ -37,13 +28,9 @@ public void EdbParse() [TestMethod] public void EdbWholeFile() { - bool ismac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); - string[] edb = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt")); - List edbParse = this.dp.Parse(edb, Creator.EDBcreator); - EDB parsedEdb = new(edbParse, this.config); - - string result = parsedEdb.Output(); - string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath("resources", "export_descr_buildings.txt")); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); + string result = edb.Output(); + string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath(TestHelper.EDB)); int rl = result.Length; int el = expected.Length; @@ -55,11 +42,8 @@ public void EdbWholeFile() [TestMethod] public void EdbGetBuildingLevels() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.EDBcreator); - EDB parsedsmf = new(smfParse, this.config); - - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "core_building", "levels"); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edb.Data, 0, "core_building", "levels"); KeyValuePair expected = new("levels", "governors_house governors_villa governors_palace proconsuls_palace imperial_palace"); Assert.AreEqual(expected, result); @@ -68,11 +52,9 @@ public void EdbGetBuildingLevels() [TestMethod] public void EdbGetRequires() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.EDBcreator); - EDB parsedsmf = new(smfParse, this.config); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "core_building", "levels", "governors_house"); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edb.Data, 0, "core_building", "levels", "governors_house"); KeyValuePair expected = new("governors_house", "requires factions { barbarian, carthaginian, eastern, parthia, egyptian, greek, roman, }"); Assert.AreEqual(expected, result); @@ -81,12 +63,10 @@ public void EdbGetRequires() [TestMethod] public void EdbModifyRequires() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.EDBcreator); - EDB parsedsmf = new(smfParse, this.config); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); - bool change = BaseWrapper.ModifyValue(parsedsmf.Data, "requires factions { barbarian, }", 0, false, "core_building", "levels", "governors_house"); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "core_building", "levels", "governors_house"); + bool change = BaseWrapper.ModifyValue(edb.Data, "requires factions { barbarian, }", 0, false, "core_building", "levels", "governors_house"); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edb.Data, 0, "core_building", "levels", "governors_house"); KeyValuePair expected = new("governors_house", "requires factions { barbarian, }"); Assert.AreEqual(expected, result); @@ -95,11 +75,9 @@ public void EdbModifyRequires() [TestMethod] public void EdbGetCapabiltyArray() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.EDBcreator); - EDB parsedsmf = new(smfParse, this.config); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); - List result = BaseWrapper.GetItemList(parsedsmf.Data, 0, "core_building", "levels", "governors_house", "capability"); + List result = BaseWrapper.GetItemList(edb.Data, 0, "core_building", "levels", "governors_house", "capability"); List expected = [ new EDBObj("recruit", "\"carthaginian peasant\" 0", 4), @@ -119,11 +97,9 @@ public void EdbGetCapabiltyArray() [TestMethod] public void EdbGetPopulationHealth() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.EDBcreator); - EDB parsedsmf = new(smfParse, this.config); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "health", "levels", "sewers", "capability", "population_health_bonus"); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edb.Data, 0, "health", "levels", "sewers", "capability", "population_health_bonus"); KeyValuePair expected = new("population_health_bonus", "bonus 1"); Assert.AreEqual(expected, result); @@ -132,12 +108,10 @@ public void EdbGetPopulationHealth() [TestMethod] public void EdbModifyPopulationHealth() { - string[] smf = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_buildings.txt"), false); - List smfParse = this.dp.Parse(smf, Creator.EDBcreator); - EDB parsedsmf = new(smfParse, this.config); - bool rb = BaseWrapper.ModifyValue(parsedsmf.Data, "bonus 3", 0, false, "health", "levels", "sewers", "capability", "population_health_bonus"); + EDB edb = Instance.InstanceEDB(TestHelper.Config, TestHelper.EDB); + bool rb = BaseWrapper.ModifyValue(edb.Data, "bonus 3", 0, false, "health", "levels", "sewers", "capability", "population_health_bonus"); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedsmf.Data, 0, "health", "levels", "sewers", "capability", "population_health_bonus"); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edb.Data, 0, "health", "levels", "sewers", "capability", "population_health_bonus"); KeyValuePair expected = new("population_health_bonus", "bonus 3"); Assert.AreEqual(expected, result); diff --git a/RTWLib_Tests/wrappers/Tests_edu.cs b/RTWLib_Tests/wrappers/Tests_edu.cs index 1721863..5d62a35 100644 --- a/RTWLib_Tests/wrappers/Tests_edu.cs +++ b/RTWLib_Tests/wrappers/Tests_edu.cs @@ -1,7 +1,6 @@ namespace RTWLib_Tests.wrappers; using Microsoft.VisualStudio.TestTools.UnitTesting; -using RTWLibPlus.data; using RTWLibPlus.dataWrappers; using RTWLibPlus.helpers; using RTWLibPlus.parsers; @@ -13,17 +12,13 @@ [TestClass] public class Tests_edu { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); [TestMethod] public void EduWithDepthParser() { - string[] edu = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_unit.txt"), false); - List eduParse = this.dp.Parse(edu, Creator.EDUcreator); - EDU parsedds = new(eduParse, this.config); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); - string result = parsedds.Output(); - string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath("resources", "export_descr_unit.txt")); + string result = edu.Output(); + string expected = DepthParse.ReadFileAsString(RFH.CurrDirPath(TestHelper.EDU)); int rl = result.Length; int el = expected.Length; @@ -35,10 +30,8 @@ public void EduWithDepthParser() [TestMethod] public void EduGetValueByCriteria() { - string[] edu = this.dp.ReadFile(RFH.CurrDirPath("resources", "export_descr_unit.txt"), false); - List eduParse = this.dp.Parse(edu, Creator.EDUcreator); - EDU parsedds = new(eduParse, this.config); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedds.Data, 0, "roman_hastati", "ownership"); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edu.Data, 0, "roman_hastati", "ownership"); KeyValuePair expected = new("ownership", "roman"); //number of ca Assert.AreEqual(expected, result); //check number of returned ca @@ -47,11 +40,9 @@ public void EduGetValueByCriteria() [TestMethod] public void EduModifyOwnership() { - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU parsedds = new(parse, this.config); - - bool change = BaseWrapper.ModifyValue(parsedds.Data, "roman", 0, false, "carthaginian_generals_cavalry_early", "ownership"); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedds.Data, 0, "carthaginian_generals_cavalry_early", "ownership"); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); + bool change = BaseWrapper.ModifyValue(edu.Data, "roman", 0, false, "carthaginian_generals_cavalry_early", "ownership"); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edu.Data, 0, "carthaginian_generals_cavalry_early", "ownership"); KeyValuePair expected = new("ownership", "roman"); Assert.AreEqual(expected, result); @@ -61,8 +52,7 @@ public void EduModifyOwnership() [TestMethod] public void EduModifyBulkModifyOwnership() { - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU edu = new(parse, this.config); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); List ownerships = edu.GetItemsByIdent("ownership"); foreach (EDUObj o in ownerships.Cast()) @@ -83,12 +73,11 @@ public void EduModifyBulkModifyOwnership() [TestMethod] public void EduRemoveAttributeGeneral() { - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU parsedds = new(parse, this.config); - parsedds.RemoveAttributesAll("general_unit", "general_unit_upgrade \"marian_reforms\""); - KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(parsedds.Data, 0, "barb_chieftain_cavalry_german", "attributes"); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); + edu.RemoveAttributesAll("general_unit", "general_unit_upgrade \"marian_reforms\""); + KeyValuePair result = BaseWrapper.GetKeyValueAtLocation(edu.Data, 0, "barb_chieftain_cavalry_german", "attributes"); KeyValuePair expected = new("attributes", "sea_faring, hide_forest, hardy"); - List attr = parsedds.GetItemsByIdent("attributes"); + List attr = edu.GetItemsByIdent("attributes"); Assert.AreEqual(expected, result); } @@ -96,22 +85,20 @@ public void EduRemoveAttributeGeneral() [TestMethod] public void EduRemoveRemasterStatBlocks() { - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU parsedds = new(parse, this.config); - parsedds.PrepareEDU(); - List result = parsedds.GetItemsByIdent("rebalance_statblock"); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); + edu.PrepareEDU(); + List result = edu.GetItemsByIdent("rebalance_statblock"); int expected = 0; - string file = parsedds.Output(); + string file = edu.Output(); Assert.AreEqual(expected, result.Count); } [TestMethod] public void EduGetUnitsFromFaction() { - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU parsedds = new(parse, this.config); - List units = parsedds.GetUnitsFromFaction("romans_julii", []); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); + List units = edu.GetUnitsFromFaction("romans_julii", []); Assert.AreEqual(27, units.Count); } @@ -119,8 +106,7 @@ public void EduGetUnitsFromFaction() [TestMethod] public void EduValuesInCorrectOrder() { - List parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); - EDU edu = new(parse, this.config); + EDU edu = Instance.InstanceEDU(TestHelper.Config, TestHelper.EDU); List names = edu.GetItemsByIdent("type"); List attributes = edu.GetItemsByIdent("attributes"); List ownerships = edu.GetItemsByIdent("ownership"); @@ -140,7 +126,7 @@ public void EduValuesInCorrectOrder() // [TestMethod] // public void eduUnitWrapper() // { - // var parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, "resources", "export_descr_unit.txt"); + // var parse = RFH.ParseFile(Creator.EDUcreator, ' ', false, TestHelper.EDU); // var parsedds = new EDU(parse, config); // parsedds.PrepareEDU(); From a134bf297f3f1a87a18c6ad26da6e73259c2598e Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 11 Sep 2024 20:08:34 +0100 Subject: [PATCH 2/2] chore: refactored tests --- RTWLib_Tests/modifiers/Tests_drMod.cs | 20 +++-------- RTWLib_Tests/modifiers/Tests_stratMod.cs | 42 +++++++++--------------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/RTWLib_Tests/modifiers/Tests_drMod.cs b/RTWLib_Tests/modifiers/Tests_drMod.cs index a1215d8..91c12a6 100644 --- a/RTWLib_Tests/modifiers/Tests_drMod.cs +++ b/RTWLib_Tests/modifiers/Tests_drMod.cs @@ -2,34 +2,24 @@ namespace RTWLib_Tests.Modifiers; using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; -using RTWLibPlus.data; using RTWLibPlus.dataWrappers; using RTWLibPlus.helpers; using RTWLibPlus.interfaces; -using RTWLibPlus.parsers; using RTWLibPlus.parsers.objects; using RTWLibPlus.Modifiers; [TestClass] public class TestsDR { - private readonly DepthParse dp = new(); - private readonly TWConfig config = TWConfig.LoadConfig(@"resources/remaster.json"); - [TestMethod] public void GetMissingRegions() { - string[] dr = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_regions.txt"), false); - List smfParse = this.dp.Parse(dr, Creator.DRcreator, '\t'); - DR parseddr = new(smfParse, this.config); - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List settlements = parsedds.GetItemsByIdent("settlement").DeepCopy(); - List missing = DRModifier.GetMissingRegionNames(settlements, parseddr); - + DR descr_regions = Instance.InstanceDR(TestHelper.Config, TestHelper.DR); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByIdent("settlement").DeepCopy(); + List missing = DRModifier.GetMissingRegionNames(settlements, descr_regions); int result = settlements.Count + missing.Count; - Assert.AreEqual(parseddr.Regions.Count, result); + Assert.AreEqual(descr_regions.Regions.Count, result); } } diff --git a/RTWLib_Tests/modifiers/Tests_stratMod.cs b/RTWLib_Tests/modifiers/Tests_stratMod.cs index cad9a77..94a8ea0 100644 --- a/RTWLib_Tests/modifiers/Tests_stratMod.cs +++ b/RTWLib_Tests/modifiers/Tests_stratMod.cs @@ -19,10 +19,8 @@ public class TestsStrat [TestMethod] public void CreateSettlement() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List settlements = parsedds.GetItemsByIdent("settlement").DeepCopy(); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByIdent("settlement").DeepCopy(); IBaseObj modifiedSettlement = StratModifier.CreateSettlement(settlements[0], "test_name"); string result = modifiedSettlement.Find("region"); Assert.AreEqual("test_name", result); @@ -31,10 +29,8 @@ public void CreateSettlement() [TestMethod] public void CreateBuilding() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List settlements = parsedds.GetItemsByIdent("settlement").DeepCopy(); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByIdent("settlement").DeepCopy(); IBaseObj modifiedBuilding = StratModifier.CreateBuilding(settlements[0].GetObject("building"), "test_name"); string result = modifiedBuilding.Find("type"); Assert.AreEqual("test_name", result); @@ -43,14 +39,12 @@ public void CreateBuilding() [TestMethod] public void AddSettlementToFaction() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List settlements = parsedds.GetItemsByCriteria("character", "settlement", "faction\tromans_julii,"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByCriteria("character", "settlement", "faction\tromans_julii,"); IBaseObj modifiedSettlement = StratModifier.CreateSettlement(settlements[0], "test_name"); - int placeAt = BaseWrapper.GetIndexByCriteria(parsedds.Data, "faction\tromans_julii,", "settlement"); - parsedds.InsertAt(placeAt + 1, modifiedSettlement); - List result = parsedds.GetItemsByCriteria("character", "settlement", "faction\tromans_julii,"); + int placeAt = BaseWrapper.GetIndexByCriteria(descr_strat.Data, "faction\tromans_julii,", "settlement"); + descr_strat.InsertAt(placeAt + 1, modifiedSettlement); + List result = descr_strat.GetItemsByCriteria("character", "settlement", "faction\tromans_julii,"); Assert.AreEqual(3, result.Count); Assert.AreEqual("test_name", result[1].Find("region")); } @@ -58,14 +52,12 @@ public void AddSettlementToFaction() [TestMethod] public void AddSettlementToFaction2() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List settlements = parsedds.GetItemsByCriteria("character", "settlement", "faction\tmacedon,"); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List settlements = descr_strat.GetItemsByCriteria("character", "settlement", "faction\tmacedon,"); IBaseObj modifiedSettlement = StratModifier.CreateSettlement(settlements[0], "test_name"); - int placeAt = BaseWrapper.GetIndexByCriteria(parsedds.Data, "faction\tmacedon,", "settlement"); - parsedds.InsertAt(placeAt + 1, modifiedSettlement); - List result = parsedds.GetItemsByCriteria("character", "settlement", "faction\tmacedon,"); + int placeAt = BaseWrapper.GetIndexByCriteria(descr_strat.Data, "faction\tmacedon,", "settlement"); + descr_strat.InsertAt(placeAt + 1, modifiedSettlement); + List result = descr_strat.GetItemsByCriteria("character", "settlement", "faction\tmacedon,"); Assert.AreEqual(5, result.Count); Assert.AreEqual("test_name", result[1].Find("region")); } @@ -73,10 +65,8 @@ public void AddSettlementToFaction2() [TestMethod] public void CreateUnit() { - string[] ds = this.dp.ReadFile(RFH.CurrDirPath("resources", "descr_strat.txt"), false); - List dsParse = this.dp.Parse(ds, Creator.DScreator); - DS parsedds = new(dsParse, this.config); - List units = parsedds.GetItemsByIdent("unit").DeepCopy(); + DS descr_strat = Instance.InstanceDS(TestHelper.Config, TestHelper.DS); + List units = descr_strat.GetItemsByIdent("unit").DeepCopy(); IBaseObj result = StratModifier.CreateUnit(units[0], "town watch"); Assert.AreEqual("unit", result.Ident); Assert.AreEqual("unit\t\t\ttown", result.Tag);