Esempio n. 1
0
def _test_round_trip_source(self,
                            source,
                            parser,
                            leave_file=False,
                            *args,
                            **kwds):
    src_name = getattr(source, 'name', None)
    src_encoding = getattr(source, 'encoding', None)
    source = list(source)
    rt_src = sfm.pprint(parser(source, *args, **kwds)).splitlines(True)

    # Try for perfect match first
    if source == rt_src:
        self.assert_(True)
        return

    # Normalise line endings
    source = map(operator.methodcaller('rstrip'), source)
    rt_src = map(operator.methodcaller('rstrip'), rt_src)
    if source == rt_src:
        self.assert_(True)
        return

    # Normalise the \f ..\f* marker forms in the source
    source = map(operator.methodcaller('replace', u'\\ft ', u'\\fr*'), source)
    rt_src = map(operator.methodcaller('replace', u'\\ft ', u'\\fr*'), rt_src)

    if leave_file and src_name:
        codecs.open(src_name + '.normalised', 'w',
                    encoding=src_encoding).writelines(l + '\n' for l in source)
        codecs.open(src_name + '.roundtrip', 'w',
                    encoding=src_encoding).writelines(l + '\n' for l in rt_src)

    self.assertEqual(source, rt_src, 'roundtriped source not equal')
Esempio n. 2
0
def _test_round_trip_source(self, source, parser, leave_file=False, *args, **kwds):
    src_name = getattr(source,'name',None)
    src_encoding = getattr(source, 'encoding', None)
    source = list(source)
#    import pdb; pdb.set_trace()
    rt_src = sfm.pprint(parser(source, *args, **kwds)).splitlines(True)
    
    # Try for perfect match first
    if source == rt_src:
        self.assert_(True)
        return
    
    # Normalise line endings
    source = map(operator.methodcaller('rstrip'), source)
    rt_src = map(operator.methodcaller('rstrip'), rt_src)
    if source == rt_src:
        self.assert_(True)
        return
    
    # Normalise the \f ..\f* marker forms in the source
    source = map(operator.methodcaller('replace', u'\\ft ',u'\\fr*'), source)
    rt_src = map(operator.methodcaller('replace', u'\\ft ',u'\\fr*'), rt_src)
    
    if leave_file and src_name:
        codecs.open(src_name+'.normalised','w', 
                    encoding=src_encoding).writelines(l+'\n' for l in source)
        codecs.open(src_name+'.roundtrip','w', 
                    encoding=src_encoding).writelines(l+'\n' for l in rt_src)
    
    self.assertEqual(source, rt_src, 'roundtriped source not equal')
Esempio n. 3
0
def _test_round_trip_parse(self, source, parser, *args, **kwds):
    #    src_name = getattr(source,'name',None)
    #    src_encoding = getattr(source, 'encoding', None)
    source = list(source)
    doc = list(parser(source, *args, **kwds))
    rt_doc = list(parser(sfm.pprint(doc).splitlines(True), *args, **kwds))
    # Check for equivilent parse.
    self.assertEqual(doc, rt_doc, 'roundtrip parse unequal')
Esempio n. 4
0
def _test_round_trip_parse(self, source, parser, *args, **kwds):
#    src_name = getattr(source,'name',None)
#    src_encoding = getattr(source, 'encoding', None)
    source = list(source)
    doc = list(parser(source, *args, **kwds))
    rt_doc = list(parser(sfm.pprint(doc).splitlines(True),*args,**kwds))
    # Check for equivilent parse.
    self.assertEqual(doc, rt_doc, 'roundtrip parse unequal')
Esempio n. 5
0
    def test_pprint(self):
        src = [
            '\\test\n',
            '\\test text\n',
            '\\sfm text\n',
            'bare text\n',
            '\\more-sfm more text\n',
            'over a line break\\marker'
            '\\le unix\n',
            '\\le windows\r\n',
            '\\le missing\n',
            '\\test\\i1\\i2 deep text\\i1*\n',
            '\\test\\i1\\i2 deep text\n',
            # These forms do not transduce identically due to whitespace differences
            '\\test \\inline text\\inline*\n',
            '\\test \\i1\\i2 deep\\i2*\\i1*\n'
        ]

        with warnings.catch_warnings(record=True) as ref_parse_errors:
            warnings.resetwarnings()
            warnings.simplefilter("always", SyntaxWarning)
            ref_parse = list(sfm.parser(src))
        trans_src = sfm.pprint(ref_parse).splitlines(True)

        with warnings.catch_warnings(record=True) as trans_parse_errors:
            warnings.resetwarnings()
            warnings.simplefilter("always", SyntaxWarning)
            trans_parse = list(sfm.parser(trans_src))

        # Check pretty printer output matches input, skip the last 2
        map(self.assertEqual, src[:10], trans_src[:10])
        # Check the parsed pretty printed doc matches the reference
        self.assertEqual(ref_parse, trans_parse)
        # Check the errors match
        map(self.assertEqual, map(str, ref_parse_errors[:10]),
            map(str, trans_parse_errors[:10]))
        # Check the positions line up for the first 10 items
        map(self.assertEqual, (e.pos for e in flatten(ref_parse[:16])),
            (e.pos for e in flatten(trans_parse[:16])))
        # Check all the line positions, meta data and annotations line up
        map(self.assertEqual, ((e.pos.line, getattr(
            e, 'meta', None), getattr(e, 'annotations', None))
                               for e in flatten(ref_parse)),
            ((e.pos.line, getattr(e, 'meta',
                                  None), getattr(e, 'annotations', None))
             for e in flatten(trans_parse)))
Esempio n. 6
0
 def test_pprint(self):
     src = ['\\test\n',
            '\\test text\n',
            '\\sfm text\n',
            'bare text\n',
            '\\more-sfm more text\n',
            'over a line break\\marker'
            '\\le unix\n',
            '\\le windows\r\n',
            '\\le missing\n',
            '\\test\\i1\\i2 deep text\\i1*\n',
            '\\test\\i1\\i2 deep text\n',
            # These forms do not transduce identically due to whitespace differences
            '\\test \\inline text\\inline*\n',
            '\\test \\i1\\i2 deep\\i2*\\i1*\n']
     
     with warnings.catch_warnings(record=True) as ref_parse_errors:
         warnings.resetwarnings()
         warnings.simplefilter("always", SyntaxWarning)
         ref_parse = list(sfm.parser(src))
     trans_src = sfm.pprint(ref_parse).splitlines(True)
     
     with warnings.catch_warnings(record=True) as trans_parse_errors:
         warnings.resetwarnings()
         warnings.simplefilter("always", SyntaxWarning)
         trans_parse = list(sfm.parser(trans_src))
     
     # Check pretty printer output matches input, skip the last 2
     map(self.assertEqual, src[:10], trans_src[:10])
     # Check the parsed pretty printed doc matches the reference
     self.assertEqual(ref_parse, trans_parse)
     # Check the errors match 
     map(self.assertEqual, 
         map(str,ref_parse_errors[:10]), 
         map(str,trans_parse_errors[:10]))
     # Check the positions line up for the first 10 items
     map(self.assertEqual, 
         (e.pos for e in flatten(ref_parse[:16])), 
         (e.pos for e in flatten(trans_parse[:16])))
     # Check all the line positions, meta data and annotations line up
     map(self.assertEqual, 
         ((e.pos.line, getattr(e,'meta',None), getattr(e,'annotations',None)) for e in flatten(ref_parse)), 
         ((e.pos.line, getattr(e,'meta',None), getattr(e,'annotations',None)) for e in flatten(trans_parse)))