Ignore:
Timestamp:
04/15/06 19:54:52 (8 years ago)
Author:
xi
Message:

Major refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/tests/test_emitter.py

    r133 r136  
    1717 
    1818    def _testEmitter(self, test_name, filename, canonical=None): 
    19         events = list(iter(Parser(Scanner(Reader(file(filename, 'rb')))))) 
    20         if canonical is not None: 
    21             events[0].canonical = canonical 
    22         #self._dump(filename, events) 
    23         writer = StringIO.StringIO() 
    24         emitter = Emitter(writer) 
    25         for event in events: 
    26             emitter.emit(event) 
    27         data = writer.getvalue() 
     19        events = list(parse(file(filename, 'rb'))) 
     20        #self._dump(filename, events, canonical) 
     21        stream = StringIO.StringIO() 
     22        emit(events, stream, canonical=canonical) 
     23        data = stream.getvalue() 
    2824        new_events = list(parse(data)) 
    2925        for event, new_event in zip(events, new_events): 
     
    3935                self.failUnlessEqual(event.value, new_event.value) 
    4036 
    41     def _dump(self, filename, events): 
    42         writer = sys.stdout 
    43         emitter = Emitter(writer) 
     37    def _dump(self, filename, events, canonical): 
    4438        print "="*30 
    4539        print "ORIGINAL DOCUMENT:" 
     
    4741        print '-'*30 
    4842        print "EMITTED DOCUMENT:" 
    49         for event in events: 
    50             emitter.emit(event) 
     43        emit(events, sys.stdout, canonical=canonical) 
    5144         
    5245TestEmitter.add_tests('testEmitterOnData', '.canonical', '.data') 
    53 #TestEmitter.add_tests('testEmitterOnCanonicalNormally', '.canonical') 
    54 #TestEmitter.add_tests('testEmitterOnCanonicalCanonically', '.canonical') 
     46TestEmitter.add_tests('testEmitterOnCanonicalNormally', '.canonical') 
     47TestEmitter.add_tests('testEmitterOnCanonicalCanonically', '.canonical') 
    5548 
    56 class EventsConstructor(Constructor): 
     49class EventsLoader(Loader): 
    5750 
    5851    def construct_event(self, node): 
     
    6760            mapping.setdefault('tag', None) 
    6861        if class_name == 'ScalarEvent': 
     62            mapping.setdefault('implicit', False) 
    6963            mapping.setdefault('value', '') 
    7064        value = getattr(yaml, class_name)(**mapping) 
    7165        return value 
    7266 
    73 EventsConstructor.add_constructor(None, EventsConstructor.construct_event) 
     67EventsLoader.add_constructor(None, EventsLoader.construct_event) 
    7468 
    7569class TestEmitterEvents(test_appliance.TestAppliance): 
    7670 
    7771    def _testEmitterEvents(self, test_name, events_filename): 
    78         events = list(load(file(events_filename, 'rb'), Constructor=EventsConstructor)) 
     72        events = list(load(file(events_filename, 'rb'), Loader=EventsLoader)) 
    7973        #self._dump(events_filename, events) 
    80         writer = StringIO.StringIO() 
    81         emitter = Emitter(writer) 
    82         for event in events: 
    83             emitter.emit(event) 
    84         data = writer.getvalue() 
     74        stream = StringIO.StringIO() 
     75        emit(events, stream) 
     76        data = stream.getvalue() 
    8577        new_events = list(parse(data)) 
    8678        self.failUnlessEqual(len(events), len(new_events)) 
     
    9789 
    9890    def _dump(self, events_filename, events): 
    99         writer = sys.stdout 
    100         emitter = Emitter(writer) 
    10191        print "="*30 
    10292        print "EVENTS:" 
     
    10494        print '-'*30 
    10595        print "OUTPUT:" 
    106         for event in events: 
    107             emitter.emit(event) 
     96        emit(events, sys.stdout) 
    10897         
    10998TestEmitterEvents.add_tests('testEmitterEvents', '.events') 
Note: See TracChangeset for help on using the changeset viewer.