MsxslScriptTests.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // System.Xml.Xsl.MsxslScriptTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2004 Novell Inc.
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Xml;
  12. using System.Xml.Xsl;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Xml.Xsl
  15. {
  16. [TestFixture]
  17. public class MsxslScriptTests
  18. {
  19. // PI calc stuff are one of MSDN samples.
  20. static XmlDocument doc;
  21. static MsxslScriptTests ()
  22. {
  23. string inputxml = @"<?xml version='1.0'?>
  24. <data>
  25. <circle>
  26. <radius>12</radius>
  27. </circle>
  28. <circle>
  29. <radius>37.5</radius>
  30. </circle>
  31. </data>";
  32. doc = new XmlDocument ();
  33. doc.LoadXml (inputxml);
  34. }
  35. static string xslstring = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  36. xmlns:msxsl='urn:schemas-microsoft-com:xslt'
  37. xmlns:user='urn:my-scripts'>
  38. ***** rewrite here *****
  39. <xsl:template match='data'>
  40. <circles>
  41. <xsl:for-each select='circle'>
  42. <circle>
  43. <xsl:copy-of select='node()'/>
  44. <circumference>
  45. <!-- xsl:value-of select='user:circumference(radius)'/ -->
  46. TEST:
  47. <xsl:value-of select='user:PadRight(&quot;test-string&quot;, 20)'/>
  48. </circumference>
  49. </circle>
  50. </xsl:for-each>
  51. </circles>
  52. </xsl:template>
  53. </xsl:stylesheet>";
  54. string cs1 = @"<msxsl:script language='C#' implements-prefix='user'>
  55. <![CDATA[
  56. public string PadRight( string str, int padding) {
  57. return str.PadRight(padding);
  58. }
  59. public double circumference(double radius){
  60. double pi = 3.14;
  61. double circ = pi*radius*2;
  62. return circ;
  63. }
  64. ]]>
  65. </msxsl:script>";
  66. string vb1 = @"<msxsl:script language='VB' implements-prefix='user'>
  67. <![CDATA[
  68. public function circumference(radius as double) as double
  69. dim pi as double = 3.14
  70. dim circ as double = pi*radius*2
  71. return circ
  72. end function
  73. public function PadRight(str as string, padding as integer) as string
  74. return str.PadRight(padding)
  75. end function
  76. ]]>
  77. </msxsl:script>";
  78. string js1 = @"<msxsl:script language='JScript' implements-prefix='user'>
  79. <![CDATA[
  80. function circumference(radius : double) : double {
  81. var pi : double = 3.14;
  82. var circ : double = pi*radius*2;
  83. return circ;
  84. }
  85. function PadRight(str : String, padding : int) {
  86. return str.PadRight(padding);
  87. }
  88. ]]>
  89. </msxsl:script>";
  90. XslTransform xslt;
  91. [SetUp]
  92. public void GetReady ()
  93. {
  94. xslt = new XslTransform ();
  95. }
  96. [Test]
  97. [Category ("MobileNotWorking")]
  98. public void TestCSharp ()
  99. {
  100. string style = xslstring.Replace ("***** rewrite here *****", cs1);
  101. XmlTextReader xr = new XmlTextReader (style, XmlNodeType.Document, null);
  102. xslt.Load (xr);
  103. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  104. }
  105. [Test]
  106. [Category ("NotWorking")] // it depends on "mbas" existence
  107. public void TestVB ()
  108. {
  109. string style = xslstring.Replace ("***** rewrite here *****", vb1);
  110. XmlTextReader xr = new XmlTextReader (style, XmlNodeType.Document, null);
  111. xslt.Load (xr);
  112. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  113. }
  114. [Test]
  115. [Category ("NotWorking")] // it depends on "mjs" existence
  116. public void TestJScript ()
  117. {
  118. string style = xslstring.Replace ("***** rewrite here *****", js1);
  119. XmlTextReader xr = new XmlTextReader (style, XmlNodeType.Document, null);
  120. xslt.Load (xr);
  121. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  122. }
  123. [Test]
  124. [ExpectedException (typeof (XsltException))]
  125. [Category ("MobileNotWorking")]
  126. public void InvalidScript ()
  127. {
  128. string script = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:user='urn:my-scripts'
  129. xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  130. <!-- -->
  131. <xsl:output method='html' indent='no' />
  132. <!-- -->
  133. <xsl:template match='/project'>
  134. <xsl:if test='user:BadScriptFunction(&apos;test&apos;)'></xsl:if>
  135. </xsl:template>
  136. <!-- -->
  137. <msxsl:script language='C#' implements-prefix='user'>
  138. <![CDATA[
  139. string BadScriptFunction(string test) {
  140. xxx;
  141. }
  142. ]]>
  143. </msxsl:script>
  144. <!-- -->
  145. </xsl:stylesheet>";
  146. xslt.Load (new XmlTextReader (script, XmlNodeType.Document, null));
  147. }
  148. [Test]
  149. public void CompilerWarningsShouldBeIgnored ()
  150. {
  151. string script = @"<xslt:stylesheet xmlns:xslt='http://www.w3.org/1999/XSL/Transform' version='1.0' xmlns:msxsl='urn:schemas-microsoft-com:xslt'
  152. xmlns:stringutils='urn:schemas-sourceforge.net-blah'>
  153. <xslt:output method='text' />
  154. <msxsl:script language='C#' implements-prefix='stringutils'>
  155. <![CDATA[
  156. string PadRight( string str, int padding) {
  157. return str.PadRight(padding);
  158. }
  159. ]]>
  160. </msxsl:script>
  161. <xslt:template match='project'>
  162. <xslt:apply-templates select='target[string(@description) != &apos;&apos; ]'>
  163. <xslt:sort select='@name' order='ascending' />
  164. </xslt:apply-templates>
  165. </xslt:template>
  166. <xslt:template match='target'>
  167. <xslt:value-of select='stringutils:PadRight(@name, 20)' />
  168. <xslt:value-of select='@description' />
  169. </xslt:template>
  170. </xslt:stylesheet>";
  171. xslt.Load (new XmlTextReader (script, XmlNodeType.Document, null));
  172. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (TextWriter.Null));
  173. }
  174. [Test]
  175. public void CompileNoLineInfoSource ()
  176. {
  177. // bug #76116
  178. string xslt = @"<xslt:stylesheet xmlns:xslt='http://www.w3.org/1999/XSL/Transform' version='1.0' xmlns:msxsl='urn:schemas-microsoft-com:xslt' xmlns:stringutils='urn:schemas-sourceforge.net-blah' xmlns:nant='unknown-at-this-time'>
  179. <xslt:output method='text' />
  180. <msxsl:script language='C#' implements-prefix='stringutils'>
  181. <![CDATA[
  182. string PadRight( string str, int padding) {
  183. return str.PadRight(padding);
  184. }
  185. ]]>
  186. </msxsl:script>
  187. <xslt:template match='/'>
  188. <foo/>
  189. </xslt:template>
  190. </xslt:stylesheet>";
  191. XmlDocument doc = new XmlDocument ();
  192. doc.LoadXml (xslt);
  193. XslTransform t = new XslTransform ();
  194. t.Load (doc);
  195. }
  196. }
  197. }