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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
require 'twine_test'
class FormatterTest < TwineTest
def setup(formatter_class)
super()
@twine_file = build_twine_file 'en' do
add_section 'Section 1' do
add_definition key1: 'value1-english', comment: 'comment key1'
add_definition key2: 'value2-english'
end
add_section 'Section 2' do
add_definition key3: 'value3-english'
add_definition key4: 'value4-english', comment: 'comment key4'
end
end
@empty_twine_file = Twine::TwineFile.new
@formatter = formatter_class.new
@formatter.twine_file = @empty_twine_file
@formatter.options = { consume_all: true, consume_comments: true }
end
def assert_translations_read_correctly
1.upto(4) do |i|
assert_equal "value#{i}-english", @empty_twine_file.definitions_by_key["key#{i}"].translations['en']
end
end
def assert_file_contents_read_correctly
assert_translations_read_correctly
assert_equal "comment key1", @empty_twine_file.definitions_by_key["key1"].comment
assert_equal "comment key4", @empty_twine_file.definitions_by_key["key4"].comment
end
end
class TestAndroidFormatter < FormatterTest
def setup
super Twine::Formatters::Android
@escape_test_values = {
'this & that' => 'this & that',
'this < that' => 'this < that',
"it's complicated" => "it\\'s complicated",
'a "good" way' => 'a \"good\" way',
'<b>bold</b>' => '<b>bold</b>',
'<a href="target">link</a>' => '<a href=\"target\">link</a>',
'<xliff:g></xliff:g>' => '<xliff:g></xliff:g>',
'<xliff:g>untouched</xliff:g>' => '<xliff:g>untouched</xliff:g>',
'<xliff:g id="42">untouched</xliff:g>' => '<xliff:g id="42">untouched</xliff:g>',
'<xliff:g id="1">first</xliff:g> inbetween <xliff:g id="2">second</xliff:g>' => '<xliff:g id="1">first</xliff:g> inbetween <xliff:g id="2">second</xliff:g>'
}
end
def test_read_format
@formatter.read content_io('formatter_android.xml'), 'en'
assert_file_contents_read_correctly
end
def test_read_multiline_translation
content = <<-EOCONTENT
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="foo">This is
a string</string>
</resources>
EOCONTENT
io = StringIO.new(content)
@formatter.read io, 'en'
assert_equal 'This is\n a string', @empty_twine_file.definitions_by_key["foo"].translations['en']
end
def test_set_translation_converts_leading_spaces
@formatter.set_translation_for_key 'key1', 'en', "\u0020value"
assert_equal ' value', @empty_twine_file.definitions_by_key['key1'].translations['en']
end
def test_set_translation_coverts_trailing_spaces
@formatter.set_translation_for_key 'key1', 'en', "value\u0020\u0020"
assert_equal 'value ', @empty_twine_file.definitions_by_key['key1'].translations['en']
end
def test_set_translation_converts_string_placeholders
@formatter.set_translation_for_key 'key1', 'en', "value %s"
assert_equal 'value %@', @empty_twine_file.definitions_by_key['key1'].translations['en']
end
def test_set_translation_unescapes_at_signs
@formatter.set_translation_for_key 'key1', 'en', '\@value'
assert_equal '@value', @empty_twine_file.definitions_by_key['key1'].translations['en']
end
def test_set_translation_unescaping
@escape_test_values.each do |expected, input|
@formatter.set_translation_for_key 'key1', 'en', input
assert_equal expected, @empty_twine_file.definitions_by_key['key1'].translations['en']
end
end
def test_format_file
formatter = Twine::Formatters::Android.new
formatter.twine_file = @twine_file
assert_equal content('formatter_android.xml'), formatter.format_file('en')
end
def test_format_key_with_space
assert_equal 'key ', @formatter.format_key('key ')
end
def test_format_value_with_leading_space
assert_equal "\\u0020value", @formatter.format_value(' value')
end
def test_format_value_with_trailing_space
assert_equal "value\\u0020", @formatter.format_value('value ')
end
def test_format_value_escaping
@escape_test_values.each do |input, expected|
assert_equal expected, @formatter.format_value(input)
end
end
def test_format_value_escapes_non_resource_identifier_at_signs
assert_equal '\@whatever \@\@', @formatter.format_value('@whatever @@')
end
def test_format_value_does_not_modify_resource_identifiers
identifier = '@android:string/cancel'
assert_equal identifier, @formatter.format_value(identifier)
end
def test_deducts_language_from_resource_folder
language = %w(en de fr).sample
assert_equal language, @formatter.determine_language_given_path("res/values-#{language}")
end
def test_deducts_language_and_region_from_resource_folder
assert_equal 'de-AT', @formatter.determine_language_given_path("res/values-de-rAT")
end
def test_does_not_deduct_language_from_device_capability_resource_folder
assert_nil @formatter.determine_language_given_path('res/values-w820dp')
end
def test_output_path_is_prefixed
assert_equal 'values-en', @formatter.output_path_for_language('en')
end
end
class TestAppleFormatter < FormatterTest
def setup
super Twine::Formatters::Apple
end
def test_read_format
@formatter.read content_io('formatter_apple.strings'), 'en'
assert_file_contents_read_correctly
end
def test_reads_quoted_keys
@formatter.read StringIO.new('"key" = "value"'), 'en'
assert_equal 'value', @empty_twine_file.definitions_by_key['key'].translations['en']
end
def test_reads_unquoted_keys
@formatter.read StringIO.new('key = "value"'), 'en'
assert_equal 'value', @empty_twine_file.definitions_by_key['key'].translations['en']
end
def test_ignores_leading_whitespace_before_quoted_keys
@formatter.read StringIO.new("\t \"key\" = \"value\""), 'en'
assert_equal 'value', @empty_twine_file.definitions_by_key['key'].translations['en']
end
def test_ignores_leading_whitespace_before_unquoted_keys
@formatter.read StringIO.new("\t key = \"value\""), 'en'
assert_equal 'value', @empty_twine_file.definitions_by_key['key'].translations['en']
end
def test_allows_quotes_in_quoted_keys
@formatter.read StringIO.new('"ke\"y" = "value"'), 'en'
assert_equal 'value', @empty_twine_file.definitions_by_key['ke"y'].translations['en']
end
def test_does_not_allow_quotes_in_quoted_keys
@formatter.read StringIO.new('ke"y = "value"'), 'en'
assert_nil @empty_twine_file.definitions_by_key['key']
end
def test_allows_equal_signs_in_quoted_keys
@formatter.read StringIO.new('"k=ey" = "value"'), 'en'
assert_equal 'value', @empty_twine_file.definitions_by_key['k=ey'].translations['en']
end
def test_does_not_allow_equal_signs_in_unquoted_keys
@formatter.read StringIO.new('k=ey = "value"'), 'en'
assert_nil @empty_twine_file.definitions_by_key['key']
end
def test_format_file
formatter = Twine::Formatters::Apple.new
formatter.twine_file = @twine_file
assert_equal content('formatter_apple.strings'), formatter.format_file('en')
end
def test_format_key_with_space
assert_equal 'key ', @formatter.format_key('key ')
end
def test_format_value_with_leading_space
assert_equal ' value', @formatter.format_value(' value')
end
def test_format_value_with_trailing_space
assert_equal 'value ', @formatter.format_value('value ')
end
end
class TestJQueryFormatter < FormatterTest
def setup
super Twine::Formatters::JQuery
end
def test_read_format
@formatter.read content_io('formatter_jquery.json'), 'en'
assert_translations_read_correctly
end
def test_format_file
formatter = Twine::Formatters::JQuery.new
formatter.twine_file = @twine_file
assert_equal content('formatter_jquery.json'), formatter.format_file('en')
end
def test_empty_sections_are_removed
@twine_file = build_twine_file 'en' do
add_section 'Section 1' do
end
add_section 'Section 2' do
add_definition key: 'value'
end
end
formatter = Twine::Formatters::JQuery.new
formatter.twine_file = @twine_file
refute_includes formatter.format_file('en'), ','
end
def test_format_value_with_newline
assert_equal "value\nwith\nline\nbreaks", @formatter.format_value("value\nwith\nline\nbreaks")
end
end
class TestGettextFormatter < FormatterTest
def setup
super Twine::Formatters::Gettext
end
def test_read_format
@formatter.read content_io('formatter_gettext.po'), 'en'
assert_file_contents_read_correctly
end
def test_read_with_multiple_line_value
@formatter.read content_io('gettext_multiline.po'), 'en'
assert_equal 'multiline\nstring', @empty_twine_file.definitions_by_key['key1'].translations['en']
end
def test_format_file
formatter = Twine::Formatters::Gettext.new
formatter.twine_file = @twine_file
assert_equal content('formatter_gettext.po'), formatter.format_file('en')
end
end
class TestTizenFormatter < FormatterTest
def setup
super Twine::Formatters::Tizen
end
def test_read_format
skip 'the current implementation of Tizen formatter does not support reading'
@formatter.read content_io('formatter_tizen.xml'), 'en'
assert_file_contents_read_correctly
end
def test_format_file
formatter = Twine::Formatters::Tizen.new
formatter.twine_file = @twine_file
assert_equal content('formatter_tizen.xml'), formatter.format_file('en')
end
end
class TestDjangoFormatter < FormatterTest
def setup
super Twine::Formatters::Django
end
def test_read_format
@formatter.read content_io('formatter_django.po'), 'en'
assert_file_contents_read_correctly
end
def test_format_file
formatter = Twine::Formatters::Django.new
formatter.twine_file = @twine_file
assert_equal content('formatter_django.po'), formatter.format_file('en')
end
end
class TestFlashFormatter < FormatterTest
def setup
super Twine::Formatters::Flash
end
def test_read_format
@formatter.read content_io('formatter_flash.properties'), 'en'
assert_file_contents_read_correctly
end
def test_set_translation_converts_placeholders
@formatter.set_translation_for_key 'key1', 'en', "value {#{rand(10)}}"
assert_equal 'value %@', @empty_twine_file.definitions_by_key['key1'].translations['en']
end
def test_format_file
formatter = Twine::Formatters::Flash.new
formatter.twine_file = @twine_file
assert_equal content('formatter_flash.properties'), formatter.format_file('en')
end
def test_format_value_converts_placeholders
assert_equal "value {0}", @formatter.format_value('value %d')
end
def test_deducts_language_from_resource_folder
language = %w(en de fr).sample
assert_equal language, @formatter.determine_language_given_path("locale/#{language}")
end
def test_deducts_language_and_region_from_resource_folder
assert_equal 'de-AT', @formatter.determine_language_given_path("locale/de-AT")
end
end
|