Ignore:
Timestamp:
04/09/06 15:51:02 (8 years ago)
Author:
xi
Message:

Emitter is done!!!

File:
1 edited

Legend:

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

    r130 r132  
    55import yaml 
    66 
    7 class TestEmitterOnCanonical(test_appliance.TestAppliance): 
     7class TestEmitter(test_appliance.TestAppliance): 
    88 
    9     def _testEmitterOnCanonical(self, test_name, canonical_filename): 
    10         events = list(iter(Parser(Scanner(Reader(file(canonical_filename, 'rb')))))) 
    11         #writer = sys.stdout 
     9    def _testEmitterOnData(self, test_name, canonical_filename, data_filename): 
     10        self._testEmitter(test_name, data_filename) 
     11 
     12    def _testEmitterOnCanonicalNormally(self, test_name, canonical_filename): 
     13        self._testEmitter(test_name, canonical_filename, False) 
     14 
     15    def _testEmitterOnCanonicalCanonically(self, test_name, canonical_filename): 
     16        self._testEmitter(test_name, canonical_filename, True) 
     17 
     18    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) 
    1223        writer = StringIO.StringIO() 
    1324        emitter = Emitter(writer) 
    14         #print "-"*30 
    15         #print "ORIGINAL DATA:" 
    16         #print file(canonical_filename, 'rb').read() 
    1725        for event in events: 
    1826            emitter.emit(event) 
    1927        data = writer.getvalue() 
    2028        new_events = list(parse(data)) 
    21         self.failUnlessEqual(len(events), len(new_events)) 
    2229        for event, new_event in zip(events, new_events): 
    2330            self.failUnlessEqual(event.__class__, new_event.__class__) 
     31            if isinstance(event, NodeEvent): 
     32                self.failUnlessEqual(event.anchor, new_event.anchor) 
     33            if isinstance(event, CollectionStartEvent): 
     34                self.failUnlessEqual(event.tag, new_event.tag) 
     35            if isinstance(event, ScalarEvent): 
     36                #self.failUnlessEqual(event.implicit, new_event.implicit) 
     37                if not event.implicit and not new_event.implicit: 
     38                    self.failUnlessEqual(event.tag, new_event.tag) 
     39                self.failUnlessEqual(event.value, new_event.value) 
    2440 
    25 TestEmitterOnCanonical.add_tests('testEmitterOnCanonical', '.canonical') 
     41    def _dump(self, filename, events): 
     42        writer = sys.stdout 
     43        emitter = Emitter(writer) 
     44        print "="*30 
     45        print "ORIGINAL DOCUMENT:" 
     46        print file(filename, 'rb').read() 
     47        print '-'*30 
     48        print "EMITTED DOCUMENT:" 
     49        for event in events: 
     50            emitter.emit(event) 
     51         
     52TestEmitter.add_tests('testEmitterOnData', '.canonical', '.data') 
     53#TestEmitter.add_tests('testEmitterOnCanonicalNormally', '.canonical') 
     54#TestEmitter.add_tests('testEmitterOnCanonicalCanonically', '.canonical') 
    2655 
    2756class EventsConstructor(Constructor): 
     
    4473EventsConstructor.add_constructor(None, EventsConstructor.construct_event) 
    4574 
    46 class TestEmitter(test_appliance.TestAppliance): 
     75class TestEmitterEvents(test_appliance.TestAppliance): 
    4776 
    48     def _testEmitter(self, test_name, events_filename): 
    49         events = load_document(file(events_filename, 'rb'), Constructor=EventsConstructor) 
    50         self._dump(events_filename, events) 
     77    def _testEmitterEvents(self, test_name, events_filename): 
     78        events = list(load_document(file(events_filename, 'rb'), Constructor=EventsConstructor)) 
     79        #self._dump(events_filename, events) 
    5180        writer = StringIO.StringIO() 
    5281        emitter = Emitter(writer) 
     
    5887        for event, new_event in zip(events, new_events): 
    5988            self.failUnlessEqual(event.__class__, new_event.__class__) 
     89            if isinstance(event, NodeEvent): 
     90                self.failUnlessEqual(event.anchor, new_event.anchor) 
     91            if isinstance(event, CollectionStartEvent): 
     92                self.failUnlessEqual(event.tag, new_event.tag) 
     93            if isinstance(event, ScalarEvent): 
     94                self.failUnless(event.implicit == new_event.implicit 
     95                        or event.tag == new_event.tag) 
     96                self.failUnlessEqual(event.value, new_event.value) 
    6097 
    6198    def _dump(self, events_filename, events): 
     
    70107            emitter.emit(event) 
    71108         
    72 TestEmitter.add_tests('testEmitter', '.events') 
     109TestEmitterEvents.add_tests('testEmitterEvents', '.events') 
    73110 
Note: See TracChangeset for help on using the changeset viewer.