PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/localize/localize_test.cpp

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