Skip to content

Commit

Permalink
Fix a case where an exception is possible in GetHierarchyToMultipleMa…
Browse files Browse the repository at this point in the history
…rkers (#144)
  • Loading branch information
rbnswartz authored Sep 11, 2024
1 parent ba92a81 commit 878e9c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions USFMToolsSharp/Models/Markers/Marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ public List<Marker> GetHierarchyToMarker(Marker target)
/// <remarks>In the case that the marker doesn't exist in the tree the dictionary will contain an empty list for that marker</remarks>
public Dictionary<Marker, List<Marker>> GetHierachyToMultipleMarkers(List<Marker> targets)
{
if (targets.Count == 0)
{
return new Dictionary<Marker, List<Marker>>();
}
if (Contents.Count == 0)
{
return targets.ToDictionary(i => i, i => new List<Marker>());
}

Dictionary<Marker, List<Marker>> output = new Dictionary<Marker, List<Marker>>(targets.Count);
var parents = new Stack<(Marker marker, bool isLastInParent)>();
int childMarkerContentsCount;
Expand Down
7 changes: 6 additions & 1 deletion USFMToolsSharpTest/USFMParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,13 @@ public void TestGetHierarchyToMultipleMarkers()
var textInFootnote = new TextBlock("Text in footnote");
var secondBlock = new TextBlock("Hello again");
var nonExistant = new VMarker();
var result = document.GetHierachyToMultipleMarkers(new List<Marker>());
Assert.AreEqual(0, result.Count);
result = document.GetHierachyToMultipleMarkers(new List<Marker>() {footnote});
Assert.AreEqual(1, result.Count);
Assert.AreEqual(0, result[footnote].Count);
document.InsertMultiple(new Marker[] { chapter, verse, textblock, footnote, footnoteText, textInFootnote, footnoteEndMarker, secondBlock });
var result = document.GetHierachyToMultipleMarkers(new List<Marker>() { textblock, secondBlock, nonExistant, textInFootnote });
result = document.GetHierachyToMultipleMarkers(new List<Marker>() { textblock, secondBlock, nonExistant, textInFootnote });
Assert.AreEqual(document, result[textblock][0]);
Assert.AreEqual(chapter, result[textblock][1]);
Assert.AreEqual(verse, result[textblock][2]);
Expand Down

0 comments on commit 878e9c1

Please sign in to comment.