~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to lib/test/gtcachetest.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import os.path
 
3
import unittest
 
4
import gettext
 
5
import logging
 
6
 
 
7
from miro import gtcache
 
8
from miro.test.framework import MiroTestCase
 
9
from miro.plat import resources
 
10
 
 
11
# FIXME this only works on GTK platforms. See #3831
 
12
 
 
13
# The miro.po/miro.mo file for this are in
 
14
# resources/testdata/locale/fr/LC_MESSAGES/ .
 
15
#
 
16
# If you need to add messages:
 
17
# 1. add new strings to teststring.py
 
18
# 2. run:
 
19
#
 
20
#    xgettext -k_ -kN_ -o messages.pot teststring.py
 
21
#
 
22
# 3. merge the differences between messages.pot and miro.po file
 
23
# 4. translate the new strings in miro.po
 
24
# 5. run:
 
25
#
 
26
#    msgfmt miro.po -o miro.mo
 
27
 
 
28
def make_french(f):
 
29
    def _make_french(*args, **kwargs):
 
30
        old_lang = None
 
31
        try:
 
32
            try:
 
33
                old_lang = os.environ["LANGUAGE"]
 
34
            except StandardError:
 
35
                pass
 
36
            os.environ["LANGUAGE"] = "fr"
 
37
            gtcache._gtcache = {}
 
38
 
 
39
            gettext.bindtextdomain("miro", resources.path("testdata/locale"))
 
40
            gettext.textdomain("miro")
 
41
            gettext.bind_textdomain_codeset("miro", "UTF-8")
 
42
 
 
43
            f(*args, **kwargs)
 
44
 
 
45
        finally:
 
46
            if old_lang is None:
 
47
                del os.environ["LANGUAGE"]
 
48
            else:
 
49
                os.environ["LANGUAGE"] = old_lang
 
50
    return _make_french
 
51
 
 
52
INPUT = "parsed %(countfiles)d files - found %(countvideos)d videos"
 
53
OUTPUT = u'%(countfiles)d fichiers analys\xe9s  - %(countvideos)d vid\xe9os trouv\xe9es'
 
54
 
 
55
class GettextTest(MiroTestCase):
 
56
    # FIXME - we probably want to test that something is logged instead of
 
57
    # ignoring the logging.
 
58
    def setUp(self):
 
59
        MiroTestCase.setUp(self)
 
60
        self.oldlevel = logging.getLogger().level
 
61
        logging.getLogger().setLevel(logging.ERROR)
 
62
 
 
63
    def tearDown(self):
 
64
        MiroTestCase.tearDown(self)
 
65
        logging.getLogger().setLevel(self.oldlevel)
 
66
 
 
67
    @make_french
 
68
    def test_gettext(self):
 
69
        self.assertEqual(gtcache.gettext("OK"), u'Valider')
 
70
        self.assertEqual(gtcache.gettext("Channels"), u'Cha\xeenes')
 
71
 
 
72
    @make_french
 
73
    def test_gettext_values(self):
 
74
        # test with no value expansion
 
75
        self.assertEqual(gtcache.gettext(INPUT), OUTPUT)
 
76
 
 
77
        # test with old value expansion
 
78
        self.assertEqual(gtcache.gettext(INPUT) % {"countfiles": 1, "countvideos": 2},
 
79
                         OUTPUT % {"countfiles": 1, "countvideos": 2})
 
80
 
 
81
        # test with value expansion done by gtcache.gettext
 
82
        self.assertEqual(gtcache.gettext(INPUT, 
 
83
                                         {"countfiles": 1, "countvideos": 2}),
 
84
                         OUTPUT % {"countfiles": 1, "countvideos": 2})
 
85
 
 
86
    @make_french
 
87
    def test_gettext_values_failures(self):
 
88
        # try gettext with a bad translation.  the string is fine, but
 
89
        # the translated version of the string is missing the d
 
90
        # characters which causes a Python formatting syntax error.
 
91
        input2 = "bad parsed %(countfiles)d files - found %(countvideos)d videos"
 
92
 
 
93
        # first we call gettext on the string by itself--this is fine,
 
94
        # so we should get the translated version of the string.
 
95
        self.assertEqual(gtcache.gettext(input2),
 
96
                         u'bad %(countfiles) fichiers analys\xe9s  - %(countvideos) vid\xe9os trouv\xe9es')
 
97
 
 
98
        # now we pass in a values dict which will kick up a ValueError
 
99
        # when trying to expand the values.  that causes gettext to
 
100
        # return the english form of the string with values expanded.
 
101
        self.assertEqual(gtcache.gettext(input2, 
 
102
                                         {"countfiles": 1, "countvideos": 2}),
 
103
                         input2 % {"countfiles": 1, "countvideos": 2})
 
104
 
 
105
    @make_french
 
106
    def test_ngettext(self):
 
107
        # french uses singular for 0, 1 and plural for everything else.
 
108
        self.assertEqual(gtcache.ngettext("%(count)d video found",
 
109
                                          "%(count)d videos found", 0),
 
110
                         u'%(count)d vid\xe9o trouv\xe9e')
 
111
 
 
112
        self.assertEqual(gtcache.ngettext("%(count)d video found",
 
113
                                          "%(count)d videos found", 1),
 
114
                         u'%(count)d vid\xe9o trouv\xe9e')
 
115
 
 
116
        self.assertEqual(gtcache.ngettext("%(count)d video found",
 
117
                                          "%(count)d videos found", 2),
 
118
                         u'%(count)d vid\xe9os trouv\xe9es')
 
119
 
 
120
    @make_french
 
121
    def test_ngettext_values(self):
 
122
        # try the bad translation with no values
 
123
        self.assertEqual(gtcache.ngettext("bad %(count)d video found",
 
124
                                          "bad %(count)d videos found", 0),
 
125
                         u'bad %(count) vid\xe9o trouv\xe9e')
 
126
 
 
127
        # try the bad translation with values
 
128
        self.assertEqual(gtcache.ngettext("bad %(count)d video found",
 
129
                                          "bad %(count)d videos found", 0,
 
130
                                          {"count": 0}),
 
131
                         u'bad 0 videos found')
 
132
 
 
133
        self.assertEqual(gtcache.ngettext("bad %(count)d video found",
 
134
                                          "bad %(count)d videos found", 1,
 
135
                                          {"count": 1}),
 
136
                         u'bad 1 video found')
 
137
 
 
138
        self.assertEqual(gtcache.ngettext("bad %(count)d video found",
 
139
                                          "bad %(count)d videos found", 2,
 
140
                                          {"count": 2}),
 
141
                         u'bad 2 videos found')