source: pyyaml/trunk/tests/lib/test_reader.py @ 330

Revision 330, 978 bytes checked in by xi, 6 years ago (diff)

Share data files between Py2 and Py3 test suites.

Line 
1
2import yaml.reader
3import codecs
4
5def _run_reader(data, verbose):
6    try:
7        stream = yaml.reader.Reader(data)
8        while stream.peek() != u'\0':
9            stream.forward()
10    except yaml.reader.ReaderError, exc:
11        if verbose:
12            print exc
13    else:
14        raise AssertionError("expected an exception")
15
16def test_stream_error(error_filename, verbose=False):
17    _run_reader(open(error_filename, 'rb'), verbose)
18    _run_reader(open(error_filename, 'rb').read(), verbose)
19    for encoding in ['utf-8', 'utf-16-le', 'utf-16-be']:
20        try:
21            data = unicode(open(error_filename, 'rb').read(), encoding)
22            break
23        except UnicodeDecodeError:
24            pass
25    else:
26        return
27    _run_reader(data, verbose)
28    _run_reader(codecs.open(error_filename, encoding=encoding), verbose)
29
30test_stream_error.unittest = ['.stream-error']
31
32if __name__ == '__main__':
33    import test_appliance
34    test_appliance.run(globals())
35
Note: See TracBrowser for help on using the repository browser.