Unknown lang bug corrected

- switch to default lang when translation does not exist for lang specified in xml node
- put lang code truncation in get_lang_class instead of get_lang_from_node

darcs-hash:20061028145049-86b55-a4a40436fab41e7ae745c2640cadfd13fb22649d.gz
This commit is contained in:
David Rousselie
2006-10-28 16:50:49 +02:00
parent 85fccbd9ee
commit df971197ee
3 changed files with 78 additions and 4 deletions

View File

@@ -36,6 +36,7 @@ from tests.test_mailconnection import *
from tests.test_mailconnection_factory import *
from tests.test_component import *
from tests.test_storage import *
from tests.test_lang import *
from test import test_support
import logging
import jmc
@@ -63,6 +64,8 @@ if __name__ == '__main__':
"test")
sqlitestorage_suite = unittest.makeSuite(SQLiteStorage_TestCase, \
"test")
lang_suite = unittest.makeSuite(Lang_TestCase, \
"test")
jmc_suite = unittest.TestSuite((mail_connection_suite, \
pop3_connection_suite, \
@@ -72,7 +75,8 @@ if __name__ == '__main__':
# component2_suite, \
storage_suite, \
dbmstorage_suite, \
sqlitestorage_suite))
sqlitestorage_suite, \
lang_suite))
#test_support.run_suite(mail_connection_suite)
#test_support.run_suite(pop3_connection_suite)
#test_support.run_suite(imap_connection_suite)
@@ -89,5 +93,6 @@ coverage.analysis(jmc.email.mailconnection_factory)
coverage.analysis(jmc.email.mailconnection)
coverage.analysis(jmc.jabber.component)
coverage.analysis(jmc.jabber.x)
coverage.analysis(jmc.utils.lang)
coverage.report([jmc.email.mailconnection_factory, jmc.email.mailconnection, \
jmc.jabber.component, jmc.jabber.x])
jmc.jabber.component, jmc.jabber.x, jmc.utils.lang])

View File

@@ -30,10 +30,14 @@ class Lang:
if lang is None:
print "Using default lang " + self.default_lang
lang = self.default_lang
return lang[:2]
return lang
def get_lang_class(self, lang):
if lang is not None:
lang = lang[:2]
if hasattr(Lang, lang):
return getattr(Lang, lang)
return getattr(Lang, self.default_lang)
def get_lang_class_from_node(self, node):
return self.get_lang_class(self.get_lang_from_node(node))

65
tests/test_lang.py Normal file
View File

@@ -0,0 +1,65 @@
# -*- coding: UTF-8 -*-
##
## test_lang.py
## Login : David Rousselie <david.rousselie@happycoders.org>
## Started on Fri May 20 10:46:58 2005
## $Id: test_lang.py,v 1.1 2005/07/11 20:39:31 dax Exp $
##
## Copyright (C) 2005
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import unittest
from jmc.utils.lang import Lang
from pyxmpp.iq import Iq
class Lang_TestCase(unittest.TestCase):
def setUp(self):
self.lang = Lang()
def tearDown(self):
self.lang = None
def test_get_lang_class_exist(self):
lang_class = self.lang.get_lang_class("fr")
self.assertEquals(lang_class, Lang.fr)
def test_get_lang_class_not_exist(self):
lang_class = self.lang.get_lang_class("not_exist")
self.assertEquals(lang_class, Lang.en)
def test_get_lang_class_long_code(self):
lang_class = self.lang.get_lang_class("fr_FR")
self.assertEquals(lang_class, Lang.fr)
def test_get_lang_from_node(self):
iq = Iq(from_jid = "test@test.com", \
to_jid = "test2@test.com", \
stanza_type = "get")
iq_node = iq.get_node()
iq_node.setLang("fr")
lang = self.lang.get_lang_from_node(iq_node)
self.assertEquals(lang, "fr")
def test_get_lang_class_from_node(self):
iq = Iq(from_jid = "test@test.com", \
to_jid = "test2@test.com", \
stanza_type = "get")
iq_node = iq.get_node()
iq_node.setLang("fr")
lang = self.lang.get_lang_class_from_node(iq_node)
self.assertEquals(lang, Lang.fr)