twine 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +56 -69
  3. data/lib/twine.rb +11 -3
  4. data/lib/twine/cli.rb +375 -155
  5. data/lib/twine/formatters.rb +0 -5
  6. data/lib/twine/formatters/abstract.rb +43 -43
  7. data/lib/twine/formatters/android.rb +58 -59
  8. data/lib/twine/formatters/apple.rb +3 -3
  9. data/lib/twine/formatters/django.rb +15 -21
  10. data/lib/twine/formatters/flash.rb +17 -20
  11. data/lib/twine/formatters/gettext.rb +11 -15
  12. data/lib/twine/formatters/jquery.rb +8 -11
  13. data/lib/twine/formatters/tizen.rb +4 -4
  14. data/lib/twine/output_processor.rb +15 -15
  15. data/lib/twine/placeholders.rb +26 -6
  16. data/lib/twine/runner.rb +95 -95
  17. data/lib/twine/{stringsfile.rb → twine_file.rb} +53 -48
  18. data/lib/twine/version.rb +1 -1
  19. data/test/{command_test_case.rb → command_test.rb} +5 -5
  20. data/test/fixtures/{consume_loc_drop.zip → consume_localization_archive.zip} +0 -0
  21. data/test/fixtures/formatter_django.po +3 -1
  22. data/test/test_abstract_formatter.rb +40 -40
  23. data/test/test_cli.rb +313 -211
  24. data/test/test_consume_localization_archive.rb +27 -0
  25. data/test/{test_consume_string_file.rb → test_consume_localization_file.rb} +19 -19
  26. data/test/test_formatters.rb +108 -43
  27. data/test/{test_generate_all_string_files.rb → test_generate_all_localization_files.rb} +18 -18
  28. data/test/{test_generate_loc_drop.rb → test_generate_localization_archive.rb} +14 -14
  29. data/test/{test_generate_string_file.rb → test_generate_localization_file.rb} +18 -18
  30. data/test/test_output_processor.rb +26 -26
  31. data/test/test_placeholders.rb +44 -9
  32. data/test/test_twine_definition.rb +111 -0
  33. data/test/test_twine_file.rb +58 -0
  34. data/test/test_validate_twine_file.rb +61 -0
  35. data/test/twine_file_dsl.rb +12 -12
  36. data/test/{twine_test_case.rb → twine_test.rb} +1 -1
  37. metadata +23 -23
  38. data/test/test_consume_loc_drop.rb +0 -27
  39. data/test/test_strings_file.rb +0 -58
  40. data/test/test_strings_row.rb +0 -47
  41. data/test/test_validate_strings_file.rb +0 -61
@@ -1,15 +1,5 @@
1
1
  module Twine
2
- class StringsSection
3
- attr_reader :name
4
- attr_reader :rows
5
-
6
- def initialize(name)
7
- @name = name
8
- @rows = []
9
- end
10
- end
11
-
12
- class StringsRow
2
+ class TwineDefinition
13
3
  attr_reader :key
14
4
  attr_accessor :comment
15
5
  attr_accessor :tags
@@ -32,34 +22,49 @@ module Twine
32
22
  @comment
33
23
  end
34
24
 
25
+ # [['tag1', 'tag2'], ['~tag3']] == (tag1 OR tag2) AND (!tag3)
35
26
  def matches_tags?(tags, include_untagged)
36
- if tags == nil || tags.empty?
37
- # The user did not specify any tags. Everything passes.
27
+ if tags == nil || tags.empty? # The user did not specify any tags. Everything passes.
38
28
  return true
39
- elsif @tags == nil
40
- # This row has no tags.
29
+ elsif @tags == nil # This definition has no tags -> check reference (if any)
41
30
  return reference ? reference.matches_tags?(tags, include_untagged) : include_untagged
42
31
  elsif @tags.empty?
43
32
  return include_untagged
44
33
  else
45
- return !(tags & @tags).empty?
34
+ return tags.all? do |set|
35
+ regular_tags, negated_tags = set.partition { |tag| tag[0] != '~' }
36
+ negated_tags.map! { |tag| tag[1..-1] }
37
+ matches_regular_tags = (!regular_tags.empty? && !(regular_tags & @tags).empty?)
38
+ matches_negated_tags = (!negated_tags.empty? && (negated_tags & @tags).empty?)
39
+ matches_regular_tags or matches_negated_tags
40
+ end
46
41
  end
47
42
 
48
43
  return false
49
44
  end
50
45
 
51
- def translated_string_for_lang(lang)
46
+ def translation_for_lang(lang)
52
47
  translation = [lang].flatten.map { |l| @translations[l] }.first
53
48
 
54
- translation = reference.translated_string_for_lang(lang) if translation.nil? && reference
49
+ translation = reference.translation_for_lang(lang) if translation.nil? && reference
55
50
 
56
51
  return translation
57
52
  end
58
53
  end
59
54
 
60
- class StringsFile
55
+ class TwineSection
56
+ attr_reader :name
57
+ attr_reader :definitions
58
+
59
+ def initialize(name)
60
+ @name = name
61
+ @definitions = []
62
+ end
63
+ end
64
+
65
+ class TwineFile
61
66
  attr_reader :sections
62
- attr_reader :strings_map
67
+ attr_reader :definitions_by_key
63
68
  attr_reader :language_codes
64
69
 
65
70
  private
@@ -73,7 +78,7 @@ module Twine
73
78
 
