Skip to content

Commit 26b8c2a

Browse files
committed
Added tests for the DOMParser #15
1 parent 380f0a9 commit 26b8c2a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

AngleSharp.Scripting.JavaScript.Tests/AngleSharp.Scripting.JavaScript.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Otherwise />
8080
</Choose>
8181
<ItemGroup>
82+
<Compile Include="ComponentTests.cs" />
8283
<Compile Include="FireEventTests.cs" />
8384
<Compile Include="GeneratorTests.cs" />
8485
<Compile Include="Helper.cs" />
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace AngleSharp.Scripting.JavaScript.Tests
2+
{
3+
using AngleSharp.Scripting.JavaScript.Services;
4+
using NUnit.Framework;
5+
using System;
6+
using System.Threading.Tasks;
7+
8+
[TestFixture]
9+
public class ComponentTests
10+
{
11+
static async Task<String> RunScriptComponent(String script)
12+
{
13+
var service = new ScriptingService();
14+
var cfg = Configuration.Default.With(service);
15+
var html = String.Concat("<!doctype html><script>", script, "</script>");
16+
var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html));
17+
var value = service.Engine.GetJint(document).GetValue("assert");
18+
return value.AsString();
19+
}
20+
21+
[Test]
22+
public async Task DomParserShouldWorkWithHtml()
23+
{
24+
var script = @"var htmlSource = '<span>Hello World!</span>';
25+
var parser = new DOMParser();
26+
var doc = parser.parseFromString(htmlSource, 'text/html');
27+
var assert = doc.querySelector('span').textContent;";
28+
var value = await RunScriptComponent(script);
29+
Assert.AreEqual("Hello World!", value);
30+
}
31+
32+
[Test]
33+
public async Task DomParserShouldWorkWithXml()
34+
{
35+
var script = @"var xmlSource = '<parsererror xmlns=""http://www.mozilla.org/newlayout/xml/parsererror.xml"">(error description)<sourcetext></sourcetext></parsererror>';
36+
var parser = new DOMParser();
37+
var doc = parser.parseFromString(xmlSource, 'application/xml');
38+
var assert = doc.querySelector('parsererror').textContent;";
39+
var value = await RunScriptComponent(script);
40+
Assert.AreEqual("(error description)", value);
41+
}
42+
43+
[Test]
44+
[ExpectedException(typeof(ArgumentException))]
45+
public async Task DomParserShouldNotWorkWithMathMl()
46+
{
47+
var script = @"var xmlSource = '<math> <mrow> <msup><mi> a </mi><mn>2</mn></msup> <mo> + </mo> <msup><mi> b </mi><mn>2</mn></msup> <mo> = </mo> <msup><mi> c </mi><mn>2</mn></msup> </mrow> </math>';
48+
var parser = new DOMParser();
49+
var doc = parser.parseFromString(xmlSource, 'application/mathml+xml');
50+
var assert = 'failed';";
51+
var value = await RunScriptComponent(script);
52+
}
53+
54+
[Test]
55+
public async Task DomParserShouldShowErrorDescriptionForMalformedXml()
56+
{
57+
var script = @"var xmlSource = '<foo>';
58+
var parser = new DOMParser();
59+
var doc = parser.parseFromString(xmlSource, 'application/xml');
60+
var assert = doc.querySelector('parsererror').textContent;";
61+
var value = await RunScriptComponent(script);
62+
Assert.AreEqual("Error while parsing the provided XML document.", value);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)