Moved consume_comments evaluation from concrete formatters to Abstract. Closes #97.

This commit is contained in:
Sebastian Ludwig 2015-12-05 01:28:26 +01:00
parent 00d2dc953e
commit be18757ce7
5 changed files with 41 additions and 25 deletions

View file

@ -103,6 +103,8 @@ module Twine
end
def set_comment_for_key(key, comment)
return unless @options[:consume_comments]
if @strings.strings_map.include?(key)
row = @strings.strings_map[key]

View file

@ -74,14 +74,14 @@ module Twine
set_comment_for_key(key, last_comment)
end
end
if @options[:consume_comments]
match = /\/\* (.*) \*\//.match(line)
if match
last_comment = match[1]
else
last_comment = nil
end
match = /\/\* (.*) \*\//.match(line)
if match
last_comment = match[1]
else
last_comment = nil
end
end
end
end

View file

@ -60,14 +60,12 @@ module Twine
line = Iconv.iconv('UTF-8', encoding, line).join
end
end
if @options[:consume_comments]
comment_match = comment_regex.match(line)
if comment_match
comment = comment_match[1]
end
else
comment = nil
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('\\"', '"')

View file

@ -58,14 +58,14 @@ module Twine
set_comment_for_key(key, last_comment)
end
end
if @options[:consume_comments]
match = /# *(.*)/.match(line)
if match
last_comment = match[1]
else
last_comment = nil
end
match = /# *(.*)/.match(line)
if match
last_comment = match[1]
else
last_comment = nil
end
end
end
end

View file

@ -91,12 +91,28 @@ class TestAbstractFormatter < TwineTestCase
end
class SetComment < TwineTestCase
def setup
super
@strings = build_twine_file 'en' do
add_section 'Section' do
add_row key: 'value'
end
end
end
def test_set_comment_for_key_does_not_update_comment
skip 'not supported by current implementation - see #97'
formatter = Twine::Formatters::Abstract.new(@strings, {})
formatter.set_comment_for_key('key', 'comment')
assert_nil formatter.strings.strings_map['key'].comment
end
def test_set_comment_for_key_updates_comment_with_update_comments
skip 'not supported by current implementation - see #97'
formatter = Twine::Formatters::Abstract.new(@strings, { consume_comments: true })
formatter.set_comment_for_key('key', 'comment')
assert_equal 'comment', formatter.strings.strings_map['key'].comment
end
end
@ -104,14 +120,14 @@ class TestAbstractFormatter < TwineTestCase
def setup
super
@strings = build_twine_file 'en', 'fr' do
@strings = build_twine_file 'en' do
add_section 'Section' do
add_row refkey: 'ref-value', comment: 'reference comment'
add_row key: 'value', ref: :refkey
end
end
@formatter = Twine::Formatters::Abstract.new(@strings, {})
@formatter = Twine::Formatters::Abstract.new(@strings, { consume_comments: true })
end
def test_set_comment_does_not_add_unchanged_comment