Cleaned up CommandTestCase. The strings file used by Runner is now injectable which eliminates the need to mock StringsFile.new (which could lead to an infinite loop).
This commit is contained in:
parent
0689b812fa
commit
559360977c
4 changed files with 20 additions and 24 deletions
|
@ -7,10 +7,10 @@ module Twine
|
|||
VALID_COMMANDS = ['generate-string-file', 'generate-all-string-files', 'consume-string-file', 'consume-all-string-files', 'generate-loc-drop', 'consume-loc-drop', 'validate-strings-file']
|
||||
|
||||
class Runner
|
||||
def initialize(args, options = nil)
|
||||
@options = options || {}
|
||||
def initialize(args, options = {}, strings = StringsFile.new)
|
||||
@args = args
|
||||
@strings = StringsFile.new
|
||||
@options = options
|
||||
@strings = strings
|
||||
end
|
||||
|
||||
def self.run(args)
|
||||
|
@ -20,13 +20,8 @@ module Twine
|
|||
def run
|
||||
# Parse all CLI arguments.
|
||||
CLI::parse_args(@args, @options)
|
||||
read_strings_data
|
||||
execute_command
|
||||
end
|
||||
|
||||
def read_strings_data
|
||||
@strings = StringsFile.new
|
||||
@strings.read @options[:strings_file]
|
||||
execute_command
|
||||
end
|
||||
|
||||
def write_strings_data(path)
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
require 'twine_test_case'
|
||||
|
||||
class CommandTestCase < TwineTestCase
|
||||
KNOWN_LANGUAGES = %w(en fr de es)
|
||||
|
||||
def prepare_mock_formatter(formatter_class)
|
||||
formatter = formatter_class.new(@mock_strings, {})
|
||||
strings = Twine::StringsFile.new
|
||||
strings.language_codes.concat KNOWN_LANGUAGES
|
||||
|
||||
formatter = formatter_class.new(strings, {})
|
||||
formatter_class.stubs(:new).returns(formatter)
|
||||
formatter
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
|
||||
@known_languages = %w(en fr de es)
|
||||
|
||||
@mock_strings = Twine::StringsFile.new
|
||||
@mock_strings.language_codes.concat @known_languages
|
||||
Twine::StringsFile.stubs(:new).returns(@mock_strings)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,10 @@ class TestConsumeStringFile < CommandTestCase
|
|||
FileUtils.touch options[:input_path]
|
||||
options[:languages] = language if language
|
||||
|
||||
Twine::Runner.new(nil, options)
|
||||
@strings = Twine::StringsFile.new
|
||||
@strings.language_codes.concat KNOWN_LANGUAGES
|
||||
|
||||
Twine::Runner.new(nil, options, @strings)
|
||||
end
|
||||
|
||||
def prepare_mock_read_file_formatter(formatter_class)
|
||||
|
@ -41,7 +44,7 @@ class TestConsumeStringFile < CommandTestCase
|
|||
end
|
||||
|
||||
def test_deducts_language_from_input_path
|
||||
random_language = @known_languages.sample
|
||||
random_language = KNOWN_LANGUAGES.sample
|
||||
formatter = prepare_mock_formatter Twine::Formatters::Android
|
||||
formatter.expects(:read_file).with(anything, random_language)
|
||||
|
||||
|
|
|
@ -6,7 +6,10 @@ class TestGenerateStringFile < CommandTestCase
|
|||
options[:output_path] = File.join(@output_dir, file) if file
|
||||
options[:languages] = language if language
|
||||
|
||||
Twine::Runner.new(nil, options)
|
||||
@strings = Twine::StringsFile.new
|
||||
@strings.language_codes.concat KNOWN_LANGUAGES
|
||||
|
||||
Twine::Runner.new(nil, options, @strings)
|
||||
end
|
||||
|
||||
def prepare_mock_write_file_formatter(formatter_class)
|
||||
|
@ -39,7 +42,7 @@ class TestGenerateStringFile < CommandTestCase
|
|||
end
|
||||
|
||||
def test_deducts_language_from_output_path
|
||||
random_language = @known_languages.sample
|
||||
random_language = KNOWN_LANGUAGES.sample
|
||||
formatter = prepare_mock_formatter Twine::Formatters::Android
|
||||
formatter.expects(:write_file).with(anything, random_language)
|
||||
|
||||
|
|
Reference in a new issue