forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ti.locale.test.js
319 lines (254 loc) ยท 11.7 KB
/
ti.locale.test.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Titanium SDK
* Copyright TiDev, Inc. 04/07/2022-Present. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');
describe('Global', () => {
it('#L() is a Function', () => should(L).be.a.Function());
});
describe('Titanium.Locale', () => {
// reset back to US english when done
after(() => Ti.Locale.language = 'en-US');
it('exists', () => {
should(Ti.Locale).not.be.undefined();
should(Ti.Locale).not.be.null();
should(Ti.Locale).be.an.Object();
});
describe('properties', () => {
it('.apiName', () => {
should(Ti.Locale).have.a.readOnlyProperty('apiName').which.is.a.String();
should(Ti.Locale.apiName).be.eql('Ti.Locale');
});
describe('.currentCountry', () => {
it('is a String', () => {
should(Ti.Locale).have.a.readOnlyProperty('currentCountry').which.is.a.String();
});
it('defaults to \'US\'', () => {
should(Ti.Locale.currentCountry).eql('US');
});
it('has no getter', () => {
should(Ti.Locale).not.have.a.getter('currentCountry');
});
});
describe.android('.applicationLocales', () => {
it('is a Array', () => {
should(Ti.Locale).have.a.property('applicationLocales').which.is.an.Array();
});
});
describe('.currentLanguage', () => {
it('is a String', () => {
should(Ti.Locale).have.a.readOnlyProperty('currentLanguage').which.is.a.String();
});
it('defaults to \'en\'', () => {
should(Ti.Locale.currentLanguage).eql('en');
});
it('has no getter', () => {
should(Ti.Locale).not.have.a.getter('currentLanguage');
});
});
describe('.currentLocale', () => {
it('is a String', () => {
should(Ti.Locale).have.a.readOnlyProperty('currentLocale').which.is.a.String();
});
it('defaults to \'en-US\'', () => {
should(Ti.Locale.currentLocale).eql('en-US');
});
it('has no getter', () => {
should(Ti.Locale).not.have.a.getter('currentLocale');
});
});
});
describe('methods', () => {
// TODO Support Ti.Locale.formatTelephoneNumber on other platforms?
it.android('#formatTelephoneNumber(String)', () => {
should(Ti.Locale.formatTelephoneNumber).be.a.Function();
// TODO Actually check inputs/outputs!
});
it('#getCurrencyCode(String)', () => {
should(Ti.Locale.getCurrencyCode).be.a.Function();
should(Ti.Locale.getCurrencyCode('en-US')).eql('USD');
should(Ti.Locale.getCurrencyCode('ja-JP')).eql('JPY');
should(Ti.Locale.getCurrencyCode('zh-CN')).eql('CNY');
should(Ti.Locale.getCurrencyCode('zh-TW')).eql('TWD');
});
// FIXME Get working on iOS
// FIXME Get working properly cross-platform. JPY gives us ยฅ on Windows and Android, JPยฅ on iOS. CNY gives us ยฅ on Windows, CNยฅ on Android
it.androidAndIosBroken('#getCurrencySymbol(String)', () => {
should(Ti.Locale.getCurrencySymbol).be.a.Function();
should(Ti.Locale.getCurrencySymbol('USD')).eql('$');
should(Ti.Locale.getCurrencySymbol('JPY')).eql('ยฅ'); // 'JPยฅ' on iOS
should(Ti.Locale.getCurrencySymbol('CNY')).eql('ยฅ'); // 'CNยฅ' on Android
should(Ti.Locale.getCurrencySymbol('TWD')).eql('NT$');
});
it('#getLocaleCurrencySymbol()', () => {
should(Ti.Locale.getLocaleCurrencySymbol).be.a.Function();
should(Ti.Locale.getLocaleCurrencySymbol('en-US')).eql('$');
});
describe('#getString()', () => {
it('is a Function', () => {
should(Ti.Locale.getString).be.a.Function();
});
beforeEach(() => {
Ti.Locale.language = 'en-US';
});
it('returns stored value for found key', () => {
should(Ti.Locale.getString('this_is_my_key')).eql('this is my value');
should(L('this_is_my_key')).eql('this is my value');
});
it('returns key if not found and no default specified', () => {
should(Ti.Locale.getString('this_should_not_be_found')).eql('this_should_not_be_found');
should(L('this_should_not_be_found')).eql('this_should_not_be_found');
});
it('returns supplied default if key not found', () => {
should(Ti.Locale.getString('this_should_not_be_found', 'this is the default value')).eql('this is the default value');
should(L('this_should_not_be_found', 'this is the default value')).eql('this is the default value');
});
// FIXME: returns null - we can fix this in a cross-platform way via same extension we used for Android to fix issue
it('returns key if supplied default is not a String and key/value pair not found', () => {
should(Ti.Locale.getString('this_should_not_be_found', null)).eql('this_should_not_be_found');
should(L('this_should_not_be_found', null)).eql('this_should_not_be_found');
should(Ti.Locale.getString('this_should_not_be_found', 123)).eql('this_should_not_be_found');
should(L('this_should_not_be_found', 123)).eql('this_should_not_be_found');
});
// https://jira-archive.titaniumsdk.com/TIMOB-26651
it('handles locale/country specific languages (i.e. en-GB vs en-US)', () => {
Ti.Locale.language = 'en-GB';
should(Ti.Locale.getString('this_is_my_key')).eql('this is my en-GB value'); // This fails on Windows, gives 'this is my value'
should(L('this_is_my_key')).eql('this is my en-GB value'); // This fails on Windows, gives 'this is my value'
});
// and then this one fails because it's using en-GB strings after we tell it to be ja...
it('handles single segment language (i.e. ja)', () => {
Ti.Locale.language = 'ja';
should(Ti.Locale.getString('this_is_my_key')).eql('ใใใฏ็งใฎๅคใงใ');
should(L('this_is_my_key')).eql('ใใใฏ็งใฎๅคใงใ');
});
// ...and this one is now using ja strings, but langauge value is en-US!
// FIXME iOS seems to ignore position info on the format string.
// We're trying to force the 1st argument into the second slot, and vice versa here. iOS handles the %2$s syntax, but ignores position
it.iosAndWindowsBroken('usage with String.format()', () => {
var i18nMissingMsg = '<no translation available>';
var string1 = 'You say ' + Ti.Locale.getString('signoff', i18nMissingMsg) + ' and I say ' + Ti.Locale.getString('greeting', i18nMissingMsg) + '!';
var string2 = String.format(L('phrase'), L('greeting', i18nMissingMsg), L('signoff', i18nMissingMsg));
should(string1).eql(string2);
if (Ti.Locale.currentLanguage === 'en') {
should(string1).eql('You say goodbye and I say hello!');
should(string2).eql('You say goodbye and I say hello!');
} else if (Ti.Locale.currentLanguage === 'ja') {
should(string1).eql('You say ใใใใชใ and I say ใใใซใกใฏ!');
should(string2).eql('You say ใใใใชใ and I say ใใใซใกใฏ!');
}
});
});
describe('#parseDecimal()', () => {
it('compared with String.formatDecimal()', () => {
should(Ti.Locale.parseDecimal).be.a.Function();
const numericValue = 1234567.8;
let numericString = String.formatDecimal(numericValue);
let parsedValue = Ti.Locale.parseDecimal(numericString);
should(parsedValue).be.a.Number();
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON);
numericString = String.formatDecimal(numericValue, 'de-DE');
parsedValue = Ti.Locale.parseDecimal(numericString, 'de-DE');
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON);
numericString = String.formatDecimal(numericValue, 'fr-FR');
parsedValue = Ti.Locale.parseDecimal(numericString, 'fr-FR');
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON);
numericString = String.formatDecimal(numericValue, 'ar-EG');
parsedValue = Ti.Locale.parseDecimal(numericString, 'ar-EG');
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON);
});
it('localized values', () => {
let result = Ti.Locale.parseDecimal('1,234,567.8', 'en-US');
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('1.234.567,8', 'de-DE');
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON);
// France uses non-breaking unicode spaces for thousands separator.
result = Ti.Locale.parseDecimal('1\u00A0234\u00A0567,8', 'fr-FR');
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON);
// But it was then changed to narrow non-breaking spaces!
result = Ti.Locale.parseDecimal('1\u202F234\u202F567,8', 'fr-FR');
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON);
});
it('various values', () => {
let result = Ti.Locale.parseDecimal('0', 'en-US');
should(Math.abs(result)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('0.', 'en-US');
should(Math.abs(result)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('.0', 'en-US');
should(Math.abs(result)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('00.00123', 'en-US');
should(Math.abs(result - 0.00123)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('+0', 'en-US');
should(Math.abs(result)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('-0', 'en-US');
should(Math.abs(result)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('+1234.5', 'en-US');
should(Math.abs(result - 1234.5)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('-1234.5', 'en-US');
should(Math.abs(result + 1234.5)).be.lessThan(Number.EPSILON);
});
it('scientific values', () => {
let result = Ti.Locale.parseDecimal('1.2E+3', 'en-US');
should(Math.abs(result - 1200.0)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('-1.2E+3', 'en-US');
should(Math.abs(result + 1200.0)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('1.2E-3', 'en-US');
should(Math.abs(result - 0.0012)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal('-1.2E-3', 'en-US');
should(Math.abs(result + 0.0012)).be.lessThan(Number.EPSILON);
});
it('padded with spaces', () => {
let result = Ti.Locale.parseDecimal(' 123 ', 'en-US');
should(Math.abs(result - 123)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal(' +123 ', 'en-US');
should(Math.abs(result - 123)).be.lessThan(Number.EPSILON);
result = Ti.Locale.parseDecimal(' -123 ', 'en-US');
should(Math.abs(result + 123)).be.lessThan(Number.EPSILON);
});
it('NaN', () => {
let result = Ti.Locale.parseDecimal('ThisShouldFail');
should(result).be.a.Number();
should(result).be.eql(Number.NaN);
result = Ti.Locale.parseDecimal('');
should(result).be.eql(Number.NaN);
result = Ti.Locale.parseDecimal(' ');
should(result).be.eql(Number.NaN);
result = Ti.Locale.parseDecimal(null);
should(result).be.eql(Number.NaN);
result = Ti.Locale.parseDecimal(undefined);
should(result).be.eql(Number.NaN);
});
});
describe.android('#set applicationLocales(String)', () => {
it('has a setter', () => {
should(Ti.Locale).have.a.setter('applicationLocales');
});
it('changes .applicationLocales', () => {
Ti.Locale.applicationLocales = 'ar';
should(Ti.Locale.applicationLocales[0].language).eql('ar');
});
});
describe('#set language(String)', () => {
it('has a setter', () => {
should(Ti.Locale).have.a.setter('language');
});
it('changes .currentLanguage', () => {
Ti.Locale.language = 'fr';
should(Ti.Locale.currentLanguage).eql('fr');
});
// FIXME Get working on iOS, setLangauge doesn't seem to affect currentLocale
it.iosBroken('changes .currentLocale', () => {
Ti.Locale.language = 'en-GB';
should(Ti.Locale.currentLocale).eql('en-GB'); // iOS returns 'en-US'
Ti.Locale.language = 'fr';
should(Ti.Locale.currentLocale).eql('fr');
});
// TODO test if it changes the currentCountry?
});
});
});