OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 '''Unit tests for the 'grit transl2tc' tool.''' |
| 7 |
| 8 |
| 9 import os |
| 10 import sys |
| 11 if __name__ == '__main__': |
| 12 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
| 13 |
| 14 import StringIO |
| 15 import unittest |
| 16 |
| 17 from grit.tool import transl2tc |
| 18 from grit import grd_reader |
| 19 from grit import util |
| 20 |
| 21 |
| 22 def MakeOptions(): |
| 23 from grit import grit_runner |
| 24 return grit_runner.Options() |
| 25 |
| 26 |
| 27 class TranslationToTcUnittest(unittest.TestCase): |
| 28 |
| 29 def testOutput(self): |
| 30 buf = StringIO.StringIO() |
| 31 tool = transl2tc.TranslationToTc() |
| 32 translations = [ |
| 33 ['1', 'Hello USERNAME, how are you?'], |
| 34 ['12', 'Howdie doodie!'], |
| 35 ['123', 'Hello\n\nthere\n\nhow are you?'], |
| 36 ['1234', 'Hello is > goodbye but < howdie pardner'], |
| 37 ] |
| 38 tool.WriteTranslations(buf, translations) |
| 39 output = buf.getvalue() |
| 40 self.failUnless(output.strip() == ''' |
| 41 1 Hello USERNAME, how are you? |
| 42 12 Howdie doodie! |
| 43 123 Hello |
| 44 |
| 45 there |
| 46 |
| 47 how are you? |
| 48 1234 Hello is > goodbye but < howdie pardner |
| 49 '''.strip()) |
| 50 |
| 51 def testExtractTranslations(self): |
| 52 path = util.PathFromRoot('grit/testdata') |
| 53 current_grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" enco
ding="UTF-8"?> |
| 54 <grit latest_public_release="2" source_lang_id="en-US" current_release="3"
base_dir="."> |
| 55 <release seq="3"> |
| 56 <messages> |
| 57 <message name="IDS_SIMPLE"> |
| 58 One |
| 59 </message> |
| 60 <message name="IDS_PLACEHOLDER"> |
| 61 <ph name="NUMBIRDS">%s<ex>3</ex></ph> birds |
| 62 </message> |
| 63 <message name="IDS_PLACEHOLDERS"> |
| 64 <ph name="ITEM">%d<ex>1</ex></ph> of <ph name="COUNT">%d<ex>3</ex>
</ph> |
| 65 </message> |
| 66 <message name="IDS_REORDERED_PLACEHOLDERS"> |
| 67 <ph name="ITEM">$1<ex>1</ex></ph> of <ph name="COUNT">$2<ex>3</ex>
</ph> |
| 68 </message> |
| 69 <message name="IDS_CHANGED"> |
| 70 This is the new version |
| 71 </message> |
| 72 <message name="IDS_TWIN_1">Hello</message> |
| 73 <message name="IDS_TWIN_2">Hello</message> |
| 74 <message name="IDS_NOT_TRANSLATEABLE" translateable="false">:</messa
ge> |
| 75 <message name="IDS_LONGER_TRANSLATED"> |
| 76 Removed document <ph name="FILENAME">$1<ex>c:\temp</ex></ph> |
| 77 </message> |
| 78 <message name="IDS_DIFFERENT_TWIN_1">Howdie</message> |
| 79 <message name="IDS_DIFFERENT_TWIN_2">Howdie</message> |
| 80 </messages> |
| 81 <structures> |
| 82 <structure type="dialog" name="IDD_ABOUTBOX" encoding="utf-16" file=
"klonk.rc" /> |
| 83 <structure type="menu" name="IDC_KLONKMENU" encoding="utf-16" file="
klonk.rc" /> |
| 84 </structures> |
| 85 </release> |
| 86 </grit>'''), path) |
| 87 current_grd.RunGatherers(recursive=True) |
| 88 |
| 89 source_rc_path = util.PathFromRoot('grit/testdata/source.rc') |
| 90 source_rc = file(source_rc_path).read() |
| 91 transl_rc_path = util.PathFromRoot('grit/testdata/transl.rc') |
| 92 transl_rc = file(transl_rc_path).read() |
| 93 |
| 94 tool = transl2tc.TranslationToTc() |
| 95 output_buf = StringIO.StringIO() |
| 96 globopts = MakeOptions() |
| 97 globopts.verbose = True |
| 98 globopts.output_stream = output_buf |
| 99 tool.Setup(globopts, []) |
| 100 translations = tool.ExtractTranslations(current_grd, |
| 101 source_rc, source_rc_path, |
| 102 transl_rc, transl_rc_path) |
| 103 |
| 104 values = translations.values() |
| 105 output = output_buf.getvalue() |
| 106 |
| 107 self.failUnless('Ein' in values) |
| 108 self.failUnless('NUMBIRDS Vogeln' in values) |
| 109 self.failUnless('ITEM von COUNT' in values) |
| 110 self.failUnless(values.count('Hallo') == 1) |
| 111 self.failIf('Dass war die alte Version' in values) |
| 112 self.failIf(':' in values) |
| 113 self.failIf('Dokument FILENAME ist entfernt worden' in values) |
| 114 self.failIf('Nicht verwendet' in values) |
| 115 self.failUnless(('Howdie' in values or 'Hallo sagt man' in values) and not |
| 116 ('Howdie' in values and 'Hallo sagt man' in values)) |
| 117 |
| 118 self.failUnless('XX01XX&SkraXX02XX&HaettaXX03XXThetta er "Klonk" sem eg fyla
XX04XXgonkurinnXX05XXKlonk && er "gott"XX06XX&HjalpXX07XX&Um...XX08XX' in values
) |
| 119 |
| 120 self.failUnless('I lagi' in values) |
| 121 |
| 122 self.failUnless(output.count('Structure of message IDS_REORDERED_PLACEHOLDER
S has changed')) |
| 123 self.failUnless(output.count('Message IDS_CHANGED has changed')) |
| 124 self.failUnless(output.count('Structure of message IDS_LONGER_TRANSLATED has
changed')) |
| 125 self.failUnless(output.count('Two different translations for "Howdie"')) |
| 126 self.failUnless(output.count('IDD_DIFFERENT_LENGTH_IN_TRANSL has wrong # of
cliques')) |
| 127 |
| 128 |
| 129 if __name__ == '__main__': |
| 130 unittest.main() |
OLD | NEW |