source: pyyaml/trunk/tests3/test_errors.py @ 328

Revision 328, 1.9 KB checked in by xi, 5 years ago (diff)

Added basic support for Python 3 (Thanks idadesub(at)users(dot)sourceforge(dot)net).

Line 
1
2import yaml, test_emitter
3
4def test_loader_error(error_filename, verbose=False):
5    try:
6        list(yaml.load_all(open(error_filename, 'rb')))
7    except yaml.YAMLError as exc:
8        if verbose:
9            print("%s:" % exc.__class__.__name__, exc)
10    else:
11        raise AssertionError("expected an exception")
12
13test_loader_error.unittest = ['.loader-error']
14
15def test_loader_error_string(error_filename, verbose=False):
16    try:
17        list(yaml.load_all(open(error_filename, 'rb').read()))
18    except yaml.YAMLError as exc:
19        if verbose:
20            print("%s:" % exc.__class__.__name__, exc)
21    else:
22        raise AssertionError("expected an exception")
23
24test_loader_error_string.unittest = ['.loader-error']
25
26def test_loader_error_single(error_filename, verbose=False):
27    try:
28        yaml.load(open(error_filename, 'rb').read())
29    except yaml.YAMLError as exc:
30        if verbose:
31            print("%s:" % exc.__class__.__name__, exc)
32    else:
33        raise AssertionError("expected an exception")
34
35test_loader_error_single.unittest = ['.single-loader-error']
36
37def test_emitter_error(error_filename, verbose=False):
38    events = list(yaml.load(open(error_filename, 'rb'),
39                    Loader=test_emitter.EventsLoader))
40    try:
41        yaml.emit(events)
42    except yaml.YAMLError as exc:
43        if verbose:
44            print("%s:" % exc.__class__.__name__, exc)
45    else:
46        raise AssertionError("expected an exception")
47
48test_emitter_error.unittest = ['.emitter-error']
49
50def test_dumper_error(error_filename, verbose=False):
51    code = open(error_filename, 'rb').read()
52    try:
53        import yaml, io
54        exec(code)
55    except yaml.YAMLError as exc:
56        if verbose:
57            print("%s:" % exc.__class__.__name__, exc)
58    else:
59        raise AssertionError("expected an exception")
60
61test_dumper_error.unittest = ['.dumper-error']
62
63if __name__ == '__main__':
64    import test_appliance
65    test_appliance.run(globals())
66
Note: See TracBrowser for help on using the repository browser.