diff options
Diffstat (limited to 'i18npool')
-rw-r--r-- | i18npool/CppunitTest_i18npool_test_languagetag.mk | 50 | ||||
-rw-r--r-- | i18npool/Module_i18npool.mk | 1 | ||||
-rw-r--r-- | i18npool/inc/i18npool/languagetag.hxx | 5 | ||||
-rw-r--r-- | i18npool/qa/cppunit/test_languagetag.cxx | 206 | ||||
-rw-r--r-- | i18npool/source/languagetag/languagetag.cxx | 87 |
5 files changed, 279 insertions, 70 deletions
diff --git a/i18npool/CppunitTest_i18npool_test_languagetag.mk b/i18npool/CppunitTest_i18npool_test_languagetag.mk new file mode 100644 index 000000000000..8c42ad610c23 --- /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 bed175a8c310..9214d0081457 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 1dcc213e1745..0066d8e92657 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 000000000000..fee5769f795c --- /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 112090763cd5..8a60d196b220 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: */ |