Skip to content

Commit b1e7ff6

Browse files
committed
Expose CorrectHomographNumbers publicly on ILexEntry
1 parent 66da667 commit b1e7ff6

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/SIL.LCModel/DomainImpl/OverridesLing_Lex.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,20 @@ public List<ILexEntry> CollectHomographs(string sForm, int hvoExclude)
20442044
return result;
20452045
}
20462046

2047+
/// <summary>See <see cref="ILexEntry.CorrectHomographNumbers"/>.</summary>
2048+
public bool CorrectHomographNumbers()
2049+
{
2050+
if (HomographFormKey == Strings.ksQuestions)
2051+
{
2052+
if (HomographNumber == 0) return true;
2053+
HomographNumber = 0;
2054+
return false;
2055+
}
2056+
var homographs = Services.GetInstance<ILexEntryRepository>()
2057+
.CollectHomographs(HomographFormKey, PrimaryMorphType);
2058+
return LexDb.CorrectHomographNumbers(homographs);
2059+
}
2060+
20472061
partial void LexemeFormOASideEffects(IMoForm oldObjValue, IMoForm newObjValue)
20482062
{
20492063
string oldVal = oldObjValue == null ? "" : oldObjValue.Form.VernacularDefaultWritingSystem.Text;

src/SIL.LCModel/InterfaceAdditions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,14 @@ string HomographFormKey
12881288
get;
12891289
}
12901290

1291+
/// <summary>
1292+
/// Validate and, if needed, correct homograph numbers for this entry's homograph set.
1293+
/// Caller should create the unit of work.
1294+
/// Primarily wraps <see cref="ILexEntry.CorrectHomographNumbers"/>.
1295+
/// </summary>
1296+
/// <returns>true if homographs were already valid, false if they had to be renumbered.</returns>
1297+
bool CorrectHomographNumbers();
1298+
12911299
/// <summary/>
12921300
ITsString HeadWord
12931301
{

tests/SIL.LCModel.Tests/DomainImpl/LingTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,42 @@ public void HomographValidationWorks()
267267
}
268268
}
269269

270+
/// <summary>
271+
/// Exercises the entry-level wrapper
272+
/// (Algorithm behaviour is covered by HomographValidationWorks.)
273+
/// </summary>
274+
[Test]
275+
public void CorrectHomographNumbers_OnEntry_RenumbersInvalidSet()
276+
{
277+
const string sLexForm = "entryCorrectHnTest";
278+
var e1 = MakeEntry(sLexForm);
279+
var e2 = MakeEntry(sLexForm);
280+
var e3 = MakeEntry(sLexForm);
281+
282+
e1.HomographNumber = 2;
283+
e2.HomographNumber = 2;
284+
e3.HomographNumber = 0;
285+
286+
Assert.IsFalse(e1.CorrectHomographNumbers(), "Invalid set should be renumbered.");
287+
CollectionAssert.AreEquivalent(new[] { 1, 2, 3 },
288+
new[] { e1.HomographNumber, e2.HomographNumber, e3.HomographNumber });
289+
Assert.IsTrue(e1.CorrectHomographNumbers(), "Already-valid set: no change.");
290+
}
291+
292+
/// <summary>
293+
/// Empty-form branch added on top of LexDb.CorrectHomographNumbers.
294+
/// </summary>
295+
[Test]
296+
public void CorrectHomographNumbers_OnEntryWithEmptyForm_ForcesZero()
297+
{
298+
var entry = MakeEntry("");
299+
Assert.AreEqual(Strings.ksQuestions, entry.HomographFormKey);
300+
entry.HomographNumber = 7;
301+
302+
Assert.IsFalse(entry.CorrectHomographNumbers());
303+
Assert.AreEqual(0, entry.HomographNumber);
304+
}
305+
270306
private ILexEntry MakeEntry(string sLexForm)
271307
{
272308
var lme = Cache.ServiceLocator.GetInstance<ILexEntryFactory>().Create();

0 commit comments

Comments
 (0)