MsxslScriptTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 : Assertion
  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 ("NotWorking")] // it depends on "mcs" existence
  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. [Ignore ("Actually it should throw compile exception")]
  125. [ExpectedException (typeof (XsltException))]
  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. [Category ("NotWorking")] // it depends on "mcs" existence
  150. public void CompilerWarningsShouldBeIgnored ()
  151. {
  152. string script = @"<xslt:stylesheet xmlns:xslt='http://www.w3.org/1999/XSL/Transform' version='1.0' xmlns:msxsl='urn:schemas-microsoft-com:xslt'
  153. xmlns:stringutils='urn:schemas-sourceforge.net-blah'>
  154. <xslt:output method='text' />
  155. <msxsl:script language='C#' implements-prefix='stringutils'>
  156. <![CDATA[
  157. string PadRight( string str, int padding) {
  158. return str.PadRight(padding);
  159. }
  160. ]]>
  161. </msxsl:script>
  162. <xslt:template match='project'>
  163. <xslt:apply-templates select='target[string(@description) != &apos;&apos; ]'>
  164. <xslt:sort select='@name' order='ascending' />
  165. </xslt:apply-templates>
  166. </xslt:template>
  167. <xslt:template match='target'>
  168. <xslt:value-of select='stringutils:PadRight(@name, 20)' />
  169. <xslt:value-of select='@description' />
  170. </xslt:template>
  171. </xslt:stylesheet>";
  172. xslt.Load (new XmlTextReader (script, XmlNodeType.Document, null));
  173. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (TextWriter.Null));
  174. }
  175. [Test]
  176. [Category ("NotWorking")] // it depends on "mcs" existence
  177. public void CompileNoLineInfoSource ()
  178. {
  179. // bug #76116
  180. 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'>
  181. <xslt:output method='text' />
  182. <msxsl:script language='C#' implements-prefix='stringutils'>
  183. <![CDATA[
  184. string PadRight( string str, int padding) {
  185. return str.PadRight(padding);
  186. }
  187. ]]>
  188. </msxsl:script>
  189. <xslt:template match='/'>
  190. <foo/>
  191. </xslt:template>
  192. </xslt:stylesheet>";
  193. XmlDocument doc = new XmlDocument ();
  194. doc.LoadXml (xslt);
  195. XslTransform t = new XslTransform ();
  196. t.Load (doc);
  197. }
  198. }
  199. }