Skip to content

Commit 1f0cf91

Browse files
committed
Add tests for expected/actual verse ref since there is so much logic in them; fix glaring verseref parsing error
1 parent 1bafc1e commit 1f0cf91

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/SIL.Machine/Corpora/UsfmVersificationErrorDetector.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ public string ExpectedVerseRef
105105

106106
// We do not want to throw an exception here, and the VerseRef constructor can throw
107107
// an exception with certain invalid verse data; use TryParse instead.
108-
if (!VerseRef.TryParse($"{_bookNum} {_expectedChapter}:{_expectedVerse}", out VerseRef defaultVerseRef))
108+
if (
109+
!VerseRef.TryParse(
110+
$"{Canon.BookNumberToId(_bookNum)} {_expectedChapter}:{_expectedVerse}",
111+
out VerseRef defaultVerseRef
112+
)
113+
)
109114
{
110115
return DefaultVerse(_expectedChapter, _expectedVerse);
111116
}
@@ -155,7 +160,12 @@ public string ActualVerseRef
155160
}
156161
else
157162
{
158-
if (VerseRef.TryParse($"{_bookNum} {_actualChapter}:{_actualVerse}", out VerseRef actualVerseRef))
163+
if (
164+
VerseRef.TryParse(
165+
$"{Canon.BookNumberToId(_bookNum)} {_actualChapter}:{_actualVerse}",
166+
out VerseRef actualVerseRef
167+
)
168+
)
159169
{
160170
return actualVerseRef.ToString();
161171
}

tests/SIL.Machine.Tests/Corpora/ParatextProjectVersificationErrorTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace SIL.Machine.Corpora;
77

88
[TestFixture]
9-
public class ParatextProjectQuoteConventionDetectorTests
9+
public class ParatextProjectVersificationErrorDetectorTests
1010
{
1111
[Test]
1212
public void GetUsfmVersificationErrors_NoErrors()
@@ -75,6 +75,8 @@ public void GetUsfmVersificationErrors_MissingVerse()
7575
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
7676
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
7777
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.MissingVerse));
78+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("3JN 1:15"));
79+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 1:14"));
7880
}
7981

8082
[Test]
@@ -93,6 +95,8 @@ public void GetUsfmVersificationErrors_MissingChapter()
9395
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
9496
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
9597
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.MissingChapter));
98+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("3JN 1:15"));
99+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 0:0"));
96100
}
97101

98102
[Test]
@@ -128,6 +132,8 @@ public void GetUsfmVersificationErrors_ExtraVerse()
128132
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
129133
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
130134
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.ExtraVerse));
135+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo(""));
136+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 1:16"));
131137
}
132138

133139
[Test]
@@ -161,6 +167,8 @@ public void GetUsfmVersificationErrors_InvalidVerse()
161167
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
162168
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
163169
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.InvalidVerseRange));
170+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("3JN 1:12-13"));
171+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 1:13-12"));
164172
}
165173

166174
[Test]
@@ -196,6 +204,8 @@ public void GetUsfmVersificationErrors_ExtraVerseSegment()
196204
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
197205
Assert.That(errors, Has.Count.EqualTo(2), JsonSerializer.Serialize(errors));
198206
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.ExtraVerseSegment));
207+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("3JN 1:14"));
208+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 1:14a"));
199209
}
200210

201211
[Test]
@@ -233,6 +243,8 @@ public void GetUsfmVersificationErrors_MissingVerseSegment()
233243
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
234244
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
235245
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.MissingVerseSegment));
246+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("3JN 1:13a"));
247+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 1:13"));
236248
}
237249

238250
[Test]
@@ -289,6 +301,8 @@ public void GetUsfmVersificationErrors_ExtraVerse_ExcludedInCustomVrs()
289301
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
290302
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
291303
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.ExtraVerse));
304+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo(""));
305+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("3JN 1:13"));
292306
}
293307

294308
[Test]
@@ -341,6 +355,8 @@ public void GetUsfmVersificationErrors_MultipleBooks()
341355
IReadOnlyList<UsfmVersificationError> errors = env.GetUsfmVersificationErrors();
342356
Assert.That(errors, Has.Count.EqualTo(1), JsonSerializer.Serialize(errors));
343357
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.MissingVerse));
358+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("2JN 1:13"));
359+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("2JN 1:12"));
344360
}
345361

346362
[Test]
@@ -375,6 +391,10 @@ public void GetUsfmVersificationErrors_MultipleChapters()
375391
Assert.That(errors, Has.Count.EqualTo(2), JsonSerializer.Serialize(errors));
376392
Assert.That(errors[0].Type, Is.EqualTo(UsfmVersificationErrorType.MissingVerse));
377393
Assert.That(errors[1].Type, Is.EqualTo(UsfmVersificationErrorType.ExtraVerse));
394+
Assert.That(errors[0].ExpectedVerseRef, Is.EqualTo("2JN 1:13"));
395+
Assert.That(errors[0].ActualVerseRef, Is.EqualTo("2JN 1:12"));
396+
Assert.That(errors[1].ExpectedVerseRef, Is.EqualTo(""));
397+
Assert.That(errors[1].ActualVerseRef, Is.EqualTo("2JN 2:1"));
378398
}
379399

380400
private class TestEnvironment(ParatextProjectSettings? settings = null, Dictionary<string, string>? files = null)

0 commit comments

Comments
 (0)