Ignore:
Timestamp:
04/01/06 16:28:20 (8 years ago)
Author:
xi
Message:

Add block styles.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyyaml/branches/working-on-emitter/tests/test_emitter.py

    r127 r129  
    33 
    44from yaml import * 
     5import yaml 
    56 
    67class TestEmitterOnCanonical(test_appliance.TestAppliance): 
     
    1617        for event in events: 
    1718            emitter.emit(event) 
     19        data = writer.getvalue() 
     20        new_events = list(parse(data)) 
     21        self.failUnlessEqual(len(events), len(new_events)) 
     22        for event, new_event in zip(events, new_events): 
     23            self.failUnlessEqual(event.__class__, new_event.__class__) 
    1824 
    1925TestEmitterOnCanonical.add_tests('testEmitterOnCanonical', '.canonical') 
    2026 
     27class EventsConstructor(Constructor): 
     28 
     29    def construct_event(self, node): 
     30        if isinstance(node, ScalarNode): 
     31            mapping = {} 
     32        else: 
     33            mapping = self.construct_mapping(node) 
     34        class_name = str(node.tag[1:])+'Event' 
     35        if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceEvent', 'MappingEvent']: 
     36            mapping.setdefault('anchor', None) 
     37        if class_name in ['ScalarEvent', 'SequenceEvent', 'MappingEvent']: 
     38            mapping.setdefault('tag', None) 
     39        if class_name == 'ScalarEvent': 
     40            mapping.setdefault('value', '') 
     41        value = getattr(yaml, class_name)(**mapping) 
     42        return value 
     43 
     44EventsConstructor.add_constructor(None, EventsConstructor.construct_event) 
     45 
     46class TestEmitter(test_appliance.TestAppliance): 
     47 
     48    def _testEmitter(self, test_name, events_filename): 
     49        events = load_document(file(events_filename, 'rb'), Constructor=EventsConstructor) 
     50        self._dump(events_filename, events) 
     51        writer = StringIO.StringIO() 
     52        emitter = Emitter(writer) 
     53        for event in events: 
     54            emitter.emit(event) 
     55        data = writer.getvalue() 
     56        new_events = list(parse(data)) 
     57        self.failUnlessEqual(len(events), len(new_events)) 
     58        for event, new_event in zip(events, new_events): 
     59            self.failUnlessEqual(event.__class__, new_event.__class__) 
     60 
     61    def _dump(self, events_filename, events): 
     62        writer = sys.stdout 
     63        emitter = Emitter(writer) 
     64        print "="*30 
     65        print "EVENTS:" 
     66        print file(events_filename, 'rb').read() 
     67        print '-'*30 
     68        print "OUTPUT:" 
     69        for event in events: 
     70            emitter.emit(event) 
     71         
     72TestEmitter.add_tests('testEmitter', '.events') 
     73 
Note: See TracChangeset for help on using the changeset viewer.