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