74
79
  def initialize
75
80
  @sections = []
76
- @strings_map = {}
81
+ @definitions_by_key = {}
77
82
  @language_codes = []
78
83
  end
79
84
 
@@ -102,7 +107,7 @@ module Twine
102
107
  File.open(path, 'r:UTF-8') do |f|
103
108
  line_num = 0
104
109
  current_section = nil
105
- current_row = nil
110
+ current_definition = nil
106
111
  while line = f.gets
107
112
  parsed = false
108
113
  line.strip!
@@ -115,20 +120,20 @@ module Twine
115
120
  if line.length > 4 && line[0, 2] == '[['
116
121
  match = /^\[\[(.+)\]\]$/.match(line)
117
122
  if match
118
- current_section = StringsSection.new(match[1])
123
+ current_section = TwineSection.new(match[1])
119
124
  @sections << current_section
120
125
  parsed = true
121
126
  end
122
127
  elsif line.length > 2 && line[0, 1] == '['
123
128
  key = match_key(line)
124
129
  if key
125
- current_row = StringsRow.new(key)
126
- @strings_map[current_row.key] = current_row
130
+ current_definition = TwineDefinition.new(key)
131
+ @definitions_by_key[current_definition.key] = current_definition
127
132
  if !current_section
128
- current_section = StringsSection.new('')
133
+ current_section = TwineSection.new('')
129
134
  @sections << current_section
130
135
  end
131
- current_section.rows << current_row
136
+ current_section.definitions << current_definition
132
137
  parsed = true
133
138
  end
134
139
  else
@@ -141,16 +146,16 @@ module Twine
141
146
 
142
147
  case key
143
148
  when 'comment'
144
- current_row.comment = value
149
+ current_definition.comment = value
145
150
  when 'tags'
146
- current_row.tags = value.split(',')
151
+ current_definition.tags = value.split(',')
147
152
  when 'ref'
148
- current_row.reference_key = value if value
153
+ current_definition.reference_key = value if value
149
154
  else
150
155
  if !@language_codes.include? key
151
156
  add_language_code(key)
152
157
  end
153
- current_row.translations[key] = value
158
+ current_definition.translations[key] = value
154
159
  end
155
160
  parsed = true
156
161
  end
@@ -163,9 +168,9 @@ module Twine
163
168
  end
164
169
 
165
170
  # resolve_references
166
- @strings_map.each do |key, row|
167
- next unless row.reference_key
168
- row.reference = @strings_map[row.reference_key]
171
+ @definitions_by_key.each do |key, definition|
172
+ next unless definition.reference_key
173
+ definition.reference = @definitions_by_key[definition.reference_key]
169
174
  end
170
175
  end
171
176
 
@@ -180,26 +185,26 @@ module Twine
180
185
 
181
186
  f.puts "[[#{section.name}]]"
182
187
 
183
- section.rows.each do |row|
184
- f.puts "\t[#{row.key}]"
188
+ section.definitions.each do |definition|
189
+ f.puts "\t[#{definition.key}]"
185
190
 
186
- value = write_value(row, dev_lang, f)
187
- if !value && !row.reference_key
188
- puts "Warning: #{row.key} does not exist in developer language '#{dev_lang}'"
191
+ value = write_value(definition, dev_lang, f)
192
+ if !value && !definition.reference_key
193
+ puts "Warning: #{definition.key} does not exist in developer language '#{dev_lang}'"
189
194
  end
190
195
 
191
- if row.reference_key
192
- f.puts "\t\tref = #{row.reference_key}"
196
+ if definition.reference_key
197
+ f.puts "\t\tref = #{definition.reference_key}"
193
198
  end
194
- if row.tags && row.tags.length > 0
195
- tag_str = row.tags.join(',')
199
+ if definition.tags && definition.tags.length > 0
200
+ tag_str = definition.tags.join(',')
196
201
  f.puts "\t\ttags = #{tag_str}"
197
202
  end
198
- if row.raw_comment and row.raw_comment.length > 0
199
- f.puts "\t\tcomment = #{row.raw_comment}"
203
+ if definition.raw_comment and definition.raw_comment.length > 0
204
+ f.puts "\t\tcomment = #{definition.raw_comment}"
200
205
  end
201
206
  @language_codes[1..-1].each do |lang|
202
- write_value(row, lang, f)
207
+ write_value(definition, lang, f)
203
208
  end
204
209
  end
205
210
  end
@@ -208,8 +213,8 @@ module Twine
208
213
 
209
214
  private
210
215
 
211
- def write_value(row, language, file)
212
- value = row.translations[language]
216
+ def write_value(definition, language, file)
217
+ value = definition.translations[language]
213
218
  return nil unless value
214
219
 
215
220
  if value[0] == ' ' || value[-1] == ' ' || (value[0] == '`' && value[-1] == '`')
@@ -1,3 +1,3 @@
1
1
  module Twine
2
- VERSION = '0.9.1'
2
+ VERSION = '0.10.0'
3
3
  end
@@ -1,12 +1,12 @@
1
- require 'twine_test_case'
1
+ require 'twine_test'
2
2
 
3
- class CommandTestCase < TwineTestCase
3
+ class CommandTest < TwineTest
4
4
  def prepare_mock_formatter(formatter_class)
