Skip to content

Commit

Permalink
Governorship conversion part I (#322) #minor
Browse files Browse the repository at this point in the history
* Create Governorship.cs

* Create GovernorshipTests.cs

* Jobs

* Create governorMappings.txt

* new Jobs constructor

* read jobs from save

* Region to title mapping

* Update commonItems.NET

* Update World.cs

* InitializeFromGovernorship

* ImportImperatorGovernorship

* Update Title.cs

* Province mapping improvement

* Update province_mappings.txt

* LandedTitles.LinkCapitals

* Governorship conversion polishing

* Governorship loc fixes

* PR tweaks

* Fix governorship loc for PRY, SEL, MRY

* PR tweak

* Naming tweaks

* New tests for TagTitleMapper

* TitleCanBeMatchedFromGovernorship test

* Remove unused field

* LocBlockCanBeCopyConstructed test

* CapitalsAreLinked test

* Fix uodating from other title

* Null checks

* Remove redundant parentheses

* Extract locak function

* Move county holder caching out of OverWriteCountiesHistory

* Reverse condition

* Fix build
  • Loading branch information
IhateTrains authored Sep 22, 2021
1 parent 8b8454c commit 05eb136
Show file tree
Hide file tree
Showing 20 changed files with 969 additions and 296 deletions.
17 changes: 17 additions & 0 deletions ImperatorToCK3.UnitTests/CK3/Titles/LandedTitlesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,22 @@ public void TitlesCanBeAddedByMods() {

Assert.Equal(10, titles.StoredTitles.Count);
}

[Fact]
public void CapitalsAreLinked() {
var reader = new BufferedReader(
"e_empire = { capital=c_county " +
"k_kingdom = { d_duchy = { c_county = { b_barony = { province = 12 } } } } " +
"}"
);
var titles = new LandedTitles();
titles.LoadTitles(reader);

var empire = titles.StoredTitles["e_empire"];
var capitalCounty = empire.CapitalCounty;
Assert.True(capitalCounty.HasValue);
Assert.Equal("c_county", capitalCounty.Value.Key);
Assert.Equal("c_county", capitalCounty.Value.Value.Name);
}
}
}
31 changes: 31 additions & 0 deletions ImperatorToCK3.UnitTests/Imperator/Jobs/GovernorshipTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using commonItems;
using ImperatorToCK3.Imperator.Jobs;
using Xunit;

namespace ImperatorToCK3.UnitTests.Imperator.Jobs {
public class GovernorshipTests {
[Fact]
public void FieldsDefaultToCorrectValues() {
var reader = new BufferedReader(string.Empty);
var governorship = new Governorship(reader);
Assert.Equal((ulong)0, governorship.CountryID);
Assert.Equal((ulong)0, governorship.CharacterID);
Assert.Equal(new Date(1, 1, 1), governorship.StartDate);
Assert.True(string.IsNullOrEmpty(governorship.RegionName));
}
[Fact]
public void FieldsCanBeSet() {
var reader = new BufferedReader(
"who=589\n" +
"character=25212\n" +
"start_date=450.10.1\n" +
"governorship = \"galatia_region\""
);
var governorship = new Governorship(reader);
Assert.Equal((ulong)589, governorship.CountryID);
Assert.Equal((ulong)25212, governorship.CharacterID);
Assert.Equal(new Date(450, 10, 1, AUC: true), governorship.StartDate);
Assert.Equal("galatia_region", governorship.RegionName);
}
}
}
39 changes: 39 additions & 0 deletions ImperatorToCK3.UnitTests/Imperator/Jobs/JobsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.IO;
using commonItems;
using Xunit;

namespace ImperatorToCK3.UnitTests.Imperator.Jobs {
[Collection("Sequential")]
[CollectionDefinition("Sequential", DisableParallelization = true)]
public class JobsTests {
[Fact]
public void GovernorshipsDefaultToEmpty() {
var jobs = new ImperatorToCK3.Imperator.Jobs.Jobs();
Assert.Empty(jobs.Governorships);
}
[Fact]
public void GovernorshipsCanBeRead() {
var reader = new BufferedReader(
"province_job={who=1} province_job={who=2}"
);
var jobs = new ImperatorToCK3.Imperator.Jobs.Jobs(reader);
Assert.Collection(jobs.Governorships,
item1 => Assert.Equal((ulong)1, item1.CountryID),
item2 => Assert.Equal((ulong)2, item2.CountryID)
);
}
[Fact]
public void IgnoredTokensAreLogged() {
var output = new StringWriter();
Console.SetOut(output);

var reader = new BufferedReader(
"useless_job = {}"
);
_ = new ImperatorToCK3.Imperator.Jobs.Jobs(reader);

Assert.Contains("Ignored Jobs tokens: useless_job", output.ToString());
}
}
}
3 changes: 3 additions & 0 deletions ImperatorToCK3.UnitTests/ImperatorToCK3.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<None Update="TestFiles\configurables\definite_form_names.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestFiles\configurables\governorMappings.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestFiles\configurables\nickname_map.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using commonItems;

