localize_test.cpp revision bdb087c9305d6d753444e1c0176a793c00f07840
1#include "XLIFFFile.h" 2#include "ValuesFile.h" 3#include "localize.h" 4#include <stdio.h> 5 6int pseudolocalize_xliff(XLIFFFile* xliff, bool expand); 7 8static int 9test_filename(const string& file, const string& locale, const string& expected) 10{ 11 string result = translated_file_name(file, locale); 12 if (result != expected) { 13 fprintf(stderr, "translated_file_name test failed\n"); 14 fprintf(stderr, " locale='%s'\n", locale.c_str()); 15 fprintf(stderr, " expected='%s'\n", expected.c_str()); 16 fprintf(stderr, " result='%s'\n", result.c_str()); 17 return 1; 18 } else { 19 if (false) { 20 fprintf(stderr, "translated_file_name test passed\n"); 21 fprintf(stderr, " locale='%s'\n", locale.c_str()); 22 fprintf(stderr, " expected='%s'\n", expected.c_str()); 23 fprintf(stderr, " result='%s'\n", result.c_str()); 24 } 25 return 0; 26 } 27} 28 29static int 30translated_file_name_test() 31{ 32 bool all = true; 33 int err = 0; 34 35 if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "zz_ZZ", 36 "//device/samples/NotePad/res/values-zz-rZZ/strings.xml"); 37 38 if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "zz", 39 "//device/samples/NotePad/res/values-zz/strings.xml"); 40 41 if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "", 42 "//device/samples/NotePad/res/values/strings.xml"); 43 44 return err; 45} 46 47bool 48return_false(const string&, const TransUnit& unit, void* cookie) 49{ 50 return false; 51} 52 53static int 54delete_trans_units() 55{ 56 XLIFFFile* xliff = XLIFFFile::Parse("testdata/strip_xliff.xliff"); 57 if (xliff == NULL) { 58 printf("couldn't read file\n"); 59 return 1; 60 } 61 if (false) { 62 printf("XLIFF was [[%s]]\n", xliff->ToString().c_str()); 63 } 64 65 xliff->Filter(return_false, NULL); 66 67 if (false) { 68 printf("XLIFF is [[%s]]\n", xliff->ToString().c_str()); 69 70 set<StringResource> const& strings = xliff->GetStringResources(); 71 printf("strings.size=%zd\n", strings.size()); 72 for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) { 73 const StringResource& str = *it; 74 printf("STRING!!! id=%s value='%s' pos=%s file=%s version=%d(%s)\n", str.id.c_str(), 75 str.value->ContentsToString(ANDROID_NAMESPACES).c_str(), 76 str.pos.ToString().c_str(), str.file.c_str(), str.version, 77 str.versionString.c_str()); 78 } 79 } 80 81 return 0; 82} 83 84static int 85filter_trans_units() 86{ 87 XLIFFFile* xliff = XLIFFFile::Parse("testdata/strip_xliff.xliff"); 88 if (xliff == NULL) { 89 printf("couldn't read file\n"); 90 return 1; 91 } 92 93 if (false) { 94 printf("XLIFF was [[%s]]\n", xliff->ToString().c_str()); 95 } 96 97 Settings setting; 98 xliff->Filter(keep_this_trans_unit, &setting); 99 100 if (false) { 101 printf("XLIFF is [[%s]]\n", xliff->ToString().c_str()); 102 103 set<StringResource> const& strings = xliff->GetStringResources(); 104 printf("strings.size=%zd\n", strings.size()); 105 for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) { 106 const StringResource& str = *it; 107 printf("STRING!!! id=%s value='%s' pos=%s file=%s version=%d(%s)\n", str.id.c_str(), 108 str.value->ContentsToString(ANDROID_NAMESPACES).c_str(), 109 str.pos.ToString().c_str(), str.file.c_str(), str.version, 110 str.versionString.c_str()); 111 } 112 } 113 114 return 0; 115} 116 117static int 118settings_test() 119{ 120 int err; 121 map<string,Settings> settings; 122 map<string,Settings>::iterator it; 123 124 err = read_settings("testdata/config.xml", &settings, "//asdf"); 125 if (err != 0) { 126 return err; 127 } 128 129 if (false) { 130 for (it=settings.begin(); it!=settings.end(); it++) { 131 const Settings& setting = it->second; 132 printf("CONFIG:\n"); 133 printf(" id='%s'\n", setting.id.c_str()); 134 printf(" oldVersion='%s'\n", setting.oldVersion.c_str()); 135 printf(" currentVersion='%s'\n", setting.currentVersion.c_str()); 136 int i=0; 137 for (vector<string>::const_iterator app=setting.apps.begin(); 138 app!=setting.apps.end(); app++) { 139 printf(" apps[%02d]='%s'\n", i, app->c_str()); 140 i++; 141 } 142 i=0; 143 for (vector<Reject>::const_iterator reject=setting.reject.begin(); 144 reject!=setting.reject.end(); reject++) { 145 i++; 146 printf(" reject[%02d]=('%s','%s','%s')\n", i, reject->file.c_str(), 147 reject->name.c_str(), reject->comment.c_str()); 148 } 149 } 150 } 151 152 for (it=settings.begin(); it!=settings.end(); it++) { 153 const Settings& setting = it->second; 154 if (it->first != setting.id) { 155 fprintf(stderr, "it->first='%s' setting.id='%s'\n", it->first.c_str(), 156 setting.id.c_str()); 157 err |= 1; 158 } 159 } 160 161 162 return err; 163} 164 165static int 166test_one_pseudo(bool big, const char* expected) 167{ 168 XLIFFFile* xliff = XLIFFFile::Parse("testdata/pseudo.xliff"); 169 if (xliff == NULL) { 170 printf("couldn't read file\n"); 171 return 1; 172 } 173 if (false) { 174 printf("XLIFF was [[%s]]\n", xliff->ToString().c_str()); 175 } 176 177 pseudolocalize_xliff(xliff, big); 178 string newString = xliff->ToString(); 179 delete xliff; 180 181 if (false) { 182 printf("XLIFF is [[%s]]\n", newString.c_str()); 183 } 184 185 if (false && newString != expected) { 186 fprintf(stderr, "xliff didn't translate as expected\n"); 187 fprintf(stderr, "newString=[[%s]]\n", newString.c_str()); 188 fprintf(stderr, "expected=[[%s]]\n", expected); 189 return 1; 190 } 191 192 return 0; 193} 194 195static int 196pseudolocalize_test() 197{ 198 int err = 0; 199 200 err |= test_one_pseudo(false, ""); 201 //err |= test_one_pseudo(true, ""); 202 203 return err; 204} 205 206int 207localize_test() 208{ 209 bool all = true; 210 int err = 0; 211 212 if (all) err |= translated_file_name_test(); 213 if (all) err |= delete_trans_units(); 214 if (all) err |= filter_trans_units(); 215 if (all) err |= settings_test(); 216 if (all) err |= pseudolocalize_test(); 217 218 return err; 219} 220 221