5
- strings = Twine::StringsFile.new
6
- strings.language_codes.concat KNOWN_LANGUAGES
5
+ twine_file = Twine::TwineFile.new
6
+ twine_file.language_codes.concat KNOWN_LANGUAGES
7
7
 
8
8
  formatter = formatter_class.new
9
- formatter.strings = strings
9
+ formatter.twine_file = twine_file
10
10
  Twine::Formatters.formatters.clear
11
11
  Twine::Formatters.formatters << formatter
12
12
  formatter
@@ -2,7 +2,9 @@
2
2
  # Django Strings File
3
3
  # Generated by Twine <%= Twine::VERSION %>
4
4
  # Language: en
5
-
5
+ msgid ""
6
+ msgstr ""
7
+ "Content-Type: text/plain; charset=UTF-8\n"
6
8
 
7
9
  #--------- Section 1 ---------#
8
10
 
@@ -1,164 +1,164 @@
1
- require 'twine_test_case'
1
+ require 'twine_test'
2
2
 
3
- class TestAbstractFormatter < TwineTestCase
4
- class SetTranslation < TwineTestCase
3
+ class TestAbstractFormatter < TwineTest
4
+ class SetTranslation < TwineTest
5
5
  def setup
6
6
  super
7
7
 
8
- @strings = build_twine_file 'en', 'fr' do
8
+ @twine_file = build_twine_file 'en', 'fr' do
9
9
  add_section 'Section' do
10
- add_row key1: 'value1-english'
11
- add_row key2: { en: 'value2-english', fr: 'value2-french' }
10
+ add_definition key1: 'value1-english'
11
+ add_definition key2: { en: 'value2-english', fr: 'value2-french' }
12
12
  end
13
13
  end
14
14
 
15
15
  @formatter = Twine::Formatters::Abstract.new
16
- @formatter.strings = @strings
16
+ @formatter.twine_file = @twine_file
17
17
  end
18
18
 
19
19
  def test_set_translation_updates_existing_value
20
20
  @formatter.set_translation_for_key 'key1', 'en', 'value1-english-updated'
21
21
 
22
- assert_equal 'value1-english-updated', @strings.strings_map['key1'].translations['en']
22
+ assert_equal 'value1-english-updated', @twine_file.definitions_by_key['key1'].translations['en']
23
23
  end
24
24
 
25
25
  def test_set_translation_does_not_alter_other_language
26
26
  @formatter.set_translation_for_key 'key2', 'en', 'value2-english-updated'
27
27
 
28
- assert_equal 'value2-french', @strings.strings_map['key2'].translations['fr']
28
+ assert_equal 'value2-french', @twine_file.definitions_by_key['key2'].translations['fr']
29
29
  end
30
30
 
31
31
  def test_set_translation_escapes_newlines
32
32
  @formatter.set_translation_for_key 'key1', 'en', "new\nline"
33
33
 
34
- assert_equal 'new\nline', @strings.strings_map['key1'].translations['en']
34
+ assert_equal 'new\nline', @twine_file.definitions_by_key['key1'].translations['en']
35
35
  end
36
36
 
37
37
  def test_set_translation_adds_translation_to_existing_key
38
38
  @formatter.set_translation_for_key 'key1', 'fr', 'value1-french'
39
39
 
40
- assert_equal 'value1-french', @strings.strings_map['key1'].translations['fr']
40
+ assert_equal 'value1-french', @twine_file.definitions_by_key['key1'].translations['fr']
41
41
  end
42
42
 
43
43
  def test_set_translation_does_not_add_new_key
44
44
  @formatter.set_translation_for_key 'new-key', 'en', 'new-key-english'
45
45
 
46
- assert_nil @strings.strings_map['new-key']
46
+ assert_nil @twine_file.definitions_by_key['new-key']
47
47
  end
48
48
 
49
49
  def test_set_translation_consume_all_adds_new_key
50
50
  formatter = Twine::Formatters::Abstract.new
51
- formatter.strings = @strings
51
+ formatter.twine_file = @twine_file
52
52
  formatter.options = { consume_all: true }
53
53
  formatter.set_translation_for_key 'new-key', 'en', 'new-key-english'
54
54
 
55
- assert_equal 'new-key-english', @strings.strings_map['new-key'].translations['en']
55
+ assert_equal 'new-key-english', @twine_file.definitions_by_key['new-key'].translations['en']
56
56
  end
57
57
 
58
58
  def test_set_translation_consume_all_adds_tags
59
59
  random_tag = SecureRandom.uuid
60
60
  formatter = Twine::Formatters::Abstract.new
61
- formatter.strings = @strings
61
+ formatter.twine_file = @twine_file
62
62
  formatter.options = { consume_all: true, tags: [random_tag] }
63
63
  formatter.set_translation_for_key 'new-key', 'en', 'new-key-english'
64
64
 
65
- assert_equal [random_tag], @strings.strings_map['new-key'].tags
65
+ assert_equal [random_tag], @twine_file.definitions_by_key['new-key'].tags
66
66
  end
67
67
 
68
68
  def test_set_translation_adds_new_keys_to_category_uncategoriezed
69
69
  formatter = Twine::Formatters::Abstract.new
70
- formatter.strings = @strings
70
+ formatter.twine_file = @twine_file
71
71
  formatter.options = { consume_all: true }
72
72
  formatter.set_translation_for_key 'new-key', 'en', 'new-key-english'
73
73
 
