2 # -*- coding: utf-8 -*-
4 from translate
.storage
import po
5 from translate
.storage
import xliff
6 from translate
.tools
import pogrep
7 from translate
.misc
import wStringIO
10 def poparse(self
, posource
):
11 """helper that parses po source without requiring files"""
12 dummyfile
= wStringIO
.StringIO(posource
)
13 pofile
= po
.pofile(dummyfile
)
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:
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
))
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"])
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"])
42 def test_simplegrep_locations(self
):
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"])
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"])
58 def test_unicode_message_searchstring(self
):
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'
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
72 def test_unicode_message_regex_searchstring(self
):
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'
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
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'
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')
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">
113 xliff_text
= xliff_skeleton
% '''<trans-unit>
115 <target>rooi</target>
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
)
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:
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
))
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
, "red"))
138 assert xliff_result
.units
[0].getsource() == u
"red"
139 assert xliff_result
.units
[0].gettarget() == u
"rooi"
141 xliff_result
= self
.xliff_parse(self
.xliff_grep(xliff_text
, "unavailable string"))
142 assert xliff_result
.isempty()