File: test_namespace.rb

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (98 lines) | stat: -rw-r--r-- 3,028 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
97
98
# frozen_string_literal: true

require "helper"

module Nokogiri
  module XML
    class TestNamespace < Nokogiri::TestCase
      def setup
        super
        @xml = Nokogiri::XML(<<-eoxml)
          <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
            <awesome/>
          </root>
        eoxml
      end

      if Nokogiri.uses_libxml?
        def test_namespace_is_in_node_cache
          node = @xml.root.namespace
          assert_includes(@xml.instance_variable_get(:@node_cache), node)
        end
      end

      def test_built_nodes_keep_namespace_decls
        doc = Document.new
        e   = Node.new("element", doc)
        c   = Node.new("child", doc)
        c.default_namespace = "woop:de:doo"

        assert(c.namespace, "has a namespace")
        e.add_child(c)
        assert(c.namespace, "has a namespace")

        doc.add_child(e)
        assert(c.namespace, "has a namespace")
      end

      def test_inspect
        ns = @xml.root.namespace
        assert_equal("#<#{ns.class.name}:#{format("0x%x", ns.object_id)} href=#{ns.href.inspect}>", ns.inspect)
      end

      def test_namespace_node_prefix
        namespaces = @xml.root.namespace_definitions
        assert_equal([nil, "foo"], namespaces.map(&:prefix))
      end

      def test_namespace_node_href
        namespaces = @xml.root.namespace_definitions
        assert_equal([
          "http://tenderlovemaking.com/",
          "bar",
        ], namespaces.map(&:href))
      end

      def test_equality
        namespaces = @xml.root.namespace_definitions
        assert_equal(namespaces, @xml.root.namespace_definitions)
      end

      def test_add_definition
        @xml.root.add_namespace_definition("baz", "bar")
        assert_equal(3, @xml.root.namespace_definitions.length)
      end

      def test_add_definition_return
        ns = @xml.root.add_namespace_definition("baz", "bar")
        assert_equal("baz", ns.prefix)
      end

      def test_remove_entity_namespace
        s = %q{<?xml version='1.0'?><!DOCTYPE schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd" [<!ENTITY % p ''>]>}
        Nokogiri::XML(s).remove_namespaces!
        # TODO: we should probably assert something here. See commit 14d2f59.
      end

      def test_maintain_element_namespaces
        doc = Document.new
        subject = Nokogiri::XML::Node.new("foo", doc)
        subject << '<foobar xmlns="barfoo"/>'
        child = subject.children.first
        assert_equal("foobar", child.name)
        assert_equal("barfoo", child.namespace.href)
        assert_empty(child.attributes)
      end

      def test_maintain_element_namespaces_in_urn
        doc = Document.new
        subject = Nokogiri::XML::Node.new("foo", doc)
        subject << '<foobar xmlns="urn:xmpp:foospec:barfoo"/>'
        child = subject.children.first
        assert_equal("foobar", child.name)
        assert_equal("urn:xmpp:foospec:barfoo", child.namespace.href)
        assert_empty(child.attributes)
      end
    end
  end
end