/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. using System;
  4. using System.IO;
  5. using NUnit.Framework;
  6. using ICSharpCode.NRefactory.VB.Parser;
  7. using ICSharpCode.NRefactory.VB.Ast;
  8. namespace ICSharpCode.NRefactory.VB.Tests.Ast
  9. {
  10. [TestFixture]
  11. public class XmlExpressionTests
  12. {
  13. #region VB.NET
  14. [Test]
  15. public void VBNetSimpleCommentTest()
  16. {
  17. XmlContentExpression content = ParseUtil.ParseExpression<XmlContentExpression>("<!-- test -->");
  18. Assert.AreEqual(XmlContentType.Comment, content.Type);
  19. Assert.AreEqual(" test ", content.Content);
  20. Assert.AreEqual(new Location(1,1), content.StartLocation);
  21. Assert.AreEqual(new Location(14,1), content.EndLocation);
  22. }
  23. [Test]
  24. public void VBNetSimplePreprocessingInstructionTest()
  25. {
  26. XmlContentExpression content = ParseUtil.ParseExpression<XmlContentExpression>("<?xml version='1.0'?>");
  27. Assert.AreEqual(XmlContentType.ProcessingInstruction, content.Type);
  28. Assert.AreEqual("xml version='1.0'", content.Content);
  29. Assert.AreEqual(new Location(1,1), content.StartLocation);
  30. Assert.AreEqual(new Location(22,1), content.EndLocation);
  31. }
  32. [Test]
  33. public void VBNetSimpleCDataTest()
  34. {
  35. XmlContentExpression content = ParseUtil.ParseExpression<XmlContentExpression>("<![CDATA[<simple> <cdata>]]>");
  36. Assert.AreEqual(XmlContentType.CData, content.Type);
  37. Assert.AreEqual("<simple> <cdata>", content.Content);
  38. Assert.AreEqual(new Location(1,1), content.StartLocation);
  39. Assert.AreEqual(new Location(29,1), content.EndLocation);
  40. }
  41. [Test]
  42. public void VBNetSimpleEmptyElementTest()
  43. {
  44. XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test />");
  45. Assert.IsFalse(element.NameIsExpression);
  46. Assert.AreEqual("Test", element.XmlName);
  47. Assert.IsEmpty(element.Attributes);
  48. Assert.IsEmpty(element.Children);
  49. Assert.AreEqual(new Location(1,1), element.StartLocation);
  50. Assert.AreEqual(new Location(9,1), element.EndLocation);
  51. }
  52. [Test]
  53. public void VBNetSimpleEmptyElementWithAttributeTest()
  54. {
  55. XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0' />");
  56. Assert.IsFalse(element.NameIsExpression);
  57. Assert.AreEqual("Test", element.XmlName);
  58. Assert.IsNotEmpty(element.Attributes);
  59. Assert.AreEqual(1, element.Attributes.Count);
  60. Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
  61. XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
  62. Assert.AreEqual("id", attribute.Name);
  63. Assert.IsTrue(attribute.IsLiteralValue);
  64. Assert.IsTrue(attribute.ExpressionValue.IsNull);
  65. Assert.AreEqual("0", attribute.LiteralValue);
  66. Assert.AreEqual(new Location(7,1), attribute.StartLocation);
  67. Assert.AreEqual(new Location(13,1), attribute.EndLocation);
  68. Assert.IsEmpty(element.Children);
  69. Assert.AreEqual(new Location(1,1), element.StartLocation);
  70. Assert.AreEqual(new Location(16,1), element.EndLocation);
  71. }
  72. [Test]
  73. public void VBNetSimpleEmptyElementWithAttributesTest()
  74. {
  75. XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0' name=<%= name %> <%= contentData %> />"); Assert.IsFalse(element.NameIsExpression);
  76. Assert.AreEqual("Test", element.XmlName);
  77. Assert.IsNotEmpty(element.Attributes);
  78. Assert.AreEqual(3, element.Attributes.Count);
  79. Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
  80. XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
  81. Assert.AreEqual("id", attribute.Name);
  82. Assert.IsTrue(attribute.IsLiteralValue);
  83. Assert.IsTrue(attribute.ExpressionValue.IsNull);
  84. Assert.AreEqual("0", attribute.LiteralValue);
  85. Assert.AreEqual(new Location(7,1), attribute.StartLocation);
  86. Assert.AreEqual(new Location(13,1), attribute.EndLocation);
  87. Assert.IsTrue(element.Attributes[1] is XmlAttributeExpression);
  88. XmlAttributeExpression attribute2 = element.Attributes[1] as XmlAttributeExpression;
  89. Assert.AreEqual("name", attribute2.Name);
  90. Assert.IsFalse(attribute2.IsLiteralValue);
  91. Assert.IsFalse(attribute2.ExpressionValue.IsNull);
  92. Assert.IsTrue(attribute2.ExpressionValue is IdentifierExpression);
  93. IdentifierExpression identifier = attribute2.ExpressionValue as IdentifierExpression;
  94. Assert.AreEqual("name", identifier.Identifier);
  95. Assert.AreEqual(new Location(23,1), identifier.StartLocation);
  96. Assert.AreEqual(new Location(27,1), identifier.EndLocation);
  97. Assert.AreEqual(new Location(14,1), attribute2.StartLocation);
  98. Assert.AreEqual(new Location(30,1), attribute2.EndLocation);
  99. Assert.IsTrue(element.Attributes[2] is XmlEmbeddedExpression);
  100. XmlEmbeddedExpression attribute3 = element.Attributes[2] as XmlEmbeddedExpression;
  101. Assert.IsTrue(attribute3.InlineVBExpression is IdentifierExpression);
  102. IdentifierExpression identifier2 = attribute3.InlineVBExpression as IdentifierExpression;
  103. Assert.AreEqual("contentData", identifier2.Identifier);
  104. Assert.AreEqual(new Location(35,1), identifier2.StartLocation);
  105. Assert.AreEqual(new Location(46,1), identifier2.EndLocation);
  106. Assert.AreEqual(new Location(31,1), attribute3.StartLocation);
  107. Assert.AreEqual(new Location(49,1), attribute3.EndLocation);
  108. Assert.IsEmpty(element.Children);
  109. Assert.AreEqual(new Location(1,1), element.StartLocation);
  110. Assert.AreEqual(new Location(52,1), element.EndLocation);
  111. }
  112. [Test]
  113. public void VBNetElementWithAttributeTest()
  114. {
  115. XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0'>\n" +
  116. " <Item />\n" +
  117. " <Item />\n" +
  118. "</Test>");
  119. Assert.IsFalse(element.NameIsExpression);
  120. Assert.AreEqual("Test", element.XmlName);
  121. Assert.IsNotEmpty(element.Attributes);
  122. Assert.AreEqual(1, element.Attributes.Count);
  123. Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
  124. XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
  125. Assert.AreEqual("id", attribute.Name);
  126. Assert.IsTrue(attribute.IsLiteralValue);
  127. Assert.IsTrue(attribute.ExpressionValue.IsNull);
  128. Assert.AreEqual("0", attribute.LiteralValue);
  129. Assert.AreEqual(new Location(7,1), attribute.StartLocation);
  130. Assert.AreEqual(new Location(13,1), attribute.EndLocation);
  131. Assert.IsNotEmpty(element.Children);
  132. Assert.AreEqual(5, element.Children.Count);
  133. CheckContent(element.Children[0], "\n\t", XmlContentType.Text, new Location(14,1), new Location(2,2));
  134. CheckContent(element.Children[2], "\n\t", XmlContentType.Text, new Location(10,2), new Location(2,3));
  135. CheckContent(element.Children[4], "\n", XmlContentType.Text, new Location(10,3), new Location(1,4));
  136. CheckElement(element.Children[1], "Item", new Location(2,2), new Location(10,2));
  137. CheckElement(element.Children[3], "Item", new Location(2,3), new Location(10,3));
  138. Assert.AreEqual(new Location(1,1), element.StartLocation);
  139. Assert.AreEqual(new Location(8,4), element.EndLocation);
  140. }
  141. [Test]
  142. public void VBNetElementWithMixedContentTest()
  143. {
  144. XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test id='0'>\n" +
  145. " <!-- test -->\n" +
  146. " <Item />\n" +
  147. " <Item />\n" +
  148. " <![CDATA[<cdata> section]]>\n" +
  149. "</Test>");
  150. Assert.IsFalse(element.NameIsExpression);
  151. Assert.AreEqual("Test", element.XmlName);
  152. Assert.IsNotEmpty(element.Attributes);
  153. Assert.AreEqual(1, element.Attributes.Count);
  154. Assert.IsTrue(element.Attributes[0] is XmlAttributeExpression);
  155. XmlAttributeExpression attribute = element.Attributes[0] as XmlAttributeExpression;
  156. Assert.AreEqual("id", attribute.Name);
  157. Assert.IsTrue(attribute.IsLiteralValue);
  158. Assert.IsTrue(attribute.ExpressionValue.IsNull);
  159. Assert.AreEqual("0", attribute.LiteralValue);
  160. Assert.AreEqual(new Location(7,1), attribute.StartLocation);
  161. Assert.AreEqual(new Location(13,1), attribute.EndLocation);
  162. Assert.IsNotEmpty(element.Children);
  163. Assert.AreEqual(9, element.Children.Count);
  164. CheckContent(element.Children[0], "\n\t", XmlContentType.Text, new Location(14,1), new Location(2,2));
  165. CheckContent(element.Children[2], "\n\t", XmlContentType.Text, new Location(15,2), new Location(2,3));
  166. CheckContent(element.Children[4], "\n\t", XmlContentType.Text, new Location(10,3), new Location(2,4));
  167. CheckContent(element.Children[6], "\n\t", XmlContentType.Text, new Location(10,4), new Location(2,5));
  168. CheckContent(element.Children[7], "<cdata> section", XmlContentType.CData, new Location(2,5), new Location(29,5));
  169. CheckContent(element.Children[8], "\n", XmlContentType.Text, new Location(29,5), new Location(1,6));
  170. CheckContent(element.Children[1], " test ", XmlContentType.Comment, new Location(2,2), new Location(15,2));
  171. CheckElement(element.Children[3], "Item", new Location(2,3), new Location(10,3));
  172. CheckElement(element.Children[5], "Item", new Location(2,4), new Location(10,4));
  173. Assert.AreEqual(new Location(1,1), element.StartLocation);
  174. Assert.AreEqual(new Location(8,6), element.EndLocation);
  175. }
  176. [Test]
  177. public void VBNetElementWithMixedContentTest2()
  178. {
  179. XmlElementExpression element = ParseUtil.ParseExpression<XmlElementExpression>("<Test> aaaa </Test>");
  180. Assert.IsFalse(element.NameIsExpression);
  181. Assert.AreEqual("Test", element.XmlName);
  182. Assert.IsNotEmpty(element.Children);
  183. Assert.AreEqual(1, element.Children.Count);
  184. CheckContent(element.Children[0], " aaaa ", XmlContentType.Text, new Location(7,1), new Location(14,1));
  185. }
  186. [Test]
  187. public void VBNetProcessingInstructionAndCommentAtEndTest()
  188. {
  189. XmlDocumentExpression document = ParseUtil.ParseExpression<XmlDocumentExpression>("<Test />\n" +
  190. "<!-- test -->\n" +
  191. "<?target some text?>");
  192. Assert.IsNotEmpty(document.Expressions);
  193. Assert.AreEqual(3, document.Expressions.Count);
  194. CheckElement(document.Expressions[0], "Test", new Location(1,1), new Location(9,1));
  195. CheckContent(document.Expressions[1], " test ", XmlContentType.Comment, new Location(1,2), new Location(14,2));
  196. CheckContent(document.Expressions[2], "target some text", XmlContentType.ProcessingInstruction, new Location(1,3), new Location(21,3));
  197. }
  198. #endregion
  199. void CheckElement(AstNode node, string name, AstLocation start, AstLocation end)
  200. {
  201. Assert.IsTrue(node is XmlElementExpression);
  202. XmlElementExpression expr = node as XmlElementExpression;
  203. Assert.IsEmpty(expr.Attributes);
  204. Assert.IsEmpty(expr.Children);
  205. Assert.IsFalse(expr.NameIsExpression);
  206. Assert.AreEqual(name, expr.XmlName);
  207. Assert.AreEqual(start, expr.StartLocation);
  208. Assert.AreEqual(end, expr.EndLocation);
  209. }
  210. void CheckContent(AstNode node, string content, XmlContentType type, AstLocation start, AstLocation end)
  211. {
  212. Assert.IsTrue(node is XmlContentExpression);
  213. XmlContentExpression expr = node as XmlContentExpression;
  214. Assert.AreEqual(type, expr.Type);
  215. Assert.AreEqual(content, expr.Content);
  216. Assert.AreEqual(start, expr.StartLocation);
  217. Assert.AreEqual(end, expr.EndLocation);
  218. }
  219. }
  220. }