forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlTextWriterTests.cs
More file actions
92 lines (79 loc) · 3.97 KB
/
XmlTextWriterTests.cs
File metadata and controls
92 lines (79 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using Xunit;
namespace System.Xml.Tests
{
public class XmlTextWriterTests
{
public static IEnumerable<object[]> PositiveTestCases
{
get
{
yield return new string[] { null }; // will be normalized to empty string
yield return new string[] { "" };
yield return new string[] { "This is some data." };
yield return new string[] { " << brackets and whitespace >> " }; // brackets & surrounding whitespace are ok
yield return new string[] { "&" }; // entities are ok (treated opaquely)
yield return new string[] { "Hello\r\nthere." }; // newlines are ok
yield return new string[] { "\U0001F643 Upside-down smiley \U0001F643" }; // correctly paired surrogates are ok
yield return new string[] { "\uFFFD\uFFFE\uFFFF" }; // replacement char & private use are ok
}
}
public static IEnumerable<object[]> BadSurrogateTestCases
{
get
{
yield return new string[] { "\uD800 Unpaired high surrogate." };
yield return new string[] { "\uDFFF Unpaired low surrogate." };
yield return new string[] { "Unpaired high surrogate at end. \uD800" };
yield return new string[] { "Unpaired low surrogate at end. \uDFFF" };
yield return new string[] { "Unpaired surrogates \uDFFF\uD800 in middle." };
}
}
[Theory]
[MemberData(nameof(PositiveTestCases))]
[InlineData("]]")] // ]] without trailing > is ok
[InlineData("-->")] // end of comment marker ok (meaningless to cdata tag)
public void WriteCData_SuccessCases(string cdataText)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.WriteCData(cdataText);
Assert.Equal($"<![CDATA[{cdataText}]]>", sw.ToString());
}
[Theory]
[MemberData(nameof(BadSurrogateTestCases), DisableDiscoveryEnumeration = true)] // disable enumeration to avoid test harness misinterpreting unpaired surrogates
[InlineData("]]>")] // end of cdata marker forbidden (ambiguous close tag)
public void WriteCData_FailureCases(string cdataText)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
Assert.Throws<ArgumentException>(() => xw.WriteCData(cdataText));
}
[Theory]
[MemberData(nameof(PositiveTestCases))]
[InlineData("-12345")] // hyphen at beginning is ok
[InlineData("123- -45")] // single hyphens are ok in middle
[InlineData("]]>")] // end of cdata marker ok (meaningless to comment tag)
public void WriteComment_SuccessCases(string commentText)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.WriteComment(commentText);
Assert.Equal($"<!--{commentText}-->", sw.ToString());
}
[Theory]
[MemberData(nameof(BadSurrogateTestCases), DisableDiscoveryEnumeration = true)] // disable enumeration to avoid test harness misinterpreting unpaired surrogates
[InlineData("123--45")] // double-hyphen in middle is forbidden (ambiguous comment close tag)
[InlineData("12345-")] // hyphen at end is forbidden (ambiguous comment close tag)
[InlineData("-->")] // end of comment marker forbidden (ambiguous close tag)
public void WriteComment_FailureCases(string commentText)
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
Assert.Throws<ArgumentException>(() => xw.WriteComment(commentText));
}
}
}