74
- assert_equal 'Uncategorized', @strings.sections[0].name
75
- assert_equal 'new-key', @strings.sections[0].rows[0].key
74
+ assert_equal 'Uncategorized', @twine_file.sections[0].name
75
+ assert_equal 'new-key', @twine_file.sections[0].definitions[0].key
76
76
  end
77
77
  end
78
78
 
79
- class ValueReference < TwineTestCase
79
+ class ValueReference < TwineTest
80
80
  def setup
81
81
  super
82
82
 
83
- @strings = build_twine_file 'en', 'fr' do
83
+ @twine_file = build_twine_file 'en', 'fr' do
84
84
  add_section 'Section' do
85
- add_row refkey: 'ref-value'
86
- add_row key: :refkey
85
+ add_definition refkey: 'ref-value'
86
+ add_definition key: :refkey
87
87
  end
88
88
  end
89
89
 
90
90
  @formatter = Twine::Formatters::Abstract.new
91
- @formatter.strings = @strings
91
+ @formatter.twine_file = @twine_file
92
92
  end
93
93
 
94
94
  def test_set_translation_does_not_add_unchanged_translation
95
95
  @formatter.set_translation_for_key 'key', 'en', 'ref-value'
96
96
 
97
- assert_nil @strings.strings_map['key'].translations['en']
97
+ assert_nil @twine_file.definitions_by_key['key'].translations['en']
98
98
  end
99
99
 
100
100
  def test_set_translation_adds_changed_translation
101
101
  @formatter.set_translation_for_key 'key', 'en', 'changed value'
102
102
 
103
- assert_equal 'changed value', @strings.strings_map['key'].translations['en']
103
+ assert_equal 'changed value', @twine_file.definitions_by_key['key'].translations['en']
104
104
  end
105
105
  end
106
106
 
107
- class SetComment < TwineTestCase
107
+ class SetComment < TwineTest
108
108
  def setup
109
109
  super
110
110
 
111
- @strings = build_twine_file 'en' do
111
+ @twine_file = build_twine_file 'en' do
112
112
  add_section 'Section' do
113
- add_row key: 'value'
113
+ add_definition key: 'value'
114
114
  end
115
115
  end
116
116
  end
117
117
 
118
118
  def test_set_comment_for_key_does_not_update_comment
119
119
  formatter = Twine::Formatters::Abstract.new
120
- formatter.strings = @strings
120
+ formatter.twine_file = @twine_file
121
121
  formatter.set_comment_for_key('key', 'comment')
122
122
 
123
- assert_nil formatter.strings.strings_map['key'].comment
123
+ assert_nil formatter.twine_file.definitions_by_key['key'].comment
124
124
  end
125
125
 
126
126
  def test_set_comment_for_key_updates_comment_with_update_comments
127
127
  formatter = Twine::Formatters::Abstract.new
128
- formatter.strings = @strings
128
+ formatter.twine_file = @twine_file
129
129
  formatter.options = { consume_comments: true }
130
130
  formatter.set_comment_for_key('key', 'comment')
131
131
 
132
- assert_equal 'comment', formatter.strings.strings_map['key'].comment
132
+ assert_equal 'comment', formatter.twine_file.definitions_by_key['key'].comment
133
133
  end
134
134
  end
135
135
 
136
- class CommentReference < TwineTestCase
136
+ class CommentReference < TwineTest
137
137
  def setup
138
138
  super
139
139
 
140
- @strings = build_twine_file 'en' do
140
+ @twine_file = build_twine_file 'en' do
141
141
  add_section 'Section' do
142
- add_row refkey: 'ref-value', comment: 'reference comment'
143
- add_row key: 'value', ref: :refkey
142
+ add_definition refkey: 'ref-value', comment: 'reference comment'
143
+ add_definition key: 'value', ref: :refkey
144
144
  end
145
145
  end
146
146
 
147
147
  @formatter = Twine::Formatters::Abstract.new
148
- @formatter.strings = @strings
148
+ @formatter.twine_file = @twine_file
149
149
  @formatter.options = { consume_comments: true }
150
150
  end
151
151
 
152
152
  def test_set_comment_does_not_add_unchanged_comment
153
153
  @formatter.set_comment_for_key 'key', 'reference comment'
154
154
 
155
- assert_nil @strings.strings_map['key'].raw_comment
155
+ assert_nil @twine_file.definitions_by_key['key'].raw_comment
156
156
  end
157
157
 
158
158
  def test_set_comment_adds_changed_comment
159
159
  @formatter.set_comment_for_key 'key', 'changed comment'
160
160
 
161
- assert_equal 'changed comment', @strings.strings_map['key'].raw_comment
161
+ assert_equal 'changed comment', @twine_file.definitions_by_key['key'].raw_comment
162
162
  end
163
163
  end
164
164
 
@@ -1,10 +1,10 @@
1
- require 'twine_test_case'
1
+ require 'twine_test'
2
2
 
3
- class CLITestCase < TwineTestCase
3
+ class CLITest < TwineTest
4
4
  def setup
5
- super
5
+ super()
6
6
 
7
- @strings_file_path = File.join @output_dir, SecureRandom.uuid
7
+ @twine_file_path = File.join @output_dir, SecureRandom.uuid
8
8
  @input_path = File.join @output_dir, SecureRandom.uuid
9
9
  @input_dir = @output_dir
10
10
  end
@@ -13,284 +13,386 @@ class CLITestCase < TwineTestCase
13
13
  @options = Twine::CLI::parse command.split
