-
Notifications
You must be signed in to change notification settings - Fork 8
Some tests #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+171
−1
Merged
Some tests #3
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a57f47
Some tests
BurdetteLamar e063b07
Switch from Minitest to test-unit
BurdetteLamar 58f74f0
Some tests
BurdetteLamar b21bd8f
Some tests
BurdetteLamar 76c56dc
Some tests
BurdetteLamar 2a8e2a6
Some tests
BurdetteLamar 9be4cdd
Some tests
BurdetteLamar 821d32d
Some tests
BurdetteLamar dbd1776
Some tests
BurdetteLamar 500cff5
Some tests
BurdetteLamar e105d81
Some tests
BurdetteLamar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| require "bundler/gem_tasks" | ||
| require 'bundler/gem_tasks' | ||
| require 'rake/testtask' | ||
|
|
||
| Rake::TestTask.new(:test) do |t| | ||
| t.libs << 'test' | ||
| t.libs << 'lib' | ||
| t.test_files = FileList['test/test_*.rb'] | ||
| end | ||
|
|
||
| task :default => :test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| require 'test/unit' | ||
| require 'getoptlong' | ||
|
|
||
| class TestGetoptLong < Test::Unit::TestCase | ||
|
|
||
| def verify(test_argv, expected_remaining_argv, expected_options) | ||
| # Save ARGV and replace it with a test ARGV. | ||
| argv_saved = ARGV | ||
| ARGV.replace(test_argv) | ||
| # Define options. | ||
| opts = GetoptLong.new( | ||
| ['--xxx', '-x', '--aaa', '-a', GetoptLong::REQUIRED_ARGUMENT], | ||
| ['--yyy', '-y', '--bbb', '-b', GetoptLong::OPTIONAL_ARGUMENT], | ||
| ['--zzz', '-z', '--ccc', '-c', GetoptLong::NO_ARGUMENT] | ||
| ) | ||
| opts.quiet = true | ||
| # Gather options. | ||
| actual_options = [] | ||
| opts.each do |opt, arg| | ||
| actual_options << "#{opt}: #{arg}" | ||
| end | ||
| # Save remaining test ARGV and restore original ARGV. | ||
| actual_remaining_argv = ARGV | ||
| ARGV.replace(argv_saved) | ||
| # Assert. | ||
| assert_equal(expected_remaining_argv, actual_remaining_argv, 'ARGV') | ||
| assert_equal(expected_options, actual_options, 'Options') | ||
| end | ||
|
|
||
| def test_no_options | ||
| expected_options = [] | ||
| expected_argv = %w[foo bar] | ||
| argv = %w[foo bar] | ||
| verify(argv, expected_argv, expected_options) | ||
| end | ||
|
|
||
| def test_required_argument | ||
| expected_options = [ | ||
| '--xxx: arg' | ||
| ] | ||
| expected_argv = %w[foo bar] | ||
| options = %w[--xxx --xx --x -x --aaa --aa --a -a] | ||
| options.each do |option| | ||
| argv = ['foo', option, 'arg', 'bar'] | ||
| verify(argv, expected_argv, expected_options) | ||
| end | ||
| end | ||
|
|
||
| def test_required_argument_missing | ||
| options = %w[--xxx --xx --x -x --aaa --aa --a -a] | ||
| options.each do |option| | ||
| argv = [option] | ||
| e = assert_raise(GetoptLong::MissingArgument) do | ||
| verify(argv, [], []) | ||
| end | ||
| assert_match('requires an argument', e.message) | ||
| end | ||
| end | ||
|
|
||
| def test_optional_argument | ||
| expected_options = [ | ||
| '--yyy: arg' | ||
| ] | ||
| expected_argv = %w[foo bar] | ||
| options = %w[--yyy --y --y -y --bbb --bb --b -b] | ||
| options.each do |option| | ||
| argv = ['foo', 'bar', option, 'arg'] | ||
| verify(argv, expected_argv, expected_options) | ||
| end | ||
| end | ||
|
|
||
| def test_optional_argument_missing | ||
| expected_options = [ | ||
| '--yyy: ' | ||
| ] | ||
| expected_argv = %w[foo bar] | ||
| options = %w[--yyy --y --y -y --bbb --bb --b -b] | ||
| options.each do |option| | ||
| argv = ['foo', 'bar', option] | ||
| verify(argv, expected_argv, expected_options) | ||
| end | ||
| end | ||
|
|
||
| def test_no_argument | ||
| expected_options = [ | ||
| '--zzz: ' | ||
| ] | ||
| expected_argv = %w[foo bar] | ||
| options = %w[--zzz --zz --z -z --ccc --cc --c -c] | ||
| options.each do |option| | ||
| argv = ['foo', option, 'bar'] | ||
| verify(argv, expected_argv, expected_options) | ||
| end | ||
| end | ||
|
|
||
| def test_new_with_empty_array | ||
| e = assert_raise(ArgumentError) do | ||
| GetoptLong.new([]) | ||
| end | ||
| assert_match(/no argument-flag/, e.message) | ||
| end | ||
|
|
||
| def test_new_with_bad_array | ||
| e = assert_raise(ArgumentError) do | ||
| GetoptLong.new('foo') | ||
| end | ||
| assert_match(/option list contains non-Array argument/, e.message) | ||
| end | ||
|
|
||
| def test_new_with_empty_subarray | ||
| e = assert_raise(ArgumentError) do | ||
| GetoptLong.new([[]]) | ||
| end | ||
| assert_match(/no argument-flag/, e.message) | ||
| end | ||
|
|
||
| def test_new_with_bad_subarray | ||
| e = assert_raise(ArgumentError) do | ||
| GetoptLong.new([1]) | ||
| end | ||
| assert_match(/no option name/, e.message) | ||
| end | ||
|
|
||
| def test_new_with_invalid_option | ||
| invalid_options = %w[verbose -verbose -- +] | ||
| invalid_options.each do |invalid_option| | ||
| e = assert_raise(ArgumentError, invalid_option.to_s) do | ||
| arguments = [ | ||
| [invalid_option, '-v', GetoptLong::NO_ARGUMENT] | ||
| ] | ||
| GetoptLong.new(*arguments) | ||
| end | ||
| assert_match(/invalid option/, e.message) | ||
| end | ||
| end | ||
|
|
||
| def test_new_with_invalid_alias | ||
| invalid_aliases = %w[v - -- +] | ||
| invalid_aliases.each do |invalid_alias| | ||
| e = assert_raise(ArgumentError, invalid_alias.to_s) do | ||
| arguments = [ | ||
| ['--verbose', invalid_alias, GetoptLong::NO_ARGUMENT] | ||
| ] | ||
| GetoptLong.new(*arguments) | ||
| end | ||
| assert_match(/invalid option/, e.message) | ||
| end | ||
| end | ||
|
|
||
| def test_new_with_invalid_flag | ||
| invalid_flags = ['foo'] | ||
| invalid_flags.each do |invalid_flag| | ||
| e = assert_raise(ArgumentError, invalid_flag.to_s) do | ||
| arguments = [ | ||
| ['--verbose', '-v', invalid_flag] | ||
| ] | ||
| GetoptLong.new(*arguments) | ||
| end | ||
| assert_match(/no argument-flag/, e.message) | ||
| end | ||
| end | ||
|
|
||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: could we #map over the collection?