diff options
author | Sebastian Ludwig <sebastian@lurado.de> | 2016-12-05 12:41:24 +0300 |
---|---|---|
committer | Sebastian Ludwig <sebastian@lurado.de> | 2016-12-20 02:47:14 +0300 |
commit | c619dd61e4574777a6d97f2d29de46aa39a902ce (patch) | |
tree | 4405e7db9a2115480b9db0cf8d15dc67a13241e9 /test | |
parent | b33d8425f800d0542e6c2f2aa035558d08814114 (diff) |
Improved CLI and only showing applicable options per command.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_cli.rb | 504 |
1 files changed, 289 insertions, 215 deletions
diff --git a/test/test_cli.rb b/test/test_cli.rb index 5813de2..dedef6d 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -2,7 +2,7 @@ require 'twine_test' class CLITest < TwineTest def setup - super + super() @twine_file_path = File.join @output_dir, SecureRandom.uuid @input_path = File.join @output_dir, SecureRandom.uuid @@ -13,292 +13,366 @@ class CLITest < TwineTest @options = Twine::CLI::parse command.split end - class TestValidateTwineFile < CLITest - def test_command - parse "validate-twine-file #{@twine_file_path}" + def parse_with(parameters) + raise "you need to implement `parse_with` in your test class" + end - assert_equal 'validate-twine-file', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - end + def assert_option_consume_all + parse_with '--consume-all' + assert @options[:consume_all] + end - def test_pedantic - parse "validate-twine-file #{@twine_file_path} --pedantic" - assert @options[:pedantic] - end + def assert_option_consume_comments + parse_with '--consume-comments' + assert @options[:consume_comments] + end - def test_missing_parameter - assert_raises Twine::Error do - parse 'validate-twine-file' - end - end + def assert_option_developer_language + random_language = KNOWN_LANGUAGES.sample + parse_with "--developer-language #{random_language}" + assert_equal random_language, @options[:developer_language] + end - def test_extra_parameter - assert_raises Twine::Error do - parse 'validate-twine-file twine_file extra' - end - end + def assert_option_encoding + parse_with '--encoding UTF16' + assert_equal 'UTF16', @options[:encoding] end - class TestGenerateLocalizationFile < CLITest - def test_command - parse "generate-localization-file #{@twine_file_path} #{@output_path}" + def assert_option_format + random_format = Twine::Formatters.formatters.sample.format_name.downcase + parse_with "--format #{random_format}" + assert_equal random_format, @options[:format] + end - assert_equal 'generate-localization-file', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - assert_equal @output_path, @options[:output_path] - end + def assert_option_include + random_set = [:all, :translated, :untranslated].sample + parse_with "--include #{random_set}" + assert_equal random_set, @options[:include] + end - def test_missing_parameter - assert_raises Twine::Error do - parse 'generate-localization-file twine_file' - end - end + def assert_option_single_language + random_language = KNOWN_LANGUAGES.sample + parse_with "--lang #{random_language}" + assert_equal [random_language], @options[:languages] + end - def test_validate - parse "generate-localization-file #{@twine_file_path} #{@output_path} --validate" - assert @options[:validate] - end + def assert_option_multiple_languages + random_languages = KNOWN_LANGUAGES.shuffle[0, 3] + parse_with "--lang #{random_languages.join(',')}" + assert_equal random_languages.sort, @options[:languages].sort + end - def test_extra_parameter - assert_raises Twine::Error do - parse 'generate-localization-file twine_file output extra' - end - end + def assert_option_languages + assert_option_single_language + assert_option_multiple_languages + end - def test_only_allows_one_language - assert_raises Twine::Error do - parse "generate-localization-file twine_file output --lang en,fr" - end - end + def assert_option_output_path + parse_with "--output-file #{@output_path}" + assert_equal @output_path, @options[:output_path] end - class TestGenerateAllLocalizationFiles < CLITest - def test_command - parse "generate-all-localization-files #{@twine_file_path} #{@output_dir}" + def assert_option_tags + # single tag + random_tag = "tag#{rand(100)}" + parse_with "--tags #{random_tag}" + assert_equal [[random_tag]], @options[:tags] + + # multiple OR tags + random_tags = ["tag#{rand(100)}", "tag#{rand(100)}", "tag#{rand(100)}"] + parse_with "--tags #{random_tags.join(',')}" + sorted_tags = @options[:tags].map { |tags| tags.sort } + assert_equal [random_tags.sort], sorted_tags + + # multiple AND tags + random_tag_1 = "tag#{rand(100)}" + random_tag_2 = "tag#{rand(100)}" + parse_with "--tags #{random_tag_1} --tags #{random_tag_2}" + assert_equal [[random_tag_1], [random_tag_2]], @options[:tags] + + # NOT tag + random_tag = "~tag#{rand(100)}" + parse_with "--tags #{random_tag}" + assert_equal [[random_tag]], @options[:tags] + end - assert_equal 'generate-all-localization-files', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - assert_equal @output_dir, @options[:output_path] - end + def assert_option_untagged + parse_with '--untagged' + assert @options[:untagged] + end - def test_missing_parameter - assert_raises Twine::Error do - parse "generate-all-localization-files twine_file" - end - end + def assert_option_validate + parse_with "--validate" + assert @options[:validate] + end +end + +class TestGenerateLocalizationFileCLI < CLITest + def parse_with(parameters) + parse "generate-localization-file #{@twine_file_path} #{@output_path} " + parameters + end + + def test_command + parse_with "" + + assert_equal 'generate-localization-file', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + assert_equal @output_path, @options[:output_path] + end - def test_validate - parse "generate-all-localization-files #{@twine_file_path} #{@output_dir} --validate" - assert @options[:validate] + def test_missing_argument + assert_raises Twine::Error do + parse "generate-localization-file #{@twine_file}" end + end - def test_extra_parameter - assert_raises Twine::Error do - parse "generate-all-localization-files twine_file output extra" - end + def test_extra_argument + assert_raises Twine::Error do + parse_with "extra" end end - class TestConsumeLocalizationFile < CLITest - def test_command - parse "consume-localization-file #{@twine_file_path} #{@input_path}" + def test_options + assert_option_developer_language + assert_option_encoding + assert_option_format + assert_option_include + assert_option_single_language + assert_raises(Twine::Error) { assert_option_multiple_languages } + assert_option_tags + assert_option_untagged + assert_option_validate + end +end - assert_equal 'consume-localization-file', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - assert_equal @input_path, @options[:input_path] - end +class TestGenerateAllLocalizationFilesCLI < CLITest + def parse_with(parameters) + parse "generate-all-localization-files #{@twine_file_path} #{@output_dir} " + parameters + end - def test_missing_parameter - assert_raises Twine::Error do - parse "consume-localization-file twine_file" - end - end + def test_command + parse_with "" + + assert_equal 'generate-all-localization-files', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + assert_equal @output_dir, @options[:output_path] + end - def test_extra_parameter - assert_raises Twine::Error do - parse "consume-localization-file twine_file output extra" - end + def test_missing_argument + assert_raises Twine::Error do + parse "generate-all-localization-files twine_file" end + end - def test_only_allows_one_language - assert_raises Twine::Error do - parse "consume-localization-file twine_file output --lang en,fr" - end + def test_extra_arguemnt + assert_raises Twine::Error do + parse_with "extra" end end - class TestConsumeAllLocalizationFiles < CLITest - def test_command - parse "consume-all-localization-files #{@twine_file_path} #{@input_dir}" + def test_options + assert_option_developer_language + assert_option_encoding + assert_option_format + assert_option_include + assert_option_tags + assert_option_untagged + assert_option_validate + end - assert_equal 'consume-all-localization-files', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - assert_equal @input_dir, @options[:input_path] - end + def test_option_create_folders + parse_with '--create-folders' + assert @options[:create_folders] + end - def test_missing_parameter - assert_raises Twine::Error do - parse "consume-all-localization-files twine_file" - end - end + def test_option_file_name + random_filename = "#{rand(10000)}" + parse_with "--file-name #{random_filename}" + assert_equal random_filename, @options[:file_name] + end +end - def test_extra_parameter - assert_raises Twine::Error do - parse "consume-all-localization-files twine_file output extra" - end - end +class TestGenerateLocDropCLI < CLITest + def parse_with(parameters) + parse "generate-loc-drop #{@twine_file_path} #{@output_path} --format apple " + parameters end - class TestGenerateLocDrop < CLITest - def test_command - parse "generate-loc-drop #{@twine_file_path} #{@output_path} --format apple" + def test_command + parse_with "" - assert_equal 'generate-loc-drop', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - assert_equal @output_path, @options[:output_path] - end + assert_equal 'generate-loc-drop', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + assert_equal @output_path, @options[:output_path] + end - def test_missing_parameter - assert_raises Twine::Error do - parse "generate-loc-drop twine_file --format apple" - end + def test_missing_argument + assert_raises Twine::Error do + parse "generate-loc-drop twine_file --format apple" end + end - def test_validate - parse "generate-loc-drop #{@twine_file_path} #{@output_path} --format apple --validate" - assert @options[:validate] + def test_extra_argument + assert_raises Twine::Error do + parse_with "extra" end + end - def test_extra_parameter - assert_raises Twine::Error do - parse "generate-loc-drop twine_file output extra --format apple" - end - end + def test_options + assert_option_developer_language + assert_option_encoding + assert_option_include + assert_option_tags + assert_option_untagged + assert_option_validate + end - def test_format_needed - assert_raises Twine::Error do - parse "generate-loc-drop twine_file output" - end + def test_option_format_required + assert_raises Twine::Error do + parse "generate-loc-drop twine_file output" end end +end - class TestConsumeLocDrop < CLITest - def test_command - parse "consume-loc-drop #{@twine_file_path} #{@input_path}" +class TestConsumeLocalizationFileCLI < CLITest + def parse_with(parameters) + parse "consume-localization-file #{@twine_file_path} #{@input_path} " + parameters + end - assert_equal 'consume-loc-drop', @options[:command] - assert_equal @twine_file_path, @options[:twine_file] - assert_equal @input_path, @options[:input_path] - end + def test_command + parse_with "" - def test_missing_parameter - assert_raises Twine::Error do - parse "consume-loc-drop twine_file" - end - end + assert_equal 'consume-localization-file', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + assert_equal @input_path, @options[:input_path] + end - def test_extra_parameter - assert_raises Twine::Error do - parse "consume-loc-drop twine_file input extra" - end + def test_missing_argument + assert_raises Twine::Error do + parse "consume-localization-file twine_file" end end - class TestParameters < CLITest - def parse_with(parameter) - parse 'validate-twine-file input.txt ' + parameter + def test_extra_argument + assert_raises Twine::Error do + parse_with "extra" end + end - def test_default_options - parse_with '' - expected = {command: 'validate-twine-file', twine_file: 'input.txt', include: :all} - assert_equal expected, @options - end + def test_options + assert_option_consume_all + assert_option_consume_comments + assert_option_developer_language + assert_option_encoding + assert_option_format + assert_option_single_language + assert_raises(Twine::Error) { assert_option_multiple_languages } + assert_option_output_path + assert_option_tags + end +end - def test_create_folders - parse_with '--create-folders' - assert @options[:create_folders] - end +class TestConsumeAllLocalizationFilesCLI < CLITest + def parse_with(parameters) + parse "consume-all-localization-files #{@twine_file_path} #{@input_dir} " + parameters + end - def test_consume_all - parse_with '--consume-all' - assert @options[:consume_all] - end + def test_command + parse_with "" - def test_consume_comments - parse_with '--consume-comments' - assert @options[:consume_comments] - end + assert_equal 'consume-all-localization-files', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + assert_equal @input_dir, @options[:input_path] + end - def test_untagged - parse_with '--untagged' - assert @options[:untagged] + def test_missing_argument + assert_raises Twine::Error do + parse "consume-all-localization-files twine_file" end + end - def test_developer_language - random_language = KNOWN_LANGUAGES.sample - parse_with "--developer-lang #{random_language}" - assert_equal random_language, @options[:developer_language] + def test_extra_argument + assert_raises Twine::Error do + parse_with "extra" end + end - def test_single_language - random_language = KNOWN_LANGUAGES.sample - parse_with "--lang #{random_language}" - assert_equal [random_language], @options[:languages] - end + def test_options + assert_option_consume_all + assert_option_consume_comments + assert_option_developer_language + assert_option_encoding + assert_option_format + assert_option_output_path + assert_option_tags + end +end - def test_multiple_languages - random_languages = KNOWN_LANGUAGES.shuffle[0, 3] - parse_with "--lang #{random_languages.join(',')}" - assert_equal random_languages.sort, @options[:languages].sort - end +class TestConsumeLocDropCLI < CLITest + def parse_with(parameters) + parse "consume-loc-drop #{@twine_file_path} #{@input_path} " + parameters + end - def test_single_tag - random_tag = "tag#{rand(100)}" - parse_with "--tags #{random_tag}" - assert_equal [[random_tag]], @options[:tags] - end + def test_command + parse_with "" - def test_multiple_OR_tags - random_tags = ["tag#{rand(100)}", "tag#{rand(100)}", "tag#{rand(100)}"] - parse_with "--tags #{random_tags.join(',')}" - sorted_tags = @options[:tags].map { |tags| tags.sort } - assert_equal [random_tags.sort], sorted_tags - end + assert_equal 'consume-loc-drop', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + assert_equal @input_path, @options[:input_path] + end - def test_multiple_AND_tags - random_tag_1 = "tag#{rand(100)}" - random_tag_2 = "tag#{rand(100)}" - parse_with "--tags #{random_tag_1} --tags #{random_tag_2}" - assert_equal [[random_tag_1], [random_tag_2]], @options[:tags] + def test_missing_argument + assert_raises Twine::Error do + parse "consume-loc-drop twine_file" end + end - def test_format - random_format = Twine::Formatters.formatters.sample.format_name.downcase - parse_with "--format #{random_format}" - assert_equal random_format, @options[:format] + def test_extra_argument + assert_raises Twine::Error do + parse_with "extra" end + end - def test_include - random_set = [:all, :translated, :untranslated].sample - parse_with "--include #{random_set}" - assert_equal random_set, @options[:include] - end + def test_options + assert_option_consume_all + assert_option_consume_comments + assert_option_developer_language + assert_option_encoding + assert_option_format + assert_option_output_path + assert_option_tags + end +end - def test_output_path - parse_with "--output-file #{@output_path}" - assert_equal @output_path, @options[:output_path] - end +class TestValidateTwineFileCLI < CLITest + def parse_with(parameters) + parse "validate-twine-file #{@twine_file_path} " + parameters + end + + def test_command + parse_with "" + + assert_equal 'validate-twine-file', @options[:command] + assert_equal @twine_file_path, @options[:twine_file] + end - def test_file_name - random_filename = "#{rand(10000)}" - parse_with "--file-name #{random_filename}" - assert_equal random_filename, @options[:file_name] + def test_missing_argument + assert_raises Twine::Error do + parse 'validate-twine-file' end + end - def test_encoding - parse_with '--encoding UTF16' - assert_equal 'UTF16', @options[:output_encoding] + def test_extra_argument + assert_raises Twine::Error do + parse_with 'extra' end end + + def test_options + assert_option_developer_language + end + + def test_option_pedantic + parse "validate-twine-file #{@twine_file_path} --pedantic" + assert @options[:pedantic] + end end |