summaryrefslogtreecommitdiffstats
path: root/tools/localize/localize_test.cpp
blob: 931ea95e623540fb063c86cffa49ac844075ade6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <cstdio>
#include "XLIFFFile.h"
#include "ValuesFile.h"
#include "localize.h"

int pseudolocalize_xliff(XLIFFFile* xliff, bool expand);

static int
test_filename(const string& file, const string& locale, const string& expected)
{
    string result = translated_file_name(file, locale);
    if (result != expected) {
        fprintf(stderr, "translated_file_name test failed\n");
        fprintf(stderr, "  locale='%s'\n", locale.c_str());
        fprintf(stderr, "  expected='%s'\n", expected.c_str());
        fprintf(stderr, "    result='%s'\n", result.c_str());
        return 1;
    } else {
        if (false) {
            fprintf(stderr, "translated_file_name test passed\n");
            fprintf(stderr, "  locale='%s'\n", locale.c_str());
            fprintf(stderr, "  expected='%s'\n", expected.c_str());
            fprintf(stderr, "    result='%s'\n", result.c_str());
        }
        return 0;
    }
}

static int
translated_file_name_test()
{
    bool all = true;
    int err = 0;

    if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "zz_ZZ",
                                  "//device/samples/NotePad/res/values-zz-rZZ/strings.xml");

    if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "zz",
                                  "//device/samples/NotePad/res/values-zz/strings.xml");

    if (all) err |= test_filename("//device/samples/NotePad/res/values/strings.xml", "",
                                  "//device/samples/NotePad/res/values/strings.xml");

    return err;
}

bool
return_false(const string&, const TransUnit& unit, void* cookie)
{
    return false;
}

static int
delete_trans_units()
{
    XLIFFFile* xliff = XLIFFFile::Parse("testdata/strip_xliff.xliff");
    if (xliff == NULL) {
        printf("couldn't read file\n");
        return 1;
    }
    if (false) {
        printf("XLIFF was [[%s]]\n", xliff->ToString().c_str());
    }

    xliff->Filter(return_false, NULL);

    if (false) {
        printf("XLIFF is [[%s]]\n", xliff->ToString().c_str());

        set<StringResource> const& strings = xliff->GetStringResources();
        printf("strings.size=%zd\n", strings.size());
        for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) {
            const StringResource& str = *it;
            printf("STRING!!! id=%s value='%s' pos=%s file=%s version=%d(%s)\n", str.id.c_str(),
                    str.value->ContentsToString(ANDROID_NAMESPACES).c_str(),
                    str.pos.ToString().c_str(), str.file.c_str(), str.version,
                    str.versionString.c_str());
        }
    }
 
    return 0;
}

static int
filter_trans_units()
{
    XLIFFFile* xliff = XLIFFFile::Parse("testdata/strip_xliff.xliff");
    if (xliff == NULL) {
        printf("couldn't read file\n");
        return 1;
    }

    if (false) {
        printf("XLIFF was [[%s]]\n", xliff->ToString().c_str());
    }

    Settings setting;
    xliff->Filter(keep_this_trans_unit, &setting);

    if (false) {
        printf("XLIFF is [[%s]]\n", xliff->ToString().c_str());

        set<StringResource> const& strings = xliff->GetStringResources();
        printf("strings.size=%zd\n", strings.size());
        for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) {
            const StringResource& str = *it;
            printf("STRING!!! id=%s value='%s' pos=%s file=%s version=%d(%s)\n", str.id.c_str(),
                    str.value->ContentsToString(ANDROID_NAMESPACES).c_str(),
                    str.pos.ToString().c_str(), str.file.c_str(), str.version,
                    str.versionString.c_str());
        }
    }
 
    return 0;
}

static int
settings_test()
{
    int err;
    map<string,Settings> settings;
    map<string,Settings>::iterator it;

    err = read_settings("testdata/config.xml", &settings, "//asdf");
    if (err != 0) {
        return err;
    }

    if (false) {
        for (it=settings.begin(); it!=settings.end(); it++) {
            const Settings& setting = it->second;
            printf("CONFIG:\n");
            printf("              id='%s'\n", setting.id.c_str());
            printf("      oldVersion='%s'\n", setting.oldVersion.c_str());
            printf("  currentVersion='%s'\n", setting.currentVersion.c_str());
            int i=0;
            for (vector<string>::const_iterator app=setting.apps.begin();
                    app!=setting.apps.end(); app++) {
                printf("        apps[%02d]='%s'\n", i, app->c_str());
                i++;
            }
            i=0;
            for (vector<Reject>::const_iterator reject=setting.reject.begin();
                    reject!=setting.reject.end(); reject++) {
                i++;
                printf("      reject[%02d]=('%s','%s','%s')\n", i, reject->file.c_str(),
                        reject->name.c_str(), reject->comment.c_str());
            }
        }
    }

    for (it=settings.begin(); it!=settings.end(); it++) {
        const Settings& setting = it->second;
        if (it->first != setting.id) {
            fprintf(stderr, "it->first='%s' setting.id='%s'\n", it->first.c_str(),
                    setting.id.c_str());
            err |= 1;
        }
    }


    return err;
}

static int
test_one_pseudo(bool big, const char* expected)
{
    XLIFFFile* xliff = XLIFFFile::Parse("testdata/pseudo.xliff");
    if (xliff == NULL) {
        printf("couldn't read file\n");
        return 1;
    }
    if (false) {
        printf("XLIFF was [[%s]]\n", xliff->ToString().c_str());
    }

    pseudolocalize_xliff(xliff, big);
    string newString = xliff->ToString();
    delete xliff;

    if (false) {
        printf("XLIFF is [[%s]]\n", newString.c_str());
    }

    if (false && newString != expected) {
        fprintf(stderr, "xliff didn't translate as expected\n");
        fprintf(stderr, "newString=[[%s]]\n", newString.c_str());
        fprintf(stderr, "expected=[[%s]]\n", expected);
        return 1;
    }

    return 0;
}

static int
pseudolocalize_test()
{
    int err = 0;
    
    err |= test_one_pseudo(false, "");
    //err |= test_one_pseudo(true, "");

    return err;
}

int
localize_test()
{
    bool all = true;
    int err = 0;

    if (all) err |= translated_file_name_test();
    if (all) err |= delete_trans_units();
    if (all) err |= filter_trans_units();
    if (all) err |= settings_test();
    if (all) err |= pseudolocalize_test();

    return err;
}