diff options
author | xi <xi@18f92427-320e-0410-9341-c67f048884a3> | 2006-04-01 21:28:20 +0000 |
---|---|---|
committer | xi <xi@18f92427-320e-0410-9341-c67f048884a3> | 2006-04-01 21:28:20 +0000 |
commit | 32bf6e148723d37bca70ebf895de9c5cbe390433 (patch) | |
tree | 7dbada52c486e9d1eeed5efcd411d8d65680f7b7 /tests | |
parent | c6f2fc9875595b01f4249fbe87b0b846d0a2adc9 (diff) | |
download | pyyaml-working-on-emitter.tar.gz |
Add block styles.working-on-emitter
git-svn-id: http://svn.pyyaml.org/pyyaml/branches/working-on-emitter@129 18f92427-320e-0410-9341-c67f048884a3
Diffstat (limited to 'tests')
-rw-r--r-- | tests/data/aliases.events | 8 | ||||
-rw-r--r-- | tests/data/documents.events | 11 | ||||
-rw-r--r-- | tests/data/mappings.events | 44 | ||||
-rw-r--r-- | tests/data/scalars.events | 24 | ||||
-rw-r--r-- | tests/data/sequences.events | 81 | ||||
-rw-r--r-- | tests/test_emitter.py | 53 |
6 files changed, 221 insertions, 0 deletions
diff --git a/tests/data/aliases.events b/tests/data/aliases.events new file mode 100644 index 0000000..b24ab06 --- /dev/null +++ b/tests/data/aliases.events @@ -0,0 +1,8 @@ +- !StreamStart +- !DocumentStart +- !Sequence +- !Scalar { anchor: 'myanchor', tag: '!mytag', value: 'data' } +- !Alias { anchor: 'myanchor' } +- !CollectionEnd +- !DocumentEnd +- !StreamEnd diff --git a/tests/data/documents.events b/tests/data/documents.events new file mode 100644 index 0000000..223a314 --- /dev/null +++ b/tests/data/documents.events @@ -0,0 +1,11 @@ +- !StreamStart +- !DocumentStart +- !Scalar { implicit: true } +- !DocumentEnd +- !DocumentStart +- !Scalar { implicit: true } +- !DocumentEnd +- !DocumentStart +- !Scalar { implicit: true } +- !DocumentEnd +- !StreamEnd diff --git a/tests/data/mappings.events b/tests/data/mappings.events new file mode 100644 index 0000000..bcdbd02 --- /dev/null +++ b/tests/data/mappings.events @@ -0,0 +1,44 @@ +- !StreamStart + +- !DocumentStart +- !Mapping +- !Scalar { implicit: true, value: 'key' } +- !Scalar { implicit: true, value: 'value' } +- !Scalar { implicit: true, value: 'empty mapping' } +- !Mapping +- !CollectionEnd +- !Scalar { implicit: true, value: 'empty mapping with tag' } +- !Mapping { tag: '!mytag' } +- !CollectionEnd +- !Scalar { implicit: true, value: 'block mapping' } +- !Mapping +- !Mapping +- !Scalar { implicit: true, value: 'complex' } +- !Scalar { implicit: true, value: 'key' } +- !Scalar { implicit: true, value: 'complex' } +- !Scalar { implicit: true, value: 'key' } +- !CollectionEnd +- !Mapping +- !Scalar { implicit: true, value: 'complex' } +- !Scalar { implicit: true, value: 'key' } +- !CollectionEnd +- !CollectionEnd +- !Scalar { implicit: true, value: 'flow mapping' } +- !Mapping { flow: true } +- !Scalar { implicit: true, value: 'key' } +- !Scalar { implicit: true, value: 'value' } +- !Mapping +- !Scalar { implicit: true, value: 'complex' } +- !Scalar { implicit: true, value: 'key' } +- !Scalar { implicit: true, value: 'complex' } +- !Scalar { implicit: true, value: 'key' } +- !CollectionEnd +- !Mapping +- !Scalar { implicit: true, value: 'complex' } +- !Scalar { implicit: true, value: 'key' } +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !DocumentEnd + +- !StreamEnd diff --git a/tests/data/scalars.events b/tests/data/scalars.events new file mode 100644 index 0000000..93d4649 --- /dev/null +++ b/tests/data/scalars.events @@ -0,0 +1,24 @@ +- !StreamStart + +- !DocumentStart +- !Mapping +- !Scalar { implicit: true, value: 'empty scalar' } +- !Scalar { implicit: true, value: '' } +- !Scalar { implicit: true, value: 'implicit scalar' } +- !Scalar { implicit: true, value: 'data' } +- !Scalar { implicit: true, value: 'quoted scalar' } +- !Scalar { value: 'data', style: '"' } +- !Scalar { implicit: true, value: 'block scalar' } +- !Scalar { value: 'data', style: '|' } +- !Scalar { implicit: true, value: 'empty scalar with tag' } +- !Scalar { implicit: true, tag: '!mytag', value: '' } +- !Scalar { implicit: true, value: 'implicit scalar with tag' } +- !Scalar { implicit: true, tag: '!mytag', value: 'data' } +- !Scalar { implicit: true, value: 'quoted scalar with tag' } +- !Scalar { value: 'data', style: '"', tag: '!mytag' } +- !Scalar { implicit: true, value: 'block scalar with tag' } +- !Scalar { value: 'data', style: '|', tag: '!mytag' } +- !CollectionEnd +- !DocumentEnd + +- !StreamEnd diff --git a/tests/data/sequences.events b/tests/data/sequences.events new file mode 100644 index 0000000..206f48c --- /dev/null +++ b/tests/data/sequences.events @@ -0,0 +1,81 @@ +- !StreamStart + +- !DocumentStart +- !Sequence +- !CollectionEnd +- !DocumentEnd + +- !DocumentStart +- !Sequence { tag: '!mytag' } +- !CollectionEnd +- !DocumentEnd + +- !DocumentStart +- !Sequence +- !Sequence +- !CollectionEnd +- !Sequence { tag: '!mytag' } +- !CollectionEnd +- !Sequence +- !Scalar +- !Scalar { value: 'data' } +- !Scalar { tag: '!mytag', value: 'data' } +- !CollectionEnd +- !Sequence +- !Sequence +- !Sequence +- !Scalar +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !Sequence +- !Sequence { tag: '!mytag' } +- !Sequence +- !Scalar { value: 'data' } +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !DocumentEnd + +- !DocumentStart +- !Sequence +- !Mapping +- !Scalar { value: 'key1' } +- !Sequence +- !Scalar { value: 'data1' } +- !Scalar { value: 'data2' } +- !CollectionEnd +- !Scalar { value: 'key2' } +- !Sequence { tag: '!mytag1' } +- !Scalar { value: 'data3' } +- !Sequence +- !Scalar { value: 'data4' } +- !Scalar { value: 'data5' } +- !CollectionEnd +- !Sequence { tag: '!mytag2' } +- !Scalar { value: 'data6' } +- !Scalar { value: 'data7' } +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !DocumentEnd + +- !DocumentStart +- !Sequence +- !Sequence { flow: true } +- !Sequence +- !CollectionEnd +- !Scalar +- !Scalar { value: 'data' } +- !Scalar { tag: '!mytag', value: 'data' } +- !Sequence { tag: '!mytag' } +- !Scalar { value: 'data' } +- !Scalar { value: 'data' } +- !CollectionEnd +- !CollectionEnd +- !CollectionEnd +- !DocumentEnd + +- !StreamEnd diff --git a/tests/test_emitter.py b/tests/test_emitter.py index fed6953..698ea50 100644 --- a/tests/test_emitter.py +++ b/tests/test_emitter.py @@ -2,6 +2,7 @@ import test_appliance, sys, StringIO from yaml import * +import yaml class TestEmitterOnCanonical(test_appliance.TestAppliance): @@ -15,6 +16,58 @@ class TestEmitterOnCanonical(test_appliance.TestAppliance): #print file(canonical_filename, 'rb').read() for event in events: emitter.emit(event) + data = writer.getvalue() + new_events = list(parse(data)) + self.failUnlessEqual(len(events), len(new_events)) + for event, new_event in zip(events, new_events): + self.failUnlessEqual(event.__class__, new_event.__class__) TestEmitterOnCanonical.add_tests('testEmitterOnCanonical', '.canonical') +class EventsConstructor(Constructor): + + def construct_event(self, node): + if isinstance(node, ScalarNode): + mapping = {} + else: + mapping = self.construct_mapping(node) + class_name = str(node.tag[1:])+'Event' + if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceEvent', 'MappingEvent']: + mapping.setdefault('anchor', None) + if class_name in ['ScalarEvent', 'SequenceEvent', 'MappingEvent']: + mapping.setdefault('tag', None) + if class_name == 'ScalarEvent': + mapping.setdefault('value', '') + value = getattr(yaml, class_name)(**mapping) + return value + +EventsConstructor.add_constructor(None, EventsConstructor.construct_event) + +class TestEmitter(test_appliance.TestAppliance): + + def _testEmitter(self, test_name, events_filename): + events = load_document(file(events_filename, 'rb'), Constructor=EventsConstructor) + self._dump(events_filename, events) + writer = StringIO.StringIO() + emitter = Emitter(writer) + for event in events: + emitter.emit(event) + data = writer.getvalue() + new_events = list(parse(data)) + self.failUnlessEqual(len(events), len(new_events)) + for event, new_event in zip(events, new_events): + self.failUnlessEqual(event.__class__, new_event.__class__) + + def _dump(self, events_filename, events): + writer = sys.stdout + emitter = Emitter(writer) + print "="*30 + print "EVENTS:" + print file(events_filename, 'rb').read() + print '-'*30 + print "OUTPUT:" + for event in events: + emitter.emit(event) + +TestEmitter.add_tests('testEmitter', '.events') + |