14
14
  end
15
15
 
16
- class TestValidateStringsFile < CLITestCase
17
- def test_command
18
- parse "validate-strings-file #{@strings_file_path}"
16
+ def parse_with(parameters)
17
+ raise "you need to implement `parse_with` in your test class"
18
+ end
19
19
 
20
- assert_equal 'validate-strings-file', @options[:command]
21
- assert_equal @strings_file_path, @options[:strings_file]
22
- end
20
+ def assert_option_consume_all
21
+ parse_with '--consume-all'
22
+ assert @options[:consume_all]
23
+ end
23
24
 
24
- def test_pedantic
25
- parse "validate-strings-file #{@strings_file_path} --pedantic"
26
- assert @options[:pedantic]
27
- end
25
+ def assert_option_consume_comments
26
+ parse_with '--consume-comments'
27
+ assert @options[:consume_comments]
28
+ end
28
29
 
29
- def test_missing_parameter
30
- assert_raises Twine::Error do
31
- parse 'validate-strings-file'
32
- end
33
- end
30
+ def assert_option_developer_language
31
+ random_language = KNOWN_LANGUAGES.sample
32
+ parse_with "--developer-language #{random_language}"
33
+ assert_equal random_language, @options[:developer_language]
34
+ end
34
35
 
35
- def test_extra_parameter
36
- assert_raises Twine::Error do
37
- parse 'validate-strings-file strings extra'
38
- end
39
- end
36
+ def assert_option_encoding
37
+ parse_with '--encoding UTF16'
38
+ assert_equal 'UTF16', @options[:encoding]
40
39
  end
41
40
 
42
- class TestGenerateStringFile < CLITestCase
43
- def test_command
44
- parse "generate-string-file #{@strings_file_path} #{@output_path}"
41
+ def assert_option_format
42
+ random_format = Twine::Formatters.formatters.sample.format_name.downcase
43
+ parse_with "--format #{random_format}"
44
+ assert_equal random_format, @options[:format]
45
+ end
45
46
 
46
- assert_equal 'generate-string-file', @options[:command]
47
- assert_equal @strings_file_path, @options[:strings_file]
48
- assert_equal @output_path, @options[:output_path]
49
- end
47
+ def assert_option_include
48
+ random_set = [:all, :translated, :untranslated].sample
49
+ parse_with "--include #{random_set}"
50
+ assert_equal random_set, @options[:include]
51
+ end
50
52
 
51
- def test_missing_parameter
52
- assert_raises Twine::Error do
53
- parse 'generate-string-file strings'
54
- end
55
- end
53
+ def assert_option_single_language
54
+ random_language = KNOWN_LANGUAGES.sample
55
+ parse_with "--lang #{random_language}"
56
+ assert_equal [random_language], @options[:languages]
57
+ end
56
58
 
57
- def test_validate
58
- parse "generate-string-file #{@strings_file_path} #{@output_path} --validate"
59
- assert @options[:validate]
60
- end
59
+ def assert_option_multiple_languages
60
+ random_languages = KNOWN_LANGUAGES.shuffle[0, 3]
61
+ parse_with "--lang #{random_languages.join(',')}"
62
+ assert_equal random_languages.sort, @options[:languages].sort
63
+ end
61
64
 
62
- def test_extra_parameter
63
- assert_raises Twine::Error do
64
- parse 'generate-string-file strings output extra'
65
- end
66
- end
65
+ def assert_option_languages
66
+ assert_option_single_language
67
+ assert_option_multiple_languages
68
+ end
67
69
 
68
- def test_only_allows_one_language
69
- assert_raises Twine::Error do
70
- parse "generate-string-file strings output --lang en,fr"
71
- end
72
- end
70
+ def assert_option_output_path
71
+ parse_with "--output-file #{@output_path}"
72
+ assert_equal @output_path, @options[:output_path]
73
73
  end
74
74
 
75
- class TestGenerateAllStringFiles < CLITestCase
76
- def test_command
77
- parse "generate-all-string-files #{@strings_file_path} #{@output_dir}"
75
+ def assert_option_tags
76
+ # single tag
77
+ random_tag = "tag#{rand(100)}"
78
+ parse_with "--tags #{random_tag}"
79
+ assert_equal [[random_tag]], @options[:tags]
80
+
81
+ # multiple OR tags
82
+ random_tags = ["tag#{rand(100)}", "tag#{rand(100)}", "tag#{rand(100)}"]
83
+ parse_with "--tags #{random_tags.join(',')}"
84
+ sorted_tags = @options[:tags].map { |tags| tags.sort }
85
+ assert_equal [random_tags.sort], sorted_tags
86
+
87
+ # multiple AND tags
88
+ random_tag_1 = "tag#{rand(100)}"
89
+ random_tag_2 = "tag#{rand(100)}"
90
+ parse_with "--tags #{random_tag_1} --tags #{random_tag_2}"
91
+ assert_equal [[random_tag_1], [random_tag_2]], @options[:tags]
92
+
93
+ # NOT tag
94
+ random_tag = "~tag#{rand(100)}"
95
+ parse_with "--tags #{random_tag}"
96
+ assert_equal [[random_tag]], @options[:tags]
97
+ end
78
98
 
