MsxslScriptTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. public void TestCSharp ()
  98. {
  99. string style = xslstring.Replace ("***** rewrite here *****", cs1);
  100. XmlTextReader xr = new XmlTextReader (style, XmlNodeType.Document, null);
  101. xslt.Load (xr);
  102. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  103. }
  104. [Test]
  105. public void TestVB ()
  106. {
  107. string style = xslstring.Replace ("***** rewrite here *****", vb1);
  108. XmlTextReader xr = new XmlTextReader (style, XmlNodeType.Document, null);
  109. xslt.Load (xr);
  110. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  111. }
  112. [Test]
  113. public void TestJScript ()
  114. {
  115. string style = xslstring.Replace ("***** rewrite here *****", js1);
  116. XmlTextReader xr = new XmlTextReader (style, XmlNodeType.Document, null);
  117. xslt.Load (xr);
  118. xslt.Transform (doc.CreateNavigator (), null, new XmlTextWriter (new StringWriter ()));
  119. }
  120. [Test]
  121. [ExpectedException (typeof (XsltException))]
  122. public void InvalidScript ()
  123. {
  124. string script = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:user='urn:my-scripts'
  125. xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  126. <!-- -->
  127. <xsl:output method='html' indent='no' />
  128. <!-- -->
  129. <xsl:template match='/project'>
  130. <xsl:if test='user:BadScriptFunction(&apos;test&apos;)'></xsl:if>
  131. </xsl:template>
  132. <!-- -->
  133. <msxsl:script language='C#' implements-prefix='user'>
  134. <![CDATA[
  135. string BadScriptFunction(string test) {
  136. xxx;
  137. }
  138. ]]>
  139. </msxsl:script>
  140. <!-- -->
  141. </xsl:stylesheet>";
  142. xslt.Load (new XmlTextReader (script, XmlNodeType.Document, null));
  143. }
  144. }
  145. }