Skip to content

Commit

Permalink
Fix CData being removed in VerifyXml (#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Nov 18, 2024
1 parent 9716e7c commit 9fb3b98
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 32 deletions.
4 changes: 2 additions & 2 deletions docs/verify-xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Task XmlIgnoreMember() =>
VerifyXml(xml)
.IgnoreMember("node");
```
<sup><a href='/src/Verify.Tests/XmlTests.cs#L30-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlIgnoreMember' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/XmlTests.cs#L95-L102' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlIgnoreMember' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will produce
Expand All @@ -92,7 +92,7 @@ public Task XmlScrubMember() =>
VerifyXml(xml)
.ScrubMember("node");
```
<sup><a href='/src/Verify.Tests/XmlTests.cs#L39-L46' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlScrubMember' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/XmlTests.cs#L104-L111' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlScrubMember' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will produce
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget</NoWarn>
<Version>28.3.1</Version>
<Version>28.3.2</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
1 change: 1 addition & 0 deletions src/Verify.Tests/XmlTests.CData.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<person><![CDATA[name is John Doe]]></person>
1 change: 1 addition & 0 deletions src/Verify.Tests/XmlTests.CDataMix.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<person><![CDATA[name is John Doe]]>value</person>
1 change: 1 addition & 0 deletions src/Verify.Tests/XmlTests.CDataMix_WithScrub.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<person><![CDATA[replaced]]>replaced</person>
1 change: 1 addition & 0 deletions src/Verify.Tests/XmlTests.CData_WithScrub.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<person><![CDATA[replaced]]></person>
3 changes: 3 additions & 0 deletions src/Verify.Tests/XmlTests.Comment.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<person>
<!-- name is John Doe -->
</person>
2 changes: 2 additions & 0 deletions src/Verify.Tests/XmlTests.Comment_Mix.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<person>
<!-- name is John Doe -->value</person>
2 changes: 2 additions & 0 deletions src/Verify.Tests/XmlTests.Comment_Mix_WithScrub.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<person>
<!-- replaced -->replaced</person>
3 changes: 3 additions & 0 deletions src/Verify.Tests/XmlTests.Comment_WithScrub.verified.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<person>
<!-- value -->
</person>
65 changes: 65 additions & 0 deletions src/Verify.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,71 @@ public Task NoDeclaration() =>
</body>
""");

[Fact]
public Task Comment() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><!-- name is John Doe --></person>
""");

[Fact]
public Task Comment_WithScrub() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><!-- value --></person>
""");
[Fact]
public Task Comment_Mix() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><!-- name is John Doe -->value</person>
""");
[Fact]
public Task Comment_Mix_WithScrub() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><!-- value -->value</person>
""")
.AddScrubber(_ => _.Replace("value", "replaced"));

[Fact]
public Task CData() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><![CDATA[name is John Doe]]></person>
""");

[Fact]
public Task CData_WithScrub() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><![CDATA[value]]></person>
""")
.AddScrubber(_ => _.Replace("value", "replaced"));

[Fact]
public Task CDataMix() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><![CDATA[name is John Doe]]>value</person>
""");

[Fact]
public Task CDataMix_WithScrub() =>
VerifyXml(
"""
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person><![CDATA[value]]>value</person>
""")
.AddScrubber(_ => _.Replace("value", "replaced"));

#region XmlIgnoreMember

[Fact]
Expand Down
78 changes: 49 additions & 29 deletions src/Verify/Verifier/InnerVerifier_Xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,52 +63,40 @@ Task<VerifyResult> VerifyXml(XContainer? target)
}

var serialization = settings.serialization;
var nodes = target
var elements = target
.Descendants()
.ToList();
foreach (var node in nodes)

foreach (var element in elements)
{
if (serialization.TryGetScrubOrIgnoreByName(node.Name.LocalName, out var scrubOrIgnore))
if (serialization.TryGetScrubOrIgnoreByName(element.Name.LocalName, out var scrubOrIgnore))
{
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
{
node.Remove();
element.Remove();
}
else
{
node.Value = "Scrubbed";
element.Value = "Scrubbed";
}

continue;
}

foreach (var attribute in node
.Attributes()
.ToList())
{
if (serialization.TryGetScrubOrIgnoreByName(attribute.Name.LocalName, out scrubOrIgnore))
{
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
{
attribute.Remove();
}
else
{
attribute.Value = "Scrubbed";
}

continue;
}

attribute.Value = ConvertValue(serialization, attribute.Value);
}
ScrubAttributes(element, serialization);
}

if (node.IsEmpty || node.HasElements)
foreach (var node in target.DescendantNodes())
{
switch (node)
{
continue;
case XText text:
text.Value = ConvertValue(serialization, text.Value);
continue;
case XComment comment:
comment.Value = ConvertValue(serialization, comment.Value);
continue;
}

node.Value = ConvertValue(serialization, node.Value);
}

return VerifyString(target.ToString(), "xml");
Expand All @@ -124,4 +112,36 @@ string ConvertValue(SerializationSettings serialization, string value)

return ApplyScrubbers.ApplyForPropertyValue(span, settings, counter).ToString();
}

void ScrubAttributes(XElement node, SerializationSettings serialization)
{
foreach (var attribute in node
.Attributes()
.ToList())
{
if (serialization.TryGetScrubOrIgnoreByName(attribute.Name.LocalName, out var scrubOrIgnore))
{
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
{
attribute.Remove();
}
else
{
attribute.Value = "Scrubbed";
}

continue;
}

var span = attribute.Value.AsSpan();
if (serialization.TryConvertString(counter, span, out var result))
{
attribute.Value = result;
}
else
{
attribute.Value = ApplyScrubbers.ApplyForPropertyValue(span, settings, counter).ToString();
}
}
}
}

0 comments on commit 9fb3b98

Please sign in to comment.