Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koliva8245 committed Sep 8, 2024
1 parent e005c19 commit de9dc60
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Heroes.XmlData/StormData/StormStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Heroes.XmlData.StormData;
internal partial class StormStorage : IStormStorage
{
private readonly StormPath _rootFilePath;

private int _loadedMapMods;

public StormStorage(bool hasRootDefaults = true)
Expand Down Expand Up @@ -72,7 +71,7 @@ public void AddGameString(StormModType stormModType, string id, GameStringText g
{
Span<Range> ranges = stackalloc Range[2];

gamestring.Split(ranges, '=', StringSplitOptions.RemoveEmptyEntries);
gamestring.Split(ranges, '=', StringSplitOptions.None);

if (gamestring[ranges[0]].IsEmpty || gamestring[ranges[0]].IsWhiteSpace())
return null;
Expand All @@ -95,7 +94,7 @@ public void AddAssetText(StormModType stormModType, string id, AssetText assetTe
{
Span<Range> ranges = stackalloc Range[2];

asset.Split(ranges, '=', StringSplitOptions.RemoveEmptyEntries);
asset.Split(ranges, '=', StringSplitOptions.None);

if (asset[ranges[0]].IsEmpty || asset[ranges[0]].IsWhiteSpace())
return null;
Expand Down
174 changes: 174 additions & 0 deletions Tests/Heroes.XmlData.Tests/StormData/StormStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,126 @@ public void GetStormAssetFile_NormalAndMapCache_MergeFromNormalAndMap()
stormAssetFile.StormPaths[^1].Path.Should().Be("map");
}

[TestMethod]
public void GetGameStringWithId_HasBothIdAndValue_ReturnsIdAndValue()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, GameStringText GameStringText)? gamestring = stormStorage.GetGameStringWithId("id=value", TestHelpers.GetStormPath("normal"));

// assert
gamestring.HasValue.Should().BeTrue();
gamestring!.Value.Id.Should().Be("id");
gamestring.Value.GameStringText.Value.Should().Be("value");
}

[TestMethod]
public void GetGameStringWithId_HasNoDelimeter_ReturnsIdAndNoValue()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, GameStringText GameStringText)? gamestring = stormStorage.GetGameStringWithId("idvalue", TestHelpers.GetStormPath("normal"));

// assert
gamestring.HasValue.Should().BeTrue();
gamestring!.Value.Id.Should().Be("idvalue");
gamestring.Value.GameStringText.Value.Should().BeEmpty();
}

[TestMethod]
public void GetGameStringWithId_NoIdAndHasValue_ReturnsNull()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, GameStringText GameStringText)? gamestring = stormStorage.GetGameStringWithId("=value", TestHelpers.GetStormPath("normal"));
(string Id, GameStringText GameStringText)? gamestringSpace = stormStorage.GetGameStringWithId(" =value", TestHelpers.GetStormPath("normal"));

// assert
gamestring.HasValue.Should().BeFalse();
gamestringSpace.Should().BeEquivalentTo(gamestring);
}

[TestMethod]
public void GetGameStringWithId_NoValueAndHasId_ReturnsIdAndValueEmpty()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, GameStringText GameStringText)? gamestring = stormStorage.GetGameStringWithId("id=", TestHelpers.GetStormPath("normal"));

// assert
gamestring.HasValue.Should().BeTrue();
gamestring!.Value.Id.Should().Be("id");
gamestring.Value.GameStringText.Value.Should().BeEmpty();
}

[TestMethod]
public void GetAssetWithId_HasBothIdAndValue_ReturnsIdAndValue()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, AssetText AssetText)? assetText = stormStorage.GetAssetWithId("id=value", TestHelpers.GetStormPath("normal"));

// assert
assetText.HasValue.Should().BeTrue();
assetText!.Value.Id.Should().Be("id");
assetText.Value.AssetText.Value.Should().Be("value");
}