79
- assert_equal 'generate-all-string-files', @options[:command]
80
- assert_equal @strings_file_path, @options[:strings_file]
81
- assert_equal @output_dir, @options[:output_path]
82
- end
99
+ def assert_option_untagged
100
+ parse_with '--untagged'
101
+ assert @options[:untagged]
102
+ end
83
103
 
84
- def test_missing_parameter
85
- assert_raises Twine::Error do
86
- parse "generate-all-string-files strings"
87
- end
88
- end
104
+ def assert_option_validate
105
+ parse_with "--validate"
106
+ assert @options[:validate]
107
+ end
108
+ end
89
109
 
90
- def test_validate
91
- parse "generate-all-string-files #{@strings_file_path} #{@output_dir} --validate"
92
- assert @options[:validate]
110
+ class TestGenerateLocalizationFileCLI < CLITest
111
+ def parse_with(parameters)
112
+ parse "generate-localization-file #{@twine_file_path} #{@output_path} " + parameters
113
+ end
114
+
115
+ def test_command
116
+ parse_with ""
117
+
118
+ assert_equal 'generate-localization-file', @options[:command]
119
+ assert_equal @twine_file_path, @options[:twine_file]
120
+ assert_equal @output_path, @options[:output_path]
121
+ end
122
+
123
+ def test_missing_argument
124
+ assert_raises Twine::Error do
125
+ parse "generate-localization-file #{@twine_file}"
93
126
  end
127
+ end
94
128
 
95
- def test_extra_parameter
96
- assert_raises Twine::Error do
97
- parse "generate-all-string-files strings output extra"
98
- end
129
+ def test_extra_argument
130
+ assert_raises Twine::Error do
131
+ parse_with "extra"
99
132
  end
100
133
  end
101
134
 
102
- class TestConsumeStringFile < CLITestCase
103
- def test_command
104
- parse "consume-string-file #{@strings_file_path} #{@input_path}"
135
+ def test_options
136
+ assert_option_developer_language
137
+ assert_option_encoding
138
+ assert_option_format
139
+ assert_option_include
140
+ assert_option_single_language
141
+ assert_raises(Twine::Error) { assert_option_multiple_languages }
142
+ assert_option_tags
143
+ assert_option_untagged
144
+ assert_option_validate
145
+ end
146
+ end
147
+
148
+ class TestGenerateAllLocalizationFilesCLI < CLITest
149
+ def parse_with(parameters)
150
+ parse "generate-all-localization-files #{@twine_file_path} #{@output_dir} " + parameters
151
+ end
105
152
 
106
- assert_equal 'consume-string-file', @options[:command]
107
- assert_equal @strings_file_path, @options[:strings_file]
108
- assert_equal @input_path, @options[:input_path]
109
- end
153
+ def test_command
154
+ parse_with ""
110
155
 
111
- def test_missing_parameter
112
- assert_raises Twine::Error do
113
- parse "consume-string-file strings"
114
- end
115
- end
156
+ assert_equal 'generate-all-localization-files', @options[:command]
157
+ assert_equal @twine_file_path, @options[:twine_file]
158
+ assert_equal @output_dir, @options[:output_path]
159
+ end
116
160
 
117
- def test_extra_parameter
118
- assert_raises Twine::Error do
119
- parse "consume-string-file strings output extra"
120
- end
161
+ def test_missing_argument
162
+ assert_raises Twine::Error do
163
+ parse "generate-all-localization-files twine_file"
121
164
  end
165
+ end
122
166
 
123
- def test_only_allows_one_language
124
- assert_raises Twine::Error do
125
- parse "consume-string-file strings output --lang en,fr"
126
- end
167
+ def test_extra_arguemnt
168
+ assert_raises Twine::Error do
169
+ parse_with "extra"
127
170
  end
128
171
  end
129
172
 
130
- class TestConsumeAllStringFiles < CLITestCase
131
- def test_command
132
- parse "consume-all-string-files #{@strings_file_path} #{@input_dir}"
173
+ def test_options
174
+ assert_option_developer_language
175
+ assert_option_encoding
176
+ assert_option_format
177
+ assert_option_include
178
+ assert_option_tags
179
+ assert_option_untagged
180
+ assert_option_validate
181
+ end
133
182
 
134
- assert_equal 'consume-all-string-files', @options[:command]
135
- assert_equal @strings_file_path, @options[:strings_file]
136
- assert_equal @input_dir, @options[:input_path]
137
- end
183
+ def test_option_create_folders
184
+ parse_with '--create-folders'
185
+ assert @options[:create_folders]
186
+ end
138
187
 
139
- def test_missing_parameter
140
- assert_raises Twine::Error do
141
- parse "consume-all-string-files strings"
142
- end
143
- end
188
+ def test_option_file_name
189
+ random_filename = "#{rand(10000)}"
190
+ parse_with "--file-name #{random_filename}"
191
+ assert_equal random_filename, @options[:file_name]
192
+ end
193
+ end
144
194
 
145
- def test_extra_parameter
146
- assert_raises Twine::Error do
147
- parse "consume-all-string-files strings output extra"
148
- end
149
- end
195
+ class TestGenerateLocalizationArchiveCLI < CLITest
196
+ def parse_with(parameters)
197
+ parse "generate-localization-archive #{@twine_file_path} #{@output_path} --format apple " + parameters
150
198
  end
151
199
 
152
- class TestGenerateLocDrop < CLITestCase
153
- def test_command
154
- parse "generate-loc-drop #{@strings_file_path} #{@output_path} --format apple"
200
+ def test_command
201
+ parse_with ""
155
202
 
