Skip to content

Commit

Permalink
Unit test for loading a CK3 name list (#2312)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Nov 12, 2024
1 parent 1725bd4 commit b958b2b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
38 changes: 38 additions & 0 deletions ImperatorToCK3.UnitTests/CK3/Cultures/NameListTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using commonItems;
using Xunit;

namespace ImperatorToCK3.UnitTests.CK3.Cultures;

public class NameListTests {
[Fact]
public void NameListIsCorrectlyLoaded() {
var reader = new BufferedReader("""
{
male_names = {
John
"Alexander"
2 = { Tom "Mark" }
}
female_names = {
Jane
"Alexandra"
2 = { Sandra "Maria" }
}
""");
var nameList = new ImperatorToCK3.CK3.Cultures.NameList("test", reader);

Assert.Equal("test", nameList.Id);
Assert.Collection(nameList.MaleNames,
item => Assert.Equal("John", item),
item => Assert.Equal("Alexander", item),
item => Assert.Equal("Tom", item),
item => Assert.Equal("Mark", item)
);
Assert.Collection(nameList.FemaleNames,
item => Assert.Equal("Jane", item),
item => Assert.Equal("Alexandra", item),
item => Assert.Equal("Sandra", item),
item => Assert.Equal("Maria", item)
);
}
}
17 changes: 8 additions & 9 deletions ImperatorToCK3/CK3/Cultures/NameList.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using commonItems;
using commonItems.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.RegularExpressions;

namespace ImperatorToCK3.CK3.Cultures;

public sealed partial class NameList : IIdentifiable<string> {
public string Id { get; }
private readonly OrderedSet<string> maleNames = new();
private readonly OrderedSet<string> femaleNames = new();
public IReadOnlyCollection<string> MaleNames => maleNames.ToImmutableList();
public IReadOnlyCollection<string> FemaleNames => femaleNames.ToImmutableList();
private readonly OrderedSet<string> maleNames = [];
private readonly OrderedSet<string> femaleNames = [];
public IReadOnlyCollection<string> MaleNames => maleNames;
public IReadOnlyCollection<string> FemaleNames => femaleNames;

public NameList(string id, BufferedReader nameListReader) {
Id = id;
Expand All @@ -25,8 +24,8 @@ public NameList(string id, BufferedReader nameListReader) {
maleNamesBlockParser.RegisterRegex(CommonRegexes.String, (_, nameStr) => {
maleNames.Add(nameStr);
});
maleNamesBlockParser.RegisterRegex(CommonRegexes.QuotedString, (_, nameStr) => {
maleNames.Add(nameStr);
maleNamesBlockParser.RegisterRegex(CommonRegexes.QuotedString, (_, quotedNameStr) => {
maleNames.Add(quotedNameStr.RemQuotes());
});
maleNamesBlockParser.IgnoreAndLogUnregisteredItems();
maleNamesBlockParser.ParseStream(maleNamesReader);
Expand All @@ -39,8 +38,8 @@ public NameList(string id, BufferedReader nameListReader) {
femaleNamesBlockParser.RegisterRegex(CommonRegexes.String, (_, nameStr) => {
femaleNames.Add(nameStr);
});
femaleNamesBlockParser.RegisterRegex(CommonRegexes.QuotedString, (_, nameStr) => {
femaleNames.Add(nameStr);
femaleNamesBlockParser.RegisterRegex(CommonRegexes.QuotedString, (_, quotedNameStr) => {
femaleNames.Add(quotedNameStr.RemQuotes());
});
femaleNamesBlockParser.IgnoreAndLogUnregisteredItems();
femaleNamesBlockParser.ParseStream(reader);
Expand Down

0 comments on commit b958b2b

Please sign in to comment.