File: tc_xpointer.rb

package info (click to toggle)
ruby-libxml 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,812 kB
  • sloc: xml: 9,628; ruby: 7,119; ansic: 6,665; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 2,383 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
# encoding: UTF-8

require './test_helper'
require "test/unit"

class TC_XML_XPointer < Test::Unit::TestCase
  def setup()
    xp = XML::Parser.string('<!DOCTYPE ra [<!ELEMENT ra (foo+)><!ATTLIST ra id ID #IMPLIED><!ELEMENT foo (#PCDATA)><!ATTLIST foo id ID #IMPLIED>]><ra id="start"><foo id="one">one</foo><foo id="two">two</foo><foo id="three">three</foo></ra>')
    @doc = xp.parse
    assert_instance_of(XML::Document, @doc)
    @root = @doc.root
    assert_instance_of(XML::Node, @root)
  end

  def teardown()
    @doc = nil
    @root = nil
    @xptr = nil
  end

  def test_libxml_xpointer_id
    xptr = @root.pointer('xpointer(id("two"))')
    assert_instance_of(XML::XPath::Object, xptr)
    xptr.each do |node|
      # It seems from the spec that the pointer should
      # be the whole node, rather than just the ID attr.
      assert_equal('two', node.content)
      assert_instance_of(XML::Node, node)
      assert_equal('two', node['id'])
    end

    # FIXME: Not sure at all about this kind of range
    if ENV['NOTWORKING']
      @xptr = @root.pointer('xpointer(id("two")) xpointer(id("three"))')
      assert_instance_of(XML::XPath, @xptr)
      assert_instance_of(XML::Node::Set, @xptr.set)
      assert_equal(2, @xptr.set.length)
      for n in @xptr.set
        assert_match(/two|three/, n.to_s)
      end
    end
  end

  # FIXME: There is a bug in these ranges...
  if ENV['NOTWORKING']
    def test_libxml_xpointer_range()
      nstart = nend = nil
      @xptr = @root.pointer('xpointer(id("one"))').set
      @xptr.each{|n| nstart = n}
      assert_instance_of(XML::Node, nstart)
      @xptr = @root.pointer('xpointer(id("three"))').set
      @xptr.each{|n| nend = n}
      assert_instance_of(XML::Node, nend)
      range = XML::XPointer.range(nstart, nend)
      assert_instance_of(XML::XPath, range)
      assert_instance_of(XML::Node::Set, range.set)

      for n in range.set
        assert_match(/one|two|three/, n.to_s)
      end
      assert_equal(3, range.set.length)
    end
  end

#  def test_libxml_xpointer_start_point()
#    @xptr = @root.pointer('xpointer(start-point("one"))')
#    assert_instance_of(XML::XPath, @xptr)
#    set = @xptr.set
#    assert_instance_of(XML::Node::Set, set)
#    for n in set
#      assert_match(/one|two|three/, n.to_s)
#    end
#  end
end