[Libreoffice-commits] .: 2 commits - i18npool/CppunitTest_i18npool_test_languagetag.mk i18npool/inc i18npool/Module_i18npool.mk i18npool/qa i18npool/source liblangtag/prj

Eike Rathke erack at kemper.freedesktop.org
Fri Aug 10 06:08:09 PDT 2012


 i18npool/CppunitTest_i18npool_test_languagetag.mk |   50 +++++
 i18npool/Module_i18npool.mk                       |    1 
 i18npool/inc/i18npool/languagetag.hxx             |    5 
 i18npool/qa/cppunit/test_languagetag.cxx          |  206 ++++++++++++++++++++++
 i18npool/source/languagetag/languagetag.cxx       |   90 ++-------
 liblangtag/prj/d.lst                              |   10 +
 6 files changed, 291 insertions(+), 71 deletions(-)

New commits:
commit 8ab887eea707f1a98e35909739be157a8ee45cb9
Author: Eike Rathke <erack at redhat.com>
Date:   Fri Aug 10 15:00:43 2012 +0200

    added LanguageTag unit test

diff --git a/i18npool/CppunitTest_i18npool_test_languagetag.mk b/i18npool/CppunitTest_i18npool_test_languagetag.mk
new file mode 100644
index 0000000..8c42ad6
--- /dev/null
+++ b/i18npool/CppunitTest_i18npool_test_languagetag.mk
@@ -0,0 +1,50 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+$(eval $(call gb_CppunitTest_CppunitTest,i18npool_test_languagetag))
+
+$(eval $(call gb_CppunitTest_use_api,i18npool_test_languagetag,\
+	udkapi \
+	offapi \
+))
+
+$(eval $(call gb_CppunitTest_use_library_objects,i18npool_test_languagetag,i18nisolang1))
+
+$(eval $(call gb_CppunitTest_use_libraries,i18npool_test_languagetag,\
+	cppu \
+	cppuhelper \
+	sal \
+	$(gb_STDLIBS) \
+))
+
+ifneq ($(OS),ANDROID)
+ifneq ($(OS),IOS)
+$(eval $(call gb_CppunitTest_use_externals,i18npool_test_languagetag,\
+	liblangtag \
+	glib \
+))
+endif
+endif
+
+ifeq ($(SYSTEM_LIBLANGTAG),YES)
+$(eval $(call gb_CppunitTest_add_cxxflags,i18npool_test_languagetag,\
+	-DSYSTEM_LIBLANGTAG \
+))
+endif
+
+$(eval $(call gb_CppunitTest_set_include,i18npool_test_languagetag,\
+	-I$(SRCDIR)/i18npool/inc \
+	$$(INCLUDE) \
+))
+
+$(eval $(call gb_CppunitTest_add_exception_objects,i18npool_test_languagetag,\
+	i18npool/qa/cppunit/test_languagetag \
+))
+
+# vim: set noet sw=4 ts=4:
diff --git a/i18npool/Module_i18npool.mk b/i18npool/Module_i18npool.mk
index bed175a..9214d00 100644
--- a/i18npool/Module_i18npool.mk
+++ b/i18npool/Module_i18npool.mk
@@ -63,6 +63,7 @@ endif
 $(eval $(call gb_Module_add_check_targets,i18npool,\
 	CppunitTest_i18npool_test_breakiterator \
 	CppunitTest_i18npool_test_characterclassification \
+	CppunitTest_i18npool_test_languagetag \
 ))
 
 # vim: set noet sw=4 ts=4:
diff --git a/i18npool/inc/i18npool/languagetag.hxx b/i18npool/inc/i18npool/languagetag.hxx
index 1dcc213..0066d8e 100644
--- a/i18npool/inc/i18npool/languagetag.hxx
+++ b/i18npool/inc/i18npool/languagetag.hxx
@@ -119,6 +119,11 @@ public:
     /** If this is a valid BCP 47 language tag. */
     bool                            isValidBcp47() const;
 
+    /** Needed for unit test in build environment, must be called before any
+        LanguageTag is instanciated.
+     */
+    static void                     overrideDataPath( const rtl::OUString& rPath );
+
 private:
 
     enum Decision
