From 878e9c1fa705e090e9ecdf4b2d37a0511496403f Mon Sep 17 00:00:00 2001 From: Reuben Swartz Date: Wed, 11 Sep 2024 15:57:44 -0400 Subject: [PATCH] Fix a case where an exception is possible in GetHierarchyToMultipleMarkers (#144) --- USFMToolsSharp/Models/Markers/Marker.cs | 9 +++++++++ USFMToolsSharpTest/USFMParserTest.cs | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/USFMToolsSharp/Models/Markers/Marker.cs b/USFMToolsSharp/Models/Markers/Marker.cs index f9b7774..1f3109a 100644 --- a/USFMToolsSharp/Models/Markers/Marker.cs +++ b/USFMToolsSharp/Models/Markers/Marker.cs @@ -114,6 +114,15 @@ public List GetHierarchyToMarker(Marker target) /// In the case that the marker doesn't exist in the tree the dictionary will contain an empty list for that marker public Dictionary> GetHierachyToMultipleMarkers(List targets) { + if (targets.Count == 0) + { + return new Dictionary>(); + } + if (Contents.Count == 0) + { + return targets.ToDictionary(i => i, i => new List()); + } + Dictionary> output = new Dictionary>(targets.Count); var parents = new Stack<(Marker marker, bool isLastInParent)>(); int childMarkerContentsCount; diff --git a/USFMToolsSharpTest/USFMParserTest.cs b/USFMToolsSharpTest/USFMParserTest.cs index 39a42d6..bf8c48d 100644 --- a/USFMToolsSharpTest/USFMParserTest.cs +++ b/USFMToolsSharpTest/USFMParserTest.cs @@ -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()); + Assert.AreEqual(0, result.Count); + result = document.GetHierachyToMultipleMarkers(new List() {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() { textblock, secondBlock, nonExistant, textInFootnote }); + result = document.GetHierachyToMultipleMarkers(new List() { textblock, secondBlock, nonExistant, textInFootnote }); Assert.AreEqual(document, result[textblock][0]); Assert.AreEqual(chapter, result[textblock][1]); Assert.AreEqual(verse, result[textblock][2]);