Skip to content

Commit bc76526

Browse files
committed
Begin adding tests
1 parent bb91859 commit bc76526

File tree

4 files changed

+250
-26
lines changed

4 files changed

+250
-26
lines changed

src/SIL.Machine/Corpora/ParatextProjectVersificationMismatchDetector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class ParatextProjectVersificationMismatchDetector
1212

1313
protected ParatextProjectVersificationMismatchDetector(IParatextProjectFileHandler paratextProjectFileHandler)
1414
{
15-
_settings = _paratextProjectFileHandler.GetSettings();
15+
_settings = paratextProjectFileHandler.GetSettings();
1616
_paratextProjectFileHandler = paratextProjectFileHandler;
1717
}
1818

src/SIL.Machine/Corpora/UsfmVersificationMismatchDetector.cs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public enum UsfmVersificationMismatchType
1010
MissingChapter,
1111
MissingVerse,
1212
ExtraVerse,
13-
InvalidVerseRange,
13+
InvalidVerseRange, //TODO This would be a nice thing to detect, but does it really fit into the UsfmVersificationMismatch category?
1414
MissingVerseSegment,
1515
ExtraVerseSegment
1616
}
@@ -165,7 +165,7 @@ public UsfmVersificationMismatchDetector(ScrVers versification)
165165
public bool HasError => _errors.Count > 0;
166166
public IReadOnlyList<UsfmVersificationMismatch> Errors => _errors;
167167

168-
public override void StartBook(UsfmParserState state, string marker, string code)
168+
public override void EndUsfm(UsfmParserState state)
169169
{
170170
if (_currentBook > 0 && Canon.IsCanonical(_currentBook))
171171
{
@@ -179,33 +179,27 @@ public override void StartBook(UsfmParserState state, string marker, string code
179179
if (versificationMismatch.CheckMismatch())
180180
_errors.Add(versificationMismatch);
181181
}
182+
}
182183

184+
public override void StartBook(UsfmParserState state, string marker, string code)
185+
{
186+
if (_currentBook > 0 && Canon.IsCanonical(_currentBook))
187+
{
188+
var versificationMismatch = new UsfmVersificationMismatch(
189+
_currentBook,
190+
_versification.GetLastChapter(_currentBook),
191+
_versification.GetLastVerse(_currentBook, _versification.GetLastChapter(_currentBook)),
192+
_currentChapter,
193+
_currentVerse.AllVerses().Last().VerseNum
194+
);
195+
if (versificationMismatch.CheckMismatch())
196+
_errors.Add(versificationMismatch);
197+
}
183198
_currentBook = state.VerseRef.BookNum;
184199
_currentChapter = 0;
185200
_currentVerse = new VerseRef();
186201
}
187202

188-
public override void Verse(
189-
UsfmParserState state,
190-
string number,
191-
string marker,
192-
string altNumber,
193-
string pubNumber
194-
)
195-
{
196-
_currentVerse = state.VerseRef;
197-
var versificationMismatch = new UsfmVersificationMismatch(
198-
_currentBook,
199-
_currentChapter,
200-
_currentVerse.AllVerses().Last().VerseNum,
201-
_currentChapter,
202-
_currentVerse.AllVerses().Last().VerseNum,
203-
_currentVerse
204-
);
205-
if (versificationMismatch.CheckMismatch())
206-
_errors.Add(versificationMismatch);
207-
}
208-
209203
public override void Chapter(
210204
UsfmParserState state,
211205
string number,
@@ -230,5 +224,26 @@ string pubNumber
230224
_currentChapter = state.VerseRef.ChapterNum;
231225
_currentVerse = new VerseRef();
232226
}
227+
228+
public override void Verse(
229+
UsfmParserState state,
230+
string number,
231+
string marker,
232+
string altNumber,
233+
string pubNumber
234+
)
235+
{
236+
_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);
247+
}
233248
}
234249
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace SIL.Machine.Corpora;
22

