diff --git a/src/SIL.LCModel/Infrastructure/Impl/RepositoryAdditions.cs b/src/SIL.LCModel/Infrastructure/Impl/RepositoryAdditions.cs index 11108333..5356490a 100644 --- a/src/SIL.LCModel/Infrastructure/Impl/RepositoryAdditions.cs +++ b/src/SIL.LCModel/Infrastructure/Impl/RepositoryAdditions.cs @@ -1365,6 +1365,25 @@ public List CollectHomographs(string sForm, IMoMorphType morphType) return CollectHomographs(sForm, 0, GetHomographs(sForm), morphType); } + /// + /// Validate and, if needed, correct homograph numbers for the set + /// participates in. Zeros the entry's HomographNumber if form is blank (so callers + /// don't need to special-case that). + /// Caller should create the unit of work. + /// + /// true if no change was needed, false if anything was renumbered. + public bool CorrectHomographNumbers(ILexEntry entry) + { + var form = entry.HomographFormKey; + if (form == Strings.ksQuestions) + { + if (entry.HomographNumber == 0) return true; + entry.HomographNumber = 0; + return false; + } + return LexDb.CorrectHomographNumbers(CollectHomographs(form, entry.PrimaryMorphType)); + } + /// /// Main method to collect all the homographs of the given form from the given list of entries. /// Set hvo to 0 to collect absolutely every matching homograph. diff --git a/src/SIL.LCModel/RepositoryInterfaceAdditions.cs b/src/SIL.LCModel/RepositoryInterfaceAdditions.cs index 851695c3..a6b7cf8c 100644 --- a/src/SIL.LCModel/RepositoryInterfaceAdditions.cs +++ b/src/SIL.LCModel/RepositoryInterfaceAdditions.cs @@ -426,6 +426,15 @@ public partial interface ILexEntryRepository /// List CollectHomographs(string sForm, IMoMorphType morphType); + /// + /// Validate and, if needed, correct homograph numbers for the set + /// participates in. Zeros the entry's HomographNumber if form is blank (so callers + /// don't need to special-case that). + /// Caller should create the unit of work. + /// + /// true if no change was needed, false if anything was renumbered. + bool CorrectHomographNumbers(ILexEntry entry); + /// /// Maps the specified morph type onto a canonical ordering that should be used in comparing two /// entries to see whether they are homographs. diff --git a/tests/SIL.LCModel.Tests/DomainImpl/LingTests.cs b/tests/SIL.LCModel.Tests/DomainImpl/LingTests.cs index 03711dc3..641f7b5f 100644 --- a/tests/SIL.LCModel.Tests/DomainImpl/LingTests.cs +++ b/tests/SIL.LCModel.Tests/DomainImpl/LingTests.cs @@ -267,6 +267,44 @@ public void HomographValidationWorks() } } + /// + /// Algorithm behaviour is covered by HomographValidationWorks. + /// This just exercises the wrapper. + /// + [Test] + public void CorrectHomographNumbers_RenumbersInvalidSet() + { + const string sLexForm = "repoCorrectHnTest"; + var e1 = MakeEntry(sLexForm); + var e2 = MakeEntry(sLexForm); + var e3 = MakeEntry(sLexForm); + + e1.HomographNumber = 2; + e2.HomographNumber = 2; + e3.HomographNumber = 0; + + var repo = Cache.ServiceLocator.GetInstance(); + Assert.IsFalse(repo.CorrectHomographNumbers(e1), "Invalid set should be renumbered."); + CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, + new[] { e1.HomographNumber, e2.HomographNumber, e3.HomographNumber }); + Assert.IsTrue(repo.CorrectHomographNumbers(e1), "Already-valid set: no change."); + } + + /// + /// Empty-form branch added on top of LexDb.CorrectHomographNumbers. + /// + [Test] + public void CorrectHomographNumbers_EmptyForm_ForcesZero() + { + var entry = MakeEntry(""); + Assert.AreEqual(Strings.ksQuestions, entry.HomographFormKey); + entry.HomographNumber = 7; + + var repo = Cache.ServiceLocator.GetInstance(); + Assert.IsFalse(repo.CorrectHomographNumbers(entry)); + Assert.AreEqual(0, entry.HomographNumber); + } + private ILexEntry MakeEntry(string sLexForm) { var lme = Cache.ServiceLocator.GetInstance().Create();