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
|
require 'command_test'
class TestConsumeLocalizationFile < CommandTest
def new_runner(language, file)
options = {}
options[:output_path] = File.join(@output_dir, file) if file
options[:input_path] = File.join(@output_dir, file) if file
FileUtils.touch options[:input_path]
options[:languages] = language if language
@twine_file = Twine::TwineFile.new
@twine_file.language_codes.concat KNOWN_LANGUAGES
Twine::Runner.new(options, @twine_file)
end
def prepare_mock_read_formatter(formatter_class)
formatter = prepare_mock_formatter(formatter_class)
formatter.expects(:read)
end
def test_deducts_android_format_from_output_path
prepare_mock_read_formatter Twine::Formatters::Android
new_runner('fr', 'fr.xml').consume_localization_file
end
def test_deducts_apple_format_from_output_path
prepare_mock_read_formatter Twine::Formatters::Apple
new_runner('fr', 'fr.strings').consume_localization_file
end
def test_deducts_jquery_format_from_output_path
prepare_mock_read_formatter Twine::Formatters::JQuery
new_runner('fr', 'fr.json').consume_localization_file
end
def test_deducts_gettext_format_from_output_path
prepare_mock_read_formatter Twine::Formatters::Gettext
new_runner('fr', 'fr.po').consume_localization_file
end
def test_deducts_language_from_input_path
random_language = KNOWN_LANGUAGES.sample
formatter = prepare_mock_formatter Twine::Formatters::Android
formatter.expects(:read).with(anything, random_language)
new_runner(nil, "#{random_language}.xml").consume_localization_file
end
class TestEncodings < CommandTest
class DummyFormatter < Twine::Formatters::Abstract
attr_reader :content
def extension
'.dummy'
end
def format_name
'dummy'
end
def read(io, lang)
@content = io.read
end
end
def new_runner(input_path, encoding = nil)
options = {}
options[:output_path] = @output_path
options[:input_path] = input_path
options[:encoding] = encoding if encoding
options[:languages] = 'en'
@twine_file = Twine::TwineFile.new
@twine_file.language_codes.concat KNOWN_LANGUAGES
Twine::Runner.new(options, @twine_file)
end
def setup
super
@expected_content = "Üß`\nda\n"
end
def test_reads_utf8
formatter = prepare_mock_formatter DummyFormatter
new_runner(fixture_path('enc_utf8.dummy')).consume_localization_file
assert_equal @expected_content, formatter.content
end
def test_reads_utf16le_bom
formatter = prepare_mock_formatter DummyFormatter
new_runner(fixture_path('enc_utf16le_bom.dummy')).consume_localization_file
assert_equal @expected_content, formatter.content
end
def test_reads_utf16be_bom
formatter = prepare_mock_formatter DummyFormatter
new_runner(fixture_path('enc_utf16be_bom.dummy')).consume_localization_file
assert_equal @expected_content, formatter.content
end
def test_reads_utf16le
formatter = prepare_mock_formatter DummyFormatter
new_runner(fixture_path('enc_utf16le.dummy'), 'UTF-16LE').consume_localization_file
assert_equal @expected_content, formatter.content
end
def test_reads_utf16be
formatter = prepare_mock_formatter DummyFormatter
new_runner(fixture_path('enc_utf16be.dummy'), 'UTF-16BE').consume_localization_file
assert_equal @expected_content, formatter.content
end
end
end
|