From 8b7cc17084a085771790ac5023462de311a0e73c Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Fri, 20 Nov 2026 13:19:28 +0700 Subject: [PATCH 1/3] Add round-trip test harness for project split and unify Split and unify had only argument-check tests; round trips were checked by hand with an external app. FwdataBuilder and SplitUnifyHarness make a test for a new edge case a few lines: build a small .fwdata, split, optionally edit the split files, unify, and compare objects by guid. Co-Authored-By: Claude Opus 5.5 --- .../Infrastructure/RoundTrip/FwdataAssert.cs | 89 +++++++++ .../Infrastructure/RoundTrip/FwdataBuilder.cs | 178 ++++++++++++++++++ .../RoundTrip/SplitUnifyHarness.cs | 166 ++++++++++++++++ .../RoundTrip/SplitUnifyRoundTripTests.cs | 113 +++++++++++ 4 files changed, 546 insertions(+) create mode 100644 src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs create mode 100644 src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs create mode 100644 src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs create mode 100644 src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs new file mode 100644 index 00000000..046c670e --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs @@ -0,0 +1,89 @@ +// Copyright (c) 2026 SIL International +// This software is licensed under the MIT License (http://opensource.org/licenses/MIT) + +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using LibFLExBridgeChorusPlugin.Infrastructure; +using NUnit.Framework; + +namespace LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip +{ + /// + /// Semantic comparisons of two .fwdata documents. Objects are matched by guid, so the order of + /// <rt> elements, the order of properties within an object, and whitespace do not matter. + /// + internal static class FwdataAssert + { + /// Both documents hold the same objects: same guids, classes and owners. + internal static void SameObjects(XDocument expected, XDocument actual) + { + Assert.That(ObjectSummaries(actual), Is.EqualTo(ObjectSummaries(expected)), "objects (guid, class, owner)"); + } + + /// Both documents declare the same custom fields with the same attributes. + internal static void SameCustomFields(XDocument expected, XDocument actual) + { + Assert.That(CustomFieldSummaries(actual), Is.EqualTo(CustomFieldSummaries(expected)), "custom fields"); + } + + /// + /// Both documents hold the same objects, each with the same properties and property contents. + /// + internal static void SameObjectContents(XDocument expected, XDocument actual) + { + SameObjects(expected, actual); + var actualObjects = SplitUnifyHarness.ObjectsByGuid(actual); + foreach (var expectedObject in SplitUnifyHarness.ObjectsByGuid(expected)) + { + Assert.That(Normalize(actualObjects[expectedObject.Key]).ToString(), + Is.EqualTo(Normalize(expectedObject.Value).ToString()), + "contents of " + expectedObject.Key); + } + } + + private static List ObjectSummaries(XDocument fwdata) + { + return SplitUnifyHarness.ObjectsByGuid(fwdata) + .Select(pair => string.Format("{0} {1} owner={2}", + pair.Key, + pair.Value.Attribute(FlexBridgeConstants.Class).Value, + ((string)pair.Value.Attribute(FlexBridgeConstants.OwnerGuid) ?? "none").ToLowerInvariant())) + .OrderBy(summary => summary) + .ToList(); + } + + private static List CustomFieldSummaries(XDocument fwdata) + { + var additionalFields = fwdata.Root.Element(FlexBridgeConstants.AdditionalFieldsTag); + if (additionalFields == null) + return new List(); + return additionalFields.Elements(FlexBridgeConstants.CustomField) + .Select(cf => string.Join(" ", cf.Attributes() + .OrderBy(attr => attr.Name.LocalName) + .Select(attr => attr.Name.LocalName + "=" + attr.Value))) + .OrderBy(summary => summary) + .ToList(); + } + + /// A copy of an element with attributes and child elements sorted and whitespace-only text dropped. + private static XElement Normalize(XElement element) + { + var copy = new XElement(element.Name, + element.Attributes().OrderBy(attr => attr.Name.LocalName).Select(attr => new XAttribute(attr))); + if (element.HasElements) + { + // Owning and reference sequences keep their order: only the order of an object's properties is free. + var children = element.Name.LocalName == FlexBridgeConstants.RtTag + ? element.Elements().OrderBy(child => child.Name.LocalName).ThenBy(child => (string)child.Attribute(FlexBridgeConstants.Name)) + : element.Elements(); + copy.Add(children.Select(Normalize)); + } + else if (!string.IsNullOrWhiteSpace(element.Value)) + { + copy.Add(new XText(element.Value)); + } + return copy; + } + } +} diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs new file mode 100644 index 00000000..8ae6974e --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs @@ -0,0 +1,178 @@ +// Copyright (c) 2026 SIL International +// This software is licensed under the MIT License (http://opensource.org/licenses/MIT) + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using LibFLExBridgeChorusPlugin.Infrastructure; + +namespace LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip +{ + /// + /// Builds a small .fwdata document that the splitter and unifier accept. + /// + /// + /// A new builder already holds the stub objects every project needs: a LangProject that owns a LexDb and + /// the other objects listed in . Add what a test cares about: + /// + /// var fwdata = new FwdataBuilder() + /// .AddCustomField("LexEntry", "Rank", "Integer") + /// .AddUnownedObject("LexEntry", EntryGuid, new XElement("Custom", new XAttribute("name", "Rank"), new XAttribute("val", "3"))) + /// .AddOwnedObject(EntryGuid, "Senses", "LexSense", SenseGuid); + /// + /// Property content is passed as s (or anything else + /// takes), so any property the model knows can be added. Use for an object that + /// needs full control. + /// + internal sealed class FwdataBuilder + { + internal const string LangProjectGuid = "c1ed46ba-e382-11de-8a39-0800200c9a66"; + + /// + /// The objects LangProject must own for the splitter and unifier to round trip a project: + /// (LangProject property, class of the owned object). LangProject must also have an AnalyzingAgents element. + /// + /// + /// Without LexDb, MorphologicalData, MsFeatureSystem, PhFeatureSystem, PhonologicalData or AnalyzingAgents + /// the split throws. Every real project has those. Without GenreList, TextMarkupTags, TranslationTags or + /// ResearchNotebook the split succeeds but the unify throws, because the unifier expects a list file or notebook that the + /// splitter writes only when the object exists. + /// + internal static readonly IReadOnlyList> RequiredLangProjectOwnedObjects = new[] + { + new KeyValuePair("GenreList", "CmPossibilityList"), + new KeyValuePair("LexDb", "LexDb"), + new KeyValuePair("MorphologicalData", "MoMorphData"), + new KeyValuePair("MsFeatureSystem", "FsFeatureSystem"), + new KeyValuePair("PhFeatureSystem", "FsFeatureSystem"), + new KeyValuePair("PhonologicalData", "PhPhonData"), + new KeyValuePair("ResearchNotebook", "RnResearchNbk"), + new KeyValuePair("TextMarkupTags", "CmPossibilityList"), + new KeyValuePair("TranslationTags", "CmPossibilityList") + }; + + private readonly List _customFields = new List(); + // The elements, in the order added. + private readonly List _objects = new List(); + private readonly string _modelVersion; + + internal FwdataBuilder() + : this(MetadataCache.MaximumModelVersion) + { + } + + internal FwdataBuilder(int modelVersion) + { + _modelVersion = modelVersion.ToString(); + AddRt(new XElement(FlexBridgeConstants.RtTag, + new XAttribute(FlexBridgeConstants.Class, FlexBridgeConstants.LangProject), + new XAttribute(FlexBridgeConstants.GuidStr, LangProjectGuid), + new XElement(FlexBridgeConstants.AnalyzingAgents))); + // Fixed guids keep the split files (for example, which Lexicon_NN.lexdb bucket a guid lands in) stable from run to run. + var i = 0; + foreach (var owned in RequiredLangProjectOwnedObjects) + AddOwnedObject(LangProjectGuid, owned.Key, owned.Value, string.Format("00000000-0000-0000-0000-{0:x12}", ++i)); + } + + /// The guid of the stub LexDb that LangProject owns. + internal string LexDbGuid + { + get { return OwnedGuid(LangProjectGuid, FlexBridgeConstants.LexDb); } + } + + /// + /// Declare a custom field in the AdditionalFields element. + /// is the fwdata type name, such as "Integer", "String", "MultiUnicode" or "OC". + /// + internal FwdataBuilder AddCustomField(string className, string name, string type, params object[] extraAttributes) + { + _customFields.Add(new XElement(FlexBridgeConstants.CustomField, + new XAttribute(FlexBridgeConstants.Name, name), + new XAttribute(FlexBridgeConstants.Class, className), + new XAttribute("type", type), + extraAttributes)); + return this; + } + + /// Add an object with no owner, such as a LexEntry. + internal FwdataBuilder AddUnownedObject(string className, string guid, params object[] content) + { + return AddRt(NewRt(className, guid, content)); + } + + /// + /// Add an object owned by (already added) in its . + /// The owner gets an <objsur t='o'/> in that property, after any it already has. + /// + internal FwdataBuilder AddOwnedObject(string ownerGuid, string owningProperty, string className, string guid, params object[] content) + { + var owner = GetRt(ownerGuid); + var property = owner.Element(owningProperty); + if (property == null) + { + property = new XElement(owningProperty); + owner.Add(property); + } + property.Add(new XElement(FlexBridgeConstants.Objsur, + new XAttribute(FlexBridgeConstants.GuidStr, guid), + new XAttribute("t", "o"))); + + var rt = NewRt(className, guid, content); + rt.Add(new XAttribute(FlexBridgeConstants.OwnerGuid, ownerGuid)); + return AddRt(rt); + } + + /// Add an <rt> element as is. The caller handles ownerguid and the owner's objsur. + internal FwdataBuilder AddRt(XElement rt) + { + var guid = rt.Attribute(FlexBridgeConstants.GuidStr).Value; + if (_objects.Any(obj => SameGuid(obj, guid))) + throw new ArgumentException("An object with guid " + guid + " was already added.", "rt"); + _objects.Add(rt); + return this; + } + + /// The <rt> element with the given guid, for a test that wants to change a stub object. + internal XElement GetRt(string guid) + { + var rt = _objects.FirstOrDefault(obj => SameGuid(obj, guid)); + if (rt == null) + throw new ArgumentException("No object with guid " + guid + " has been added.", "guid"); + return rt; + } + + internal XDocument Build() + { + var root = new XElement("languageproject", new XAttribute("version", _modelVersion)); + if (_customFields.Count > 0) + root.Add(new XElement(FlexBridgeConstants.AdditionalFieldsTag, _customFields.Select(cf => new XElement(cf)))); + root.Add(_objects.Select(rt => new XElement(rt))); + return new XDocument(new XDeclaration("1.0", "utf-8", null), root); + } + + public override string ToString() + { + var doc = Build(); + return doc.Declaration + Environment.NewLine + doc; + } + + private string OwnedGuid(string ownerGuid, string owningProperty) + { + return GetRt(ownerGuid).Element(owningProperty).Element(FlexBridgeConstants.Objsur).Attribute(FlexBridgeConstants.GuidStr).Value; + } + + private static XElement NewRt(string className, string guid, object[] content) + { + return new XElement(FlexBridgeConstants.RtTag, + new XAttribute(FlexBridgeConstants.Class, className), + new XAttribute(FlexBridgeConstants.GuidStr, guid), + content); + } + + private static bool SameGuid(XElement rt, string guid) + { + return string.Equals(rt.Attribute(FlexBridgeConstants.GuidStr).Value, guid, StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs new file mode 100644 index 00000000..3f0db108 --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs @@ -0,0 +1,166 @@ +// Copyright (c) 2026 SIL International +// This software is licensed under the MIT License (http://opensource.org/licenses/MIT) + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using LibFLExBridgeChorusPlugin.DomainServices; +using LibFLExBridgeChorusPlugin.Infrastructure; +using LibTriboroughBridgeChorusPlugin; +using SIL.Progress; +using SIL.TestUtilities; + +namespace LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip +{ + /// + /// Runs and + /// on a .fwdata file in a temporary folder. + /// + /// + /// To add a test for a new split/unify edge case: + /// + /// Describe the smallest project that shows the case with a . + /// Create a harness from it in a using block. The harness starts and ends with a fresh MetadataCache, + /// because the cache is process-wide and split and unify both add the project's custom fields to it. + /// Call , then change the split files if the case needs it (as a merge would): + /// for one object found by guid, or for a whole file, + /// such as . + /// Call , which returns the rejoined .fwdata, and assert on it. + /// compares objects by guid; gives direct access. + /// + /// + /// using (var project = new SplitUnifyHarness(new FwdataBuilder().AddUnownedObject("LexEntry", EntryGuid))) + /// { + /// project.Split(); + /// project.EditSplitObject(EntryGuid, entry => entry.Add(new XElement("HomographNumber", new XAttribute("val", "2")))); + /// var rejoined = project.Unify(); + /// Assert.That(SplitUnifyHarness.ObjectsByGuid(rejoined)[EntryGuid].Element("HomographNumber"), Is.Not.Null); + /// } + /// + /// + internal sealed class SplitUnifyHarness : IDisposable + { + private readonly TemporaryFolder _projectFolder; + + internal SplitUnifyHarness(FwdataBuilder fwdata) + : this(fwdata.ToString()) + { + } + + internal SplitUnifyHarness(string fwdataContents) + { + MetadataCache = MetadataCache.TestOnlyNewCache; + _projectFolder = new TemporaryFolder("SplitUnifyHarness_" + Guid.NewGuid().ToString("N")); + FwdataPathname = Path.Combine(_projectFolder.Path, "Proj" + LibTriboroughBridgeSharedConstants.FwXmlExtension); + File.WriteAllText(FwdataPathname, fwdataContents); + Original = XDocument.Parse(fwdataContents); + } + + /// The cache split and unify use. It is reset when the harness is created and disposed. + internal MetadataCache MetadataCache { get; private set; } + + internal string ProjectFolder + { + get { return _projectFolder.Path; } + } + + internal string FwdataPathname { get; private set; } + + /// The .fwdata the harness started with. + internal XDocument Original { get; private set; } + + internal void Split() + { + FLExProjectSplitter.PushHumptyOffTheWall(new NullProgress(), false, FwdataPathname); + } + + /// Rejoin the split files into the .fwdata file and return it. + internal XDocument Unify() + { + FLExProjectUnifier.PutHumptyTogetherAgain(new NullProgress(), false, FwdataPathname); + return ReadFwdata(); + } + + /// Split, then unify with no changes in between. + internal XDocument RoundTrip() + { + Split(); + return Unify(); + } + + internal XDocument ReadFwdata() + { + return XDocument.Load(FwdataPathname); + } + + /// The full pathname of a split file, given its path relative to the project folder. + internal string SplitFilePathname(string relativePath) + { + return Path.Combine(ProjectFolder, relativePath); + } + + /// The XML files the splitter wrote (everything but the .fwdata file and the model version file). + internal IEnumerable SplitFiles() + { + return Directory.GetFiles(ProjectFolder, "*", SearchOption.AllDirectories) + .Where(pathname => pathname != FwdataPathname + && Path.GetFileName(pathname) != FlexBridgeConstants.ModelVersionFilename) + .OrderBy(pathname => pathname, StringComparer.OrdinalIgnoreCase); + } + + /// Load a split file, let change it, and save it. + internal void EditSplitFile(string relativePath, Action edit) + { + var pathname = SplitFilePathname(relativePath); + var doc = XDocument.Load(pathname); + edit(doc); + doc.Save(pathname); + } + + /// + /// Find the split file holding the object with , let change + /// the object's element, and save the file. In split files an object's element is named for its class + /// (for example <LexEntry guid='...'>), or is <ownseq class='...' guid='...'> in an owning sequence. + /// + internal void EditSplitObject(string guid, Action edit) + { + var pathname = SplitFileContaining(guid); + var doc = XDocument.Load(pathname); + edit(FindByGuid(doc, guid).Single()); + doc.Save(pathname); + } + + /// The split file holding the object with . + internal string SplitFileContaining(string guid) + { + var pathnames = SplitFiles().Where(pathname => FindByGuid(XDocument.Load(pathname), guid).Any()).ToList(); + if (pathnames.Count != 1) + throw new InvalidOperationException(string.Format("Expected one split file to hold {0}, but found {1}: {2}", + guid, pathnames.Count, string.Join(", ", pathnames))); + return pathnames[0]; + } + + /// The <rt> elements of a .fwdata document, keyed by lowercase guid. + internal static Dictionary ObjectsByGuid(XDocument fwdata) + { + return fwdata.Root.Elements(FlexBridgeConstants.RtTag) + .ToDictionary(rt => rt.Attribute(FlexBridgeConstants.GuidStr).Value.ToLowerInvariant()); + } + + public void Dispose() + { + _projectFolder.Dispose(); + MetadataCache = MetadataCache.TestOnlyNewCache; + } + + private static IEnumerable FindByGuid(XDocument doc, string guid) + { + // elements refer to an object; they are not the object. + return doc.Descendants() + .Where(element => element.Name.LocalName != FlexBridgeConstants.Objsur + && string.Equals((string)element.Attribute(FlexBridgeConstants.GuidStr), guid, StringComparison.OrdinalIgnoreCase)); + } + } +} diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs new file mode 100644 index 00000000..f7ea3d41 --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs @@ -0,0 +1,113 @@ +// Copyright (c) 2026 SIL International +// This software is licensed under the MIT License (http://opensource.org/licenses/MIT) + +using System.Xml.Linq; +using LibFLExBridgeChorusPlugin.Infrastructure; +using NUnit.Framework; + +namespace LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip +{ + /// + /// Split a small project, optionally change the split files, unify, and check the rejoined .fwdata. + /// See for how to add a test for a new edge case. + /// + [TestFixture] + public class SplitUnifyRoundTripTests + { + private const string EntryGuid = "c1ed46bc-e382-11de-8a39-0800200c9a66"; + private const string SenseGuid = "c1ed46bd-e382-11de-8a39-0800200c9a66"; + private const string SubsenseGuid = "c1ed46be-e382-11de-8a39-0800200c9a66"; + + private static XElement CustomInteger(string name, int value) + { + return new XElement("Custom", new XAttribute(FlexBridgeConstants.Name, name), new XAttribute("val", value)); + } + + [Test] + public void RoundTrip_MinimalProject_KeepsObjectsAndCustomFields() + { + var fwdata = new FwdataBuilder() + .AddCustomField("LexEntry", "Rank", "Integer") + .AddCustomField("LexSense", "Notes", "MultiUnicode", new XAttribute("wsSelector", "-1")) + .AddUnownedObject("LexEntry", EntryGuid); + + using (var project = new SplitUnifyHarness(fwdata)) + { + var rejoined = project.RoundTrip(); + + FwdataAssert.SameObjects(project.Original, rejoined); + FwdataAssert.SameCustomFields(project.Original, rejoined); + } + } + + [Test] + public void RoundTrip_MinimalProject_KeepsObjectContents() + { + var fwdata = new FwdataBuilder() + .AddCustomField("LexEntry", "Rank", "Integer") + .AddUnownedObject("LexEntry", EntryGuid, CustomInteger("Rank", 3)) + .AddOwnedObject(EntryGuid, "Senses", "LexSense", SenseGuid); + + using (var project = new SplitUnifyHarness(fwdata)) + { + FwdataAssert.SameObjectContents(project.Original, project.RoundTrip()); + } + } + + [Test] + public void RoundTrip_LexEntryCustomFieldValue_Survives() + { + var fwdata = new FwdataBuilder() + .AddCustomField("LexEntry", "Rank", "Integer") + .AddUnownedObject("LexEntry", EntryGuid, CustomInteger("Rank", 3)); + + using (var project = new SplitUnifyHarness(fwdata)) + { + var entry = SplitUnifyHarness.ObjectsByGuid(project.RoundTrip())[EntryGuid]; + + var custom = entry.Element("Custom"); + Assert.That(custom, Is.Not.Null); + Assert.That(custom.Attribute(FlexBridgeConstants.Name).Value, Is.EqualTo("Rank")); + Assert.That(custom.Attribute("val").Value, Is.EqualTo("3")); + } + } + + [Test] + public void RoundTrip_NestedOwnedObjects_KeepTheirOwners() + { + var fwdata = new FwdataBuilder() + .AddUnownedObject("LexEntry", EntryGuid) + .AddOwnedObject(EntryGuid, "Senses", "LexSense", SenseGuid) + .AddOwnedObject(SenseGuid, "Senses", "LexSense", SubsenseGuid); + + using (var project = new SplitUnifyHarness(fwdata)) + { + var objects = SplitUnifyHarness.ObjectsByGuid(project.RoundTrip()); + + Assert.That(objects[SenseGuid].Attribute(FlexBridgeConstants.OwnerGuid).Value, Is.EqualTo(EntryGuid)); + Assert.That(objects[SubsenseGuid].Attribute(FlexBridgeConstants.OwnerGuid).Value, Is.EqualTo(SenseGuid)); + Assert.That(objects[EntryGuid].Element("Senses").Element(FlexBridgeConstants.Objsur).Attribute(FlexBridgeConstants.GuidStr).Value, + Is.EqualTo(SenseGuid)); + Assert.That(objects[SenseGuid].Element("Senses").Element(FlexBridgeConstants.Objsur).Attribute(FlexBridgeConstants.GuidStr).Value, + Is.EqualTo(SubsenseGuid)); + } + } + + [Test] + public void Unify_EditedSplitObject_ReachesFwdata() + { + var fwdata = new FwdataBuilder() + .AddCustomField("LexEntry", "Rank", "Integer") + .AddUnownedObject("LexEntry", EntryGuid, CustomInteger("Rank", 3)); + + using (var project = new SplitUnifyHarness(fwdata)) + { + project.Split(); + project.EditSplitObject(EntryGuid, entry => entry.Element("Custom").Attribute("val").Value = "7"); + var entryAfter = SplitUnifyHarness.ObjectsByGuid(project.Unify())[EntryGuid]; + + Assert.That(entryAfter.Element("Custom").Attribute("val").Value, Is.EqualTo("7")); + } + } + } +} From 1e79ebdd9c1f6524f01e16136e50d402d2af8152 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Fri, 20 Nov 2026 13:19:28 +0700 Subject: [PATCH 2/3] Build CustomPropertyCacheTests project with FwdataBuilder The stub LangProject that split and unify need now lives in one place. Co-Authored-By: Claude Opus 5.5 --- .../CustomPropertyCacheTests.cs | 51 ++----------------- 1 file changed, 5 insertions(+), 46 deletions(-) diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs index 7cd1c054..a0431f02 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs @@ -1,14 +1,13 @@ // Copyright (c) 2026 SIL International // This software is licensed under the MIT License (http://opensource.org/licenses/MIT) -using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Linq; using Chorus.merge; using LibFLExBridgeChorusPlugin.DomainServices; using LibFLExBridgeChorusPlugin.Infrastructure; +using LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip; using LibTriboroughBridgeChorusPlugin; using NUnit.Framework; using SIL.Progress; @@ -24,7 +23,6 @@ namespace LibFLExBridgeChorusPluginTests.Infrastructure [TestFixture] public class CustomPropertyCacheTests { - private const string LangProjGuid = "c1ed46ba-e382-11de-8a39-0800200c9a66"; private const string EntryGuid = "c1ed46bc-e382-11de-8a39-0800200c9a66"; private TemporaryFolder _projectFolder; @@ -46,51 +44,12 @@ public void TearDown() _mdc = MetadataCache.TestOnlyNewCache; } - /// - /// The objects LangProject must own for the splitter and unifier to round trip a project: - /// (LangProject property, class of the owned object). - /// - private static readonly string[,] RequiredLangProjectOwnedObjects = - { - { "GenreList", "CmPossibilityList" }, - { "LexDb", "LexDb" }, - { "MorphologicalData", "MoMorphData" }, - { "MsFeatureSystem", "FsFeatureSystem" }, - { "PhFeatureSystem", "FsFeatureSystem" }, - { "PhonologicalData", "PhPhonData" }, - { "ResearchNotebook", "RnResearchNbk" }, - { "TextMarkupTags", "CmPossibilityList" }, - { "TranslationTags", "CmPossibilityList" } - }; - private static string FwdataWithIntegerCustomField() { - var langProject = new XElement("rt", - new XAttribute("class", "LangProject"), - new XAttribute("guid", LangProjGuid), - new XElement("AnalyzingAgents"), - new XElement("Styles")); - var ownedObjects = new List(); - for (var i = 0; i < RequiredLangProjectOwnedObjects.GetLength(0); ++i) - { - var guid = Guid.NewGuid().ToString(); - langProject.Add(new XElement(RequiredLangProjectOwnedObjects[i, 0], - new XElement("objsur", new XAttribute("guid", guid), new XAttribute("t", "o")))); - ownedObjects.Add(new XElement("rt", - new XAttribute("class", RequiredLangProjectOwnedObjects[i, 1]), - new XAttribute("guid", guid), - new XAttribute("ownerguid", LangProjGuid))); - } - var entry = XElement.Parse(string.Format( - "", EntryGuid)); - - var root = new XElement("languageproject", - new XAttribute("version", MetadataCache.MaximumModelVersion), - XElement.Parse(""), - langProject, - ownedObjects, - entry); - return new XDocument(new XDeclaration("1.0", "utf-8", null), root).ToString(); + return new FwdataBuilder() + .AddCustomField("LexEntry", "Rank", "Integer") + .AddUnownedObject("LexEntry", EntryGuid, new XElement("Custom", new XAttribute("name", "Rank"), new XAttribute("val", "3"))) + .ToString(); } private void Split() From ef436507da5c76ea8bb20f2a9841759f4b9cf2dd Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Fri, 20 Nov 2026 13:39:47 +0700 Subject: [PATCH 3/3] Use Guid for object ids in the split/unify harness Callers no longer have to match the lowercase string format of guids in fwdata files; the harness parses and formats them. Co-Authored-By: Claude Opus 5.5 --- .../CustomPropertyCacheTests.cs | 3 +- .../Infrastructure/RoundTrip/FwdataAssert.cs | 5 +++- .../Infrastructure/RoundTrip/FwdataBuilder.cs | 29 ++++++++----------- .../RoundTrip/SplitUnifyHarness.cs | 21 +++++++++----- .../RoundTrip/SplitUnifyRoundTripTests.cs | 15 +++++----- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs index a0431f02..db05eeb8 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs @@ -1,6 +1,7 @@ // Copyright (c) 2026 SIL International // This software is licensed under the MIT License (http://opensource.org/licenses/MIT) +using System; using System.IO; using System.Linq; using System.Xml.Linq; @@ -23,7 +24,7 @@ namespace LibFLExBridgeChorusPluginTests.Infrastructure [TestFixture] public class CustomPropertyCacheTests { - private const string EntryGuid = "c1ed46bc-e382-11de-8a39-0800200c9a66"; + private static readonly Guid EntryGuid = new Guid("c1ed46bc-e382-11de-8a39-0800200c9a66"); private TemporaryFolder _projectFolder; private string _fwdataPathname; diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs index 046c670e..9e54cb7f 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs @@ -1,6 +1,7 @@ // Copyright (c) 2026 SIL International // This software is licensed under the MIT License (http://opensource.org/licenses/MIT) +using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; @@ -48,7 +49,9 @@ private static List ObjectSummaries(XDocument fwdata) .Select(pair => string.Format("{0} {1} owner={2}", pair.Key, pair.Value.Attribute(FlexBridgeConstants.Class).Value, - ((string)pair.Value.Attribute(FlexBridgeConstants.OwnerGuid) ?? "none").ToLowerInvariant())) + pair.Value.Attribute(FlexBridgeConstants.OwnerGuid) == null + ? "none" + : new Guid(pair.Value.Attribute(FlexBridgeConstants.OwnerGuid).Value).ToString())) .OrderBy(summary => summary) .ToList(); } diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs index 8ae6974e..122e3e25 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs @@ -27,7 +27,7 @@ namespace LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip /// internal sealed class FwdataBuilder { - internal const string LangProjectGuid = "c1ed46ba-e382-11de-8a39-0800200c9a66"; + internal static readonly Guid LangProjectGuid = new Guid("c1ed46ba-e382-11de-8a39-0800200c9a66"); /// /// The objects LangProject must own for the splitter and unifier to round trip a project: @@ -72,11 +72,11 @@ internal FwdataBuilder(int modelVersion) // Fixed guids keep the split files (for example, which Lexicon_NN.lexdb bucket a guid lands in) stable from run to run. var i = 0; foreach (var owned in RequiredLangProjectOwnedObjects) - AddOwnedObject(LangProjectGuid, owned.Key, owned.Value, string.Format("00000000-0000-0000-0000-{0:x12}", ++i)); + AddOwnedObject(LangProjectGuid, owned.Key, owned.Value, new Guid(string.Format("00000000-0000-0000-0000-{0:x12}", ++i))); } /// The guid of the stub LexDb that LangProject owns. - internal string LexDbGuid + internal Guid LexDbGuid { get { return OwnedGuid(LangProjectGuid, FlexBridgeConstants.LexDb); } } @@ -96,7 +96,7 @@ internal FwdataBuilder AddCustomField(string className, string name, string type } /// Add an object with no owner, such as a LexEntry. - internal FwdataBuilder AddUnownedObject(string className, string guid, params object[] content) + internal FwdataBuilder AddUnownedObject(string className, Guid guid, params object[] content) { return AddRt(NewRt(className, guid, content)); } @@ -105,7 +105,7 @@ internal FwdataBuilder AddUnownedObject(string className, string guid, params ob /// Add an object owned by (already added) in its . /// The owner gets an <objsur t='o'/> in that property, after any it already has. /// - internal FwdataBuilder AddOwnedObject(string ownerGuid, string owningProperty, string className, string guid, params object[] content) + internal FwdataBuilder AddOwnedObject(Guid ownerGuid, string owningProperty, string className, Guid guid, params object[] content) { var owner = GetRt(ownerGuid); var property = owner.Element(owningProperty); @@ -126,17 +126,17 @@ internal FwdataBuilder AddOwnedObject(string ownerGuid, string owningProperty, s /// Add an <rt> element as is. The caller handles ownerguid and the owner's objsur. internal FwdataBuilder AddRt(XElement rt) { - var guid = rt.Attribute(FlexBridgeConstants.GuidStr).Value; - if (_objects.Any(obj => SameGuid(obj, guid))) + var guid = SplitUnifyHarness.GuidOf(rt); + if (_objects.Any(obj => SplitUnifyHarness.GuidOf(obj) == guid)) throw new ArgumentException("An object with guid " + guid + " was already added.", "rt"); _objects.Add(rt); return this; } /// The <rt> element with the given guid, for a test that wants to change a stub object. - internal XElement GetRt(string guid) + internal XElement GetRt(Guid guid) { - var rt = _objects.FirstOrDefault(obj => SameGuid(obj, guid)); + var rt = _objects.FirstOrDefault(obj => SplitUnifyHarness.GuidOf(obj) == guid); if (rt == null) throw new ArgumentException("No object with guid " + guid + " has been added.", "guid"); return rt; @@ -157,22 +157,17 @@ public override string ToString() return doc.Declaration + Environment.NewLine + doc; } - private string OwnedGuid(string ownerGuid, string owningProperty) + private Guid OwnedGuid(Guid ownerGuid, string owningProperty) { - return GetRt(ownerGuid).Element(owningProperty).Element(FlexBridgeConstants.Objsur).Attribute(FlexBridgeConstants.GuidStr).Value; + return SplitUnifyHarness.GuidOf(GetRt(ownerGuid).Element(owningProperty).Element(FlexBridgeConstants.Objsur)); } - private static XElement NewRt(string className, string guid, object[] content) + private static XElement NewRt(string className, Guid guid, object[] content) { return new XElement(FlexBridgeConstants.RtTag, new XAttribute(FlexBridgeConstants.Class, className), new XAttribute(FlexBridgeConstants.GuidStr, guid), content); } - - private static bool SameGuid(XElement rt, string guid) - { - return string.Equals(rt.Attribute(FlexBridgeConstants.GuidStr).Value, guid, StringComparison.OrdinalIgnoreCase); - } } } diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs index 3f0db108..ec5d7bd4 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs @@ -124,7 +124,7 @@ internal void EditSplitFile(string relativePath, Action edit) /// the object's element, and save the file. In split files an object's element is named for its class /// (for example <LexEntry guid='...'>), or is <ownseq class='...' guid='...'> in an owning sequence. /// - internal void EditSplitObject(string guid, Action edit) + internal void EditSplitObject(Guid guid, Action edit) { var pathname = SplitFileContaining(guid); var doc = XDocument.Load(pathname); @@ -133,7 +133,7 @@ internal void EditSplitObject(string guid, Action edit) } /// The split file holding the object with . - internal string SplitFileContaining(string guid) + internal string SplitFileContaining(Guid guid) { var pathnames = SplitFiles().Where(pathname => FindByGuid(XDocument.Load(pathname), guid).Any()).ToList(); if (pathnames.Count != 1) @@ -142,11 +142,11 @@ internal string SplitFileContaining(string guid) return pathnames[0]; } - /// The <rt> elements of a .fwdata document, keyed by lowercase guid. - internal static Dictionary ObjectsByGuid(XDocument fwdata) + /// The <rt> elements of a .fwdata document, keyed by guid. + internal static Dictionary ObjectsByGuid(XDocument fwdata) { return fwdata.Root.Elements(FlexBridgeConstants.RtTag) - .ToDictionary(rt => rt.Attribute(FlexBridgeConstants.GuidStr).Value.ToLowerInvariant()); + .ToDictionary(GuidOf); } public void Dispose() @@ -155,12 +155,19 @@ public void Dispose() MetadataCache = MetadataCache.TestOnlyNewCache; } - private static IEnumerable FindByGuid(XDocument doc, string guid) + /// The guid of an element with a guid attribute, such as an <rt> or <objsur>. + internal static Guid GuidOf(XElement element) + { + return new Guid(element.Attribute(FlexBridgeConstants.GuidStr).Value); + } + + private static IEnumerable FindByGuid(XDocument doc, Guid guid) { // elements refer to an object; they are not the object. return doc.Descendants() .Where(element => element.Name.LocalName != FlexBridgeConstants.Objsur - && string.Equals((string)element.Attribute(FlexBridgeConstants.GuidStr), guid, StringComparison.OrdinalIgnoreCase)); + && element.Attribute(FlexBridgeConstants.GuidStr) != null + && GuidOf(element) == guid); } } } diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs index f7ea3d41..0d2a3ac3 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs @@ -1,6 +1,7 @@ // Copyright (c) 2026 SIL International // This software is licensed under the MIT License (http://opensource.org/licenses/MIT) +using System; using System.Xml.Linq; using LibFLExBridgeChorusPlugin.Infrastructure; using NUnit.Framework; @@ -14,9 +15,9 @@ namespace LibFLExBridgeChorusPluginTests.Infrastructure.RoundTrip [TestFixture] public class SplitUnifyRoundTripTests { - private const string EntryGuid = "c1ed46bc-e382-11de-8a39-0800200c9a66"; - private const string SenseGuid = "c1ed46bd-e382-11de-8a39-0800200c9a66"; - private const string SubsenseGuid = "c1ed46be-e382-11de-8a39-0800200c9a66"; + private static readonly Guid EntryGuid = new Guid("c1ed46bc-e382-11de-8a39-0800200c9a66"); + private static readonly Guid SenseGuid = new Guid("c1ed46bd-e382-11de-8a39-0800200c9a66"); + private static readonly Guid SubsenseGuid = new Guid("c1ed46be-e382-11de-8a39-0800200c9a66"); private static XElement CustomInteger(string name, int value) { @@ -84,11 +85,11 @@ public void RoundTrip_NestedOwnedObjects_KeepTheirOwners() { var objects = SplitUnifyHarness.ObjectsByGuid(project.RoundTrip()); - Assert.That(objects[SenseGuid].Attribute(FlexBridgeConstants.OwnerGuid).Value, Is.EqualTo(EntryGuid)); - Assert.That(objects[SubsenseGuid].Attribute(FlexBridgeConstants.OwnerGuid).Value, Is.EqualTo(SenseGuid)); - Assert.That(objects[EntryGuid].Element("Senses").Element(FlexBridgeConstants.Objsur).Attribute(FlexBridgeConstants.GuidStr).Value, + Assert.That(new Guid(objects[SenseGuid].Attribute(FlexBridgeConstants.OwnerGuid).Value), Is.EqualTo(EntryGuid)); + Assert.That(new Guid(objects[SubsenseGuid].Attribute(FlexBridgeConstants.OwnerGuid).Value), Is.EqualTo(SenseGuid)); + Assert.That(SplitUnifyHarness.GuidOf(objects[EntryGuid].Element("Senses").Element(FlexBridgeConstants.Objsur)), Is.EqualTo(SenseGuid)); - Assert.That(objects[SenseGuid].Element("Senses").Element(FlexBridgeConstants.Objsur).Attribute(FlexBridgeConstants.GuidStr).Value, + Assert.That(SplitUnifyHarness.GuidOf(objects[SenseGuid].Element("Senses").Element(FlexBridgeConstants.Objsur)), Is.EqualTo(SubsenseGuid)); } }