source: pyyaml/trunk/tests/test_errors.py @ 141

Revision 141, 2.3 KB checked in by xi, 8 years ago (diff)

Add more unit tests for error messages.

Line 
1
2import test_appliance
3import test_emitter
4
5import StringIO
6
7from yaml import *
8
9class TestErrors(test_appliance.TestAppliance):
10
11    def _testLoaderErrors(self, test_name, invalid_filename):
12        #self._load(invalid_filename)
13        self.failUnlessRaises(YAMLError, lambda: self._load(invalid_filename))
14
15    def _testLoaderStringErrors(self, test_name, invalid_filename):
16        #self._load_string(invalid_filename)
17        self.failUnlessRaises(YAMLError, lambda: self._load_string(invalid_filename))
18
19    def _testEmitterErrors(self, test_name, invalid_filename):
20        events = list(load(file(invalid_filename, 'rb').read(),
21            Loader=test_emitter.EventsLoader))
22        self.failUnlessRaises(YAMLError, lambda: self._emit(events))
23
24    def _testDumperErrors(self, test_name, invalid_filename):
25        code = file(invalid_filename, 'rb').read()
26        self.failUnlessRaises(YAMLError, lambda: self._dump(code))
27
28    def _dump(self, code):
29        try:
30            exec code
31        except YAMLError, exc:
32            #print '.'*70
33            #print "%s:" % exc.__class__.__name__, exc
34            raise
35
36    def _emit(self, events):
37        try:
38            emit(events)
39        except EmitterError, exc:
40            #print '.'*70
41            #print "%s:" % exc.__class__.__name__, exc
42            raise
43
44    def _load(self, filename):
45        try:
46            return list(load_all(file(filename, 'rb')))
47        except YAMLError, exc:
48        #except ScannerError, exc:
49        #except ParserError, exc:
50        #except ComposerError, exc:
51        #except ConstructorError, exc:
52            #print '.'*70
53            #print "%s:" % exc.__class__.__name__, exc
54            raise
55
56    def _load_string(self, filename):
57        try:
58            return list(load_all(file(filename, 'rb').read()))
59        except YAMLError, exc:
60        #except ScannerError, exc:
61        #except ParserError, exc:
62        #except ComposerError, exc:
63        #except ConstructorError, exc:
64            #print '.'*70
65            #print "%s:" % filename
66            #print "%s:" % exc.__class__.__name__, exc
67            raise
68
69TestErrors.add_tests('testLoaderErrors', '.loader-error')
70TestErrors.add_tests('testLoaderStringErrors', '.loader-error')
71TestErrors.add_tests('testEmitterErrors', '.emitter-error')
72TestErrors.add_tests('testDumperErrors', '.dumper-error')
73
Note: See TracBrowser for help on using the repository browser.