diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs index 7cd1c054..db05eeb8 100644 --- a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/CustomPropertyCacheTests.cs @@ -2,13 +2,13 @@ // 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,8 +24,7 @@ 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 static readonly Guid EntryGuid = new Guid("c1ed46bc-e382-11de-8a39-0800200c9a66"); private TemporaryFolder _projectFolder; private string _fwdataPathname; @@ -46,51 +45,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() diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs new file mode 100644 index 00000000..9e54cb7f --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataAssert.cs @@ -0,0 +1,92 @@ +// 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; +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, + pair.Value.Attribute(FlexBridgeConstants.OwnerGuid) == null + ? "none" + : new Guid(pair.Value.Attribute(FlexBridgeConstants.OwnerGuid).Value).ToString())) + .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..122e3e25 --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/FwdataBuilder.cs @@ -0,0 +1,173 @@ +// 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 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: + /// (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, new Guid(string.Format("00000000-0000-0000-0000-{0:x12}", ++i))); + } + + /// The guid of the stub LexDb that LangProject owns. + internal Guid 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, Guid 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(Guid ownerGuid, string owningProperty, string className, Guid 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 = 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(Guid 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; + } + + 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 Guid OwnedGuid(Guid ownerGuid, string owningProperty) + { + return SplitUnifyHarness.GuidOf(GetRt(ownerGuid).Element(owningProperty).Element(FlexBridgeConstants.Objsur)); + } + + 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); + } + } +} diff --git a/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs new file mode 100644 index 00000000..ec5d7bd4 --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyHarness.cs @@ -0,0 +1,173 @@ +// 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(Guid 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(Guid 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 guid. + internal static Dictionary ObjectsByGuid(XDocument fwdata) + { + return fwdata.Root.Elements(FlexBridgeConstants.RtTag) + .ToDictionary(GuidOf); + } + + public void Dispose() + { + _projectFolder.Dispose(); + MetadataCache = MetadataCache.TestOnlyNewCache; + } + + /// 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 + && 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 new file mode 100644 index 00000000..0d2a3ac3 --- /dev/null +++ b/src/LibFLExBridge-ChorusPluginTests/Infrastructure/RoundTrip/SplitUnifyRoundTripTests.cs @@ -0,0 +1,114 @@ +// 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; + +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 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) + { + 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(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(SplitUnifyHarness.GuidOf(objects[SenseGuid].Element("Senses").Element(FlexBridgeConstants.Objsur)), + 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")); + } + } + } +}