diff --git a/i18npool/qa/cppunit/test_languagetag.cxx b/i18npool/qa/cppunit/test_languagetag.cxx
new file mode 100644
index 0000000..fee5769
--- /dev/null
+++ b/i18npool/qa/cppunit/test_languagetag.cxx
@@ -0,0 +1,206 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/TestAssert.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+
+#include <i18npool/languagetag.hxx>
+
+#include <rtl/ustring.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <osl/file.hxx>
+
+#include <com/sun/star/lang/Locale.hpp>
+
+using namespace com::sun::star;
+
+// To test the replacement code add '&& 0' and also in
+// source/languagetag/languagetag.cxx
+#define USE_LIBLANGTAG (!defined(ANDROID) && !defined(IOS))
+
+namespace {
+
+class TestLanguageTag : public CppUnit::TestFixture
+{
+public:
+    TestLanguageTag() {}
+    virtual ~TestLanguageTag() {}
+
+    void testInit();
+    void testAllTags();
+
+    CPPUNIT_TEST_SUITE(TestLanguageTag);
+    CPPUNIT_TEST(testInit);
+    CPPUNIT_TEST(testAllTags);
+    CPPUNIT_TEST_SUITE_END();
+};
+
+void TestLanguageTag::testInit()
+{
+#if USE_LIBLANGTAG && !defined(SYSTEM_LIBLANGTAG)
+    // file:///$SRCDIR/solver/$INPATH/share/liblangtag
+    // file:///$OUTDIR/share/liblangtag
+    OUStringBuffer aBuf(128);
+    const char* pEnv = getenv("OUTDIR");
+    CPPUNIT_ASSERT_MESSAGE("No $OUTDIR", pEnv);
+    aBuf.append( "file:///").append( OStringToOUString( pEnv, RTL_TEXTENCODING_UTF8)).append( "/share/liblangtag");
+    OUString aPath( aBuf.makeStringAndClear());
+    OUString aData( aPath);
+    aData += "/language-subtag-registry.xml";
+    osl::DirectoryItem aDirItem;
+    CPPUNIT_ASSERT_MESSAGE("liblangtag data not found",
+            osl::DirectoryItem::get( aData, aDirItem) == osl::DirectoryItem::E_None);
+    LanguageTag::overrideDataPath( aPath);
+#endif
+}
+
+void TestLanguageTag::testAllTags()
+{
+    {
+        OUString s_de_Latn_DE( "de-Latn-DE" );
+        LanguageTag de_DE( s_de_Latn_DE, true );
+        OUString aBcp47 = de_DE.getBcp47();
+        lang::Locale aLocale = de_DE.getLocale();
+        LanguageType nLanguageType = de_DE.getLanguageType();
+#if USE_LIBLANGTAG
+        CPPUNIT_ASSERT_MESSAGE("Default script should be stripped after canonicalize.", aBcp47 == "de-DE" );
+        CPPUNIT_ASSERT( aLocale.Language == "de" );
+        CPPUNIT_ASSERT( aLocale.Country == "DE" );
+        CPPUNIT_ASSERT( aLocale.Variant == "" );
+        CPPUNIT_ASSERT( nLanguageType == LANGUAGE_GERMAN );
+#else
+        // The simple replacement code doesn't do any fancy stuff.
+        CPPUNIT_ASSERT_MESSAGE("Default script was stripped after canonicalize!?!", aBcp47 == s_de_Latn_DE );
+        CPPUNIT_ASSERT( aLocale.Language == "qlt" );
+        CPPUNIT_ASSERT( aLocale.Country == "DE" );
+        CPPUNIT_ASSERT( aLocale.Variant == "de-Latn-DE" );
+        (void)nLanguageType; //XXX CPPUNIT_ASSERT( nLanguageType == LANGUAGE_GERMAN );
+#endif
+    }
+
+    {
+        OUString s_klingon( "i-klingon" );
+        LanguageTag klingon( s_klingon, true );
+        lang::Locale aLocale = klingon.getLocale();
+#if USE_LIBLANGTAG
+        CPPUNIT_ASSERT( klingon.getBcp47() == "tlh" );
+        CPPUNIT_ASSERT( aLocale.Language == "tlh" );
+        CPPUNIT_ASSERT( aLocale.Country == "" );
+        CPPUNIT_ASSERT( aLocale.Variant == "" );
+        CPPUNIT_ASSERT( klingon.getLanguageType() == LANGUAGE_SYSTEM );
+        CPPUNIT_ASSERT( klingon.isValidBcp47() == true );
+        CPPUNIT_ASSERT( klingon.isIsoLocale() == true );
+        CPPUNIT_ASSERT( klingon.isIsoODF() == true );
+#else
+        CPPUNIT_ASSERT( klingon.getBcp47() == s_klingon );
+        CPPUNIT_ASSERT( aLocale.Language == "qlt" );
+        CPPUNIT_ASSERT( aLocale.Country == "" );
+        CPPUNIT_ASSERT( aLocale.Variant == s_klingon );
+        CPPUNIT_ASSERT( klingon.getLanguageType() == LANGUAGE_SYSTEM );
+        CPPUNIT_ASSERT( klingon.isValidBcp47() == true );
+        CPPUNIT_ASSERT( klingon.isIsoLocale() == false );
+        CPPUNIT_ASSERT( klingon.isIsoODF() == false );
+#endif
+    }
+
+    {
+        OUString s_sr_RS( "sr-RS" );
+        LanguageTag sr_RS( s_sr_RS, true );
+        lang::Locale aLocale = sr_RS.getLocale();
+        CPPUNIT_ASSERT( sr_RS.getBcp47() == s_sr_RS );
+        CPPUNIT_ASSERT( aLocale.Language == "sr" );
+        CPPUNIT_ASSERT( aLocale.Country == "RS" );
+        CPPUNIT_ASSERT( aLocale.Variant == "" );
+        CPPUNIT_ASSERT( sr_RS.getLanguageType() == LANGUAGE_USER_SERBIAN_CYRILLIC_SERBIA );
+        CPPUNIT_ASSERT( sr_RS.isValidBcp47() == true );
+        CPPUNIT_ASSERT( sr_RS.isIsoLocale() == true );
+        CPPUNIT_ASSERT( sr_RS.isIsoODF() == true );
+    }
+
+    {
+        OUString s_sr_Latn_RS( "sr-Latn-RS" );
+        LanguageTag sr_RS( s_sr_Latn_RS, true );
+        lang::Locale aLocale = sr_RS.getLocale();
+        CPPUNIT_ASSERT( sr_RS.getBcp47() == s_sr_Latn_RS );
+        CPPUNIT_ASSERT( aLocale.Language == "qlt" );
+        CPPUNIT_ASSERT( aLocale.Country == "RS" );
+        CPPUNIT_ASSERT( aLocale.Variant == s_sr_Latn_RS );
+        /* TODO: conversion doesn't know this yet, once it does activate test. */
+#if 0
+        CPPUNIT_ASSERT( sr_RS.getLanguageType() == LANGUAGE_USER_SERBIAN_LATIN_SERBIA );
+#else
+        CPPUNIT_ASSERT( sr_RS.getLanguageType() == LANGUAGE_SYSTEM );
+#endif
+        CPPUNIT_ASSERT( sr_RS.isValidBcp47() == true );
+        CPPUNIT_ASSERT( sr_RS.isIsoLocale() == false );
+        CPPUNIT_ASSERT( sr_RS.isIsoODF() == true );
+    }
+
+    {
+        OUString s_de_DE( "de-DE" );
+        LanguageTag de_DE( s_de_DE, true );
+        lang::Locale aLocale = de_DE.getLocale();
+        CPPUNIT_ASSERT( de_DE.getBcp47() == s_de_DE );
+        CPPUNIT_ASSERT( aLocale.Language == "de" );
+        CPPUNIT_ASSERT( aLocale.Country == "DE" );
+        CPPUNIT_ASSERT( aLocale.Variant == "" );
+        CPPUNIT_ASSERT( de_DE.getLanguageType() == LANGUAGE_GERMAN );
+        CPPUNIT_ASSERT( de_DE.isValidBcp47() == true );
+        CPPUNIT_ASSERT( de_DE.isIsoLocale() == true );
+        CPPUNIT_ASSERT( de_DE.isIsoODF() == true );
+    }
+
+    {
+        OUString s_de_DE( "de-DE" );
+        LanguageTag de_DE( lang::Locale( "de", "DE", "" ) );
+        lang::Locale aLocale = de_DE.getLocale();
+        CPPUNIT_ASSERT( de_DE.getBcp47() == s_de_DE );
+        CPPUNIT_ASSERT( aLocale.Language == "de" );
+        CPPUNIT_ASSERT( aLocale.Country == "DE" );
+        CPPUNIT_ASSERT( aLocale.Variant == "" );
+        CPPUNIT_ASSERT( de_DE.getLanguageType() == LANGUAGE_GERMAN );
+    }
+
+    {
+        OUString s_de_DE( "de-DE" );
+        LanguageTag de_DE( LANGUAGE_GERMAN );
+        lang::Locale aLocale = de_DE.getLocale();
+        CPPUNIT_ASSERT( de_DE.getBcp47() == s_de_DE );
+        CPPUNIT_ASSERT( aLocale.Language == "de" );
+        CPPUNIT_ASSERT( aLocale.Country == "DE" );
+        CPPUNIT_ASSERT( aLocale.Variant == "" );
+        CPPUNIT_ASSERT( de_DE.getLanguageType() == LANGUAGE_GERMAN );
+    }
+
+    {
+        OUString s_uab( "unreg-and-bad" );
+        LanguageTag uab( s_uab, true );
+        lang::Locale aLocale = uab.getLocale();
+        CPPUNIT_ASSERT( uab.getBcp47() == s_uab );
+        CPPUNIT_ASSERT( aLocale.Language == "qlt" );
+        CPPUNIT_ASSERT( aLocale.Country == "" );
+        CPPUNIT_ASSERT( aLocale.Variant == s_uab );
+        CPPUNIT_ASSERT( uab.getLanguageType() == LANGUAGE_SYSTEM );
+        CPPUNIT_ASSERT( uab.isValidBcp47() == false );
+        CPPUNIT_ASSERT( uab.isIsoLocale() == false );
+        CPPUNIT_ASSERT( uab.isIsoODF() == false );
+    }
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestLanguageTag );
+
+}
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/i18npool/source/languagetag/languagetag.cxx b/i18npool/source/languagetag/languagetag.cxx
index 1120907..8a60d19 100644
--- a/i18npool/source/languagetag/languagetag.cxx
+++ b/i18npool/source/languagetag/languagetag.cxx
@@ -61,6 +61,7 @@ public:
         if (mnRef != SAL_MAX_UINT32 && mnRef && !--mnRef)
             teardown();
     }
