File: xpath_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (215 lines) | stat: -rw-r--r-- 6,242 bytes parent folder | download | duplicates (2)
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
require "spec"
require "xml"

private def doc
  XML.parse(%(\
    <?xml version='1.0' encoding='UTF-8'?>
    <people>
      <person id="1">
        <name>John</name>
      </person>
      <person id="2">
        <name>Peter</name>
      </person>
    </people>
    ))
end

module XML
  describe XPathContext do
    it "finds nodes" do
      doc = doc()

      nodes = doc.xpath("//people/person").as(NodeSet)
      nodes.size.should eq(2)

      nodes[0].name.should eq("person")
      nodes[0]["id"].should eq("1")

      nodes[1].name.should eq("person")
      nodes[1]["id"].should eq("2")

      nodes = doc.xpath_nodes("//people/person")
      nodes.size.should eq(2)
    end

    it "finds string" do
      doc = doc()

      id = doc.xpath("string(//people/person[1]/@id)").as(String)
      id.should eq("1")

      id = doc.xpath_string("string(//people/person[1]/@id)")
      id.should eq("1")
    end

    it "finds number" do
      doc = doc()

      count = doc.xpath("count(//people/person)").as(Float64)
      count.should eq(2)

      count = doc.xpath_float("count(//people/person)")
      count.should eq(2)
    end

    it "finds boolean" do
      doc = doc()

      id = doc.xpath("boolean(//people/person[1]/@id)").as(Bool)
      id.should be_true

      id = doc.xpath_bool("boolean(//people/person[1]/@id)")
      id.should be_true
    end

    it "raises on invalid xpath" do
      expect_raises XML::Error do
        doc = doc()
        doc.xpath("coco()")
      end
    end

    it "returns nil with invalid xpath" do
      doc = doc()
      doc.xpath_node("//invalid").should be_nil
    end

    it "finds with explicit namespace" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
        </feed>
        ))
      nodes = doc.xpath("//atom:feed", namespaces: {"atom" => "http://www.w3.org/2005/Atom"}).as(NodeSet)
      nodes.size.should eq(1)
      nodes[0].name.should eq("feed")
      ns = nodes[0].namespace.not_nil!
      ns.href.should eq("http://www.w3.org/2005/Atom")
      ns.prefix.should be_nil
    end

    it "finds with implicit (root) namespaces" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <openSearch:feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
          <openSearch:something>
          </openSearch:something>
        </openSearch:feed>
        ))
      nodes = doc.xpath("//openSearch:feed/openSearch:something").as(NodeSet)
      nodes.size.should eq(1)
      nodes[0].name.should eq("something")
      ns = nodes[0].namespace.not_nil!
      ns.href.should eq("http://a9.com/-/spec/opensearchrss/1.0/")
      ns.prefix.should eq("openSearch")
    end

    it "finds with root namespaces" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
        </feed>
        ))
      nodes = doc.xpath("//xmlns:feed", namespaces: doc.root.not_nil!.namespaces).as(NodeSet)
      nodes.size.should eq(1)
      nodes[0].name.should eq("feed")
      ns = nodes[0].namespace.not_nil!
      ns.href.should eq("http://www.w3.org/2005/Atom")
      ns.prefix.should be_nil
    end

    it "finds with root namespaces (using prefix)" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <openSearch:feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
        </openSearch:feed>
        ))
      nodes = doc.xpath("//openSearch:feed", namespaces: doc.root.not_nil!.namespaces).as(NodeSet)
      nodes.size.should eq(1)
      nodes[0].name.should eq("feed")
      ns = nodes[0].namespace.not_nil!
      ns.href.should eq("http://a9.com/-/spec/opensearchrss/1.0/")
      ns.prefix.should eq("openSearch")
    end

    it "finds with variable binding" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed>
          <person id="1"/>
          <person id="2"/>
        </feed>
        ))
      nodes = doc.xpath("//feed/person[@id=$value]", variables: {"value" => 2}).as(NodeSet)
      nodes.size.should eq(1)
      nodes[0]["id"].should eq("2")
    end

    it "finds with variable binding (bool)" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed>
          <person id="1"/>
          <person id="2"/>
        </feed>
        ))
      result = doc.xpath_bool("count(//feed/person[@id=$value]) = 1", variables: {"value" => 2})
      result.should be_true
    end

    it "finds with variable binding (float)" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed>
          <person id="1"/>
          <person id="2"/>
        </feed>
        ))
      result = doc.xpath_float("count(//feed/person[@id=$value])", variables: {"value" => 2})
      result.should eq(1.0)
    end

    it "finds with variable binding (nodes)" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed>
          <person id="1"/>
          <person id="2"/>
        </feed>
        ))
      nodes = doc.xpath_nodes("//feed/person[@id=$value]", variables: {"value" => 2})
      nodes.size.should eq(1)
      nodes[0]["id"].should eq("2")
    end

    it "finds with variable binding (node)" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed>
          <person id="1"/>
          <person id="2"/>
        </feed>
        ))
      node = doc.xpath_node("//feed/person[@id=$value]", variables: {"value" => 2}).not_nil!
      node["id"].should eq("2")
    end

    it "finds with variable binding (string)" do
      doc = XML.parse(%(\
        <?xml version="1.0" encoding="UTF-8"?>
        <feed>
          <person id="1"/>
          <person id="2"/>
        </feed>
        ))
      result = doc.xpath_string("string(//feed/person[@id=$value]/@id)", variables: {"value" => 2})
      result.should eq("2")
    end

    it "NodeSet#to_s" do
      doc = doc()
      doc.xpath("//people/person").to_s
    end
  end
end