Skip to content

Commit f84327c

Browse files
Merge pull request #14 from webexpress-framework/copilot/implement-reverse-html-renderer
Add reverse HTML parser for WebExpress.WebCore.WebHtml
2 parents 9fd22e2 + 1e86fb9 commit f84327c

10 files changed

Lines changed: 1931 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using WebExpress.WebCore.WebHtml;
2+
using WebExpress.WebCore.WebHtml.Parser;
3+
4+
namespace WebExpress.WebCore.Test.Html.Parser
5+
{
6+
/// <summary>
7+
/// Unit tests for the <see cref="HtmlElementFactory"/> class.
8+
/// </summary>
9+
[Collection("NonParallelTests")]
10+
public class UnitTestHtmlElementFactory
11+
{
12+
/// <summary>
13+
/// Known tags are resolved to their specific subclass.
14+
/// </summary>
15+
[Fact]
16+
public void KnownTag_Div_ReturnsCorrectType()
17+
{
18+
var element = HtmlElementFactory.Create("div");
19+
20+
Assert.IsType<HtmlElementTextContentDiv>(element);
21+
}
22+
23+
/// <summary>
24+
/// Known tags are resolved to their specific subclass.
25+
/// </summary>
26+
[Fact]
27+
public void KnownTag_Span_ReturnsCorrectType()
28+
{
29+
var element = HtmlElementFactory.Create("span");
30+
31+
Assert.IsType<HtmlElementTextSemanticsSpan>(element);
32+
}
33+
34+
/// <summary>
35+
/// Known tags are resolved to their specific subclass.
36+
/// </summary>
37+
[Fact]
38+
public void KnownTag_Img_ReturnsCorrectType()
39+
{
40+
var element = HtmlElementFactory.Create("img");
41+
42+
Assert.IsType<HtmlElementMultimediaImg>(element);
43+
}
44+
45+
/// <summary>
46+
/// Known tags are resolved to their specific subclass.
47+
/// </summary>
48+
[Fact]
49+
public void KnownTag_Input_ReturnsCorrectType()
50+
{
51+
var element = HtmlElementFactory.Create("input");
52+
53+
Assert.IsType<HtmlElementFieldInput>(element);
54+
}
55+
56+
/// <summary>
57+
/// Known tags are resolved to their specific subclass.
58+
/// </summary>
59+
[Fact]
60+
public void KnownTag_Table_ReturnsCorrectType()
61+
{
62+
var element = HtmlElementFactory.Create("table");
63+
64+
Assert.IsType<HtmlElementTableTable>(element);
65+
}
66+
67+
/// <summary>
68+
/// Factory is case-insensitive – upper-case tag names resolve correctly.
69+
/// </summary>
70+
[Fact]
71+
public void CaseInsensitive_UpperCase_ReturnsCorrectType()
72+
{
73+
var element = HtmlElementFactory.Create("DIV");
74+
75+
Assert.IsType<HtmlElementTextContentDiv>(element);
76+
}
77+
78+
/// <summary>
79+
/// An unknown tag name returns a generic <see cref="HtmlElement"/>.
80+
/// </summary>
81+
[Fact]
82+
public void UnknownTag_ReturnsGenericElement()
83+
{
84+
var element = HtmlElementFactory.Create("x-custom-widget");
85+
86+
Assert.IsType<HtmlElement>(element);
87+
}
88+
89+
/// <summary>
90+
/// <see cref="HtmlElementFactory.IsKnown"/> returns <c>true</c> for known tags.
91+
/// </summary>
92+
[Fact]
93+
public void IsKnown_KnownTag_ReturnsTrue()
94+
{
95+
Assert.True(HtmlElementFactory.IsKnown("div"));
96+
}
97+
98+
/// <summary>
99+
/// <see cref="HtmlElementFactory.IsKnown"/> returns <c>false</c> for unknown tags.
100+
/// </summary>
101+
[Fact]
102+
public void IsKnown_UnknownTag_ReturnsFalse()
103+
{
104+
Assert.False(HtmlElementFactory.IsKnown("x-unknown"));
105+
}
106+
107+
/// <summary>
108+
/// Passing <c>null</c> to <see cref="HtmlElementFactory.Create"/> throws an
109+
/// <see cref="System.ArgumentNullException"/>.
110+
/// </summary>
111+
[Fact]
112+
public void NullTagName_Throws()
113+
{
114+
Assert.Throws<System.ArgumentNullException>(() => HtmlElementFactory.Create(null));
115+
}
116+
117+
/// <summary>
118+
/// Both 'kbd' and 'kdb' map to the existing <see cref="HtmlElementTextSemanticsKdb"/>.
119+
/// </summary>
120+
[Fact]
121+
public void KbdTag_MapsToKdbElement()
122+
{
123+
var element = HtmlElementFactory.Create("kbd");
124+
125+
Assert.IsType<HtmlElementTextSemanticsKdb>(element);
126+
}
127+
128+
/// <summary>
129+
/// The 'keygen' tag is resolved to <see cref="HtmlElementFormKeygen"/>.
130+
/// </summary>
131+
[Fact]
132+
public void KnownTag_Keygen_ReturnsCorrectType()
133+
{
134+
var element = HtmlElementFactory.Create("keygen");
135+
136+
Assert.IsType<HtmlElementFormKeygen>(element);
137+
}
138+
139+
/// <summary>
140+
/// The 'command' tag is resolved to <see cref="HtmlElementInteractiveCommand"/>.
141+
/// </summary>
142+
[Fact]
143+
public void KnownTag_Command_ReturnsCorrectType()
144+
{
145+
var element = HtmlElementFactory.Create("command");
146+
147+
Assert.IsType<HtmlElementInteractiveCommand>(element);
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)