public static ElementLinkPair <CT> CreateFromView <CT>(CT view, ProjectXmlPair pre = null) where CT : ProjectElement { var real = ViewValidation.GetRealObject(view); return(new ElementLinkPair <CT>(pre, view, real)); }
private void CopyFromInternal(ProjectRootElement sourceProject) { // quite a few complexity in the ExternalProjectProvider implementation is because of // ProjectElement.CopyFrom and ProjectElementContainer.DeepCopyFrom.... bool externalSource = sourceProject != null; var projectPair = GetNewInMemoryProject("CopyFrom", TestCollectionGroup.BigProjectFile); var xmlPair = new ProjectXmlPair(projectPair); Assert.True(xmlPair.View.HasUnsavedChanges); xmlPair.View.Save(); Assert.False(xmlPair.View.HasUnsavedChanges); sourceProject = sourceProject ?? xmlPair.View; var existingItemGroupList = sourceProject.AllChildren.OfType <ProjectItemGroupElement>().Where(((ig) => ig.Label == "Group1")).ToList(); Assert.Single(existingItemGroupList); var existingItemGroup = existingItemGroupList[0]; Assert.NotNull(existingItemGroup); var realExistingItemGroup = ViewValidation.GetRealObject(existingItemGroup); var ourGroup1 = xmlPair.QuerySingleChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "Group1"); var newCopyFrom = xmlPair.AddNewLabeledChaildWithVerify <ProjectItemGroupElement>(ObjectType.View, "newGrop", (p, l) => p.AddItemGroup()); newCopyFrom.View.CopyFrom(existingItemGroup); xmlPair.QueryChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "Group1", 2); newCopyFrom.View.Label = "CopyFrom"; newCopyFrom.VerifySame(xmlPair.QuerySingleChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "CopyFrom")); ourGroup1.VerifySame(xmlPair.QuerySingleChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "Group1")); // children are not copied. Assert.Empty(newCopyFrom.View.Items); // but attributes are (even non standard) //Assert.Equal("2", ProjectElementLink.GetAttributeValue(existingItemGroup, "FunnyAttribute", true)); //Assert.Equal("2", ProjectElementLink.GetAttributeValue(newCopyFrom.View, "FunnyAttribute", true)); newCopyFrom.VerifyNotSame(ourGroup1); Assert.True(xmlPair.View.HasUnsavedChanges); Assert.False(externalSource && sourceProject.HasUnsavedChanges); var newDeepCopy = xmlPair.AddNewLabeledChaildWithVerify <ProjectItemGroupElement>(ObjectType.View, "newGrop", (p, l) => p.AddItemGroup()); newDeepCopy.View.DeepCopyFrom(existingItemGroup); xmlPair.QueryChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "Group1", 2); // slightly cheting but we know that the large groups should be the same, even though there are not the same object // note do that before changing the label. Assert.NotSame(realExistingItemGroup, newDeepCopy.Real); // TODO XmlLocation is (correctly) different for the items, need to find a way to bypass it. var context = new ValidationContext(); context.ValidateLocation = delegate(ElementLocation a, ElementLocation e) { return; }; ViewValidation.Verify(newDeepCopy.View, realExistingItemGroup, context); newDeepCopy.View.Label = "DeepCopyFrom"; newDeepCopy.VerifySame(xmlPair.QuerySingleChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "DeepCopyFrom")); ourGroup1.VerifySame(xmlPair.QuerySingleChildrenWithValidation <ProjectItemGroupElement>((ig) => ig.Label == "Group1")); newDeepCopy.VerifyNotSame(ourGroup1); Assert.False(externalSource && sourceProject.HasUnsavedChanges); }