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