1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
7
using ICSharpCode.NRefactory.VB.Parser;
8
using ICSharpCode.NRefactory.VB.Ast;
10
namespace ICSharpCode.NRefactory.VB.Tests.Ast
13
public class XmlExpressionTests
17
public void VBNetSimpleCommentTest()
19
XmlContentExpression content = ParseUtil.ParseExpression<XmlContentExpression>("<!-- test -->");
20
Assert.AreEqual(XmlContentType.Comment, content.Type);
21
Assert.AreEqual(" test ", content.Content);
22
Assert.AreEqual(new Location(1,1), content.StartLocation);
23
Assert.AreEqual(new Location(14,1), content.EndLocation);
27
public void VBNetSimplePreprocessingInstructionTest()
29
XmlContentExpression content = ParseUtil.ParseExpression<XmlContentExpression>("<?xml version='1.0'?>");
30
Assert.AreEqual(XmlContentType.ProcessingInstruction, content.Type);
31
Assert.AreEqual("xml version='1.0'", content.Content);
32
Assert.AreEqual(new Location(1,1), content.StartLocation);
33
Assert.AreEqual(new Location(22,1), content.EndLocation);
37
public void VBNetSimpleCDataTest()
39
XmlContentExpression content = ParseUtil.ParseExpression<XmlContentExpression>("<![CDATA[<simple> <cdata>]]>");
40
Assert.AreEqual(XmlContentType.CData, content.Type);
41
Assert.AreEqual("<simple> <cdata>", content.Content);
42
Assert.AreEqual(new Location(1,1), content.StartLocation);
43
Assert.AreEqual(new Location(29,1), content.EndLocation);
47
public void VBNetSimpleEmptyElementTest()
49
XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test />");
50
Assert.IsFalse(element.NameIsExpression);
51
Assert.AreEqual("Test", element.XmlName);
52
Assert.IsEmpty(element.Attributes);
53
Assert.IsEmpty(element.Children);
54
Assert.AreEqual(new Location(1,1), element.StartLocation);
55
Assert.AreEqual(new Location(9,1), element.EndLocation);
59
public void VBNetSimpleEmptyElementWithAttributeTest()
61
XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0' />");
62
Assert.IsFalse(element.NameIsExpression);
63
Assert.AreEqual("Test", element.XmlName);
64
Assert.IsNotEmpty(element.Attributes);
65
Assert.AreEqual(1, element.Attributes.Count);
66
Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
67
XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
68
Assert.AreEqual("id", attribute.Name);
69
Assert.IsTrue(attribute.IsLiteralValue);
70
Assert.IsTrue(attribute.ExpressionValue.IsNull);
71
Assert.AreEqual("0", attribute.LiteralValue);
72
Assert.AreEqual(new Location(7,1), attribute.StartLocation);
73
Assert.AreEqual(new Location(13,1), attribute.EndLocation);
74
Assert.IsEmpty(element.Children);
75
Assert.AreEqual(new Location(1,1), element.StartLocation);
76
Assert.AreEqual(new Location(16,1), element.EndLocation);
80
public void VBNetSimpleEmptyElementWithAttributesTest()
82
XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0' name=<%= name %> <%= contentData %> />"); Assert.IsFalse(element.NameIsExpression);
83
Assert.AreEqual("Test", element.XmlName);
84
Assert.IsNotEmpty(element.Attributes);
85
Assert.AreEqual(3, element.Attributes.Count);
87
Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
88
XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
89
Assert.AreEqual("id", attribute.Name);
90
Assert.IsTrue(attribute.IsLiteralValue);
91
Assert.IsTrue(attribute.ExpressionValue.IsNull);
92
Assert.AreEqual("0", attribute.LiteralValue);
93
Assert.AreEqual(new Location(7,1), attribute.StartLocation);
94
Assert.AreEqual(new Location(13,1), attribute.EndLocation);
96
Assert.IsTrue(element.Attributes[1] is XmlAttributeExpression);
97
XmlAttributeExpression attribute2 = element.Attributes[1] as XmlAttributeExpression;
98
Assert.AreEqual("name", attribute2.Name);
99
Assert.IsFalse(attribute2.IsLiteralValue);
100
Assert.IsFalse(attribute2.ExpressionValue.IsNull);
102
Assert.IsTrue(attribute2.ExpressionValue is IdentifierExpression);
103
IdentifierExpression identifier = attribute2.ExpressionValue as IdentifierExpression;
104
Assert.AreEqual("name", identifier.Identifier);
105
Assert.AreEqual(new Location(23,1), identifier.StartLocation);
106
Assert.AreEqual(new Location(27,1), identifier.EndLocation);
108
Assert.AreEqual(new Location(14,1), attribute2.StartLocation);
109
Assert.AreEqual(new Location(30,1), attribute2.EndLocation);
111
Assert.IsTrue(element.Attributes[2] is XmlEmbeddedExpression);
112
XmlEmbeddedExpression attribute3 = element.Attributes[2] as XmlEmbeddedExpression;
114
Assert.IsTrue(attribute3.InlineVBExpression is IdentifierExpression);
115
IdentifierExpression identifier2 = attribute3.InlineVBExpression as IdentifierExpression;
117
Assert.AreEqual("contentData", identifier2.Identifier);
118
Assert.AreEqual(new Location(35,1), identifier2.StartLocation);
119
Assert.AreEqual(new Location(46,1), identifier2.EndLocation);
121
Assert.AreEqual(new Location(31,1), attribute3.StartLocation);
122
Assert.AreEqual(new Location(49,1), attribute3.EndLocation);
124
Assert.IsEmpty(element.Children);
125
Assert.AreEqual(new Location(1,1), element.StartLocation);
126
Assert.AreEqual(new Location(52,1), element.EndLocation);
130
public void VBNetElementWithAttributeTest()
132
XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0'>\n" +
136
Assert.IsFalse(element.NameIsExpression);
137
Assert.AreEqual("Test", element.XmlName);
139
Assert.IsNotEmpty(element.Attributes);
140
Assert.AreEqual(1, element.Attributes.Count);
141
Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
142
XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
143
Assert.AreEqual("id", attribute.Name);
144
Assert.IsTrue(attribute.IsLiteralValue);
145
Assert.IsTrue(attribute.ExpressionValue.IsNull);
146
Assert.AreEqual("0", attribute.LiteralValue);
147
Assert.AreEqual(new Location(7,1), attribute.StartLocation);
148
Assert.AreEqual(new Location(13,1), attribute.EndLocation);
150
Assert.IsNotEmpty(element.Children);
151
Assert.AreEqual(5, element.Children.Count);
153
CheckContent(element.Children[0], "\n\t", XmlContentType.Text, new Location(14,1), new Location(2,2));
154
CheckContent(element.Children[2], "\n\t", XmlContentType.Text, new Location(10,2), new Location(2,3));
155
CheckContent(element.Children[4], "\n", XmlContentType.Text, new Location(10,3), new Location(1,4));
157
CheckElement(element.Children[1], "Item", new Location(2,2), new Location(10,2));
158
CheckElement(element.Children[3], "Item", new Location(2,3), new Location(10,3));
160
Assert.AreEqual(new Location(1,1), element.StartLocation);
161
Assert.AreEqual(new Location(8,4), element.EndLocation);
165
public void VBNetElementWithMixedContentTest()
167
XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0'>\n" +
171
" <![CDATA[<cdata> section]]>\n" +
173
Assert.IsFalse(element.NameIsExpression);
174
Assert.AreEqual("Test", element.XmlName);
176
Assert.IsNotEmpty(element.Attributes);
177
Assert.AreEqual(1, element.Attributes.Count);
178
Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
179
XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
180
Assert.AreEqual("id", attribute.Name);
181
Assert.IsTrue(attribute.IsLiteralValue);
182
Assert.IsTrue(attribute.ExpressionValue.IsNull);
183
Assert.AreEqual("0", attribute.LiteralValue);
184
Assert.AreEqual(new Location(7,1), attribute.StartLocation);
185
Assert.AreEqual(new Location(13,1), attribute.EndLocation);
187
Assert.IsNotEmpty(element.Children);
188
Assert.AreEqual(9, element.Children.Count);
190
CheckContent(element.Children[0], "\n\t", XmlContentType.Text, new Location(14,1), new Location(2,2));
191
CheckContent(element.Children[2], "\n\t", XmlContentType.Text, new Location(15,2), new Location(2,3));
192
CheckContent(element.Children[4], "\n\t", XmlContentType.Text, new Location(10,3), new Location(2,4));
193
CheckContent(element.Children[6], "\n\t", XmlContentType.Text, new Location(10,4), new Location(2,5));
194
CheckContent(element.Children[7], "<cdata> section", XmlContentType.CData, new Location(2,5), new Location(29,5));
195
CheckContent(element.Children[8], "\n", XmlContentType.Text, new Location(29,5), new Location(1,6));
197
CheckContent(element.Children[1], " test ", XmlContentType.Comment, new Location(2,2), new Location(15,2));
198
CheckElement(element.Children[3], "Item", new Location(2,3), new Location(10,3));
199
CheckElement(element.Children[5], "Item", new Location(2,4), new Location(10,4));
201
Assert.AreEqual(new Location(1,1), element.StartLocation);
202
Assert.AreEqual(new Location(8,6), element.EndLocation);
206
public void VBNetElementWithMixedContentTest2()
208
XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test> aaaa </Test>");
209
Assert.IsFalse(element.NameIsExpression);
210
Assert.AreEqual("Test", element.XmlName);
212
Assert.IsNotEmpty(element.Children);
213
Assert.AreEqual(1, element.Children.Count);
215
CheckContent(element.Children[0], " aaaa ", XmlContentType.Text, new Location(7,1), new Location(14,1));
219
public void VBNetProcessingInstructionAndCommentAtEndTest()
221
XmlDocumentExpression document = ParseUtil.ParseExpression<XmlDocumentExpression>("<Test />\n" +
223
"<?target some text?>");
224
Assert.IsNotEmpty(document.Expressions);
225
Assert.AreEqual(3, document.Expressions.Count);
227
CheckElement(document.Expressions[0], "Test", new Location(1,1), new Location(9,1));
228
CheckContent(document.Expressions[1], " test ", XmlContentType.Comment, new Location(1,2), new Location(14,2));
229
CheckContent(document.Expressions[2], "target some text", XmlContentType.ProcessingInstruction, new Location(1,3), new Location(21,3));
233
void CheckElement(AstNode node, string name, AstLocation start, AstLocation end)
235
Assert.IsTrue(node is XmlElementExpression);
236
XmlElementExpression expr = node as XmlElementExpression;
237
Assert.IsEmpty(expr.Attributes);
238
Assert.IsEmpty(expr.Children);
239
Assert.IsFalse(expr.NameIsExpression);
240
Assert.AreEqual(name, expr.XmlName);
241
Assert.AreEqual(start, expr.StartLocation);
242
Assert.AreEqual(end, expr.EndLocation);
245
void CheckContent(AstNode node, string content, XmlContentType type, AstLocation start, AstLocation end)
247
Assert.IsTrue(node is XmlContentExpression);
248
XmlContentExpression expr = node as XmlContentExpression;
249
Assert.AreEqual(type, expr.Type);
250
Assert.AreEqual(content, expr.Content);
251
Assert.AreEqual(start, expr.StartLocation);
252
Assert.AreEqual(end, expr.EndLocation);