File: test_attr.rb

package info (click to toggle)
ruby-nokogiri 1.10.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,088 kB
  • sloc: xml: 28,081; ruby: 16,687; java: 13,293; ansic: 4,954; yacc: 265; sh: 76; makefile: 19
file content (96 lines) | stat: -rw-r--r-- 3,060 bytes parent folder | download
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
require "helper"

module Nokogiri
  module XML
    class TestAttr < Nokogiri::TestCase
      def test_new
        100.times {
          doc = Nokogiri::XML::Document.new
          assert doc
          assert Nokogiri::XML::Attr.new(doc, 'foo')
        }
      end

      def test_new_raises_argerror_on_nondocument
        document = Nokogiri::XML "<root><foo/></root>"
        assert_raises ArgumentError do
          Nokogiri::XML::Attr.new document.at_css("foo"), "bar"
        end
      end

      def test_content=
        xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
        address = xml.xpath('//address')[3]
        street = address.attributes['street']
        street.content = "Y&ent1;"
        assert_equal "Y&ent1;", street.value
      end

      def test_set_value
        xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
        address = xml.xpath('//address')[3]
        street = address.attributes['street']
        street.value = "Y&ent1;"
        assert_equal "Y&ent1;", street.value
        assert_includes %Q{ street="Y&amp;ent1;"}, street.to_xml
      end

      def test_set_value_with_entity_string_in_html_file
        html = Nokogiri::HTML("<html><body><div foo='asdf'>")
        foo = html.at_css("div").attributes["foo"]
        foo.value = "Y&ent1;"
        assert_includes %Q{ foo="Y&amp;ent1;"}, foo.to_html
      end

      def test_set_value_with_blank_string_in_html_file
        html = Nokogiri::HTML("<html><body><div foo='asdf'>")
        foo = html.at_css("div").attributes["foo"]
        foo.value = ""
        assert_includes %Q{ foo=""}, foo.to_html
      end

      def test_set_value_of_boolean_attr_with_nil_in_html_file
        html = Nokogiri::HTML("<html><body><div disabled='asdf'>")
        disabled = html.at_css("div").attributes["disabled"]
        disabled.value = nil
        assert_includes %Q{ disabled}, disabled.to_html
      end

      def test_unlink # aliased as :remove
        xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
        address = xml.xpath('/staff/employee/address').first
        assert_equal 'Yes', address['domestic']

        attr = address.attribute_nodes.first
        return_val = attr.unlink
        assert_nil address['domestic']
        assert_equal attr, return_val
      end

      def test_parsing_attribute_namespace
        doc = Nokogiri::XML <<-EOXML
<root xmlns='http://google.com/' xmlns:f='http://flavorjon.es/'>
  <div f:myattr='foo'></div>
</root>
        EOXML

        node = doc.at_css "div"
        attr = node.attributes["myattr"]
        assert_equal "http://flavorjon.es/", attr.namespace.href
      end

      def test_setting_attribute_namespace
        doc = Nokogiri::XML <<-EOXML
<root xmlns='http://google.com/' xmlns:f='http://flavorjon.es/'>
  <div f:myattr='foo'></div>
</root>
        EOXML

        node = doc.at_css "div"
        attr = node.attributes["myattr"]
        attr.add_namespace("fizzle", "http://fizzle.com/")
        assert_equal "http://fizzle.com/", attr.namespace.href
      end
    end
  end
end