File: test_text.rb

package info (click to toggle)
ruby-nokogiri 1.11.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,576 kB
  • sloc: xml: 28,086; ruby: 18,456; java: 13,067; ansic: 5,138; yacc: 265; sh: 208; makefile: 27
file content (69 lines) | stat: -rw-r--r-- 2,035 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
require "helper"

module Nokogiri
  module XML
    class TestText < Nokogiri::TestCase
      def test_css_path
        doc  = Nokogiri.XML "<root> foo <a>something</a> bar bazz </root>"
        node = doc.root.children[2]
        assert_instance_of Nokogiri::XML::Text, node
        assert_equal node, doc.at_css(node.css_path)
      end

      def test_inspect
        node = Text.new('hello world', Document.new)
        assert_equal "#<#{node.class.name}:#{sprintf("0x%x",node.object_id)} #{node.text.inspect}>", node.inspect
      end

      def test_new
        node = Text.new('hello world', Document.new)
        assert node
        assert_equal('hello world', node.content)
        assert_instance_of Nokogiri::XML::Text, node
      end

      def test_lots_of_text
        100.times { Text.new('hello world', Document.new) }
      end

      def test_new_without_document
        doc = Document.new
        node = Nokogiri::XML::Element.new('foo', doc)

        assert Text.new('hello world', node)
      end

      def test_content=
        node = Text.new('foo', Document.new)
        assert_equal('foo', node.content)
        node.content = '& <foo> &amp;'
        assert_equal('& <foo> &amp;', node.content)
        assert_equal('&amp; &lt;foo&gt; &amp;amp;', node.to_xml)
      end

      def test_add_child
        node = Text.new('foo', Document.new)
        if Nokogiri.jruby?
          exc = RuntimeError
        else
          exc = ArgumentError
        end
        assert_raises(exc) {
          node.add_child Text.new('bar', Document.new)
        }
        assert_raises(exc) {
          node << Text.new('bar', Document.new)
        }
      end

      def test_wrap
        xml = '<root><thing><div class="title">important thing</div></thing></root>'
        doc = Nokogiri::XML(xml)
        text = doc.at_css("div").children.first
        text.wrap("<wrapper/>")
        assert_equal 'wrapper', text.parent.name
        assert_equal 'wrapper', doc.at_css("div").children.first.name
      end
    end
  end
end