File: test_ultra_light.rb

package info (click to toggle)
ruby2.3 2.3.3-1%2Bdeb9u8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 65,344 kB
  • sloc: ruby: 639,947; ansic: 317,772; xml: 25,445; yacc: 9,068; javascript: 6,648; lisp: 2,568; tcl: 949; makefile: 623; sh: 533; perl: 62; sed: 53; python: 47; awk: 36; asm: 35
file content (70 lines) | stat: -rw-r--r-- 1,808 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: false
require "test/unit"
require "rexml/parsers/ultralightparser"

module REXMLTests
class TestUltraLightParser < Test::Unit::TestCase
  class TestDocumentTypeDeclaration < self
    def test_entity_declaration
      assert_equal([
                     [
                       :start_doctype,
                       :parent,
                       "root",
                       "SYSTEM",
                       "urn:x-test",
                       nil,
                       [:entitydecl, "name", "value"]
                     ],
                     [:text, "\n"],
                     [:start_element, :parent, "root", {}],
                     [:text, "\n"],
                   ],
                   parse(<<-INTERNAL_SUBSET))
<!ENTITY name "value">
      INTERNAL_SUBSET
    end

    private
    def xml(internal_subset)
      <<-XML
<!DOCTYPE root SYSTEM "urn:x-test" [
#{internal_subset}
]>
<root/>
      XML
    end

    def parse(internal_subset)
      parser = REXML::Parsers::UltraLightParser.new(xml(internal_subset))
      normalize(parser.parse)
    end

    def normalize(root)
      root.collect do |child|
        normalize_child(child)
      end
    end

    def normalize_child(child)
      tag = child.first
      case tag
      when :start_doctype
        normalized_parent = :parent
        normalized_doctype = child.dup
        normalized_doctype[1] = normalized_parent
        normalized_doctype
      when :start_element
        tag, parent, name, attributes, *children = child
        normalized_parent = :parent
        normalized_children = children.collect do |sub_child|
          normalize_child(sub_child)
        end
        [tag, normalized_parent, name, attributes, *normalized_children]
      else
        child
      end
    end
  end
end
end