def test_sytax_warning(self): with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("error", SyntaxWarning) self.assertRaises(SyntaxWarning,list,usfm.parser(['\\id TEST\\mt \\whoops'])) self.assertRaises(SyntaxError, list,usfm.parser(['\\id TEST\\mt \\whoops'], error_level=sfm.level.Marker)) self.assertRaises(SyntaxWarning,list,usfm.parser(['\\id TEST\\mt \\zwhoops'])) self.assertRaises(SyntaxWarning,list,usfm.parser(['\\id TEST\\mt \\zwhoops'], error_level=sfm.level.Marker))
def test_footnote_content(self): def ft(src, doc): return (r'\id TEST\mt ' + src, [elem('id', text('TEST'), elem('mt', doc))]) tests = [ ft(r'\f - bare text\f*', elem(('f', '-'), text('bare text'))), ft(r'\f - \ft bare text\ft*\f*', elem(('f', '-'), text('bare text'))), ft( r'\f + \fk Issac:\ft In Hebrew means "laughter"\f*', elem(('f', '+'), elem('fk', text('Issac:')), text('In Hebrew means "laughter"'))), ft( r'\f + \fk Issac:\fk*In Hebrew means "laughter"\f*', elem(('f', '+'), elem('fk', text('Issac:')), text('In Hebrew means "laughter"'))), ft( r'\f + \fr 1.14 \fq religious festivals;\ft or \fq seasons.\f*', elem(('f', '+'), elem('fr', text('1.14 ')), elem('fq', text('religious festivals;')), text('or '), elem('fq', text('seasons.')))), ft( r'\f + \fr 1.14 \fr*\fq religious festivals;\fq*or \fq seasons.\fq*\f*', elem(('f', '+'), elem('fr', text('1.14 ')), elem('fq', text('religious festivals;')), text('or '), elem('fq', text('seasons.')))) ] for r in [(list(usfm.parser([s], error_level=usfm.level.Note)), r) for s, r in tests]: self.assertEqual(*r)
def test_inline_markers(self): tests = [(r'\test', [elem('test')]), (r'\test text', [elem('test'), text(' text')]), (r'\id JHN\ior text\ior*', [elem('id',text('JHN'), elem('ior',text('text')))]), (r'\id MAT\mt Text \f + \fk deep\fk*\f*more text.',[elem('id', text('MAT'), elem('mt', text('Text '), elem(('f','+'), elem('fk',text('deep'))), text('more text.')))])] for r in [(list(usfm.parser([s], private=False)),r) for s,r in tests]: self.assertEqual(*r)
def test_parameters(self): tests = [(r'\id TEST' r'\c 1', [elem('id', text('TEST'), elem(('c','1')))]), (r'\id TEST' r'\c 2 \s text', [elem('id', text('TEST'), elem(('c','2'), elem('s','text')))]), (r'\id TEST\c 0\p' r'\v 1', [elem('id', text('TEST'), elem(('c','0'), elem('p', elem(('v','1')))))]), (r'\id TEST\c 0\p' r'\v 1-3', [elem('id', text('TEST'), elem(('c','0'), elem('p', elem(('v','1-3')))))]), (r'\id TEST\c 0\p' r'\v 2 text', [elem('id', text('TEST'), elem(('c','0'), elem('p', elem(('v','2')), text(' text'))))]), (r'\id TEST' r'\c 2 \p \v 3 text\v 4 verse', [elem('id', text('TEST'), elem(('c','2'), elem('p', elem(('v','3')), text(' text'), elem(('v','4')), text(' verse'))))]),] for r in [(list(usfm.parser([s], private=False)),r) for s,r in tests]: self.assertEqual(*r)
def test_footnote_content(self): def ft(src,doc): return (r'\id TEST\mt '+src, [elem('id', text('TEST'), elem('mt', doc))]) tests = [ft(r'\f - bare text\f*', elem(('f','-'), text('bare text'))), ft(r'\f - \ft bare text\ft*\f*', elem(('f','-'), text('bare text'))), ft(r'\f + \fk Issac:\ft In Hebrew means "laughter"\f*', elem(('f','+'), elem('fk',text('Issac:')), text('In Hebrew means "laughter"'))), ft(r'\f + \fk Issac:\fk*In Hebrew means "laughter"\f*', elem(('f','+'), elem('fk',text('Issac:')), text('In Hebrew means "laughter"'))), ft(r'\f + \fr 1.14 \fq religious festivals;\ft or \fq seasons.\f*', elem(('f','+'), elem('fr',text('1.14 ')), elem('fq',text('religious festivals;')), text('or '), elem('fq',text('seasons.')))), ft(r'\f + \fr 1.14 \fr*\fq religious festivals;\fq*or \fq seasons.\fq*\f*', elem(('f','+'), elem('fr',text('1.14 ')), elem('fq',text('religious festivals;')), text('or '), elem('fq',text('seasons.'))))] for r in [(list(usfm.parser([s], private=False)),r) for s,r in tests]: self.assertEqual(*r)
def test_sytax_error(self): # Chapter number parsing self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\c\\p \\v 1 text'])) self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\c A\\p \\v 1 text'])) self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\c 1 text\\p \\v 1 text'])) # Verse number parsing self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\c 1\\p \\v \\p text'])) self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\c 1\\p \\v text'])) # Note text parsing self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\mt \\f \\fk key\\fk* text.\\f*']))
def test_reference(self): p = flatten( usfm.decorate_references( usfm.parser('\\id MAT EN\n\\c 1\n\\p\n\\v 1 \\v 2-3\n' '\\id JHN DE\n\\c 3\n\\p\n\\v 16'.splitlines()))) self.assertEqual( [(*e.pos, e.pos.book, e.pos.chapter, e.pos.verse, str(e)) for e in p], [(1, 1, 'MAT', None, None, '\\id'), (1, 5, 'MAT', None, None, 'MAT EN'), (2, 1, 'MAT', '1', None, '\\c 1'), (3, 1, 'MAT', '1', None, '\\p'), (4, 1, 'MAT', '1', '1', '\\v 1 '), (4, 6, 'MAT', '1', '2-3', '\\v 2-3 '), (5, 1, 'JHN', None, None, '\\id'), (5, 5, 'JHN', None, None, 'JHN DE'), (6, 1, 'JHN', '3', None, '\\c 3'), (7, 1, 'JHN', '3', None, '\\p'), (8, 1, 'JHN', '3', '16', '\\v 16 ')])
else: opts.tags = range(1, len(args)) else: opts.tags = [tag.strip() for tag in opts.tags.split(', ')] output = codecs.open(os.path.expanduser(args[0]), mode='w', encoding='utf_8_sig') paths = chain.from_iterable(map(glob.iglob, args[1:])) files = (codecs.open(p, mode='r', encoding='utf_8_sig') for p in paths) try: with warnings.catch_warnings(): warnings.simplefilter("always" if opts.warnings else "ignore", SyntaxWarning) docs = [ list( usfm.parser(stylesheet=opts.stylesheet, error_level=opts.error_level)) for f in files ] validate_structure(*docs) output.write(sfm.generate(merge(opts.tags, *docs))) except IOError as err: sys.stderr.write( parser.expand_prog_name(f'%prog: IO error: {err!s}\n')) sys.exit(2) except StructureError: sys.stderr.write( parser.expand_prog_name( '%prog: Structure error: A USFM file does not match the others\n' )) sys.exit(3)
def test_endmarker_is_prefix(self): self.assertEqual(list(usfm.parser(['\\id TEST\\mt \\f + text\\f*suffixed text'])), [elem('id', text('TEST'), elem('mt', elem(('f','+'), text('text')), text('suffixed text')))]) self.assertEqual(list(usfm.parser(['\\id TEST\\mt \\f + \\fr ref \\ft text\\f*suffixed text'])), [elem('id', text('TEST'), elem('mt', elem(('f','+'), elem('fr', text('ref ')), text('text')), text('suffixed text')))])
def test_sytax_error(self): self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\mt text\\f*'])) self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST \\p 1 text'])) self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\mt \\f + text\\fe*'])) self.assertRaises(SyntaxError,list,usfm.parser(['\\id TEST\\mt \\f + text']))