Skip to content

Commit 9ffe651

Browse files
committed
Add tests to improve coverage in XmlSyntaxFactory
1 parent 4d9ec81 commit 9ffe651

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.HelperTests
5+
{
6+
using System;
7+
using Analyzers.Helpers;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Xunit;
11+
12+
public class XmlSyntaxFactoryTests
13+
{
14+
[Fact]
15+
public void TestPlainTextAttribute()
16+
{
17+
Assert.Equal(" name=\"value\"", XmlSyntaxFactory.TextAttribute("name", "value").ToFullString());
18+
}
19+
20+
[Fact(Skip = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1590")]
21+
public void TestEscapedTextAttribute()
22+
{
23+
Assert.Equal(" name=\""value"\"", XmlSyntaxFactory.TextAttribute("name", "\"value\"").ToFullString());
24+
Assert.Equal(" name=\"value&2\"", XmlSyntaxFactory.TextAttribute("name", "value&2").ToFullString());
25+
}
26+
27+
[Fact]
28+
public void TestTextAttributeQuotes()
29+
{
30+
SyntaxTokenList textTokens = SyntaxFactory.TokenList(XmlSyntaxFactory.TextLiteral("value"));
31+
Assert.Equal(" name=\"value\"", XmlSyntaxFactory.TextAttribute("name", SyntaxKind.DoubleQuoteToken, textTokens).ToFullString());
32+
Assert.Equal(" name='value'", XmlSyntaxFactory.TextAttribute("name", SyntaxKind.SingleQuoteToken, textTokens).ToFullString());
33+
}
34+
35+
[Fact]
36+
public void TestNameAttribute()
37+
{
38+
Assert.Equal(" name=\"value\"", XmlSyntaxFactory.NameAttribute("value").ToFullString());
39+
}
40+
41+
[Fact]
42+
public void TestTextNewLine()
43+
{
44+
Assert.Equal("\r\n/// ", XmlSyntaxFactory.TextNewLine("\r\n", true).ToFullString());
45+
Assert.Equal("\r\n", XmlSyntaxFactory.TextNewLine("\r\n", false).ToFullString());
46+
Assert.Equal("\n/// ", XmlSyntaxFactory.TextNewLine("\n", true).ToFullString());
47+
Assert.Equal("\n", XmlSyntaxFactory.TextNewLine("\n", false).ToFullString());
48+
}
49+
50+
[Fact]
51+
public void TestSummaryElement()
52+
{
53+
string expected =
54+
"<summary>\r\n"
55+
+ "/// Summary.\r\n"
56+
+ "/// </summary>";
57+
Assert.Equal(expected, XmlSyntaxFactory.SummaryElement("\r\n", XmlSyntaxFactory.Text("Summary.")).ToFullString());
58+
}
59+
60+
[Fact]
61+
public void TestRemarksElement()
62+
{
63+
string expected =
64+
"<remarks>\r\n"
65+
+ "/// <para>Remarks.</para>\r\n"
66+
+ "/// </remarks>";
67+
Assert.Equal(expected, XmlSyntaxFactory.RemarksElement("\r\n", XmlSyntaxFactory.ParaElement(XmlSyntaxFactory.Text("Remarks."))).ToFullString());
68+
}
69+
70+
[Fact]
71+
public void TestReturnsElement()
72+
{
73+
string expected =
74+
"<returns>\r\n"
75+
+ "/// Returns.\r\n"
76+
+ "/// </returns>";
77+
Assert.Equal(expected, XmlSyntaxFactory.ReturnsElement("\r\n", XmlSyntaxFactory.Text("Returns.")).ToFullString());
78+
}
79+
80+
[Fact]
81+
public void TestValueElement()
82+
{
83+
string expected =
84+
"<value>\r\n"
85+
+ "/// Value.\r\n"
86+
+ "/// </value>";
87+
Assert.Equal(expected, XmlSyntaxFactory.ValueElement("\r\n", XmlSyntaxFactory.Text("Value.")).ToFullString());
88+
}
89+
90+
[Fact]
91+
public void TestExceptionElement()
92+
{
93+
string expected = "<exception cref=\"System.ArgumentNullException\">Condition.</exception>";
94+
Assert.Equal(expected, XmlSyntaxFactory.ExceptionElement(SyntaxFactory.TypeCref(SyntaxFactory.ParseTypeName(typeof(ArgumentNullException).FullName)), XmlSyntaxFactory.Text("Condition.")).ToFullString());
95+
}
96+
97+
[Fact]
98+
public void TestParamElement()
99+
{
100+
string expected = "<param name=\"parameterName\">Parameter.</param>";
101+
Assert.Equal(expected, XmlSyntaxFactory.ParamElement("parameterName", XmlSyntaxFactory.Text("Parameter.")).ToFullString());
102+
}
103+
104+
[Fact]
105+
public void TestParamRefElement()
106+
{
107+
string expected = "<paramref name=\"parameterName\"/>";
108+
Assert.Equal(expected, XmlSyntaxFactory.ParamRefElement("parameterName").ToFullString());
109+
}
110+
111+
[Fact]
112+
public void TestSeeAlsoElementCref()
113+
{
114+
string expected = "<seealso cref=\"System.ArgumentNullException\"/>";
115+
Assert.Equal(expected, XmlSyntaxFactory.SeeAlsoElement(SyntaxFactory.TypeCref(SyntaxFactory.ParseTypeName(typeof(ArgumentNullException).FullName))).ToFullString());
116+
}
117+
118+
[Fact]
119+
public void TestSeeAlsoElementHref()
120+
{
121+
string expected = "<seealso href=\"http://www.example.com/\">Text.</seealso>";
122+
Assert.Equal(expected, XmlSyntaxFactory.SeeAlsoElement(new Uri("http://www.example.com/"), XmlSyntaxFactory.List(XmlSyntaxFactory.Text("Text."))).ToFullString());
123+
}
124+
125+
[Fact]
126+
public void TestNullKeywordElement()
127+
{
128+
string expected = "<see langword=\"null\"/>";
129+
Assert.Equal(expected, XmlSyntaxFactory.NullKeywordElement().ToFullString());
130+
}
131+
132+
[Fact]
133+
public void TestPlaceholderElement()
134+
{
135+
string expected = "<placeholder>Content.</placeholder>";
136+
Assert.Equal(expected, XmlSyntaxFactory.PlaceholderElement(XmlSyntaxFactory.Text("Content.")).ToFullString());
137+
}
138+
139+
[Fact]
140+
public void TestThreadSafetyElement()
141+
{
142+
string expected = "<threadsafety static=\"true\" instance=\"false\"/>";
143+
Assert.Equal(expected, XmlSyntaxFactory.ThreadSafetyElement().ToFullString());
144+
}
145+
146+
[Fact]
147+
public void TestPreliminaryElement()
148+
{
149+
string expected = "<preliminary/>";
150+
Assert.Equal(expected, XmlSyntaxFactory.PreliminaryElement().ToFullString());
151+
}
152+
153+
[Fact]
154+
public void TestTokenElement()
155+
{
156+
string expected = "<token>Identifier</token>";
157+
Assert.Equal(expected, XmlSyntaxFactory.TokenElement("Identifier").ToFullString());
158+
}
159+
}
160+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Compile Include="HelperTests\AccessLevelHelperTests.cs" />
174174
<Compile Include="HelperTests\TokenHelperTests.cs" />
175175
<Compile Include="HelperTests\TriviaHelperTests.cs" />
176+
<Compile Include="HelperTests\XmlSyntaxFactoryTests.cs" />
176177
<Compile Include="LayoutRules\SA1500\SA1500UnitTests.Blocks.cs" />
177178
<Compile Include="LayoutRules\SA1500\SA1500UnitTests.Classes.cs" />
178179
<Compile Include="LayoutRules\SA1500\SA1500UnitTests.Constructors.cs" />

0 commit comments

Comments
 (0)