Package translate :: Package tools :: Module test_pogrep
[hide private]
[frames] | no frames]

Source Code for Module translate.tools.test_pogrep

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  from translate.storage import po 
  5  from translate.storage import xliff 
  6  from translate.tools import pogrep 
  7  from translate.misc import wStringIO 
  8   
9 -class TestPOGrep:
10 - def poparse(self, posource):
11 """helper that parses po source without requiring files""" 12 dummyfile = wStringIO.StringIO(posource) 13 pofile = po.pofile(dummyfile) 14 return pofile
15
16 - def pogrep(self, posource, searchstring, cmdlineoptions=None):
17 """helper that parses po source and passes it through a filter""" 18 if cmdlineoptions is None: 19 cmdlineoptions = [] 20 options, args = pogrep.cmdlineparser().parse_args(["xxx.po"] + cmdlineoptions) 21 grepfilter = pogrep.GrepFilter(searchstring, options.searchparts, options.ignorecase, options.useregexp, options.invertmatch, options.accelchar) 22 tofile = grepfilter.filterfile(self.poparse(posource)) 23 print str(tofile) 24 return str(tofile)
25
26 - def test_simplegrep_msgid(self):
27 """grep for a string in the source""" 28 posource = '#: test.c\nmsgid "test"\nmsgstr "rest"\n' 29 poresult = self.pogrep(posource, "test", ["--search=msgid"]) 30 assert poresult == posource 31 poresult = self.pogrep(posource, "rest", ["--search=msgid"]) 32 assert poresult == ""
33
34 - def test_simplegrep_msgstr(self):
35 """grep for a string in the target""" 36 posource = '#: test.c\nmsgid "test"\nmsgstr "rest"\n' 37 poresult = self.pogrep(posource, "rest", ["--search=msgstr"]) 38 assert poresult == posource 39 poresult = self.pogrep(posource, "test", ["--search=msgstr"]) 40 assert poresult == ""
41
43 """grep for a string in the location comments""" 44 posource = '#: test.c\nmsgid "test"\nmsgstr "rest"\n' 45 poresult = self.pogrep(posource, "test.c", ["--search=locations"]) 46 assert poresult == posource 47 poresult = self.pogrep(posource, "rest.c", ["--search=locations"]) 48 assert poresult == ""
49
50 - def test_simplegrep_comments(self):
51 """grep for a string in the comments""" 52 posource = '# (review) comment\n#: test.c\nmsgid "test"\nmsgstr "rest"\n' 53 poresult = self.pogrep(posource, "review", ["--search=comment"]) 54 assert poresult == posource 55 poresult = self.pogrep(posource, "test", ["--search=comment"]) 56 assert poresult == ""
57
59 """check that we can grep unicode messages and use unicode search strings""" 60 poascii = '# comment\n#: test.c\nmsgid "test"\nmsgstr "rest"\n' 61 pounicode = '# comment\n#: test.c\nmsgid "test"\nmsgstr "rešṱ"\n' 62 queryascii = 'rest' 63 queryunicode = 'rešṱ' 64 for source, search, expected in [(poascii, queryascii, poascii), 65 (poascii, queryunicode, ''), 66 (pounicode, queryascii, ''), 67 (pounicode, queryunicode, pounicode)]: 68 print "Source:\n%s\nSearch: %s\n" % (source, search) 69 poresult = self.pogrep(source, search) 70 assert poresult == expected
71
73 """check that we can grep unicode messages and use unicode regex search strings""" 74 poascii = '# comment\n#: test.c\nmsgid "test"\nmsgstr "rest"\n' 75 pounicode = '# comment\n#: test.c\nmsgid "test"\nmsgstr "rešṱ"\n' 76 queryascii = 'rest' 77 queryunicode = 'rešṱ' 78 for source, search, expected in [(poascii, queryascii, poascii), 79 (poascii, queryunicode, ''), 80 (pounicode, queryascii, ''), 81 (pounicode, queryunicode, pounicode)]: 82 print "Source:\n%s\nSearch: %s\n" % (source, search) 83 poresult = self.pogrep(source, search, ["--regexp"]) 84 assert poresult == expected
85
86 - def test_unicode_normalise(self):
87 """check that we normlise unicode strings before comparing""" 88 source_template = u'# comment\n#: test.c\nmsgid "test"\nmsgstr "t%sst"\n' 89 # é, e + ' 90 # Ḽ, L + ^ 91 # Ṏ 92 groups = [(u"\u00e9", u"\u0065\u0301"), \ 93 (u"\u1e3c", u"\u004c\u032d"), \ 94 (u"\u1e4e", u"\u004f\u0303\u0308", u"\u00d5\u0308")] 95 for letters in groups: 96 for source_letter in letters: 97 source = source_template % source_letter 98 for search_letter in letters: 99 print search_letter.encode('utf-8') 100 poresult = self.pogrep(source, search_letter) 101 assert poresult == source.encode('utf-8')
102
103 -class TestXLiffGrep:
104 xliff_skeleton = '''<?xml version="1.0" ?> 105 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 106 <file original="filename.po" source-language="en-US" datatype="po"> 107 <body> 108 %s 109 </body> 110 </file> 111 </xliff>''' 112 113 xliff_text = xliff_skeleton % '''<trans-unit> 114 <source>rêd</source> 115 <target>rooi</target> 116 </trans-unit>''' 117
118 - def xliff_parse(self, xliff_text):
119 """helper that parses po source without requiring files""" 120 dummyfile = wStringIO.StringIO(xliff_text) 121 xliff_file = xliff.xlifffile(dummyfile) 122 return xliff_file
123
124 - def xliff_grep(self, xliff_text, searchstring, cmdlineoptions=None):
125 """helper that parses xliff text and passes it through a filter""" 126 if cmdlineoptions is None: 127 cmdlineoptions = [] 128 options, args = pogrep.cmdlineparser().parse_args(["xxx.xliff"] + cmdlineoptions) 129 grepfilter = pogrep.GrepFilter(searchstring, options.searchparts, options.ignorecase, options.useregexp, options.invertmatch, options.accelchar) 130 tofile = grepfilter.filterfile(self.xliff_parse(xliff_text)) 131 return str(tofile)
132
133 - def test_simplegrep(self):
134 """grep for a simple string.""" 135 xliff_text = self.xliff_text 136 xliff_file = self.xliff_parse(xliff_text) 137 xliff_result = self.xliff_parse(self.xliff_grep(xliff_text, "rêd")) 138 assert xliff_result.units[0].getsource() == u"rêd" 139 assert xliff_result.units[0].gettarget() == u"rooi" 140 141 xliff_result = self.xliff_parse(self.xliff_grep(xliff_text, "unavailable string")) 142 assert xliff_result.isempty()
143