MsxslScriptTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  55. <![CDATA[
  56. string PadRight( string str, int padding) {
  57. return str.PadRight(padding);
  58. }
  59. ]]>
  60. </msxsl:script>";
  61. string cs2 = @"<msxsl:script language='C#' implements-prefix='user' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  62. <![CDATA[
  63. public double circumference(double radius){
  64. double pi = 3.14;
  65. double circ = pi*radius*2;
  66. return circ;
  67. }
  68. ]]>
  69. </msxsl:script>";
  70. string vb1 = @"<msxsl:script language='VB' implements-prefix='user' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  71. <![CDATA[
  72. public function circumference(radius as double) as double
  73. dim pi as double = 3.14
  74. dim circ as double = pi*radius*2
  75. return circ
  76. end function
  77. public function greet () as string
  78. return " + "\"Hey! you should not depend on proprietary scripting!!\"" + @"
  79. end function
  80. ]]>
  81. </msxsl:script>";
  82. string js1 = @"<msxsl:script language='JScript' implements-prefix='user' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  83. <![CDATA[
  84. function circumference(radius : double) : double {
  85. var pi : double = 3.14;
  86. var circ : double = pi*radius*2;
  87. return circ;
  88. }
  89. function greet () : String {
  90. return " + "\"Hey! you should not depend on proprietary scripting!!\"" + @";
  91. }
  92. ]]>
  93. </msxsl:script>";
  94. XslTransform xslt;
  95. [SetUp]
  96. public void GetReady ()
  97. {
  98. xslt = new XslTransform ();
  99. }
  100. [Test]
  101. public void TestCSharp ()
  102. {
  103. XmlTextReader xr = new XmlTextReader (cs1, XmlNodeType.Document, null);
  104. xslt.Load (xr);
  105. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  106. xr = new XmlTextReader (cs2, XmlNodeType.Document, null);
  107. xslt.Load (xr);
  108. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  109. }
  110. [Test]
  111. public void TestVB ()
  112. {
  113. XmlTextReader xr = new XmlTextReader (vb1, XmlNodeType.Document, null);
  114. xslt.Load (xr);
  115. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  116. }
  117. [Test]
  118. public void TestJScript ()
  119. {
  120. XmlTextReader xr = new XmlTextReader (js1, XmlNodeType.Document, null);
  121. xslt.Load (xr);
  122. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  123. }
  124. [Test]
  125. [ExpectedException (typeof (XsltCompileException))]
  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. }
  149. }