diff --git a/docs/verify-xml.md b/docs/verify-xml.md index 21649cd52..5db99b20c 100644 --- a/docs/verify-xml.md +++ b/docs/verify-xml.md @@ -66,7 +66,7 @@ public Task XmlIgnoreMember() => VerifyXml(xml) .IgnoreMember("node"); ``` -snippet source | anchor +snippet source | anchor Will produce @@ -92,7 +92,7 @@ public Task XmlScrubMember() => VerifyXml(xml) .ScrubMember("node"); ``` -snippet source | anchor +snippet source | anchor Will produce diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 1530e71ba..93b59dd83 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget - 28.3.1 + 28.3.2 enable preview 1.0.0 diff --git a/src/Verify.Tests/XmlTests.CData.verified.xml b/src/Verify.Tests/XmlTests.CData.verified.xml new file mode 100644 index 000000000..ebe8cdef2 --- /dev/null +++ b/src/Verify.Tests/XmlTests.CData.verified.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.CDataMix.verified.xml b/src/Verify.Tests/XmlTests.CDataMix.verified.xml new file mode 100644 index 000000000..298464404 --- /dev/null +++ b/src/Verify.Tests/XmlTests.CDataMix.verified.xml @@ -0,0 +1 @@ +value \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.CDataMix_WithScrub.verified.xml b/src/Verify.Tests/XmlTests.CDataMix_WithScrub.verified.xml new file mode 100644 index 000000000..88aa0358e --- /dev/null +++ b/src/Verify.Tests/XmlTests.CDataMix_WithScrub.verified.xml @@ -0,0 +1 @@ +replaced \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.CData_WithScrub.verified.xml b/src/Verify.Tests/XmlTests.CData_WithScrub.verified.xml new file mode 100644 index 000000000..7dcd4ec33 --- /dev/null +++ b/src/Verify.Tests/XmlTests.CData_WithScrub.verified.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.Comment.verified.xml b/src/Verify.Tests/XmlTests.Comment.verified.xml new file mode 100644 index 000000000..888654dcb --- /dev/null +++ b/src/Verify.Tests/XmlTests.Comment.verified.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.Comment_Mix.verified.xml b/src/Verify.Tests/XmlTests.Comment_Mix.verified.xml new file mode 100644 index 000000000..7cbdab728 --- /dev/null +++ b/src/Verify.Tests/XmlTests.Comment_Mix.verified.xml @@ -0,0 +1,2 @@ + + value \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.Comment_Mix_WithScrub.verified.xml b/src/Verify.Tests/XmlTests.Comment_Mix_WithScrub.verified.xml new file mode 100644 index 000000000..be9844faf --- /dev/null +++ b/src/Verify.Tests/XmlTests.Comment_Mix_WithScrub.verified.xml @@ -0,0 +1,2 @@ + + replaced \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.Comment_WithScrub.verified.xml b/src/Verify.Tests/XmlTests.Comment_WithScrub.verified.xml new file mode 100644 index 000000000..616bee4fe --- /dev/null +++ b/src/Verify.Tests/XmlTests.Comment_WithScrub.verified.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/Verify.Tests/XmlTests.cs b/src/Verify.Tests/XmlTests.cs index b73862e48..7fc977d04 100644 --- a/src/Verify.Tests/XmlTests.cs +++ b/src/Verify.Tests/XmlTests.cs @@ -27,6 +27,71 @@ public Task NoDeclaration() => """); + [Fact] + public Task Comment() => + VerifyXml( + """ + + + """); + + [Fact] + public Task Comment_WithScrub() => + VerifyXml( + """ + + + """); + [Fact] + public Task Comment_Mix() => + VerifyXml( + """ + + value + """); + [Fact] + public Task Comment_Mix_WithScrub() => + VerifyXml( + """ + + value + """) + .AddScrubber(_ => _.Replace("value", "replaced")); + + [Fact] + public Task CData() => + VerifyXml( + """ + + + """); + + [Fact] + public Task CData_WithScrub() => + VerifyXml( + """ + + + """) + .AddScrubber(_ => _.Replace("value", "replaced")); + + [Fact] + public Task CDataMix() => + VerifyXml( + """ + + value + """); + + [Fact] + public Task CDataMix_WithScrub() => + VerifyXml( + """ + + value + """) + .AddScrubber(_ => _.Replace("value", "replaced")); + #region XmlIgnoreMember [Fact] diff --git a/src/Verify/Verifier/InnerVerifier_Xml.cs b/src/Verify/Verifier/InnerVerifier_Xml.cs index fae133656..dd300f183 100644 --- a/src/Verify/Verifier/InnerVerifier_Xml.cs +++ b/src/Verify/Verifier/InnerVerifier_Xml.cs @@ -63,52 +63,40 @@ Task 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"); @@ -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(); + } + } + } } \ No newline at end of file