** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
** Distributed under the terms of the MIT License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <Application.h>
#include <Catalog.h>
#include <DefaultCatalog.h>
#include <Entry.h>
#include <Locale.h>
#include <Path.h>
#include <Roster.h>
class CatalogTest {
public:
void Run();
void Check();
};
#define B_TRANSLATION_CONTEXT "CatalogTest"
#define catSig "x-vnd.Be.locale.catalogTest"
#define catName catSig".catalog"
void
CatalogTest::Run()
{
printf("app...");
status_t res;
BString s;
s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
size_t hashVal = CatKey::HashFun(s.String());
assert(be_locale != NULL);
system("mkdir -p ./locale/catalogs/"catSig);
BPrivate::EditableCatalog cata("Default", catSig, "German");
assert(cata.InitCheck() == B_OK);
res = cata.SetString("string", "Schnur", B_TRANSLATION_CONTEXT);
assert(res == B_OK);
res = cata.SetString(hashVal, "Schnur_id");
assert(res == B_OK);
res = cata.SetString("string", "String", "programming");
assert(res == B_OK);
res = cata.SetString("string", "Textpuffer", "programming",
"Deutsches Fachbuch");
assert(res == B_OK);
res = cata.SetString("string", "Leine", B_TRANSLATION_CONTEXT,
"Deutsches Fachbuch");
assert(res == B_OK);
res = cata.WriteToFile("./locale/catalogs/"catSig"/german.catalog");
assert(res == B_OK);
s = cata.GetString(("string"), B_TRANSLATION_CONTEXT);
assert(s == "Schnur");
s = cata.GetString(hashVal);
assert(s == "Schnur_id");
s = cata.GetString("string", "programming");
assert(s == "String");
s = cata.GetString("string", "programming", "Deutsches Fachbuch");
assert(s == "Textpuffer");
s = cata.GetString("string", B_TRANSLATION_CONTEXT, "Deutsches Fachbuch");
assert(s == "Leine");
BPrivate::EditableCatalog catb("Default", catSig, "English");
assert(catb.InitCheck() == B_OK);
res = catb.SetString("string", "string", "base");
assert(res == B_OK);
res = catb.SetString(32, "hashed string");
assert(res == B_OK);
res = catb.SetString("string", "hidden", B_TRANSLATION_CONTEXT);
assert(res == B_OK);
app_info appInfo;
res = be_app->GetAppInfo(&appInfo);
assert(res == B_OK);
res = catb.WriteToResource(&appInfo.ref);
assert(res == B_OK);
printf("ok.\n");
Check();
}
void
CatalogTest::Check()
{
status_t res;
printf("app-check...");
BString s;
s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
size_t hashVal = CatKey::HashFun(s.String());
BCatalog cat;
res = be_locale->GetAppCatalog(&cat);
assert(res == B_OK);
uint32 fingerprint = 0;
res = cat.GetFingerprint(&fingerprint);
assert(res == B_OK);
BString lang;
res = cat.GetLanguage(&lang);
assert(res == B_OK);
BString sig;
res = cat.GetSignature(&sig);
assert(res == B_OK);
s = B_TRANSLATE_ID(hashVal);
assert(s == "Schnur_id");
s = B_TRANSLATE_ALL("string", "programming", "");
assert(s == "String");
s = B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch");
assert(s == "Textpuffer");
s = B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch");
assert(s == "Leine");
s = B_TRANSLATE_ALL("string", "base", NULL);
assert(s == "string");
s = B_TRANSLATE_ID(32);
assert(s == "hashed string");
s = B_TRANSLATE_ID(-1);
assert(s == "");
s = B_TRANSLATE("string");
assert(s == "Schnur");
BCatalog cat2(sig.String(), lang.String(), fingerprint);
assert(cat2.InitCheck() == B_OK);
BCatalog cat3(sig.String(), lang.String(), fingerprint*-1);
assert(cat3.InitCheck() == B_NO_INIT);
s = cat3.GetString("string");
assert(s == "string");
printf("ok.\n");
}
int
main(int argc, char **argv)
{
BApplication* testApp
= new BApplication("application/"catSig);
app_info appInfo;
be_app->GetAppInfo(&appInfo);
BEntry appEntry(&appInfo.ref);
BEntry appFolder;
appEntry.GetParent( &appFolder);
BPath appPath;
appFolder.GetPath( &appPath);
chdir( appPath.Path());
CatalogTest catTest;
catTest.Run();
char cwd[B_FILE_NAME_LENGTH];
getcwd(cwd, B_FILE_NAME_LENGTH);
BString addonName(cwd);
addonName << "/" "catalogTestAddOn";
image_id image = load_add_on(addonName.String());
assert(image >= B_OK);
void (*runAddonFunc)() = 0;
get_image_symbol(image, "run_test_add_on",
B_SYMBOL_TYPE_TEXT, (void **)&runAddonFunc);
assert(runAddonFunc);
runAddonFunc();
catTest.Check();
delete testApp;
}