root/test/test_stream.rb

Revision 549:e07d72733cae, 2.4 kB (checked in by Sean Russell <ser@…>, 7 months ago)

Moved and renamed files to conform to Ruby's test suite.

Line 
1require "test/unit/testcase"
2require "rexml/document"
3require 'rexml/streamlistener'
4require 'stringio'
5
6class MyListener
7  include REXML::StreamListener
8end
9
10
11class StreamTester < Test::Unit::TestCase
12  # Submitted by Han Holl
13  def test_listener
14    data = %Q{<session1 user="han" password="rootWeiler" />\n<session2 user="han" password="rootWeiler" />}
15   
16    b = RequestReader.new( data )
17    b = RequestReader.new( data )
18  end
19 
20  def test_ticket_49
21    source = StringIO.new( <<-EOL )
22    <!DOCTYPE foo [
23      <!ENTITY ent "replace">
24    ]>
25    <a>&ent;</a>
26    EOL
27    REXML::Document.parse_stream(source, MyListener.new)
28  end
29 
30  def test_ticket_10
31    source = StringIO.new( <<-EOL )
32    <!DOCTYPE foo [
33      <!ENTITY ent "replace">
34      <!ATTLIST a
35             xmlns:human CDATA #FIXED "http://www.foo.com/human">
36      <!ELEMENT bar (#PCDATA)>
37      <!NOTATION n1 PUBLIC "-//HM//NOTATION TEST1//EN" 'urn:x-henrikmartensson.org:test5'>
38    ]>
39    <a/>
40    EOL
41    listener = MyListener.new
42    class << listener
43      attr_accessor :events
44      def entitydecl( content )
45        @events[ :entitydecl ] = true
46      end
47      def attlistdecl( element_name, attributes, raw_content )
48        @events[ :attlistdecl ] = true
49      end
50      def elementdecl( content )
51        @events[ :elementdecl ] = true
52      end
53      def notationdecl( content )
54        @events[ :notationdecl ] = true
55      end
56    end
57    listener.events = {}
58   
59    REXML::Document.parse_stream( source, listener )
60
61    assert( listener.events[:entitydecl] )
62    assert( listener.events[:attlistdecl] )
63    assert( listener.events[:elementdecl] )
64    assert( listener.events[:notationdecl] )
65  end
66end
67
68
69# For test_listener
70class RequestReader
71  attr_reader :doc
72  def initialize(io)
73    @stack = []
74    @doc = nil
75    catch(:fini) do
76      REXML::Document.parse_stream(io, self)
77      raise IOError
78    end
79  end
80  def tag_start(name, args)
81    if @doc
82      @stack.push(REXML::Element.new(name, @stack.last))
83    else
84      @doc = REXML::Document.new("<#{name}/>")
85      @stack.push(@doc.root)
86    end
87    args.each do |attr,val|
88      @stack.last.add_attribute(attr, val)
89    end
90  end
91  def tag_end(name, *args)
92    @stack.pop
93    throw(:fini) if @stack.empty?
94  end
95  def text(str)
96    @stack.last.text = str
97  end
98  def comment(str)
99  end
100  def doctype( name, pub_sys, long_name, uri )
101  end
102  def doctype_end
103  end
104end
Note: See TracBrowser for help on using the browser.