156
- assert_equal 'generate-loc-drop', @options[:command]
157
- assert_equal @strings_file_path, @options[:strings_file]
158
- assert_equal @output_path, @options[:output_path]
159
- end
203
+ assert_equal 'generate-localization-archive', @options[:command]
204
+ assert_equal @twine_file_path, @options[:twine_file]
205
+ assert_equal @output_path, @options[:output_path]
206
+ end
160
207
 
161
- def test_missing_parameter
162
- assert_raises Twine::Error do
163
- parse "generate-loc-drop strings --format apple"
164
- end
208
+ def test_missing_argument
209
+ assert_raises Twine::Error do
210
+ parse "generate-localization-archive twine_file --format apple"
165
211
  end
212
+ end
166
213
 
167
- def test_validate
168
- parse "generate-loc-drop #{@strings_file_path} #{@output_path} --format apple --validate"
169
- assert @options[:validate]
214
+ def test_extra_argument
215
+ assert_raises Twine::Error do
216
+ parse_with "extra"
170
217
  end
218
+ end
171
219
 
172
- def test_extra_parameter
173
- assert_raises Twine::Error do
174
- parse "generate-loc-drop strings output extra --format apple"
175
- end
176
- end
220
+ def test_options
221
+ assert_option_developer_language
222
+ assert_option_encoding
223
+ assert_option_include
224
+ assert_option_tags
225
+ assert_option_untagged
226
+ assert_option_validate
227
+ end
177
228
 
178
- def test_format_needed
179
- assert_raises Twine::Error do
180
- parse "generate-loc-drop strings output"
181
- end
229
+ def test_option_format_required
230
+ assert_raises Twine::Error do
231
+ parse "generate-localization-archive twine_file output"
182
232
  end
183
233
  end
184
234
 
185
- class TestConsumeLocDrop < CLITestCase
186
- def test_command
187
- parse "consume-loc-drop #{@strings_file_path} #{@input_path}"
235
+ def test_supports_deprecated_command
236
+ parse "generate-loc-drop #{@twine_file_path} #{@output_path} --format apple"
237
+ assert_equal 'generate-localization-archive', @options[:command]
238
+ end
188
239
 
189
- assert_equal 'consume-loc-drop', @options[:command]
190
- assert_equal @strings_file_path, @options[:strings_file]
191
- assert_equal @input_path, @options[:input_path]
192
- end
240
+ def test_deprecated_command_prints_warning
241
+ parse "generate-loc-drop #{@twine_file_path} #{@output_path} --format apple"
242
+ assert_match "WARNING: Twine commands names have changed.", Twine::stderr.string
243
+ end
244
+ end
193
245
 
194
- def test_missing_parameter
195
- assert_raises Twine::Error do
196
- parse "consume-loc-drop strings"
197
- end
198
- end
246
+ class TestConsumeLocalizationFileCLI < CLITest
247
+ def parse_with(parameters)
248
+ parse "consume-localization-file #{@twine_file_path} #{@input_path} " + parameters
249
+ end
199
250
 
200
- def test_extra_parameter
201
- assert_raises Twine::Error do
202
- parse "consume-loc-drop strings input extra"
203
- end
204
- end
251
+ def test_command
252
+ parse_with ""
253
+
254
+ assert_equal 'consume-localization-file', @options[:command]
255
+ assert_equal @twine_file_path, @options[:twine_file]
256
+ assert_equal @input_path, @options[:input_path]
205
257
  end
206
258
 
207
- class TestParameters < CLITestCase
208
- def parse_with(parameter)
209
- parse 'validate-strings-file input.txt ' + parameter
259
+ def test_missing_argument
260
+ assert_raises Twine::Error do
261
+ parse "consume-localization-file twine_file"
210
262
  end
263
+ end
211
264
 
212
- def test_default_options
213
- parse_with ''
214
- expected = {command: 'validate-strings-file', strings_file: 'input.txt', include: :all}
215
- assert_equal expected, @options
265
+ def test_extra_argument
266
+ assert_raises Twine::Error do
267
+ parse_with "extra"
216
268
  end
269
+ end
217
270
 
218
- def test_create_folders
219
- parse_with '--create-folders'
220
- assert @options[:create_folders]
221
- end
271
+ def test_options
272
+ assert_option_consume_all
273
+ assert_option_consume_comments
274
+ assert_option_developer_language
275
+ assert_option_encoding
276
+ assert_option_format
277
+ assert_option_single_language
278
+ assert_raises(Twine::Error) { assert_option_multiple_languages }
279
+ assert_option_output_path
280
+ assert_option_tags
281
+ end
282
+ end
222
283
 
223
- def test_consume_all
224
- parse_with '--consume-all'
225
- assert @options[:consume_all]
226
- end
284
+ class TestConsumeAllLocalizationFilesCLI < CLITest
285
+ def parse_with(parameters)
286
+ parse "consume-all-localization-files #{@twine_file_path} #{@input_dir} " + parameters
287
+ end
227
288
 
228
- def test_consume_comments
229
- parse_with '--consume-comments'
230
- assert @options[:consume_comments]
231
- end
289
+ def test_command
290
+ parse_with ""
232
291
 
