1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# -*- coding: utf-8 -*-
# ====================================================================
# Copyright (c) 2005-2010 Open Source Applications Foundation.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ====================================================================
#
import sys, os
from unittest import TestCase, main
from icu import *
class TestMessageFormat(TestCase):
def testFormatMessage(self):
x = 'x'
f = Formattable(UnicodeString(x))
text = MessageFormat.formatMessage("This is a string: {0}.", [f])
self.assertTrue(text == "This is a string: x.")
def testFormat(self):
x = 'x'
f = Formattable(UnicodeString(x))
msgFormat = MessageFormat("This is a string: {0}")
text = msgFormat.format([f], FieldPosition())
self.assertTrue(text == "This is a string: x")
if (sys.version_info < (3,) and 'PyPy' not in sys.version):
# Reduced features of % operator might be intentional
f = UnicodeString(x)
text = msgFormat % f
self.assertTrue(text == "This is a string: x")
def testFormatAppend(self):
x = 'x'
f = Formattable(UnicodeString(x))
msgFormat = MessageFormat("This is a string: {0}")
text = msgFormat.format([f], UnicodeString("x"), FieldPosition())
self.assertTrue(text == "xThis is a string: x")
def testFormats(self):
orig = TimeZone.createDefault().getID()
tzid = 'America/Indiana/Vevay'
msgFormat = MessageFormat("{0,time,short} {0,time,z}")
formats = msgFormat.getFormats()
formats[0].setTimeZone(TimeZone.createTimeZone(tzid))
self.assertTrue(msgFormat.getFormats()[0].getTimeZone().getID() == orig)
msgFormat.setFormats(formats)
self.assertTrue(msgFormat.getFormats()[0].getTimeZone().getID() == tzid)
def testSelectFormat(self):
try:
from icu import SelectFormat
except ImportError:
return
format = u"{0} est {1, select, female {allée} other {allé}} à Paris."
msgFormat = MessageFormat(format, Locale("fr"))
args = [Formattable("Kirti"), Formattable("female")]
self.assertTrue(msgFormat.format(args) == u"Kirti est allée à Paris.")
def testNamedArgs(self):
if ICU_VERSION >= '4.0':
msg = \
'{num_emails_to_send, plural, ' \
'=0 {unused plural form} ' \
'=1 {One email will be sent.} ' \
'other {# emails will be sent.}}'
messageFormat = MessageFormat(msg, Locale("en"))
name0 = "num_emails_to_send"
arg0 = Formattable(5)
result = messageFormat.format([name0], [arg0])
self.assertTrue(result == u'5 emails will be sent.')
if __name__ == "__main__":
main()
|