+    void presetDataPath( const rtl::OUString& rPath );
 private:
     rtl::OString maDataPath;   // path to liblangtag data, "|" if system
     sal_uInt32   mnRef;
@@ -100,6 +101,15 @@ void LiblantagDataRef::teardown()
     lt_db_finalize();
 }
 
+void LiblantagDataRef::presetDataPath( const rtl::OUString& rPath )
+{
+    if (maDataPath.isEmpty())
+    {
+        maDataPath = OUStringToOString( rPath, RTL_TEXTENCODING_UTF8);
+        lt_db_set_datadir( maDataPath.getStr());
+    }
+}
+
 void LiblantagDataRef::setupDataPath()
 {
     // maDataPath is assumed to be empty here.
@@ -124,6 +134,13 @@ void LiblantagDataRef::setupDataPath()
 }
 
 
+// static
+void LanguageTag::overrideDataPath( const rtl::OUString& rPath )
+{
+    theDataRef.presetDataPath( rPath);
+}
+
+
 LanguageTag::LanguageTag( const rtl::OUString & rBcp47LanguageTag, bool bCanonicalize )
     :
         maBcp47( rBcp47LanguageTag),
@@ -698,74 +715,4 @@ bool LanguageTag::isValidBcp47() const
 }
 
 
-#ifdef erDEBUG
-void dbg_languagetag()
-{
-    LanguageTag de_DE( "de-Latn-DE", true);
-    de_DE.getBcp47();
-    de_DE.getLocale();
-    de_DE.getLanguageType();
-    de_DE.getLanguage();
-    de_DE.getLanguageAndScript();
-    de_DE.getScript();
-    de_DE.getCountry();
-    de_DE.getRegion();
-    de_DE.isIsoLocale();
-    de_DE.isIsoODF();
-
-    LanguageTag SystemLocale( lang::Locale("","",""));
-    SystemLocale.getBcp47();
-    SystemLocale.getLocale();
-    SystemLocale.getLanguageType();
-    SystemLocale.getLanguage();
-    SystemLocale.getLanguageAndScript();
-    SystemLocale.getScript();
-    SystemLocale.getCountry();
-    SystemLocale.getRegion();
-    SystemLocale.isIsoLocale();
-    SystemLocale.isIsoODF();
-    SystemLocale.isValidBcp47();
-
-    LanguageTag SystemLang( LANGUAGE_SYSTEM);
-    SystemLang.getBcp47();
-    SystemLang.getLocale();
-    SystemLang.getLanguageType();
-    SystemLang.getLanguage();
-    SystemLang.getLanguageAndScript();
-    SystemLang.getScript();
-    SystemLang.getCountry();
-    SystemLang.getRegion();
-    SystemLang.isIsoLocale();
-    SystemLang.isIsoODF();
-    SystemLang.isValidBcp47();
-
-    LanguageTag SystemBcp47( "");
-    SystemBcp47.getBcp47();
-    SystemBcp47.getLocale();
-    SystemBcp47.getLanguageType();
-    SystemBcp47.getLanguage();
-    SystemBcp47.getLanguageAndScript();
-    SystemBcp47.getScript();
-    SystemBcp47.getCountry();
-    SystemBcp47.getRegion();
-    SystemBcp47.isIsoLocale();
-    SystemBcp47.isIsoODF();
-    SystemBcp47.isValidBcp47();
-
-    LanguageTag wab( "wrong-and-bad");
-    wab.getBcp47();
-    wab.getLocale();
-    wab.getLanguageType();
-    wab.getLanguage();
-    wab.getLanguageAndScript();
-    wab.getScript();
-    wab.getCountry();
-    wab.getRegion();
-    wab.isIsoLocale();
-    wab.isIsoODF();
-    wab.isValidBcp47();
-}
-#endif
-
-
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/liblangtag/prj/d.lst b/liblangtag/prj/d.lst
index 7a52fff..8b30436 100644
--- a/liblangtag/prj/d.lst
+++ b/liblangtag/prj/d.lst
@@ -8,3 +8,13 @@ mkdir: %_DEST%\inc\liblangtag
 
 symlink: %_DEST%\lib\liblangtag.so.0.1.0    %_DEST%\lib\liblangtag.so.0
 symlink: %_DEST%\lib\liblangtag.so.0.1.0    %_DEST%\lib\liblangtag.so
