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