|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Xml; |
| 7 | +using Shouldly; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Egil.RazorComponents.Testing.Library.Diffing |
| 11 | +{ |
| 12 | + public class XmlNodeAssertExtensionsTests |
| 13 | + { |
| 14 | + [Fact(DisplayName = "xmlNode.ShouldBe(xmlNode) throws correct exception when nodes are different")] |
| 15 | + public void MyTestMethod() |
| 16 | + { |
| 17 | + var (renderedHtml, expectedHtml) = CreateTestXml( |
| 18 | + $"<h1 class='foo'></h1>", |
| 19 | + $"<div class='bar'></div>" |
| 20 | + ); |
| 21 | + |
| 22 | + Should.Throw<RazorComponentDoesNotMatchException>(() => renderedHtml.ShouldBe(expectedHtml)); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact(DisplayName = "ShouldNotMatch does not throws exception when expected and rendered does not matches")] |
| 26 | + public void ShoudNotMatchDoesNotThrowsWhenNoMatche() |
| 27 | + { |
| 28 | + var (renderedHtml, expectedHtml) = CreateTestXml( |
| 29 | + $"<h1 class='foo'></h1>", |
| 30 | + $"<div class='bar'></div>" |
| 31 | + ); |
| 32 | + |
| 33 | + renderedHtml.ShouldNotBe(expectedHtml); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact(DisplayName = "ShouldNotMatch throws exception when expected and rendered matches")] |
| 37 | + public void ShoudNotMatchThrowsWhenMatches() |
| 38 | + { |
| 39 | + var (renderedHtml, expectedHtml) = CreateTestXml( |
| 40 | + $"<div class='foo'></div>", |
| 41 | + $"<div class='foo'></div>" |
| 42 | + ); |
| 43 | + |
| 44 | + Should.Throw<RazorComponentsMatchException>(() => renderedHtml.ShouldNotBe(expectedHtml)); |
| 45 | + } |
| 46 | + |
| 47 | + [Theory(DisplayName = "Order of classes and whitespace in class='...' attribute doesn't matter when comparing")] |
| 48 | + [InlineData("", " ")] |
| 49 | + [InlineData("foo bar", "foo bar")] |
| 50 | + [InlineData("foo bar", "bar foo")] |
| 51 | + [InlineData("foo bar", "foo bar")] |
| 52 | + [InlineData("foo bar", "foo bar")] |
| 53 | + [InlineData(" foo bar", "foo bar ")] |
| 54 | + [InlineData(" foo bar", " bar foo")] |
| 55 | + public void ClassAttributeOrderTest(string inputClass, string expectedOutput) |
| 56 | + { |
| 57 | + var (renderedHtml, expectedHtml) = CreateTestXml( |
| 58 | + $"<div class='{inputClass}'></div>", |
| 59 | + $"<div class='{expectedOutput}'></div>" |
| 60 | + ); |
| 61 | + |
| 62 | + renderedHtml.ShouldBe(expectedHtml); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact(DisplayName = "Expected HTML with 'RegEx:' at the start of their attribute uses the following regex string to compare attribute")] |
| 66 | + public void RegexAttrTest() |
| 67 | + { |
| 68 | + var (renderedHtml, expectedHtml) = CreateTestXml( |
| 69 | + $"<div class='id-{GetHashCode()}'></div>", |
| 70 | + $"<div class='RegEx:^id-[\\d]+$'></div>" |
| 71 | + ); |
| 72 | + |
| 73 | + renderedHtml.ShouldBe(expectedHtml); |
| 74 | + } |
| 75 | + |
| 76 | + private static (XmlNode RenderedHtml, XmlNode ExpectedHtml) CreateTestXml(string renderedHtml, string expectedHtml) |
| 77 | + { |
| 78 | + var doc = new XmlDocument(); |
| 79 | + doc.LoadXml("<RenderResult>" + |
| 80 | + $"<RenderedHtml><Html>{renderedHtml}</Html></RenderedHtml>" + |
| 81 | + $"<ExpectedHtml><Html>{expectedHtml}</Html></ExpectedHtml>" + |
| 82 | + "</RenderResult>"); |
| 83 | + |
| 84 | + var test = doc.SelectSingleNode("RenderResult/RenderedHtml"); |
| 85 | + var control = doc.SelectSingleNode("RenderResult/ExpectedHtml"); |
| 86 | + |
| 87 | + return (test, control); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + } |
| 92 | +} |
0 commit comments