-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathXmlExpressionTests.cs
More file actions
368 lines (320 loc) · 13 KB
/
XmlExpressionTests.cs
File metadata and controls
368 lines (320 loc) · 13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.Tests.TestRunners;
using Xunit;
namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests;
/// <summary>
/// See https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/xml/how-to-transform-xml-by-using-linq
/// </summary>
public class XmlExpressionTests : ConverterTestBase
{
[Fact]
public async Task NestedXmlEchoSimpleAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim hello = ""Hello""
dim x = <h1><%= hello %></h1>
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
string hello = ""Hello"";
var x = new XElement(""h1"", hello);
}
}");
}
[Fact]
public async Task TwoLayerNestedXmlWithExpressionsAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim var1 = 1
Dim var2 = 2
dim x = <h1><%= var1 %><%= var2 %><span><%= var2 %><%= var1 %></span></h1>
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
int var1 = 1;
int var2 = 2;
var x = new XElement(""h1"", var1, var2, new XElement(""span"", var2, var1));
}
}");
}
[Fact]
public async Task MultiLineXmlExpressionsAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Linq
Class TestClass
Private Sub TestMethod()
Dim catalog =
<?xml version=""1.0""?>
<Catalog>
<Book id=""bk101"">
<Author>Garghentini, Davide</Author>
<Title>XML Developer's Guide</Title>
<Price>44.95</Price>
<Description>
An in-depth look at creating applications
with <technology>XML</technology>. For
<audience>beginners</audience> or
<audience>advanced</audience> developers.
</Description>
</Book>
<Book id=""bk331"">
<Author>Spencer, Phil</Author>
<Title>Developing Applications with Visual Basic .NET</Title>
<Price>45.95</Price>
<Description>
Get the expert insights, practical code samples,
and best practices you need
to advance your expertise with <technology>Visual
Basic .NET</technology>.
Learn how to create faster, more reliable applications
based on professional,
pragmatic guidance by today's top <audience>developers</audience>.
</Description>
</Book>
</Catalog>
Dim htmlOutput =
<html>
<body>
<%= From book In catalog.<Catalog>.<Book>
Select <div>
<h1><%= book.<Title>.Value %></h1>
<h3><%= ""By "" & book.<Author>.Value %></h3>
<h3><%= ""Price = "" & book.<Price>.Value %></h3>
<h2>Description</h2>
<%= TransformDescription(book.<Description>(0)) %>
<hr/>
</div> %>
</body>
</html>
End Sub
Public Function TransformDescription(s As String) As String
Return s
End Function
End Class", @"using System.Linq;
using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
var catalog = new XDocument(
new XElement(""Catalog"",
new XElement(""Book"", new XAttribute(""id"", ""bk101""),
new XElement(""Author"", ""Garghentini, Davide""),
new XElement(""Title"", ""XML Developer's Guide""),
new XElement(""Price"", ""44.95""),
new XElement(""Description"", ""\n An in-depth look at creating applications\n with "", new XElement(""technology"", ""XML""), "". For\n "", new XElement(""audience"", ""beginners""), "" or\n "", new XElement(""audience"", ""advanced""), "" developers.\n "")
),
new XElement(""Book"", new XAttribute(""id"", ""bk331""),
new XElement(""Author"", ""Spencer, Phil""),
new XElement(""Title"", ""Developing Applications with Visual Basic .NET""),
new XElement(""Price"", ""45.95""),
new XElement(""Description"", ""\n Get the expert insights, practical code samples,\n and best practices you need\n to advance your expertise with "", new XElement(""technology"", ""Visual\n Basic .NET""), "".\n Learn how to create faster, more reliable applications\n based on professional,\n pragmatic guidance by today's top "", new XElement(""audience"", ""developers""), "".\n "")
)
)
);
var htmlOutput = new XElement(""html"",
new XElement(""body"", from book in catalog.Elements(""Catalog"").Elements(""Book"")
select new XElement(""div"",
new XElement(""h1"", book.Elements(""Title"").Value),
new XElement(""h3"", ""By "" + book.Elements(""Author"").Value),
new XElement(""h3"", ""Price = "" + book.Elements(""Price"").Value),
new XElement(""h2"", ""Description""), TransformDescription((string)book.Elements(""Description"").ElementAtOrDefault(0)), new XElement(""hr"")
)
)
);
}
public string TransformDescription(string s)
{
return s;
}
}
1 target compilation errors:
CS1061: 'IEnumerable<XElement>' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'IEnumerable<XElement>' could be found (are you missing a using directive or an assembly reference?)", incompatibleWithAutomatedCommentTesting: true /* auto-testing of comments doesn't work because it tries to put VB comments inside the xml literal */);
//BUG: See compilation error
}
[Fact]
public async Task AssignmentStatementWithXmlElementAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim b = <someXmlTag></someXmlTag>
Dim c = <someXmlTag><bla anAttribute=""itsValue"">tata</bla><someContent>tata</someContent></someXmlTag>
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
var b = new XElement(""someXmlTag"");
var c = new XElement(""someXmlTag"", new XElement(""bla"", new XAttribute(""anAttribute"", ""itsValue""), ""tata""), new XElement(""someContent"", ""tata""));
}
}");
}
[Fact]
public async Task AssignmentStatementWithXmlElementAndEmbeddedExpressionAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Const value1 = ""something""
Dim xElement = <Elem1 Attr1=<%= value1 %> Attr2=<%= 100 %>></Elem1>
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
const string value1 = ""something"";
var xElement = new XElement(""Elem1"", new XAttribute(""Attr1"", value1), new XAttribute(""Attr2"", 100));
}
}");
}
[Fact]
public async Task SelfClosingXmlTagAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim xElement = <Elem1 Attr1=""something"" />
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
var xElement = new XElement(""Elem1"", new XAttribute(""Attr1"", ""something""));
}
}");
}
[Fact]
public async Task ConditionalMemberAccessAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim xDocument = <Test></Test>
Dim elements1 = xDocument.<Something>.SingleOrDefault()?.<SomethingElse>
End Sub
End Class", @"using System.Linq;
using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
var xDocument = new XElement(""Test"");
var elements1 = xDocument.Elements(""Something"").SingleOrDefault()?.Elements(""SomethingElse"");
}
}");
}
[Fact]
public async Task NamespaceImportAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"' Place Imports statements at the top of your program.
Imports <xmlns=""http://DefaultNamespace"">
Imports <xmlns:ns=""http://NewNamespace"">
Module Module1
Sub Main()
' Create element by using the default global XML namespace.
Dim inner = <innerElement/>
' Create element by using both the default global XML namespace and the namespace identified with the ""ns"" prefix.
Dim outer = <ns:outer><ns:innerElement attr=""value""></ns:innerElement><siblingElement></siblingElement><%= inner %></ns:outer>
' Display element to see its final form.
Console.WriteLine(outer)
End Sub
End Module", @"using System;
using System.Xml.Linq;
using XmlImports = XmlImportsCodeToConvert;
internal static class XmlImportsCodeToConvert
{
// Place Imports statements at the top of your program.
internal static readonly XNamespace Default = ""http://DefaultNamespace"";
internal static readonly XNamespace ns = ""http://NewNamespace"";
private static readonly XAttribute[] namespaceAttributes = {
new XAttribute(""xmlns"", Default.NamespaceName),
new XAttribute(XNamespace.Xmlns + ""ns"", ns.NamespaceName)
};
internal static XElement Apply(XElement x)
{
foreach (var d in x.DescendantsAndSelf())
{
foreach (var n in namespaceAttributes)
{
var a = d.Attribute(n.Name);
if (a != null && a.Value == n.Value)
{
a.Remove();
}
}
}
x.Add(namespaceAttributes);
return x;
}
internal static XDocument Apply(XDocument x)
{
Apply(x.Root);
return x;
}
}
internal static partial class Module1
{
public static void Main()
{
// Create element by using the default global XML namespace.
var inner = XmlImports.Apply(new XElement(XmlImports.Default + ""innerElement""));
// Create element by using both the default global XML namespace and the namespace identified with the ""ns"" prefix.
var outer = XmlImports.Apply(new XElement(XmlImports.ns + ""outer"", new XElement(XmlImports.ns + ""innerElement"", new XAttribute(""attr"", ""value"")), new XElement(XmlImports.Default + ""siblingElement""), inner));
// Display element to see its final form.
Console.WriteLine(outer);
}
}");
}
[Fact]
public async Task XmlBuiltinNamespaceAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim xElement = <Name xml:lang=""de"">Beispiel</Name>
Dim xElement2 = <Name xmlns:ns1=""http://www.example.com/namespace/1"">Beispiel</Name>
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
var xElement = new XElement(""Name"", new XAttribute(XNamespace.Xml + ""lang"", ""de""), ""Beispiel"");
var xElement2 = new XElement(""Name"", new XAttribute(XNamespace.Xmlns + ""ns1"", ""http://www.example.com/namespace/1""), ""Beispiel"");
}
}");
}
[Fact]
public async Task XmlCDataAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
Dim processedHtml = <![CDATA[
<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><title>
</head>
<body>
<p class=""cs95E872D0""><span class=""cs9D249CCB""> Regards,</span></p>
</body>
</html>
]]>.Value()
End Sub
End Class", @"using System.Xml.Linq;
internal partial class TestClass
{
private void TestMethod()
{
string processedHtml = new XCData(""\n<!DOCTYPE html PUBLIC \""-//W3C//DTD XHTML 1.0 Transitional//EN\"" \""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"">\n<html xmlns=\""http://www.w3.org/1999/xhtml\"">\n\t<head>\n\t\t<meta http-equiv=\""Content-Type\"" content=\""text/html; charset=utf-8\"" /><title>\n\t</head>\n\t<body>\n\t\t<p class=\""cs95E872D0\""><span class=\""cs9D249CCB\""> Regards,</span></p>\n </body>\n</html>\n "").Value;
}
}", incompatibleWithAutomatedCommentTesting: true);
}
}