Kirill Simonov avatar Kirill Simonov committed 9434779

Add more unit tests for error messages.

Comments (0)

Files changed (25)

lib/yaml/emitter.py

                         or (self.style != '' and self.event.implicit[1]))):
                 self.prepared_tag = None
                 return
-            if self.event.implicit[0] and not tag:
+            if self.event.implicit[0] and tag is None:
                 tag = u'!'
                 self.prepared_tag = None
         else:
             if (not self.canonical or tag is None) and self.event.implicit:
                 self.prepared_tag = None
                 return
-        if not tag:
+        if tag is None:
             raise EmitterError("tag is not specified")
         if self.prepared_tag is None:
             self.prepared_tag = self.prepare_tag(tag)
             if not (u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'    \
                     or ch in u'-_'):
                 raise EmitterError("invalid character %r in the anchor: %r"
-                        % (ch.encode('utf-8'), text.encode('utf-8')))
+                        % (ch.encode('utf-8'), anchor.encode('utf-8')))
         return anchor
 
     def analyze_scalar(self, scalar):

tests/data/empty-anchor.emitter-error

+- !StreamStart
+- !DocumentStart
+- !Scalar { anchor: '', value: 'foo' }
+- !DocumentEnd
+- !StreamEnd

tests/data/empty-tag-handle.emitter-error

+- !StreamStart
+- !DocumentStart { tags: { '': 'bar' } }
+- !Scalar { value: 'foo' }
+- !DocumentEnd
+- !StreamEnd

tests/data/empty-tag-prefix.emitter-error

+- !StreamStart
+- !DocumentStart { tags: { '!': '' } }
+- !Scalar { value: 'foo' }
+- !DocumentEnd
+- !StreamEnd

tests/data/empty-tag.emitter-error

+- !StreamStart
+- !DocumentStart
+- !Scalar { tag: '', value: 'key', implicit: [false,false] }
+- !DocumentEnd
+- !StreamEnd

tests/data/expected-document-end.emitter-error

+- !StreamStart
+- !DocumentStart
+- !Scalar { value: 'data 1' }
+- !Scalar { value: 'data 2' }
+- !DocumentEnd
+- !StreamEnd

tests/data/expected-document-start.emitter-error

+- !StreamStart
+- !MappingStart
+- !MappingEnd
+- !StreamEnd

tests/data/expected-node-1.emitter-error

+- !StreamStart
+- !DocumentStart
+- !DocumentEnd
+- !StreamEnd

tests/data/expected-node-2.emitter-error

+- !StreamStart
+- !DocumentStart
+- !MappingStart
+- !Scalar { value: 'key' }
+- !MappingEnd
+- !DocumentEnd
+- !StreamEnd

tests/data/expected-nothing.emitter-error

+- !StreamStart
+- !StreamEnd
+- !StreamStart
+- !StreamEnd

tests/data/expected-stream-start.emitter-error

+- !DocumentStart
+- !DocumentEnd

tests/data/invalid-anchor.emitter-error

+- !StreamStart
+- !DocumentStart
+- !Scalar { anchor: '5*5=25', value: 'foo' }
+- !DocumentEnd
+- !StreamEnd

tests/data/invalid-tag-handle-1.emitter-error

+- !StreamStart
+- !DocumentStart { tags: { '!foo': 'bar' } }
+- !Scalar { value: 'foo' }
+- !DocumentEnd
+- !StreamEnd

tests/data/invalid-tag-handle-2.emitter-error

+- !StreamStart
+- !DocumentStart { tags: { '!!!': 'bar' } }
+- !Scalar { value: 'foo' }
+- !DocumentEnd
+- !StreamEnd

tests/data/no-alias-anchor.emitter-error

+- !StreamStart
+- !DocumentStart
+- !SequenceStart
+- !Scalar { anchor: A, value: data }
+- !Alias { }
+- !SequenceEnd
+- !DocumentEnd
+- !StreamEnd

tests/data/no-tag.emitter-error

+- !StreamStart
+- !DocumentStart
+- !Scalar { value: 'foo', implicit: [false,false] }
+- !DocumentEnd
+- !StreamEnd

tests/data/recursive.dumper-error

+data = []
+data.append(data)
+dump(data)

tests/data/serializer-is-already-opened.dumper-error

+dumper = Dumper(StringIO.StringIO())
+dumper.open()
+dumper.open()

tests/data/serializer-is-closed-1.dumper-error

+dumper = Dumper(StringIO.StringIO())
+dumper.open()
+dumper.close()
+dumper.open()

tests/data/serializer-is-closed-2.dumper-error

+dumper = Dumper(StringIO.StringIO())
+dumper.open()
+dumper.close()
+dumper.serialize(ScalarNode(tag='!foo', value='bar'))

tests/data/serializer-is-not-opened-1.dumper-error

+dumper = Dumper(StringIO.StringIO())
+dumper.close()

tests/data/serializer-is-not-opened-2.dumper-error

+dumper = Dumper(StringIO.StringIO())
+dumper.serialize(ScalarNode(tag='!foo', value='bar'))

tests/data/unknown.dumper-error

+safe_dump(object)

tests/data/unsupported-version.emitter-error

+- !StreamStart
+- !DocumentStart { version: [5,6] }
+- !Scalar { value: foo }
+- !DocumentEnd
+- !StreamEnd

tests/test_errors.py

 
 import test_appliance
+import test_emitter
+
+import StringIO
 
 from yaml import *
 
         #self._load_string(invalid_filename)
         self.failUnlessRaises(YAMLError, lambda: self._load_string(invalid_filename))
 
+    def _testEmitterErrors(self, test_name, invalid_filename):
+        events = list(load(file(invalid_filename, 'rb').read(),
+            Loader=test_emitter.EventsLoader))
+        self.failUnlessRaises(YAMLError, lambda: self._emit(events))
+
+    def _testDumperErrors(self, test_name, invalid_filename):
+        code = file(invalid_filename, 'rb').read()
+        self.failUnlessRaises(YAMLError, lambda: self._dump(code))
+
+    def _dump(self, code):
+        try:
+            exec code
+        except YAMLError, exc:
+            #print '.'*70
+            #print "%s:" % exc.__class__.__name__, exc
+            raise
+
+    def _emit(self, events):
+        try:
+            emit(events)
+        except EmitterError, exc:
+            #print '.'*70
+            #print "%s:" % exc.__class__.__name__, exc
+            raise
+
     def _load(self, filename):
         try:
             return list(load_all(file(filename, 'rb')))
 
 TestErrors.add_tests('testLoaderErrors', '.loader-error')
 TestErrors.add_tests('testLoaderStringErrors', '.loader-error')
+TestErrors.add_tests('testEmitterErrors', '.emitter-error')
+TestErrors.add_tests('testDumperErrors', '.dumper-error')
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.