Skip to content

Commit

Permalink
Merge pull request #1273 from lg2de/XmlAssert5X
Browse files Browse the repository at this point in the history
Fix XPath index calculation for XML comparison
  • Loading branch information
jnyrup committed Mar 21, 2020
2 parents 4e1b8e9 + 357578e commit 4787885
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 8 additions & 3 deletions Src/FluentAssertions/Xml/Equivalency/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,21 @@ private IEnumerable<Node> GetPath()

public Node Parent { get; }

public Node Push(string name)
public Node Push(string localName)
{
Node node = children.Find(e => e.name == name)
?? AddChildNode(name);
Node node = children.Find(e => e.name == localName)
?? AddChildNode(localName);

node.count++;

return node;
}

public void Pop()
{
children.Clear();
}

private Node AddChildNode(string name)
{
Node node = new Node(this, name);
Expand Down
1 change: 1 addition & 0 deletions Src/FluentAssertions/Xml/Equivalency/XmlReaderValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private Failure Validate()
// No need to verify end element, if it doesn't match
// the start element it isn't valid XML, so the parser
// would handle that.
currentNode.Pop();
currentNode = currentNode.Parent;
break;

Expand Down
16 changes: 16 additions & 0 deletions Tests/Shared.Specs/XDocumentAssertionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,22 @@ public void When_asserting_equivalence_of_document_with_repeating_element_names_
act.Should().Throw<XunitException>().WithMessage("*\"/xml/xml[3]/xml[2]\"*");
}

[Fact]
public void When_asserting_equivalence_of_document_with_repeating_element_names_with_different_parents_but_differs_it_should_fail_with_index_xpath_to_difference()
{
// Arrange
XDocument actual = XDocument.Parse(
"<root><xml1 /><xml1><xml2 /><xml2 a=\"x\" /></xml1><xml1><xml2 /><xml2 a=\"x\" /></xml1></root>");
XDocument expected = XDocument.Parse(
"<root><xml1 /><xml1><xml2 /><xml2 a=\"x\" /></xml1><xml1><xml2 /><xml2 a=\"y\" /></xml1></root>");

// Act
Action act = () => actual.Should().BeEquivalentTo(expected);

// Assert
act.Should().Throw<XunitException>().WithMessage("*\"/root/xml1[3]/xml2[2]\"*");
}

#endregion

#region BeNull / NotBeNull
Expand Down
9 changes: 8 additions & 1 deletion docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ sidebar:
nav: "sidebar"
---

## 5.10.1
## 5.10.3
**Fixes**
* Fixed XPath index calculation in XML comparison when child node names are repeated in repeated parent nodes - [#1273](https://github.com/fluentassertions/fluentassertions/pull/1273)

## 5.10.2
**Fixes**
* Added missing dependency on System.Xml - [#79](https://github.com/fluentassertions/fluentassertions/issues/79)

## 5.10.1
This version was skipped.

## 5.10.0

**What's New**
Expand Down

0 comments on commit 4787885

Please sign in to comment.