Skip to content

Commit b998cad

Browse files
authored
Unit tests for the DoctrineCategory class (#2725)
1 parent 04e59d3 commit b998cad

File tree

5 files changed

+68
-18
lines changed

5 files changed

+68
-18
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using ImperatorToCK3.CK3.Religions;
2+
using commonItems;
3+
using System.Collections.Generic;
4+
using Xunit;
5+
6+
namespace ImperatorToCK3.UnitTests.CK3.Religions;
7+
8+
public class DoctrineCategoryTests {
9+
[Fact]
10+
public void Constructor_SetsId() {
11+
var reader = new BufferedReader("");
12+
var category = new DoctrineCategory("test_id", reader);
13+
Assert.Equal("test_id", category.Id);
14+
}
15+
16+
[Fact]
17+
public void Constructor_ParsesGroupId() {
18+
var reader = new BufferedReader("group = test_group");
19+
var category = new DoctrineCategory("cat", reader);
20+
Assert.Equal("test_group", category.GroupId);
21+
}
22+
23+
[Fact]
24+
public void Constructor_ParsesNumberOfPicks() {
25+
var reader = new BufferedReader("number_of_picks = 3");
26+
var category = new DoctrineCategory("cat", reader);
27+
Assert.Equal(3, category.NumberOfPicks);
28+
}
29+
30+
[Fact]
31+
public void Constructor_DefaultsNumberOfPicksTo1() {
32+
var reader = new BufferedReader("");
33+
var category = new DoctrineCategory("cat", reader);
34+
Assert.Equal(1, category.NumberOfPicks);
35+
}
36+
37+
[Fact]
38+
public void Constructor_ParsesDoctrineIds() {
39+
var reader = new BufferedReader("doctrine1={} doctrine2={} doctrine3={}");
40+
var category = new DoctrineCategory("cat", reader);
41+
Assert.Equal(["doctrine1", "doctrine2", "doctrine3"], category.DoctrineIds);
42+
}
43+
44+
[Fact]
45+
public void DoctrineIds_IsReadOnly() {
46+
var reader = new BufferedReader("doctrine1");
47+
var category = new DoctrineCategory("cat", reader);
48+
Assert.IsType<IReadOnlyCollection<string>>(category.DoctrineIds, exactMatch: false);
49+
}
50+
}

ImperatorToCK3/CK3/Characters/Character.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public string GetAgeSex(Date date) {
9696
public Date BirthDate {
9797
get => History.Fields["birth"].DateToEntriesDict.AsValueEnumerable().First().Key;
9898
set {
99-
var field = History.Fields["birth"];
100-
field.RemoveAllEntries();
101-
field.AddEntryToHistory(value, "birth", true);
99+
var historyField = History.Fields["birth"];
100+
historyField.RemoveAllEntries();
101+
historyField.AddEntryToHistory(value, "birth", true);
102102
}
103103
}
104104

@@ -108,10 +108,10 @@ public Date? DeathDate {
108108
return entriesDict.Count == 0 ? null : entriesDict.AsValueEnumerable().First().Key;
109109
}
110110
set {
111-
var field = History.Fields["death"];
112-
field.RemoveAllEntries();
111+
var historyField = History.Fields["death"];
112+
historyField.RemoveAllEntries();
113113
if (value is not null) {
114-
field.AddEntryToHistory(value, "death", true);
114+
historyField.AddEntryToHistory(value, "death", true);
115115
}
116116
}
117117
}
@@ -596,8 +596,8 @@ public void UpdateChildrenCacheOfParents() {
596596

597597
public string? MotherId {
598598
get {
599-
var field = History.Fields["mother"];
600-
var entries = field.InitialEntries;
599+
var historyField = History.Fields["mother"];
600+
var entries = historyField.InitialEntries;
601601
if (entries.Count == 0) {
602602
return null;
603603
}
@@ -614,8 +614,8 @@ public string? MotherId {
614614
}
615615

616616
idStr = idStr.RemQuotes();
617-
field.RemoveAllEntries();
618-
field.AddEntryToHistory(null, "mother", idStr);
617+
historyField.RemoveAllEntries();
618+
historyField.AddEntryToHistory(null, "mother", idStr);
619619
return idStr;
620620
}
621621
}
@@ -644,8 +644,8 @@ public Character? Mother {
644644

645645
public string? FatherId {
646646
get {
647-
var field = History.Fields["father"];
648-
var entries = field.InitialEntries;
647+
var historyField = History.Fields["father"];
648+
var entries = historyField.InitialEntries;
649649
if (entries.Count == 0) {
650650
return null;
651651
}
@@ -662,8 +662,8 @@ public string? FatherId {
662662
}
663663

664664
idStr = idStr.RemQuotes();
665-
field.RemoveAllEntries();
666-
field.AddEntryToHistory(null, "father", idStr);
665+
historyField.RemoveAllEntries();
666+
historyField.AddEntryToHistory(null, "father", idStr);
667667
return idStr;
668668
}
669669
}

ImperatorToCK3/CK3/Religions/Religion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ImperatorToCK3.CK3.Religions;
1111

1212
internal sealed class Religion : IIdentifiable<string>, IPDXSerializable {
1313
public string Id { get; }
14-
public OrderedSet<string> DoctrineIds { get; } = new();
14+
public OrderedSet<string> DoctrineIds { get; } = [];
1515

1616
public ReligionCollection ReligionCollection { get; }
1717

@@ -43,7 +43,7 @@ public Religion(string id, BufferedReader religionReader, ReligionCollection rel
4343
$"{string.Join(", ", doctrinesInCategory)}. Keeping the last {category.NumberOfPicks} of them.");
4444

4545
DoctrineIds.ExceptWith(doctrinesInCategory);
46-
foreach (var doctrine in doctrinesInCategory.Reverse().Take(category.NumberOfPicks)) {
46+
foreach (var doctrine in Enumerable.Reverse(doctrinesInCategory).Take(category.NumberOfPicks)) {
4747
DoctrineIds.Add(doctrine);
4848
}
4949
}

ImperatorToCK3/CommonUtils/EnumerableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class EnumerableExtensions {
1212
return null;
1313
}
1414

15-
foreach (var element in enumerable.Reverse()) {
15+
foreach (var element in Enumerable.Reverse(enumerable)) {
1616
if (predicate(element)) {
1717
return element;
1818
}

ImperatorToCK3/ImperatorToCK3.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<RuntimeIdentifiers>win-x64;linux-x64;osx-arm64</RuntimeIdentifiers>
88
<ApplicationIcon>thumbnail.ico</ApplicationIcon>
9-
<LangVersion>13</LangVersion>
9+
<LangVersion>latest</LangVersion>
1010
<CETCompat>false</CETCompat> <!-- see https://github.com/ParadoxGameConverters/ImperatorToCK3/issues/2638 -->
1111
</PropertyGroup>
1212

0 commit comments

Comments
 (0)