~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.XmlEditor/Tests/XPathQuery/RunXPathQueryTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using MonoDevelop.XmlEditor;
 
3
using NUnit.Framework;
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Collections.ObjectModel;
 
7
using System.Xml;
 
8
using System.Xml.XPath;
 
9
 
 
10
namespace MonoDevelop.XmlEditor.Tests.XPathQuery
 
11
{
 
12
        [TestFixture]
 
13
        public class RunXPathQueryTests
 
14
        {               
 
15
                [Test]
 
16
                public void OneElementNode()
 
17
                {
 
18
                        string xml = "<root>\r\n" +
 
19
                                "\t<foo/>\r\n" +
 
20
                                "</root>";
 
21
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//foo");
 
22
                        XPathNodeMatch node = nodes[0];
 
23
                        IXmlLineInfo lineInfo = node as IXmlLineInfo;
 
24
                        Assert.AreEqual(1, nodes.Length);
 
25
                        Assert.AreEqual(1, node.LineNumber);
 
26
                        Assert.AreEqual(2, node.LinePosition);
 
27
                        Assert.AreEqual("foo", node.Value);
 
28
                        Assert.AreEqual("<foo/>", node.DisplayValue);
 
29
                        Assert.AreEqual(XPathNodeType.Element, node.NodeType);
 
30
                        Assert.IsNotNull(lineInfo);
 
31
                }
 
32
                
 
33
                [Test]
 
34
                public void OneElementNodeWithNamespace()
 
35
                {
 
36
                        string xml = "<root xmlns='http://foo.com'>\r\n" +
 
37
                                "\t<foo></foo>\r\n" +
 
38
                                "</root>";
 
39
                        List<XmlNamespace> namespaces = new List<XmlNamespace>();
 
40
                        namespaces.Add(new XmlNamespace("f", "http://foo.com"));
 
41
                        ReadOnlyCollection<XmlNamespace> readOnlyNamespaces = new ReadOnlyCollection<XmlNamespace>(namespaces);
 
42
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//f:foo", readOnlyNamespaces);
 
43
                        XPathNodeMatch node = nodes[0];
 
44
                        IXmlLineInfo lineInfo = node as IXmlLineInfo;
 
45
                        Assert.AreEqual(1, nodes.Length);
 
46
                        Assert.AreEqual(1, node.LineNumber);
 
47
                        Assert.AreEqual(2, node.LinePosition);
 
48
                        Assert.AreEqual("foo", node.Value);
 
49
                        Assert.AreEqual("<foo>", node.DisplayValue);
 
50
                        Assert.AreEqual(XPathNodeType.Element, node.NodeType);
 
51
                        Assert.IsNotNull(lineInfo);
 
52
                }
 
53
                
 
54
                [Test]
 
55
                public void ElementWithNamespacePrefix()
 
56
                {
 
57
                        string xml = "<f:root xmlns:f='http://foo.com'>\r\n" +
 
58
                                "\t<f:foo></f:foo>\r\n" +
 
59
                                "</f:root>";
 
60
                        List<XmlNamespace> namespaces = new List<XmlNamespace>();
 
61
                        namespaces.Add(new XmlNamespace("fo", "http://foo.com"));
 
62
                        ReadOnlyCollection<XmlNamespace> readOnlyNamespaces = new ReadOnlyCollection<XmlNamespace>(namespaces);
 
63
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//fo:foo", readOnlyNamespaces);
 
64
                        XPathNodeMatch node = nodes[0];
 
65
                        IXmlLineInfo lineInfo = node as IXmlLineInfo;
 
66
                        Assert.AreEqual(1, nodes.Length);
 
67
                        Assert.AreEqual(1, node.LineNumber);
 
68
                        Assert.AreEqual(2, node.LinePosition);
 
69
                        Assert.AreEqual("f:foo", node.Value);
 
70
                        Assert.AreEqual("<f:foo>", node.DisplayValue);
 
71
                        Assert.AreEqual(XPathNodeType.Element, node.NodeType);
 
72
                        Assert.IsNotNull(lineInfo);
 
73
                }
 
74
                
 
75
                [Test]
 
76
                public void NoNodeFound()
 
77
                {
 
78
                        string xml = "<root>\r\n" +
 
79
                                "\t<foo/>\r\n" +
 
80
                                "</root>";
 
81
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//bar");
 
82
                        Assert.AreEqual(0, nodes.Length);
 
83
                }
 
84
                
 
85
                [Test]
 
86
                public void TextNode()
 
87
                {
 
88
                        string xml = "<root>\r\n" +
 
89
                                "\t<foo>test</foo>\r\n" +
 
90
                                "</root>";
 
91
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//foo/text()");
 
92
                        XPathNodeMatch node = nodes[0];
 
93
                        Assert.AreEqual(1, nodes.Length);
 
94
                        Assert.AreEqual(1, node.LineNumber);
 
95
                        Assert.AreEqual(6, node.LinePosition);
 
96
                        Assert.AreEqual("test", node.Value);
 
97
                        Assert.AreEqual("test", node.DisplayValue);
 
98
                }
 
99
                
 
100
                [Test]
 
101
                public void CommentNode()
 
102
                {
 
103
                        string xml = "<!-- Test --><root/>";
 
104
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//comment()");
 
105
                        XPathNodeMatch node = nodes[0];
 
106
                        Assert.AreEqual(1, nodes.Length);
 
107
                        Assert.AreEqual(0, node.LineNumber);
 
108
                        Assert.AreEqual(4, node.LinePosition);
 
109
                        Assert.AreEqual(" Test ", node.Value);
 
110
                        Assert.AreEqual("<!-- Test -->", node.DisplayValue);
 
111
                }
 
112
                
 
113
                [Test]
 
114
                public void EmptyCommentNode()
 
115
                {
 
116
                        string xml = "<!----><root/>";
 
117
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//comment()");
 
118
                        XPathNodeMatch node = nodes[0];
 
119
                        Assert.AreEqual(1, nodes.Length);
 
120
                        Assert.AreEqual(0, node.LineNumber);
 
121
                        Assert.AreEqual(4, node.LinePosition);
 
122
                        Assert.AreEqual(String.Empty, node.Value);
 
123
                        Assert.AreEqual("<!---->", node.DisplayValue);
 
124
                }
 
125
                
 
126
                [Test]
 
127
                public void NamespaceNode()
 
128
                {
 
129
                        string xml = "<root xmlns='http://foo.com'/>";
 
130
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//namespace::*");
 
131
                        XPathNodeMatch node = nodes[0];
 
132
                        XPathNodeMatch xmlNamespaceNode = nodes[1];
 
133
                        Assert.AreEqual(2, nodes.Length);
 
134
                        Assert.AreEqual(0, node.LineNumber);
 
135
                        Assert.AreEqual(6, node.LinePosition);
 
136
                        Assert.AreEqual("xmlns=\"http://foo.com\"", node.Value);
 
137
                        Assert.AreEqual("xmlns=\"http://foo.com\"", node.DisplayValue);
 
138
                        Assert.IsFalse(xmlNamespaceNode.HasLineInfo());
 
139
                        Assert.AreEqual("xmlns:xml=\"http://www.w3.org/XML/1998/namespace\"", xmlNamespaceNode.Value);
 
140
                }
 
141
                
 
142
                [Test]
 
143
                public void ProcessingInstructionNode()
 
144
                {
 
145
                        string xml = "<root><?test processinstruction='1.0'?></root>";
 
146
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//processing-instruction()");
 
147
                        XPathNodeMatch node = nodes[0];
 
148
                        Assert.AreEqual("test", node.Value);
 
149
                        Assert.AreEqual("<?test processinstruction='1.0'?>", node.DisplayValue);
 
150
                        Assert.AreEqual(0, node.LineNumber);
 
151
                        Assert.AreEqual(8, node.LinePosition);
 
152
                }
 
153
                
 
154
                [Test]
 
155
                public void AttributeNode()
 
156
                {
 
157
                        string xml = "<root>\r\n" +
 
158
                                "\t<foo Id='ab'></foo>\r\n" +
 
159
                                "</root>";
 
160
                        XPathNodeMatch[] nodes = XmlEditorView.SelectNodes(xml, "//foo/@Id");
 
161
                        XPathNodeMatch node = nodes[0];
 
162
                        Assert.AreEqual(1, nodes.Length);
 
163
                        Assert.AreEqual(1, node.LineNumber);
 
164
                        Assert.AreEqual(6, node.LinePosition);
 
165
                        Assert.AreEqual("Id", node.Value);
 
166
                        Assert.AreEqual("@Id", node.DisplayValue);
 
167
                }
 
168
        }
 
169
}