diff --git a/run_test.py b/run_test.py index 5979040..ffd7ffe 100644 --- a/run_test.py +++ b/run_test.py @@ -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]) diff --git a/src/jmc/utils/lang.py b/src/jmc/utils/lang.py index e422cb7..6afdb8b 100644 --- a/src/jmc/utils/lang.py +++ b/src/jmc/utils/lang.py @@ -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): - return getattr(Lang, 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)) diff --git a/tests/test_lang.py b/tests/test_lang.py new file mode 100644 index 0000000..1c35a4a --- /dev/null +++ b/tests/test_lang.py @@ -0,0 +1,65 @@ +# -*- coding: UTF-8 -*- +## +## test_lang.py +## Login : David Rousselie +## 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) +