233
- def test_untagged
234
- parse_with '--untagged'
235
- assert @options[:untagged]
236
- end
292
+ assert_equal 'consume-all-localization-files', @options[:command]
293
+ assert_equal @twine_file_path, @options[:twine_file]
294
+ assert_equal @input_dir, @options[:input_path]
295
+ end
237
296
 
238
- def test_developer_language
239
- random_language = KNOWN_LANGUAGES.sample
240
- parse_with "--developer-lang #{random_language}"
241
- assert_equal random_language, @options[:developer_language]
297
+ def test_missing_argument
298
+ assert_raises Twine::Error do
299
+ parse "consume-all-localization-files twine_file"
242
300
  end
301
+ end
243
302
 
244
- def test_single_language
245
- random_language = KNOWN_LANGUAGES.sample
246
- parse_with "--lang #{random_language}"
247
- assert_equal [random_language], @options[:languages]
303
+ def test_extra_argument
304
+ assert_raises Twine::Error do
305
+ parse_with "extra"
248
306
  end
307
+ end
249
308
 
250
- def test_multiple_languages
251
- random_languages = KNOWN_LANGUAGES.shuffle[0, 3]
252
- parse_with "--lang #{random_languages.join(',')}"
253
- assert_equal random_languages.sort, @options[:languages].sort
254
- end
309
+ def test_options
310
+ assert_option_consume_all
311
+ assert_option_consume_comments
312
+ assert_option_developer_language
313
+ assert_option_encoding
314
+ assert_option_format
315
+ assert_option_output_path
316
+ assert_option_tags
317
+ end
318
+ end
255
319
 
256
- def test_single_tag
257
- random_tag = "tag#{rand(100)}"
258
- parse_with "--tags #{random_tag}"
259
- assert_equal [random_tag], @options[:tags]
260
- end
320
+ class TestConsumeLocalizationArchiveCLI < CLITest
321
+ def parse_with(parameters)
322
+ parse "consume-localization-archive #{@twine_file_path} #{@input_path} " + parameters
323
+ end
261
324
 
262
- def test_multiple_tags
263
- random_tags = ([nil] * 3).map { "tag#{rand(100)}" }
264
- parse_with "--tags #{random_tags.join(',')}"
265
- assert_equal random_tags.sort, @options[:tags].sort
266
- end
325
+ def test_command
326
+ parse_with ""
267
327
 
268
- def test_format
269
- random_format = Twine::Formatters.formatters.sample.format_name.downcase
270
- parse_with "--format #{random_format}"
271
- assert_equal random_format, @options[:format]
272
- end
328
+ assert_equal 'consume-localization-archive', @options[:command]
329
+ assert_equal @twine_file_path, @options[:twine_file]
330
+ assert_equal @input_path, @options[:input_path]
331
+ end
273
332
 
274
- def test_include
275
- random_set = [:all, :translated, :untranslated].sample
276
- parse_with "--include #{random_set}"
277
- assert_equal random_set, @options[:include]
333
+ def test_missing_argument
334
+ assert_raises Twine::Error do
335
+ parse "consume-localization-archive twine_file"
278
336
  end
337
+ end
279
338
 
280
- def test_output_path
281
- parse_with "--output-file #{@output_path}"
282
- assert_equal @output_path, @options[:output_path]
339
+ def test_extra_argument
340
+ assert_raises Twine::Error do
341
+ parse_with "extra"
283
342
  end
343
+ end
344
+
345
+ def test_options
346
+ assert_option_consume_all
347
+ assert_option_consume_comments
348
+ assert_option_developer_language
349
+ assert_option_encoding
350
+ assert_option_format
351
+ assert_option_output_path
352
+ assert_option_tags
353
+ end
354
+
355
+ def test_supports_deprecated_command
356
+ parse "consume-loc-drop #{@twine_file_path} #{@input_path}"
357
+ assert_equal 'consume-localization-archive', @options[:command]
358
+ end
359
+
360
+ def test_deprecated_command_prints_warning
361
+ parse "consume-loc-drop #{@twine_file_path} #{@input_path}"
362
+ assert_match "WARNING: Twine commands names have changed.", Twine::stderr.string
363
+ end
364
+ end
365
+
366
+ class TestValidateTwineFileCLI < CLITest
367
+ def parse_with(parameters)
368
+ parse "validate-twine-file #{@twine_file_path} " + parameters
369
+ end
370
+
371
+ def test_command
372
+ parse_with ""
373
+
374
+ assert_equal 'validate-twine-file', @options[:command]
375
+ assert_equal @twine_file_path, @options[:twine_file]
376
+ end
284
377
 
285
- def test_file_name
286
- random_filename = "#{rand(10000)}"
287
- parse_with "--file-name #{random_filename}"
288
- assert_equal random_filename, @options[:file_name]
378
+ def test_missing_argument
379
+ assert_raises Twine::Error do
380
+ parse 'validate-twine-file'
289
381
  end
382
+ end
290
383
 
291
- def test_encoding
292
- parse_with '--encoding UTF16'
293
- assert_equal 'UTF16', @options[:output_encoding]
384
+ def test_extra_argument
385
+ assert_raises Twine::Error do
386
+ parse_with 'extra'
294
387
  end
295
388
  end
389
+
390
+ def test_options
391
+ assert_option_developer_language
392
+ end
393
+
394
+ def test_option_pedantic
395
+ parse "validate-twine-file #{@twine_file_path} --pedantic"
396
+ assert @options[:pedantic]
397
+ end
296
398
  end