Project

Profile

Help

How to connect?
Download (10.1 KB) Statistics
| Branch: | Revision:

he / src / test / nunit / SaxonNUnit / SaxonNUnit / TestXPathCompiler31.cs @ 3df04590

1
using System;
2
using System.Globalization;
3
using System.Collections.Generic;
4
using System.Collections;
5
using System.IO;
6
using Saxon.Api;
7
using NUnit.Framework;
8
using System.Xml;
9
using SaxonNUnit;
10
using System.Text;
11
using net.sf.saxon;
12
using net.sf.saxon.dotnet;
13
using System.Linq;
14

    
15

    
16

    
17
namespace Saxon9.NUnit
18
{
19

    
20
    
21
    class TestXPathCompiler31
22
    {
23

    
24

    
25

    
26

    
27
        public Processor p = new Processor(true);
28
        XPathCompiler compiler = null;
29
        XdmNode document;
30

    
31
        public XPathCompiler GetCompiler() {
32
            if (compiler == null)
33
            {
34
                compiler = p.NewXPathCompiler();
35
            }
36
            return compiler;
37
        }
38

    
39

    
40
        public TestXPathCompiler31()
41
        {
42
            try
43
            {
44
                GetCompiler().XPathLanguageVersion =  "3.1";
45
                DocumentBuilder b = p.NewDocumentBuilder();
46
                b.BaseUri = new Uri("http://www.example.com");
47
                document = b.Build(new StringReader("<foo x='1'><bar/><baz/></foo>"));
48
            }
49
            catch (Exception e)
50
            {
51
                Assert.Fail(e.Message);
52
            }
53
        }
54

    
55
        private String good(String expr)
56
        {
57
            try
58
            {
59
                XPathExecutable exec = GetCompiler().Compile(expr);
60
                XPathSelector selector = exec.Load();
61
                selector.ContextItem = document;
62
                return selector.Evaluate().ToString();
63
            }
64
            catch (Exception e1)
65
            {
66

    
67
                Assert.Fail(e1.Message);
68
                return null;
69
            }
70
        }
71

    
72
        private void bad(String expr)
73
        {
74
            try
75
            {
76
                XPathExecutable exec = GetCompiler().Compile(expr);
77
                XPathSelector selector = exec.Load();
78
                selector.ContextItem = (document);
79
                Assert.Fail("unexpected success");
80
            }
81
            catch (Exception e1)
82
            {
83
                // Failure was expected
84
            }
85
        }
86

    
87
        [Test]
88
        public void TestArith()
89
        {
90
            String result = good("2+2");
91
            Assert.AreEqual("4", result);
92
        }
93

    
94
        [Test]
95
        public void TestNumberFormat()
96
        {
97
            //CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("de-DE");
98
            //CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("de-DE");
99
            decimal d = 2.5m;
100
            //String value2 = d.ToString(System.Globalization.CultureInfo.InvariantCulture);
101
            XdmAtomicValue value = new XdmAtomicValue(d);
102
            Assert.AreEqual("2.5", value.ToString());
103
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
104
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en-GB");
105
        }
106

    
107
        [Test]
108
        public void TestIf()
109
        {
110
            String result = good("if (//*) then 3 else 4");
111
            Assert.AreEqual("3", result);
112
        }
113

    
114
        [Test]
115
        public void TestFor()
116
        {
117
            String result = good("string-join(for $i in 1 to 4 return $i, '-')");
118
            Assert.AreEqual("1-2-3-4", result);
119
        }
120

    
121
        [Test]
122
        public void TestExists()
123
        {
124
            String result = good("exists(//*)");
125
            Assert.AreEqual("true", result);
126
        }
127

    
128
        [Test]
129
        public void TestBang()
130
        {
131
            String result = good("string-join((1 to 3)!(. + 1), '-')");
132
            Assert.AreEqual("2-3-4", result);
133
        }
134

    
135
        [Test]
136
        public void TestConcat()
137
        {
138
            String result = good("'abc' || 'def'");
139
            Assert.AreEqual("abcdef", result);
140
        }
141

    
142
        [Test]
143
        public void TestLet()
144
        {
145
            String result = good("let $x := 3 return $x + 1");
146
            Assert.AreEqual("4", result);
147
        }
148

    
149
        [Test]
150
        public void TestInlineFunctions()
151
        {
152
            String result = good("function($x){$x + 1}(3)");
153
            Assert.AreEqual("4", result);
154
        }
155

    
156
        [Test]
157
        public void TestNoAnnotations()
158
        {
159
            bad("%private function($x){$x + 1}(3)");
160
        }
161

    
162
        [Test]
163
        public void TestDynamicFunctionCall()
164
        {
165
            String result = good("exists#1(3)");
166
            Assert.AreEqual("true", result);
167
        }
168

    
169
        [Test]
170
        public void TestEQNames()
171
        {
172
            String result = good("Q{http://www.w3.org/2005/xpath-functions}exists(3)");
173
            Assert.AreEqual("true", result);
174
        }
175

    
176
        [Test]
177
        public void TestNoConstructors()
178
        {
179
            bad("<a/>");
180
        }
181

    
182
        [Test]
183
        public void TestMaps()
184
        {
185
            String result = good("map{1:'a'}?1");
186
            Assert.AreEqual("a", result);
187
        }
188

    
189
        [Test]
190
        public void TestMapType()
191
        {
192
            String result = good("map{1:'a'} instance of map(*)");
193
            Assert.AreEqual("true", result);
194
        }
195

    
196
        [Test]
197
        public void TestArrays()
198
        {
199
            String result = good("['1a','2a','3a','4a']?2");
200
            Assert.AreEqual("2a", result);
201
        }
202

    
203
        [Test]
204
        public void TestArrayType()
205
        {
206
            String result = good("['1a','2a','3a','4a'] instance of array(*)");
207
            Assert.AreEqual("true", result);
208
        }
209

    
210
        [Test]
211
        public void TestNoWhere()
212
        {
213
            bad("for $i in 1 to 10 where $i gt 3 return $i");
214
        }
215

    
216
        [Test]
217
        public void TestNoOrderBy()
218
        {
219
            bad("for $i in 1 to 10 order by -$i return $i");
220
        }
221

    
222
        [Test]
223
        public void TestNoCurrent()
224
        {
225
            bad("current()");
226
        }
227

    
228
        [Test]
229
        public void TestRound2()
230
        {
231
            String result = good("round(12.341, 2)");
232
            Assert.AreEqual("12.34", result);
233
        }
234

    
235
        [Test]
236
        public void TestGenerateId()
237
        {
238
            String result = good("generate-id(/)");
239
            Assert.True(result.Length > 0);
240
        }
241

    
242
        [Test]
243
        public void TestQFlag()
244
        {
245
            String result = good("matches('quid', 'quod', 'q')");
246
            Assert.AreEqual("false", result);
247
        }
248

    
249
        [Test]
250
        public void TestNoNonCapturingGroups()
251
        {
252
            String result = good("replace('abcdef', '(?:abc)(def)', '$1')");
253
            Assert.AreEqual("def", result);
254
        }
255

    
256
        [Test]
257
        public void TestMapFunctions()
258
        {
259
            String result = good("Q{http://www.w3.org/2005/xpath-functions/map}get(Q{http://www.w3.org/2005/xpath-functions/map}entry('a', 'b'), 'a')");
260
            Assert.AreEqual("b", result);
261
        }
262

    
263
        [Test]
264
        public void TestArrayFunctions()
265
        {
266
            String result = good("Q{http://www.w3.org/2005/xpath-functions/array}flatten(Q{http://www.w3.org/2005/xpath-functions/array}append([], '12'))");
267
            Assert.AreEqual("12", result);
268
        }
269

    
270
        [Test]
271
        public void TestDefaultLanguage()
272
        {
273
            String result = good("default-language()");
274
            Assert.AreEqual("en", result);
275
        }
276

    
277
        [Test]
278
        public void TestBug4081()
279
        {
280
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
281
                    + "<Employees>\n"
282
                    + "    <Employee id=\"1\">"+ Environment.NewLine
283
                    + "        <age:ag xmlns:age=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">29</age:ag>" + Environment.NewLine 
284
                    + "        <name>Pankaj</name>" + Environment.NewLine
285
                    + "        <gender>Male</gender>" + Environment.NewLine
286
                    + "        <role>Java Developer</role>" + Environment.NewLine
287
                    + "    </Employee>" + Environment.NewLine
288
                    + "    <Employee id=\"2\">" + Environment.NewLine
289
                    + "        <age:ag xmlns:age=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">35</age:ag>" + Environment.NewLine
290
                    + "        <name>Lisa</name>" + Environment.NewLine
291
                    + "        <gender>Female</gender>" + Environment.NewLine
292
                    + "        <role>CEO</role>" + Environment.NewLine
293
                    + "    </Employee>" + Environment.NewLine
294
                    + "    <Employee id=\"3\">" + Environment.NewLine
295
                    + "        <age:ag xmlns:age=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">45</age:ag>" + Environment.NewLine
296
                    + "        <name>Tom</name>" + Environment.NewLine
297
                    + "        <gender>Male</gender>" + Environment.NewLine 
298
                    + "        <role>Manager</role>" + Environment.NewLine 
299
                    + "    </Employee>@\n"
300
                    + "    <Employee id=\"4\">" + Environment.NewLine 
301
                    + "        <age:ag xmlns:age=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">55</age:ag>" + Environment.NewLine 
302
                    + "        <name>Meghan</name>" + Environment.NewLine 
303
                    + "        <gender>Female</gender>" + Environment.NewLine 
304
                    + "        <role>Manager</role>" + Environment.NewLine 
305
                    + "    </Employee>" + Environment.NewLine 
306
                    + "</Employees>";
307

    
308
         
309

    
310
            String xpath = "//Employees/Employee[1]/age:ag";
311
            try
312
            {
313
                Processor p = new Processor(false);
314
                XPathCompiler c = p.NewXPathCompiler();
315
                c.DeclareNamespace("age", "http://www.w3.org/2003/01/geo/wgs84_pos#");
316
                XPathExecutable e = c.Compile(xpath);
317
                XPathSelector s = e.Load();
318
                DocumentBuilder builder = p.NewDocumentBuilder();
319
                builder.BaseUri = new Uri("http://www.example.com");
320
                s.ContextItem = (builder.Build(new StringReader(xml)));
321
                XdmItem val = s.EvaluateSingle();
322
                Assert.AreEqual("29", val.GetStringValue());
323
                Assert.AreEqual("<age:ag xmlns:age=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">29</age:ag>", val.ToString());
324
            }
325
            catch (Exception e1)
326
            {
327
                Assert.Fail();
328
            }
329
        }
330
    }
331
}
332

    
(14-14/19)