Project

Profile

Help

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

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

1
using System;
2
using System.Collections.Generic;
3
using Saxon.Api;
4
using NUnit.Framework;
5

    
6

    
7
namespace Saxon9.NUnit
8
{
9
    class TestXdmMap
10
    {
11

    
12
        [Test]
13
        public void TestMapOperations() {
14
            XdmMap map = new XdmMap();
15
            map = map.Put(new XdmAtomicValue("a"), new XdmAtomicValue(1));
16
            map = map.Put(new XdmAtomicValue("b"), new XdmAtomicValue(2));
17
            map = map.Put(new XdmAtomicValue("c"), new XdmAtomicValue(3));
18
            Assert.True(3== map.Size, "Map has correct size");
19
            try
20
            {
21
                Assert.True(((XdmAtomicValue)map.Get(new XdmAtomicValue("b"))).GetLongValue()== 2);
22
            }
23
            catch (Exception e)
24
            {
25
                Assert.Fail(e.Message);
26
            }
27
            map = map.Remove(new XdmAtomicValue("c"));
28
            map = map.Remove(new XdmAtomicValue("d"));
29
            map = map.Put(new XdmAtomicValue("a"), new XdmAtomicValue(4));
30
            Assert.True(map.Size== 2);
31
            try
32
            {
33
                Assert.True(((XdmAtomicValue)map.Get(new XdmAtomicValue("a"))).GetLongValue()== 4);
34
            }
35
            catch (Exception e)
36
            {
37
                Assert.Fail(e.Message);
38
            }
39

    
40
        }
41

    
42
        [Test]
43
        public void TestConversionFromMap()
44
        {
45
            Dictionary<XdmAtomicValue, XdmAtomicValue> jam = new Dictionary<XdmAtomicValue, XdmAtomicValue>();
46
            jam.Add(new XdmAtomicValue("a"), new XdmAtomicValue(1));
47
            jam.Add(new XdmAtomicValue("b"), new XdmAtomicValue(2));
48
            jam.Add(new XdmAtomicValue("c"), new XdmAtomicValue(3));
49
            XdmMap map = XdmMap.MakeMap(jam);
50
            Assert.True(3 == map.Size);
51
            try
52
            {
53
                Assert.True(((XdmAtomicValue)map.Get(new XdmAtomicValue("b"))).GetLongValue() == 2);
54
            }
55
            catch (Exception e)
56
            {
57
                Assert.Fail(e.Message);
58
            }
59
            map = map.Remove(new XdmAtomicValue("c"));
60
            map = map.Remove(new XdmAtomicValue("d"));
61
            map = map.Put(new XdmAtomicValue("a"), new XdmAtomicValue(4));
62
            Assert.True(map.Size== 2);
63
            try
64
            {
65
                Assert.True(((XdmAtomicValue)map.Get(new XdmAtomicValue("a"))).GetLongValue() == 4);
66
            }
67
            catch (Exception e)
68
            {
69
                Assert.Fail(e.Message);
70
            }
71
        }
72

    
73
        [Test]
74
        public void TestMapAsFunction()
75
        {
76
            XdmMap map = new XdmMap();
77
            map = map.Put(new XdmAtomicValue("a"), new XdmAtomicValue(1));
78
            map = map.Put(new XdmAtomicValue("b"), new XdmAtomicValue(2));
79
            map = map.Put(new XdmAtomicValue("c"), new XdmAtomicValue(3));
80
            Assert.True(3 == map.Size);
81
            Processor proc = new Processor(true);
82
            try
83
            {
84
                XdmValue result = map.Get(new XdmAtomicValue("c"));
85
                Assert.True(result is XdmAtomicValue);
86
                Assert.True(((XdmAtomicValue)result).GetLongValue() == 3);
87
            }
88
            catch (Exception e)
89
            {
90
                Assert.Fail("Failure in TestMapAsFunction: " + e.Message);
91
            }
92
        }
93

    
94
        [Test]
95
        public void TestMapAsQueryParameter()
96
        {
97
            try
98
            {
99
                XdmMap map = new XdmMap();
100
                map = map.Put(new XdmAtomicValue("a"), new XdmAtomicValue(1));
101
                map = map.Put(new XdmAtomicValue("b"), new XdmAtomicValue(2));
102
                map = map.Put(new XdmAtomicValue("c"), new XdmAtomicValue(3));
103
                Processor proc = new Processor(true);
104
                String query =
105
                        "declare namespace m='http://www.w3.org/2005/xpath-functions/map';" +
106
                                "declare variable $a as map(*) external; " +
107
                                "m:size($a)";
108
                XQueryCompiler comp = proc.NewXQueryCompiler();
109
                XQueryEvaluator q = comp.Compile(query).Load();
110
                q.SetExternalVariable(new QName("a"), map);
111
                XdmAtomicValue result = (XdmAtomicValue)q.EvaluateSingle();
112
                Assert.True(3 == result.GetLongValue());
113
            }
114
            catch (Exception e)
115
            {
116
                Assert.Fail(e.Message);
117
            }
118
        }
119

    
120
        [Test]
121
        public void TestMapAsQueryResult()
122
        {
123
            try
124
            {
125
                Processor proc = new Processor(true);
126
                String query = "map{1:2, 2:3, 3:4}";
127
                XQueryCompiler comp = proc.NewXQueryCompiler();
128
                XQueryEvaluator q = comp.Compile(query).Load();
129
                XdmMap result = (XdmMap)q.EvaluateSingle();
130
                Assert.True(3== result.Size);
131
            }
132
            catch (Exception e)
133
            {
134
               
135
                Assert.Fail(e.Message);
136
            }
137
        }
138

    
139

    
140
        [Test]
141
        public void TestConversionToMap()
142
        {
143
            try
144
            {
145
                Processor proc = new Processor(true);
146
                String query = "map{1:2, 2:3, 3:4}";
147
                XQueryCompiler comp = proc.NewXQueryCompiler();
148
                XQueryEvaluator q = comp.Compile(query).Load();
149
                XdmMap result = (XdmMap)q.EvaluateSingle();
150
                Dictionary<XdmAtomicValue, XdmValue> jmap = result.AsDictionary();
151
                Assert.True(3 == jmap.Count);
152
                object output = jmap[new XdmAtomicValue(3)];
153
                Assert.True(4 == ((XdmAtomicValue)output).GetLongValue());
154
            }
155
            catch (Exception e)
156
            {
157
                
158
                Assert.Fail(e.Message);
159
            }
160
        }
161

    
162

    
163
        [Test]
164
        public void TestConversionFromMap2()
165
        {
166
            try
167
            {
168
                Dictionary <int, String > dictionary = new Dictionary<int, String>();
169
            dictionary.Add(1, "a");
170
            dictionary.Add(2, "b");
171
            XdmMap map = XdmMap.MakeMap(dictionary);
172
            Assert.True(2 == map.Size);
173
            XdmAtomicValue val1 = (XdmAtomicValue)XdmValue.MakeValue("b");
174
            XdmAtomicValue val2 = (XdmAtomicValue)map.Get(2);
175
            Assert.True(val1.Equals(val2));
176
                //Assert.True(ItemType.ANY_MAP.matches(map));
177
            }
178
            catch (Exception e)
179
            {
180

    
181
                Assert.Fail(e.Message);
182
            }
183

    
184
        }
185
    }
186
}
(17-17/19)