namespace ImperatorToCK3.UnitTests.Mappers.Localization {
public class LocalizationMapperTests {
[Fact] public void LocalisationsCanBeLoadedAndMatched() {
public class LocalizationMapperTests {
[Fact]
public void LocalisationsCanBeLoadedAndMatched() {
var reader1 = new BufferedReader(
"l_english:\n" +
" key1:0 \"value 1\" # comment\n" +
Expand Down Expand Up @@ -82,7 +83,8 @@ public void LocalisationsReturnsEnglishForMissingLanguage() {
Assert.Equal("value 1", ((LocBlock)locs.GetLocBlockForKey("key1")).french);
}

[Fact] public void LocCanBeModifiedByMethodForEveryLanguage() {
[Fact]
public void LocCanBeModifiedByMethodForEveryLanguage() {
var nameLocBlock = new LocBlock {
english = "$ADJ$ Revolt",
french = "$ADJ$ révolte",
Expand Down Expand Up @@ -110,11 +112,13 @@ [Fact] public void LocCanBeModifiedByMethodForEveryLanguage() {
Assert.Equal("Romana revuelta", nameLocBlock.spanish);
}

[Fact] public void LocalizationCanBeAddedForKey() {
[Fact]
public void LocalizationCanBeAddedForKey() {
var localizationMapper = new LocalizationMapper();
Assert.Null(localizationMapper.GetLocBlockForKey("key1"));
localizationMapper.AddLocalization("key1", new LocBlock {
english = "Roman", french = "Romain"
english = "Roman",
french = "Romain"
});

var locBlock = localizationMapper.GetLocBlockForKey("key1");
Expand All @@ -124,6 +128,25 @@ [Fact] public void LocalizationCanBeAddedForKey() {
Assert.Equal("Roman", locBlock.russian);
Assert.Equal("Roman", locBlock.simp_chinese);
Assert.Equal("Roman", locBlock.spanish);
}
}

[Fact]
public void LocBlockCanBeCopyConstructed() {
var origLocBlock = new LocBlock {
english = "a",
french = "b",
german = "c",
russian = "d",
simp_chinese = "e",
spanish = "f"
};
var copyLocBlock = new LocBlock(origLocBlock);
Assert.Equal("a", copyLocBlock.english);
Assert.Equal("b", copyLocBlock.french);
Assert.Equal("c", copyLocBlock.german);
Assert.Equal("d", copyLocBlock.russian);
Assert.Equal("e", copyLocBlock.simp_chinese);
Assert.Equal("f", copyLocBlock.spanish);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,57 @@
using Xunit;

namespace ImperatorToCK3.UnitTests.Mappers.TagTitle {
public class TagTitleMappingTests {
public class MappingTests {
[Fact]
public void SimpleTagMatch() {
var reader = new BufferedReader("{ ck3 = e_roman_empire imp = ROM }");
var mapping = TagTitleMapping.Parse(reader);
var match = mapping.TagRankMatch("ROM", "");
var mapping = Mapping.Parse(reader);
var match = mapping.RankMatch("ROM", "");

Assert.Equal("e_roman_empire", match);
}

[Fact]
public void SimpleTagMatchFailsOnWrongTag() {
var reader = new BufferedReader("{ ck3 = e_roman_empire imp = REM }");
var mapping = TagTitleMapping.Parse(reader);
var match = mapping.TagRankMatch("ROM", "");
var mapping = Mapping.Parse(reader);
var match = mapping.RankMatch("ROM", "");

Assert.Null(match);
}

[Fact]
public void SimpleTagMatchFailsOnNoTag() {
var reader = new BufferedReader("{ ck3 = e_roman_empire }");
var mapping = TagTitleMapping.Parse(reader);
var match = mapping.TagRankMatch("ROM", "");
var mapping = Mapping.Parse(reader);
var match = mapping.RankMatch("ROM", "");

Assert.Null(match);
}

[Fact]
public void TagRankMatch() {
var reader = new BufferedReader("{ ck3 = e_roman_empire imp = ROM rank = e }");
var mapping = TagTitleMapping.Parse(reader);
var match = mapping.TagRankMatch("ROM", "e");
var mapping = Mapping.Parse(reader);
var match = mapping.RankMatch("ROM", "e");

Assert.Equal("e_roman_empire", match);
}

[Fact]
public void TagRankMatchFailsOnWrongRank() {
var reader = new BufferedReader("{ ck3 = e_roman_empire imp = ROM rank = k }");
var mapping = TagTitleMapping.Parse(reader);
var match = mapping.TagRankMatch("ROM", "e");
var mapping = Mapping.Parse(reader);
var match = mapping.RankMatch("ROM", "e");

Assert.Null(match);
}

[Fact]
public void TagRankMatchSucceedsOnNoRank() {
var reader = new BufferedReader("{ ck3 = e_roman_empire imp = ROM }");
var mapping = TagTitleMapping.Parse(reader);
var match = mapping.TagRankMatch("ROM", "e");
var mapping = Mapping.Parse(reader);
var match = mapping.RankMatch("ROM", "e");

Assert.Equal("e_roman_empire", match);
}
Expand Down
50 changes: 42 additions & 8 deletions ImperatorToCK3.UnitTests/Mappers/TagTitle/TagTitleMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,81 @@ namespace ImperatorToCK3.UnitTests.Mappers.TagTitle {
[Collection("Sequential")]
[CollectionDefinition("Sequential", DisableParallelization = true)]
public class TagTitleMapperTests {
const string tagTitleMappingsPath = "TestFiles/configurables/title_map.txt";
const string governorshipTitleMappingsPath = "TestFiles/configurables/governorMappings.txt";

[Fact]
public void TitleCanBeMatched() {
var mapper = new TagTitleMapper("TestFiles/configurables/title_map.txt"); // reads title_map.txt from TestFiles
public void TitleCanBeMatchedFromTag() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath); // reads title_map.txt from TestFiles
var match = mapper.GetTitleForTag("CRT", CountryRank.majorPower);

Assert.Equal("k_krete", match);
}
[Fact]
public void TitleCanBeMatchedFromGovernorship() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath); // reads title_map.txt from TestFiles
var match = mapper.GetTitleForGovernorship("central_italy_region", "ROM", "e_roman_empire");

Assert.Equal("k_romagna", match);
}

[Fact]
public void TitleCanBeMatchedByRanklessLink() {
var mapper = new TagTitleMapper("TestFiles/configurables/title_map.txt"); // reads title_map.txt from TestFiles
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath); // reads title_map.txt from TestFiles
var match = mapper.GetTitleForTag("RAN", CountryRank.majorPower);

Assert.Equal("d_rankless", match);
}

[Fact]
public void TitleCanBeGenerated() {
var mapper = new TagTitleMapper("TestFiles/configurables/title_map.txt");
public void TitleCanBeGeneratedFromTag() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath);
var match = mapper.GetTitleForTag("ROM", CountryRank.localPower, "Rome");
var match2 = mapper.GetTitleForTag("DRE", CountryRank.localPower, "Dre Empire");

Assert.Equal("k_IMPTOCK3_ROM", match);
Assert.Equal("e_IMPTOCK3_DRE", match2);
}
[Fact]
public void TitleCanBeGeneratedFromGovernorship() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath);
var match = mapper.GetTitleForGovernorship("apulia_region", "ROM", "e_roman_empire");
var match2 = mapper.GetTitleForGovernorship("pepe_region", "DRE", "k_dre_empire");

Assert.Equal("k_IMPTOCK3_ROM_apulia_region", match);
Assert.Equal("d_IMPTOCK3_DRE_pepe_region", match2);
}

[Fact]
public void GetTitleForTagReturnsNulloptOnEmptyParameter() {
var mapper = new TagTitleMapper("TestFiles/configurables/title_map.txt");
public void GetTitleForTagReturnsNullOnEmptyParameter() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath);
var match = mapper.GetTitleForTag("", CountryRank.migrantHorde, "");

Assert.Null(match);
}
[Fact]
public void GetTitleGovernorshipTagReturnsNullOnEmptyParameter() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath);
var match = mapper.GetTitleForGovernorship("", "", "");

Assert.Null(match);
}

[Fact]
public void TagCanBeRegistered() {
var mapper = new TagTitleMapper("TestFiles/configurables/title_map.txt");
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath);
mapper.RegisterTag("BOR", "e_boredom");
var match = mapper.GetTitleForTag("BOR", CountryRank.localPower);

Assert.Equal("e_boredom", match);
}
[Fact]
public void GovernorshipCanBeRegistered() {
var mapper = new TagTitleMapper(tagTitleMappingsPath, governorshipTitleMappingsPath);
mapper.RegisterGovernorship("aquitaine_region", "BOR", "k_atlantis");
var match = mapper.GetTitleForGovernorship("aquitaine_region", "BOR", "e_roman_empire");

Assert.Equal("k_atlantis", match);
}
}
}
Loading

0 comments on commit 05eb136

Please sign in to comment.