Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/twine.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Ludwig <sebastian@lurado.de>2016-02-29 01:15:12 +0300
committerSebastian Ludwig <sebastian@lurado.de>2016-03-01 21:56:42 +0300
commit9e7e51e4c677146d0d8a7f0b479de2a7e115e52e (patch)
treef511efd1cc27a33263e7e3b40ef4318d67f068bb
parent7124b36b2dd016444a8d9ee4efd6c1e2af6f4dc3 (diff)
Moved heavily redundant file opening logic (necessary to read string files) from formatters to Runner and unified the encoding handling.
-rw-r--r--lib/twine/cli.rb2
-rw-r--r--lib/twine/encoding.rb30
-rw-r--r--lib/twine/formatters/abstract.rb4
-rw-r--r--lib/twine/formatters/android.rb40
-rw-r--r--lib/twine/formatters/apple.rb66
-rw-r--r--lib/twine/formatters/django.rb74
-rw-r--r--lib/twine/formatters/flash.rb66
-rw-r--r--lib/twine/formatters/gettext.rb49
-rw-r--r--lib/twine/formatters/jquery.rb10
-rw-r--r--lib/twine/formatters/tizen.rb54
-rw-r--r--lib/twine/runner.rb8
-rw-r--r--test/fixtures/enc_utf16be.dummybin0 -> 14 bytes
-rw-r--r--test/fixtures/enc_utf16be_bom.dummybin0 -> 16 bytes
-rw-r--r--test/fixtures/enc_utf16le.dummybin0 -> 14 bytes
-rw-r--r--test/fixtures/enc_utf16le_bom.dummybin0 -> 16 bytes
-rw-r--r--test/fixtures/enc_utf8.dummy2
-rw-r--r--test/test_consume_loc_drop.rb2
-rw-r--r--test/test_consume_string_file.rb80
-rw-r--r--test/test_formatters.rb34
-rw-r--r--test/test_strings_file.rb2
-rw-r--r--test/twine_test_case.rb9
21 files changed, 256 insertions, 276 deletions
diff --git a/lib/twine/cli.rb b/lib/twine/cli.rb
index 881ef27..5657da3 100644
--- a/lib/twine/cli.rb
+++ b/lib/twine/cli.rb
@@ -83,7 +83,7 @@ module Twine
opts.on('-c', '--consume-comments', 'Normally, when consuming a string file, Twine will ignore all comments in the file. With this flag set, any comments encountered will be read and parsed into the strings data file. This is especially useful when creating your first strings data file from an existing project.') do |c|
options[:consume_comments] = true
end
- opts.on('-e', '--encoding ENCODING', 'Twine defaults to encoding all output files in UTF-8. This flag will tell Twine to use an alternate encoding for these files. For example, you could use this to write Apple .strings files in UTF-16. This flag is currently only supported in Ruby 1.9.3 or greater.') do |e|
+ opts.on('-e', '--encoding ENCODING', 'Twine defaults to encoding all output files in UTF-8. This flag will tell Twine to use an alternate encoding for these files. For example, you could use this to write Apple .strings files in UTF-16. When reading files, Twine does its best to determine the encoding automatically. However, if the files are UTF-16 without BOM, you need to specify if it\'s UTF-16LE or UTF16-BE.') do |e|
options[:output_encoding] = e
end
opts.on('--validate', 'Validate the strings file before formatting it') do
diff --git a/lib/twine/encoding.rb b/lib/twine/encoding.rb
index d268a36..7cdd710 100644
--- a/lib/twine/encoding.rb
+++ b/lib/twine/encoding.rb
@@ -1,20 +1,22 @@
module Twine
module Encoding
- def self.encoding_for_path path
- File.open(path, 'rb') do |f|
- begin
- a = f.readbyte
- b = f.readbyte
- if (a == 0xfe && b == 0xff)
- return 'UTF-16BE'
- elsif (a == 0xff && b == 0xfe)
- return 'UTF-16LE'
- end
- rescue EOFError
- end
- end
- 'UTF-8'
+ def self.bom(path)
+ first_bytes = IO.binread(path, 2)
+ return nil unless first_bytes
+ first_bytes = first_bytes.codepoints.map.to_a
+ return 'UTF-16BE' if first_bytes == [0xFE, 0xFF]
+ return 'UTF-16LE' if first_bytes == [0xFF, 0xFE]
+ rescue EOFError
+ return nil
+ end
+
+ def self.has_bom?(path)
+ !bom(path).nil?
+ end
+
+ def self.encoding_for_path(path)
+ bom(path) || 'UTF-8'
end
end
end
diff --git a/lib/twine/formatters/abstract.rb b/lib/twine/formatters/abstract.rb
index 68d227e..012346e 100644
--- a/lib/twine/formatters/abstract.rb
+++ b/lib/twine/formatters/abstract.rb
@@ -83,8 +83,8 @@ module Twine
lang
end
- def read_file(path, lang)
- raise NotImplementedError.new("You must implement read_file in your formatter class.")
+ def read(io, lang)
+ raise NotImplementedError.new("You must implement read in your formatter class.")
end
def format_file(lang)
diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb
index 2a92ae4..448826d 100644
--- a/lib/twine/formatters/android.rb
+++ b/lib/twine/formatters/android.rb
@@ -68,7 +68,7 @@ module Twine
super(key, lang, value)
end
- def read_file(path, lang)
+ def read(io, lang)
resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m
key_regex = /<string name="(\w+)">/
comment_regex = /<!-- (.*) -->/
@@ -77,27 +77,25 @@ module Twine
value = nil
comment = nil
- File.open(path, 'r:UTF-8') do |f|
- content_match = resources_regex.match(f.read)
- if content_match
- for line in content_match[1].split(/\r?\n/)
- key_match = key_regex.match(line)
- if key_match
- key = key_match[1]
- value_match = value_regex.match(line)
- value = value_match ? value_match[1] : ""
-
- set_translation_for_key(key, lang, value)
- if comment and comment.length > 0 and !comment.start_with?("SECTION:")
- set_comment_for_key(key, comment)
- end
- comment = nil
- end
-
- comment_match = comment_regex.match(line)
- if comment_match
- comment = comment_match[1]
+ content_match = resources_regex.match(io.read)
+ if content_match
+ for line in content_match[1].split(/\r?\n/)
+ key_match = key_regex.match(line)
+ if key_match
+ key = key_match[1]
+ value_match = value_regex.match(line)
+ value = value_match ? value_match[1] : ""
+
+ set_translation_for_key(key, lang, value)
+ if comment and comment.length > 0 and !comment.start_with?("SECTION:")
+ set_comment_for_key(key, comment)
end
+ comment = nil
+ end
+
+ comment_match = comment_regex.match(line)
+ if comment_match
+ comment = comment_match[1]
end
end
end
diff --git a/lib/twine/formatters/apple.rb b/lib/twine/formatters/apple.rb
index ebd9cac..94e7a87 100644
--- a/lib/twine/formatters/apple.rb
+++ b/lib/twine/formatters/apple.rb
@@ -35,56 +35,26 @@ module Twine
"#{lang}.lproj"
end
- def read_file(path, lang)
- encoding = Twine::Encoding.encoding_for_path(path)
- sep = nil
- if !encoding.respond_to?(:encode)
- # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
- if encoding.end_with? 'LE'
- sep = "\x0a\x00"
- elsif encoding.end_with? 'BE'
- sep = "\x00\x0a"
- else
- sep = "\n"
- end
- end
-
- if encoding.index('UTF-16')
- mode = "rb:#{encoding}"
- else
- mode = "r:#{encoding}"
- end
-
- File.open(path, mode) do |f|
- last_comment = nil
- while line = (sep) ? f.gets(sep) : f.gets
- if encoding.index('UTF-16')
- if line.respond_to? :encode!
- line.encode!('UTF-8')
- else
- require 'iconv'
- line = Iconv.iconv('UTF-8', encoding, line).join
- end
- end
- match = /"((?:[^"\\]|\\.)+)"\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
- if match
- key = match[1]
- key.gsub!('\\"', '"')
- value = match[2]
- value.gsub!('\\"', '"')
- set_translation_for_key(key, lang, value)
- if last_comment
- set_comment_for_key(key, last_comment)
- end
- end
-
- match = /\/\* (.*) \*\//.match(line)
- if match
- last_comment = match[1]
- else
- last_comment = nil
+ def read(io, lang)
+ last_comment = nil
+ while line = io.gets
+ match = /"((?:[^"\\]|\\.)+)"\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
+ if match
+ key = match[1]
+ key.gsub!('\\"', '"')
+ value = match[2]
+ value.gsub!('\\"', '"')
+ set_translation_for_key(key, lang, value)
+ if last_comment
+ set_comment_for_key(key, last_comment)
end
+ end
+ match = /\/\* (.*) \*\//.match(line)
+ if match
+ last_comment = match[1]
+ else
+ last_comment = nil
end
end
end
diff --git a/lib/twine/formatters/django.rb b/lib/twine/formatters/django.rb
index d0b5083..b63657e 100644
--- a/lib/twine/formatters/django.rb
+++ b/lib/twine/formatters/django.rb
@@ -29,67 +29,35 @@ module Twine
return
end
- def read_file(path, lang)
+ def read(io, lang)
comment_regex = /#\. *"?(.*)"?$/
key_regex = /msgid *"(.*)"$/
value_regex = /msgstr *"(.*)"$/m
- encoding = Twine::Encoding.encoding_for_path(path)
- sep = nil
- if !encoding.respond_to?(:encode)
- # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
- if encoding.end_with? 'LE'
- sep = "\x0a\x00"
- elsif encoding.end_with? 'BE'
- sep = "\x00\x0a"
- else
- sep = "\n"
+ last_comment = nil
+ while line = io.gets
+ comment_match = comment_regex.match(line)
+ if comment_match
+ comment = comment_match[1]
end
- end
-
- if encoding.index('UTF-16')
- mode = "rb:#{encoding}"
- else
- mode = "r:#{encoding}"
- end
-
- File.open(path, mode) do |f|
- last_comment = nil
- while line = (sep) ? f.gets(sep) : f.gets
- if encoding.index('UTF-16')
- if line.respond_to? :encode!
- line.encode!('UTF-8')
- else
- require 'iconv'
- line = Iconv.iconv('UTF-8', encoding, line).join
- end
- end
-
- comment_match = comment_regex.match(line)
- if comment_match
- comment = comment_match[1]
- end
-
- key_match = key_regex.match(line)
- if key_match
- key = key_match[1].gsub('\\"', '"')
- end
- value_match = value_regex.match(line)
- if value_match
- value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
- end
+ key_match = key_regex.match(line)
+ if key_match
+ key = key_match[1].gsub('\\"', '"')
+ end
+ value_match = value_regex.match(line)
+ if value_match
+ value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
+ end
- if key and key.length > 0 and value and value.length > 0
- set_translation_for_key(key, lang, value)
- if comment and comment.length > 0 and !comment.start_with?("--------- ")
- set_comment_for_key(key, comment)
- end
- key = nil
- value = nil
- comment = nil
+ if key and key.length > 0 and value and value.length > 0
+ set_translation_for_key(key, lang, value)
+ if comment and comment.length > 0 and !comment.start_with?("--------- ")
+ set_comment_for_key(key, comment)
end
-
+ key = nil
+ value = nil
+ comment = nil
end
end
end
diff --git a/lib/twine/formatters/flash.rb b/lib/twine/formatters/flash.rb
index c0dbada..8cc29da 100644
--- a/lib/twine/formatters/flash.rb
+++ b/lib/twine/formatters/flash.rb
@@ -21,55 +21,25 @@ module Twine
return
end
- def read_file(path, lang)
- encoding = Twine::Encoding.encoding_for_path(path)
- sep = nil
- if !encoding.respond_to?(:encode)
- # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
- if encoding.end_with? 'LE'
- sep = "\x0a\x00"
- elsif encoding.end_with? 'BE'
- sep = "\x00\x0a"
- else
- sep = "\n"
- end
- end
-
- if encoding.index('UTF-16')
- mode = "rb:#{encoding}"
- else
- mode = "r:#{encoding}"
- end
-
- File.open(path, mode) do |f|
- last_comment = nil
- while line = (sep) ? f.gets(sep) : f.gets
- if encoding.index('UTF-16')
- if line.respond_to? :encode!
- line.encode!('UTF-8')
- else
- require 'iconv'
- line = Iconv.iconv('UTF-8', encoding, line).join
- end
+ def read(io, lang)
+ last_comment = nil
+ while line = io.gets
+ match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line)
+ if match
+ key = match[1]
+ value = match[2].strip
+ value.gsub!(/\{[0-9]\}/, '%@')
+ set_translation_for_key(key, lang, value)
+ if last_comment
+ set_comment_for_key(key, last_comment)
end
- match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line)
- if match
- key = match[1]
- value = match[2].strip
- value.gsub!(/\{[0-9]\}/, '%@')
- set_translation_for_key(key, lang, value)
- if last_comment
- set_comment_for_key(key, last_comment)
- end
- end
-
- match = /# *(.*)/.match(line)
- if match
- last_comment = match[1]
- else
- last_comment = nil
- end
-
+ end
+
+ match = /# *(.*)/.match(line)
+ if match
+ last_comment = match[1]
+ else
+ last_comment = nil
end
end
end
diff --git a/lib/twine/formatters/gettext.rb b/lib/twine/formatters/gettext.rb
index d49bc4a..71b9a84 100644
--- a/lib/twine/formatters/gettext.rb
+++ b/lib/twine/formatters/gettext.rb
@@ -31,35 +31,34 @@ module Twine
return
end
- def read_file(path, lang)
+ def read(io, lang)
comment_regex = /#.? *"(.*)"$/
key_regex = /msgctxt *"(.*)"$/
value_regex = /msgstr *"(.*)"$/m
- File.open(path, 'r:UTF-8') do |f|
- while item = f.gets("\n\n")
- key = nil
- value = nil
- comment = nil
-
- comment_match = comment_regex.match(item)
- if comment_match
- comment = comment_match[1]
- end
- key_match = key_regex.match(item)
- if key_match
- key = key_match[1].gsub('\\"', '"')
- end
- value_match = value_regex.match(item)
- if value_match
- value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
- end
- if key and key.length > 0 and value and value.length > 0
- set_translation_for_key(key, lang, value)
- if comment and comment.length > 0 and !comment.start_with?("SECTION:")
- set_comment_for_key(key, comment)
- end
- comment = nil
+
+ while item = io.gets("\n\n")
+ key = nil
+ value = nil
+ comment = nil
+
+ comment_match = comment_regex.match(item)
+ if comment_match
+ comment = comment_match[1]
+ end
+ key_match = key_regex.match(item)
+ if key_match
+ key = key_match[1].gsub('\\"', '"')
+ end
+ value_match = value_regex.match(item)
+ if value_match
+ value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
+ end
+ if key and key.length > 0 and value and value.length > 0
+ set_translation_for_key(key, lang, value)
+ if comment and comment.length > 0 and !comment.start_with?("SECTION:")
+ set_comment_for_key(key, comment)
end
+ comment = nil
end
end
end
diff --git a/lib/twine/formatters/jquery.rb b/lib/twine/formatters/jquery.rb
index badab5a..310f041 100644
--- a/lib/twine/formatters/jquery.rb
+++ b/lib/twine/formatters/jquery.rb
@@ -29,18 +29,16 @@ module Twine
return
end
- def read_file(path, lang)
+ def read(io, lang)
begin
require "json"
rescue LoadError
raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
end
- open(path) do |io|
- json = JSON.load(io)
- json.each do |key, value|
- set_translation_for_key(key, lang, value)
- end
+ json = JSON.load(io)
+ json.each do |key, value|
+ set_translation_for_key(key, lang, value)
end
end
diff --git a/lib/twine/formatters/tizen.rb b/lib/twine/formatters/tizen.rb
index c4abd99..8c9fae8 100644
--- a/lib/twine/formatters/tizen.rb
+++ b/lib/twine/formatters/tizen.rb
@@ -49,7 +49,7 @@ module Twine
return
end
- def read_file(path, lang)
+ def read(io, lang)
resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m
key_regex = /<string name="(\w+)">/
comment_regex = /<!-- (.*) -->/
@@ -58,35 +58,33 @@ module Twine
value = nil
comment = nil
- File.open(path, 'r:UTF-8') do |f|
- content_match = resources_regex.match(f.read)
- if content_match
- for line in content_match[1].split(/\r?\n/)
- key_match = key_regex.match(line)
- if key_match
- key = key_match[1]
- value_match = value_regex.match(line)
- if value_match
- value = value_match[1]
- value = CGI.unescapeHTML(value)
- value.gsub!('\\\'', '\'')
- value.gsub!('\\"', '"')
- value = convert_placeholders_from_android_to_twine(value)
- value.gsub!(/(\\u0020)*|(\\u0020)*\z/) { |spaces| ' ' * (spaces.length / 6) }
- else
- value = ""
- end
- set_translation_for_key(key, lang, value)
- if comment and comment.length > 0 and !comment.start_with?("SECTION:")
- set_comment_for_key(key, comment)
- end
- comment = nil
+ content_match = resources_regex.match(io.read)
+ if content_match
+ for line in content_match[1].split(/\r?\n/)
+ key_match = key_regex.match(line)
+ if key_match
+ key = key_match[1]
+ value_match = value_regex.match(line)
+ if value_match
+ value = value_match[1]
+ value = CGI.unescapeHTML(value)
+ value.gsub!('\\\'', '\'')
+ value.gsub!('\\"', '"')
+ value = convert_placeholders_from_android_to_twine(value)
+ value.gsub!(/(\\u0020)*|(\\u0020)*\z/) { |spaces| ' ' * (spaces.length / 6) }
+ else
+ value = ""
end
-
- comment_match = comment_regex.match(line)
- if comment_match
- comment = comment_match[1]
+ set_translation_for_key(key, lang, value)
+ if comment and comment.length > 0 and !comment.start_with?("SECTION:")
+ set_comment_for_key(key, comment)
end
+ comment = nil
+ end
+
+ comment_match = comment_regex.match(line)
+ if comment_match
+ comment = comment_match[1]
end
end
end
diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb
index 664d190..92a6c71 100644
--- a/lib/twine/runner.rb
+++ b/lib/twine/runner.rb
@@ -295,7 +295,13 @@ module Twine
end
formatter, lang = prepare_read_write(path, lang)
- formatter.read_file(path, lang)
+
+ encoding = @options[:encoding] || Twine::Encoding.encoding_for_path(path)
+
+ IO.open(IO.sysopen(path, 'rb'), 'rb', external_encoding: encoding, internal_encoding: 'UTF-8') do |io|
+ io.read(2) if Twine::Encoding.has_bom?(path)
+ formatter.read(io, lang)
+ end
end
def prepare_read_write(path, lang)
diff --git a/test/fixtures/enc_utf16be.dummy b/test/fixtures/enc_utf16be.dummy
new file mode 100644
index 0000000..c5986a0
--- /dev/null
+++ b/test/fixtures/enc_utf16be.dummy
Binary files differ
diff --git a/test/fixtures/enc_utf16be_bom.dummy b/test/fixtures/enc_utf16be_bom.dummy
new file mode 100644
index 0000000..894c71d
--- /dev/null
+++ b/test/fixtures/enc_utf16be_bom.dummy
Binary files differ
diff --git a/test/fixtures/enc_utf16le.dummy b/test/fixtures/enc_utf16le.dummy
new file mode 100644
index 0000000..d4feca3
--- /dev/null
+++ b/test/fixtures/enc_utf16le.dummy
Binary files differ
diff --git a/test/fixtures/enc_utf16le_bom.dummy b/test/fixtures/enc_utf16le_bom.dummy
new file mode 100644
index 0000000..d3333f0
--- /dev/null
+++ b/test/fixtures/enc_utf16le_bom.dummy
Binary files differ
diff --git a/test/fixtures/enc_utf8.dummy b/test/fixtures/enc_utf8.dummy
new file mode 100644
index 0000000..ea17334
--- /dev/null
+++ b/test/fixtures/enc_utf8.dummy
@@ -0,0 +1,2 @@
+Üß`
+da
diff --git a/test/test_consume_loc_drop.rb b/test/test_consume_loc_drop.rb
index 8bac0a5..deab5c0 100644
--- a/test/test_consume_loc_drop.rb
+++ b/test/test_consume_loc_drop.rb
@@ -5,7 +5,7 @@ class TestConsumeLocDrop < CommandTestCase
super
options = {}
- options[:input_path] = fixture 'consume_loc_drop.zip'
+ options[:input_path] = fixture_path 'consume_loc_drop.zip'
options[:output_path] = @output_path
options[:format] = 'apple'
diff --git a/test/test_consume_string_file.rb b/test/test_consume_string_file.rb
index bca1a03..c56781f 100644
--- a/test/test_consume_string_file.rb
+++ b/test/test_consume_string_file.rb
@@ -14,31 +14,31 @@ class TestConsumeStringFile < CommandTestCase
Twine::Runner.new(options, @strings)
end
- def prepare_mock_read_file_formatter(formatter_class)
+ def prepare_mock_read_formatter(formatter_class)
formatter = prepare_mock_formatter(formatter_class)
- formatter.expects(:read_file)
+ formatter.expects(:read)
end
def test_deducts_android_format_from_output_path
- prepare_mock_read_file_formatter Twine::Formatters::Android
+ prepare_mock_read_formatter Twine::Formatters::Android
new_runner('fr', 'fr.xml').consume_string_file
end
def test_deducts_apple_format_from_output_path
- prepare_mock_read_file_formatter Twine::Formatters::Apple
+ prepare_mock_read_formatter Twine::Formatters::Apple
new_runner('fr', 'fr.strings').consume_string_file
end
def test_deducts_jquery_format_from_output_path
- prepare_mock_read_file_formatter Twine::Formatters::JQuery
+ prepare_mock_read_formatter Twine::Formatters::JQuery
new_runner('fr', 'fr.json').consume_string_file
end
def test_deducts_gettext_format_from_output_path
- prepare_mock_read_file_formatter Twine::Formatters::Gettext
+ prepare_mock_read_formatter Twine::Formatters::Gettext
new_runner('fr', 'fr.po').consume_string_file
end
@@ -46,8 +46,74 @@ class TestConsumeStringFile < CommandTestCase
def test_deducts_language_from_input_path
random_language = KNOWN_LANGUAGES.sample
formatter = prepare_mock_formatter Twine::Formatters::Android
- formatter.expects(:read_file).with(anything, random_language)
+ formatter.expects(:read).with(anything, random_language)
new_runner(nil, "#{random_language}.xml").consume_string_file
end
+
+ class TestEncodings < CommandTestCase
+ 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'
+
+ @strings = Twine::StringsFile.new
+ @strings.language_codes.concat KNOWN_LANGUAGES
+
+ Twine::Runner.new(options, @strings)
+ 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_string_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_string_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_string_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_string_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_string_file
+ assert_equal @expected_content, formatter.content
+ end
+ end
end
diff --git a/test/test_formatters.rb b/test/test_formatters.rb
index 5fbe584..affb9ea 100644
--- a/test/test_formatters.rb
+++ b/test/test_formatters.rb
@@ -41,8 +41,8 @@ class TestAndroidFormatter < FormatterTest
super Twine::Formatters::Android
end
- def test_read_file_format
- @formatter.read_file fixture('formatter_android.xml'), 'en'
+ def test_read_format
+ @formatter.read content_io('formatter_android.xml'), 'en'
assert_file_contents_read_correctly
end
@@ -131,8 +131,8 @@ class TestAppleFormatter < FormatterTest
super Twine::Formatters::Apple
end
- def test_read_file_format
- @formatter.read_file fixture('formatter_apple.strings'), 'en'
+ def test_read_format
+ @formatter.read content_io('formatter_apple.strings'), 'en'
assert_file_contents_read_correctly
end
@@ -162,8 +162,8 @@ class TestJQueryFormatter < FormatterTest
super Twine::Formatters::JQuery
end
- def test_read_file_format
- @formatter.read_file fixture('formatter_jquery.json'), 'en'
+ def test_read_format
+ @formatter.read content_io('formatter_jquery.json'), 'en'
assert_translations_read_correctly
end
@@ -185,14 +185,14 @@ class TestGettextFormatter < FormatterTest
super Twine::Formatters::Gettext
end
- def test_read_file_format
- @formatter.read_file fixture('formatter_gettext.po'), 'en'
+ def test_read_format
+ @formatter.read content_io('formatter_gettext.po'), 'en'
assert_file_contents_read_correctly
end
- def test_read_file_with_multiple_line_value
- @formatter.read_file fixture('gettext_multiline.po'), 'en'
+ def test_read_with_multiple_line_value
+ @formatter.read content_io('gettext_multiline.po'), 'en'
assert_equal 'multiline\nstring', @strings.strings_map['key1'].translations['en']
end
@@ -211,9 +211,9 @@ class TestTizenFormatter < FormatterTest
super Twine::Formatters::Tizen
end
- def test_read_file_format
- skip 'the current implementation of Tizen formatter does not support read_file'
- @formatter.read_file fixture('formatter_tizen.xml'), 'en'
+ 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
@@ -231,8 +231,8 @@ class TestDjangoFormatter < FormatterTest
super Twine::Formatters::Django
end
- def test_read_file_format
- @formatter.read_file fixture('formatter_django.po'), 'en'
+ def test_read_format
+ @formatter.read content_io('formatter_django.po'), 'en'
assert_file_contents_read_correctly
end
@@ -249,8 +249,8 @@ class TestFlashFormatter < FormatterTest
super Twine::Formatters::Flash
end
- def test_read_file_format
- @formatter.read_file fixture('formatter_flash.properties'), 'en'
+ def test_read_format
+ @formatter.read content_io('formatter_flash.properties'), 'en'
assert_file_contents_read_correctly
end
diff --git a/test/test_strings_file.rb b/test/test_strings_file.rb
index a3b9788..be51976 100644
--- a/test/test_strings_file.rb
+++ b/test/test_strings_file.rb
@@ -6,7 +6,7 @@ class TestStringsFile < TwineTestCase
super
@strings = Twine::StringsFile.new
- @strings.read fixture('twine_accent_values.txt')
+ @strings.read fixture_path('twine_accent_values.txt')
end
def test_reading_keeps_leading_accent
diff --git a/test/twine_test_case.rb b/test/twine_test_case.rb
index f9753b3..cf4d4d9 100644
--- a/test/twine_test_case.rb
+++ b/test/twine_test_case.rb
@@ -34,12 +34,15 @@ class TwineTestCase < Minitest::Test
Twine::Runner.run(command.split(" "))
end
- def fixture(filename)
+ def fixture_path(filename)
File.join File.dirname(__FILE__), 'fixtures', filename
end
- alias :f :fixture
def content(filename)
- ERB.new(File.read fixture(filename)).result
+ ERB.new(File.read fixture_path(filename)).result
+ end
+
+ def content_io(filename)
+ StringIO.new ERB.new(File.read fixture_path(filename)).result
end
end