Skip to content

Commit b0dca36

Browse files
committed
Add additional tests
1 parent bc76526 commit b0dca36

File tree

2 files changed

+126
-11
lines changed

2 files changed

+126
-11
lines changed

src/SIL.Machine/Corpora/UsfmVersificationMismatchDetector.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public override void Chapter(
208208
string pubNumber
209209
)
210210
{
211-
if (_currentChapter != 0)
211+
if (_currentBook > 0 && Canon.IsCanonical(_currentBook) && _currentChapter > 0)
212212
{
213213
var versificationMismatch = new UsfmVersificationMismatch(
214214
_currentBook,
@@ -234,16 +234,19 @@ string pubNumber
234234
)
235235
{
236236
_currentVerse = state.VerseRef;
237-
var versificationMismatch = new UsfmVersificationMismatch(
238-
_currentBook,
239-
_currentChapter,
240-
_currentVerse.AllVerses().Last().VerseNum,
241-
_currentChapter,
242-
_currentVerse.AllVerses().Last().VerseNum,
243-
_currentVerse
244-
);
245-
if (versificationMismatch.CheckMismatch())
246-
_errors.Add(versificationMismatch);
237+
if (_currentBook > 0 && Canon.IsCanonical(_currentBook) && _currentChapter > 0)
238+
{
239+
var versificationMismatch = new UsfmVersificationMismatch(
240+
_currentBook,
241+
_currentChapter,
242+
_currentVerse.AllVerses().Last().VerseNum,
243+
_currentChapter,
244+
_currentVerse.AllVerses().Last().VerseNum,
245+
_currentVerse
246+
);
247+
if (versificationMismatch.CheckMismatch())
248+
_errors.Add(versificationMismatch);
249+
}
247250
}
248251
}
249252
}

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System.Text;
12
using System.Text.Json;
23
using NUnit.Framework;
4+
using SIL.Scripture;
35

46
namespace SIL.Machine.Corpora;
57

@@ -196,6 +198,99 @@ public void GetUsfmVersificationMismatches_ExtraVerseSegment()
196198
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.ExtraVerseSegment));
197199
}
198200

201+
[Test]
202+
public void GetUsfmVersificationMismatches_MissingVerseSegment()
203+
{
204+
var env = new TestEnvironment(
205+
settings: new MemoryParatextProjectFileHandler.DefaultParatextProjectSettings(
206+
versification: GetCustomVersification(@"*3JN 1:13,a,b")
207+
),
208+
files: new Dictionary<string, string>()
209+
{
210+
{
211+
"653JNTest.SFM",
212+
@"\id 3JN
213+
\c 1
214+
\v 1
215+
\v 2
216+
\v 3
217+
\v 4
218+
\v 5
219+
\v 6
220+
\v 7
221+
\v 8
222+
\v 9
223+
\v 10
224+
\v 11
225+
\v 12
226+
\v 13
227+
\v 14
228+
\v 15
229+
"
230+
}
231+
}
232+
);
233+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
234+
Assert.That(mismatches, Has.Count.EqualTo(1), JsonSerializer.Serialize(mismatches));
235+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.MissingVerseSegment));
236+
}
237+
238+
[Test]
239+
public void GetUsfmVersificationMismatches_IgnoreNonCanonicals()
240+
{
241+
var env = new TestEnvironment(
242+
files: new Dictionary<string, string>()
243+
{
244+
{
245+
"98XXETest.SFM",
246+
@"\id XXE
247+
\c 1
248+
\v 3-2
249+
"
250+
}
251+
}
252+
);
253+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
254+
Assert.That(mismatches, Has.Count.EqualTo(0), JsonSerializer.Serialize(mismatches));
255+
}
256+
257+
[Test]
258+
public void GetUsfmVersificationMismatches_ExtraVerse_ExcludedInCustomVrs()
259+
{
260+
var env = new TestEnvironment(
261+
settings: new MemoryParatextProjectFileHandler.DefaultParatextProjectSettings(
262+
versification: GetCustomVersification(@"-3JN 1:13")
263+
),
264+
files: new Dictionary<string, string>()
265+
{
266+
{
267+
"653JNTest.SFM",
268+
@"\id 3JN
269+
\c 1
270+
\v 1
271+
\v 2
272+
\v 3
273+
\v 4
274+
\v 5
275+
\v 6
276+
\v 7
277+
\v 8
278+
\v 9
279+
\v 10
280+
\v 11
281+
\v 12
282+
\v 13
283+
\v 14
284+
\v 15
285+
"
286+
}
287+
}
288+
);
289+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
290+
Assert.That(mismatches, Has.Count.EqualTo(1), JsonSerializer.Serialize(mismatches));
291+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.ExtraVerse));
292+
}
293+
199294
private class TestEnvironment(ParatextProjectSettings? settings = null, Dictionary<string, string>? files = null)
200295
{
201296
public ParatextProjectVersificationMismatchDetector Detector { get; } =
@@ -206,4 +301,21 @@ public IReadOnlyList<UsfmVersificationMismatch> GetUsfmVersificationMismatches()
206301
return Detector.GetUsfmVersificationMismatches();
207302
}
208303
}
304+
305+
private static ScrVers GetCustomVersification(string customVrsContents, ScrVers? baseVersification = null)
306+
{
307+
baseVersification ??= ScrVers.English;
308+
ScrVers customVersification = baseVersification;
309+
using (var reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(customVrsContents))))
310+
{
311+
customVersification = Versification.Table.Implementation.Load(
312+
reader,
313+
"custom.vrs",
314+
baseVersification,
315+
baseVersification.ToString() + "-" + customVrsContents.GetHashCode()
316+
);
317+
}
318+
Versification.Table.Implementation.RemoveAllUnknownVersifications();
319+
return customVersification;
320+
}
209321
}

0 commit comments

Comments
 (0)