PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.VB.Tests/Parser/Expressions/XmlExpressionTests.cs

http://github.com/icsharpcode/ILSpy
C# | 255 lines | 218 code | 35 blank | 2 comment | 0 complexity | 67b55769f31d50eb13e1f8e470aa57b3 MD5 | raw file
  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)
  3
  4using System;
  5using System.IO;
  6using NUnit.Framework;
  7using ICSharpCode.NRefactory.VB.Parser;
  8using ICSharpCode.NRefactory.VB.Ast;
  9
 10namespace ICSharpCode.NRefactory.VB.Tests.Ast
 11{
 12	[TestFixture]
 13	public class XmlExpressionTests
 14	{
 15		#region VB.NET
 16		[Test]
 17		public void VBNetSimpleCommentTest()
 18		{
 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);
 24		}
 25		
 26		[Test]
 27		public void VBNetSimplePreprocessingInstructionTest()
 28		{
 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);
 34		}
 35		
 36		[Test]
 37		public void VBNetSimpleCDataTest()
 38		{
 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);
 44		}
 45		
 46		[Test]
 47		public void VBNetSimpleEmptyElementTest()
 48		{
 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);
 56		}
 57		
 58		[Test]
 59		public void VBNetSimpleEmptyElementWithAttributeTest()
 60		{
 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);
 77		}
 78		
 79		[Test]
 80		public void VBNetSimpleEmptyElementWithAttributesTest()
 81		{
 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);
 86			
 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);
 95			
 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);
101			
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);
107			
108			Assert.AreEqual(new Location(14,1), attribute2.StartLocation);
109			Assert.AreEqual(new Location(30,1), attribute2.EndLocation);
110			
111			Assert.IsTrue(element.Attributes[2] is XmlEmbeddedExpression);
112			XmlEmbeddedExpression attribute3 = element.Attributes[2] as XmlEmbeddedExpression;
113			
114			Assert.IsTrue(attribute3.InlineVBExpression is IdentifierExpression);
115			IdentifierExpression identifier2 = attribute3.InlineVBExpression as IdentifierExpression;
116			
117			Assert.AreEqual("contentData", identifier2.Identifier);
118			Assert.AreEqual(new Location(35,1), identifier2.StartLocation);
119			Assert.AreEqual(new Location(46,1), identifier2.EndLocation);
120			
121			Assert.AreEqual(new Location(31,1), attribute3.StartLocation);
122			Assert.AreEqual(new Location(49,1), attribute3.EndLocation);
123			
124			Assert.IsEmpty(element.Children);
125			Assert.AreEqual(new Location(1,1), element.StartLocation);
126			Assert.AreEqual(new Location(52,1), element.EndLocation);
127		}
128		
129		[Test]
130		public void VBNetElementWithAttributeTest()
131		{
132			XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0'>\n" +
133			                                                                                    "	<Item />\n" +
134			                                                                                    "	<Item />\n" +
135			                                                                                    "</Test>");
136			Assert.IsFalse(element.NameIsExpression);
137			Assert.AreEqual("Test", element.XmlName);
138			
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);
149			
150			Assert.IsNotEmpty(element.Children);
151			Assert.AreEqual(5, element.Children.Count);
152			
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));
156			
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));
159			
160			Assert.AreEqual(new Location(1,1), element.StartLocation);
161			Assert.AreEqual(new Location(8,4), element.EndLocation);
162		}
163		
164		[Test]
165		public void VBNetElementWithMixedContentTest()
166		{
167			XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0'>\n" +
168			                                                                                    "	<!-- test -->\n" +
169			                                                                                    "	<Item />\n" +
170			                                                                                    "	<Item />\n" +
171			                                                                                    "	<![CDATA[<cdata> section]]>\n" +
172			                                                                                    "</Test>");
173			Assert.IsFalse(element.NameIsExpression);
174			Assert.AreEqual("Test", element.XmlName);
175			
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);
186			
187			Assert.IsNotEmpty(element.Children);
188			Assert.AreEqual(9, element.Children.Count);
189			
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));
196			
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));
200			
201			Assert.AreEqual(new Location(1,1), element.StartLocation);
202			Assert.AreEqual(new Location(8,6), element.EndLocation);
203		}
204		
205		[Test]
206		public void VBNetElementWithMixedContentTest2()
207		{
208			XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test>  aaaa	</Test>");
209			Assert.IsFalse(element.NameIsExpression);
210			Assert.AreEqual("Test", element.XmlName);
211			
212			Assert.IsNotEmpty(element.Children);
213			Assert.AreEqual(1, element.Children.Count);
214			
215			CheckContent(element.Children[0], "  aaaa	", XmlContentType.Text, new Location(7,1), new Location(14,1));
216		}
217		
218		[Test]
219		public void VBNetProcessingInstructionAndCommentAtEndTest()
220		{
221			XmlDocumentExpression document = ParseUtil.ParseExpression<XmlDocumentExpression>("<Test />\n" +
222			                                                                                       "<!-- test -->\n" +
223			                                                                                       "<?target some text?>");
224			Assert.IsNotEmpty(document.Expressions);
225			Assert.AreEqual(3, document.Expressions.Count);
226			
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));
230		}
231		#endregion
232		
233		void CheckElement(AstNode node, string name, AstLocation start, AstLocation end)
234		{
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);
243		}
244		
245		void CheckContent(AstNode node, string content, XmlContentType type, AstLocation start, AstLocation end)
246		{
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);
253		}
254	}
255}