File: test_sax_jy.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (49 lines) | stat: -rw-r--r-- 1,425 bytes parent folder | download | duplicates (8)
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
import sys
import StringIO
import unittest

from xml.sax import saxutils
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces

from test import test_support

file = StringIO.StringIO("""<collection>
  <comic title="Sandman" number='62'>
    <writer>Neil Gaiman</writer>
    <penciller pages='1-9,18-24'>Glyn Dillon</penciller>
    <penciller pages="10-17">Charles Vess</penciller>
  </comic>
  <comic title="Shade, the Changing Man" number="7">
    <writer>Peter Milligan</writer>
    <penciller>Chris Bachalo</penciller>
  </comic>
</collection>""")

class FindIssue(saxutils.DefaultHandler):
    def __init__(self, title, number):
        self.search_title, self.search_number = title, number
        self.match = 0

    def startElement(self,name,attrs):
        global match
        if name != 'comic' : return

        title = attrs.get('title', None)
        number = attrs.get('number',None)
        if title == self.search_title and number == self.search_number:
            self.match += 1

class SimpleSaxTest(unittest.TestCase):
    def test_find_issue(self):
        parser = make_parser()
        parser.setFeature(feature_namespaces,0)
        dh = FindIssue('Sandman', '62')
        parser.setContentHandler(dh)
        parser.parse(file)
        self.assertEquals(1, dh.match)
def test_main():
    test_support.run_unittest(SimpleSaxTest)

if __name__ == "__main__":
    test_main()