source: pyyaml/trunk/tests/lib3/test_errors.py @ 330

Revision 330, 2.0 KB checked in by xi, 6 years ago (diff)

Share data files between Py2 and Py3 test suites.

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