33
public class MemoryParatextProjectVersificationMismatchDetector(
4-
IDictionary<string, string> files,
5-
ParatextProjectSettings settings
4+
IDictionary<string, string>? files = null,
5+
ParatextProjectSettings? settings = null
66
) : ParatextProjectVersificationMismatchDetector(new MemoryParatextProjectFileHandler(files, settings)) { }
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
using System.Text.Json;
2+
using NUnit.Framework;
3+
4+
namespace SIL.Machine.Corpora;
5+
6+
[TestFixture]
7+
public class ParatextProjectQuoteConventionDetectorTests
8+
{
9+
[Test]
10+
public void GetUsfmVersificationMismatches_NoMismatches()
11+
{
12+
var env = new TestEnvironment(
13+
files: new Dictionary<string, string>()
14+
{
15+
{
16+
"653JNTest.SFM",
17+
@"\id 3JN
18+
\c 1
19+
\v 1
20+
\v 2
21+
\v 3
22+
\v 4
23+
\v 5
24+
\v 6
25+
\v 7
26+
\v 8
27+
\v 9
28+
\v 10
29+
\v 11
30+
\v 12
31+
\v 13
32+
\v 14
33+
\v 15
34+
"
35+
}
36+
}
37+
);
38+
Assert.That(
39+
env.GetUsfmVersificationMismatches(),
40+
Has.Count.EqualTo(0),
41+
JsonSerializer.Serialize(env.GetUsfmVersificationMismatches())
42+
);
43+
}
44+
45+
[Test]
46+
public void GetUsfmVersificationMismatches_MissingVerse()
47+
{
48+
var env = new TestEnvironment(
49+
files: new Dictionary<string, string>()
50+
{
51+
{
52+
"653JNTest.SFM",
53+
@"\id 3JN
54+
\c 1
55+
\v 1
56+
\v 2
57+
\v 3
58+
\v 4
59+
\v 5
60+
\v 6
61+
\v 7
62+
\v 8
63+
\v 9
64+
\v 10
65+
\v 11
66+
\v 12
67+
\v 13
68+
\v 14
69+
"
70+
}
71+
}
72+
);
73+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
74+
Assert.That(mismatches, Has.Count.EqualTo(1), JsonSerializer.Serialize(mismatches));
75+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.MissingVerse));
76+
}
77+
78+
[Test]
79+
public void GetUsfmVersificationMismatches_MissingChapter()
80+
{
81+
var env = new TestEnvironment(
82+
files: new Dictionary<string, string>()
83+
{
84+
{
85+
"653JNTest.SFM",
86+
@"\id 3JN
87+
"
88+
}
89+
}
90+
);
91+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
92+
Assert.That(mismatches, Has.Count.EqualTo(1), JsonSerializer.Serialize(mismatches));
93+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.MissingChapter));
94+
}
95+
96+
[Test]
97+
public void GetUsfmVersificationMismatches_ExtraVerse()
98+
{
99+
var env = new TestEnvironment(
100+
files: new Dictionary<string, string>()
101+
{
102+
{
103+
"653JNTest.SFM",
104+
@"\id 3JN
105+
\c 1
106+
\v 1
107+
\v 2
108+
\v 3
109+
\v 4
110+
\v 5
111+
\v 6
112+
\v 7
113+
\v 8
114+
\v 9
115+
\v 10
116+
\v 11
117+
\v 12
118+
\v 13
119+
\v 14
120+
\v 15
121+
\v 16
122+
"
123+
}
124+
}
125+
);
126+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
127+
Assert.That(mismatches, Has.Count.EqualTo(1), JsonSerializer.Serialize(mismatches));
128+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.ExtraVerse));
129+
}
130+
131+
[Test]
132+
public void GetUsfmVersificationMismatches_InvalidVerse()
133+
{
134+
var env = new TestEnvironment(
135+
files: new Dictionary<string, string>()
136+
{
137+
{
138+
"653JNTest.SFM",
139+
@"\id 3JN
140+
\c 1
141+
\v 1
142+
\v 2
143+
\v 3
144+
\v 4
145+
\v 5
146+
\v 6
147+
\v 7
148+
\v 8
149+
\v 9
150+
\v 10
151+
\v 11
152+
\v 13-12
153+
\v 14
154+
\v 15
155+
"
156+
}
157+
}
158+
);
159+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
160+
Assert.That(mismatches, Has.Count.EqualTo(1), JsonSerializer.Serialize(mismatches));
161+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.InvalidVerseRange));
162+
}
163+
164+
[Test]
165+
public void GetUsfmVersificationMismatches_ExtraVerseSegment()
166+
{
167+
var env = new TestEnvironment(
168+
files: new Dictionary<string, string>()
169+
{
170+
{
171+
"653JNTest.SFM",
172+
@"\id 3JN
173+
\c 1
174+
\v 1
175+
\v 2
176+
\v 3
177+
\v 4
178+
\v 5
179+
\v 6
180+
\v 7
181+
\v 8
182+
\v 9
183+
\v 10
184+
\v 11
185+
\v 12
186+
\v 13
187+
\v 14a
188+
\v 14b
189+
\v 15
190+
"
191+
}
192+
}
193+
);
194+
IReadOnlyList<UsfmVersificationMismatch> mismatches = env.GetUsfmVersificationMismatches();
195+
Assert.That(mismatches, Has.Count.EqualTo(2), JsonSerializer.Serialize(mismatches));
196+
Assert.That(mismatches[0].Type, Is.EqualTo(UsfmVersificationMismatchType.ExtraVerseSegment));
197+
}
198+
199+
private class TestEnvironment(ParatextProjectSettings? settings = null, Dictionary<string, string>? files = null)
200+
{
201+
public ParatextProjectVersificationMismatchDetector Detector { get; } =
202+
new MemoryParatextProjectVersificationMismatchDetector(files, settings);
203+
204+
public IReadOnlyList<UsfmVersificationMismatch> GetUsfmVersificationMismatches()
205+
{
206+
return Detector.GetUsfmVersificationMismatches();
207+
}
208+
}
209+
}

0 commit comments

Comments
 (0)