+
+# Unpacked data needed for test_languagetag in i18npool.
+mkdir: %_DEST%\share
+mkdir: %_DEST%\share\liblangtag
+mkdir: %_DEST%\share\liblangtag\common
+mkdir: %_DEST%\share\liblangtag\common\bcp47
+..\%__SRC%\misc\install\install-liblangtag\share\liblangtag\*               %_DEST%\share\liblangtag\
+..\%__SRC%\misc\install\install-liblangtag\share\liblangtag\common\bcp47\*  %_DEST%\share\liblangtag\common\bcp47\
+..\%__SRC%\misc\install\@.__________________________________________________OOO\share\liblangtag\*               %_DEST%\share\liblangtag\
+..\%__SRC%\misc\install\@.__________________________________________________OOO\share\liblangtag\common\bcp47\*  %_DEST%\share\liblangtag\common\bcp47\
commit 1166c01b84b5206feacc3af078eeb525bbb1eb86
Author: Eike Rathke <erack at redhat.com>
Date:   Fri Aug 10 14:07:15 2012 +0200

    region may be empty and then is a valid ISO country for our purposes
    
    Change-Id: I157ea5b5b32ca0664c8eec775de01958f7b55f06

diff --git a/i18npool/source/languagetag/languagetag.cxx b/i18npool/source/languagetag/languagetag.cxx
index caaf0cc..1120907 100644
--- a/i18npool/source/languagetag/languagetag.cxx
+++ b/i18npool/source/languagetag/languagetag.cxx
@@ -567,7 +567,8 @@ bool LanguageTag::isIsoLanguage( const rtl::OUString& rLanguage )
 bool LanguageTag::isIsoCountry( const rtl::OUString& rRegion )
 {
     /* TODO: ignore case? For now let's see where rubbish is used. */
-    if (rRegion.getLength() == 2 && isUpperAscii( rRegion[0]) && isUpperAscii( rRegion[1]))
+    if (rRegion.isEmpty() ||
+            (rRegion.getLength() == 2 && isUpperAscii( rRegion[0]) && isUpperAscii( rRegion[1])))
         return true;
     SAL_WARN_IF( rRegion.getLength() == 2 && (isLowerAscii( rRegion[0]) || isLowerAscii( rRegion[1])),
             "i18npool.langtag", "LanguageTag::isIsoCountry: rejecting lower case");


More information about the Libreoffice-commits mailing list