Skip to content

Commit

Permalink
Bug fix for Issue #1.
Browse files Browse the repository at this point in the history
Null exception being thrown in the case when an element is removed from the target config.
Added tests for removal of elements and attributes.
  • Loading branch information
Cameron Wills committed Aug 11, 2016
1 parent 5d86e38 commit d3c3a5b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 11 deletions.
124 changes: 116 additions & 8 deletions FatAntelope.Tests/XdtDiffWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,97 @@ public void InsertEnd()
AssertCanTransform(source, target);
}

[TestMethod]
public void RemoveWithNoLocator()
{
var source = @"
<root>
<child1 />
<child3 />
</root>";

var target = @"
<root>
<child1 />
</root>";

var patch = GetPatch(source, target);

// Locator = none
AssertNoLocator(patch.SelectSingleNode("/root/child3"));

// Transform = SetAttribute(type)
AssertTransform(patch.SelectSingleNode("/root/child3"), "Remove");

AssertCanTransform(source, target);
}

[TestMethod]
public void RemoveWithMatchLocator()
{
var source = @"
<root>
<child name='child1' />
<child name='child2' />
</root>";

var target = @"
<root>
<child name='child1' />
</root>";

var patch = GetPatch(source, target);

// Locator = Match(name
AssertLocator(patch.SelectSingleNode("/root/child[(@name='child2')]"), "Match(name)");

Assert.AreEqual(patch.SelectSingleNode("/root").ChildNodes.Count, 1);

// Transform = SetAttribute(type)
AssertTransform(patch.SelectSingleNode("/root/child[(@name='child2')]"), "Remove");

AssertCanTransform(source, target);
}

[TestMethod]
public void RemoveAndChangeAttributes()
{
var source = @"
<root>
<child name='child1' test='true' optional='value' />
<child name='child2' test='true' optional='value' />
</root>";

var target = @"
<root>
<child name='child1' test='false' optional='value' />
<child name='child2' test='false' />
</root>";

var patch = GetPatch(source, target);


// Two transform nodes for the same child
Assert.AreEqual(patch.SelectSingleNode("/root").ChildNodes.Count, 3);
Assert.AreEqual(patch.SelectNodes("/root/child[(@name='child2')]").Count, 2);

// First child has single attribute changed (test='false')
AssertLocator(patch.SelectSingleNode("/root/child[(@name='child1')]"), "Match(name)");
AssertTransform(patch.SelectSingleNode("/root/child[(@name='child1')]"), "SetAttributes(test)");
AssertValue(patch.SelectSingleNode("/root/child[(@name='child1')]/@test"), "false");

// Second child has both attribute changed (test='false') and attribute removed (optional='value')
AssertLocator(patch.SelectSingleNode("/root/child[2]"), "Match(name)");
AssertValue(patch.SelectSingleNode("/root/child[2]/@name"), "child2");
AssertTransform(patch.SelectSingleNode("/root/child[2]"), "RemoveAttributes(optional)");

AssertLocator(patch.SelectSingleNode("/root/child[3]"), "Match(name)");
AssertValue(patch.SelectSingleNode("/root/child[3]/@name"), "child2");
AssertTransform(patch.SelectSingleNode("/root/child[3]"), "SetAttributes(test)");

AssertCanTransform(source, target);
}

[TestMethod]
public void MatchAndReplace()
{
Expand Down Expand Up @@ -277,11 +368,26 @@ public void ConditionAndReplace()
AssertCanTransform(source, target);
}

#region Private helpers

private void AssertTransform(XmlNode node, string expected)
{
Assert.IsNotNull(node);
var value = node.SelectSingleNode("@*[local-name() = 'Transform']").Value;
Assert.AreEqual(expected, value);
AssertAttribute(node, expected, "Transform");
}

private void AssertNoTransform(XmlNode node)
{
AssertNoAttribute(node, "Transform");
}

private void AssertLocator(XmlNode node, string expected)
{
AssertAttribute(node, expected, "Locator");
}

private void AssertNoLocator(XmlNode node)
{
AssertNoAttribute(node, "Locator");
}

private void AssertValue(XmlNode node, string expected)
Expand All @@ -290,18 +396,18 @@ private void AssertValue(XmlNode node, string expected)
Assert.AreEqual(expected, node.Value);
}

private void AssertLocator(XmlNode node, string expected)
private void AssertAttribute(XmlNode node, string expected, string attributeName)
{
Assert.IsNotNull(node);
var value = node.SelectSingleNode("@*[local-name() = 'Locator']").Value;
var value = node.SelectSingleNode(string.Format("@*[local-name() = '{0}']", attributeName)).Value;
Assert.AreEqual(expected, value);
}

private void AssertNoLocator(XmlNode node)
private void AssertNoAttribute(XmlNode node, string attributeName)
{
Assert.IsNotNull(node);
var attribute = node.SelectSingleNode("@*[local-name() = 'Locator']");
Assert.IsNull(attribute);
var attribute = node.SelectSingleNode(string.Format("@*[local-name() = '{0}']", attributeName));
Assert.IsNull(attribute, string.Format("Attribute not expected: {0}", attributeName));
}

private void AssertCanTransform(string sourceXml, string targetXml)
Expand Down Expand Up @@ -354,5 +460,7 @@ private XmlDocument Transform(XmlDocument sourceXml, XmlDocument patchXml)

return source;
}

#endregion
}
}
6 changes: 3 additions & 3 deletions FatAntelope/Writers/XdtDiffWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ private string GetInsertTransform(XNode element, string path, int index)
/// </summary>
private Trait GetUniqueTrait(XNode element)
{
var duplicates = new List<XNode>();
var parent = element.Parent;
if (parent != null)
if (element != null && element.Parent != null)
{
var parent = element.Parent;
var duplicates = new List<XNode>();
var index = -1;

// Check for siblings with the same name
Expand Down

0 comments on commit d3c3a5b

Please sign in to comment.