[TestMethod]
public void GetAssetWithId_HasNoDelimeter_ReturnsIdAndNoValue()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, AssetText AssetText)? assetText = stormStorage.GetAssetWithId("idvalue", TestHelpers.GetStormPath("normal"));

// assert
assetText.HasValue.Should().BeTrue();
assetText!.Value.Id.Should().Be("idvalue");
assetText.Value.AssetText.Value.Should().BeEmpty();
}

[TestMethod]
public void GetAssetWithId_NoIdAndHasValue_ReturnsNull()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, AssetText AssetText)? assetText = stormStorage.GetAssetWithId("=value", TestHelpers.GetStormPath("normal"));
(string Id, AssetText AssetText)? assetTextSpace = stormStorage.GetAssetWithId(" =value", TestHelpers.GetStormPath("normal"));

// assert
assetText.HasValue.Should().BeFalse();
assetTextSpace.Should().BeEquivalentTo(assetText);
}

[TestMethod]
public void GetAssetWithId_NoValueAndHasId_ReturnsIdAndValueEmpty()
{
// arrange
StormStorage stormStorage = new(false);

// act
(string Id, AssetText AssetText)? assetText = stormStorage.GetAssetWithId("id=", TestHelpers.GetStormPath("normal"));

// assert
assetText.HasValue.Should().BeTrue();
assetText!.Value.Id.Should().Be("id");
assetText.Value.AssetText.Value.Should().BeEmpty();
}

[TestMethod]
[DataRow("""<const id="$Var1" value="7" />""", "7")]
[DataRow("""<const id="$Var1" value="" />""", "")]
Expand Down Expand Up @@ -2201,6 +2321,60 @@ public void GetValueFromConstElement_HasExpressionAndIs1_ReturnsValueAsString()
result.Should().Be("9.125");
}

[TestMethod]
public void AddStormLayoutFilePath_AddingSingleLayout_AddsOneLayout()
{
// arrange
StormStorage stormStorage = new(false);

// act
stormStorage.AddStormLayoutFilePath(StormModType.Normal, "this/is/a/path/file.txt", TestHelpers.GetStormPath("normal"));

// assert
stormStorage.StormCache.UiStormPathsByRelativeUiPath.Should().ContainSingle();
}

[TestMethod]
public void AddStormLayoutFilePath_AddingDuplicateSingleLayout_StillOnlyOneLayout()
{
// arrange
StormStorage stormStorage = new(false);
stormStorage.AddStormLayoutFilePath(StormModType.Normal, "this/is/a/path/file.txt", TestHelpers.GetStormPath("normal"));

// act
stormStorage.AddStormLayoutFilePath(StormModType.Normal, "this/is/a/path/file.txt", TestHelpers.GetStormPath("normal"));

// assert
stormStorage.StormCache.UiStormPathsByRelativeUiPath.Should().ContainSingle();
}

[TestMethod]
public void AddAssetFilePath_AddingSingleAssetFilePath_AddsOneAssetFilePath()
{
// arrange
StormStorage stormStorage = new(false);

// act
stormStorage.AddAssetFilePath(StormModType.Normal, "this/is/a/path/file.txt", TestHelpers.GetStormPath("normal"));

// assert
stormStorage.StormCache.AssetFilesByRelativeAssetsPath.Should().ContainSingle();
}

[TestMethod]
public void AddAssetFilePath_AddingDuplicateAssetFilePath_StillOnlyOneAssetFilePath()
{
// arrange
StormStorage stormStorage = new(false);
stormStorage.AddAssetFilePath(StormModType.Normal, "this/is/a/path/file.txt", TestHelpers.GetStormPath("normal"));

// act
stormStorage.AddAssetFilePath(StormModType.Normal, "this/is/a/path/file.txt", TestHelpers.GetStormPath("normal"));

// assert
stormStorage.StormCache.AssetFilesByRelativeAssetsPath.Should().ContainSingle();
}

[TestMethod]
public void GetValueFromConstElement_HasExpressionAndIs0_ReturnsValueAsString()
{
Expand Down

0 comments on commit de9dc60

Please sign in to comment.