diff options
| author | Jack Nagel | 2013-01-23 00:26:23 -0600 |
|---|---|---|
| committer | Jack Nagel | 2013-01-26 11:37:01 -0600 |
| commit | 8f5ea8eea6dfb3da758417e2c9bdda7d2a169408 (patch) | |
| tree | 8c2791f46ae0e3d811dfc768af3b03b56332f871 /Library/Homebrew/test | |
| parent | 91f15daf73c4c30c3ccd68eeee5d512935a0685e (diff) | |
| download | homebrew-8f5ea8eea6dfb3da758417e2c9bdda7d2a169408.tar.bz2 | |
Refactor option handling internals
Currently we handle options in several ways, and it is hard to remember
what code needs an option string ("--foo"), what needs only the name
("foo") and what needs an Option object.
Now that Option objects can act as strings and be converted to JSON, we
can start using them instead of passing around strings between Formula
objects, Tab objects, and ARGV-style arrays.
The Options class is a special collection that can be queried for the
inclusion of options in any form: '--foo', 'foo', or Option.new("foo").
Diffstat (limited to 'Library/Homebrew/test')
| -rw-r--r-- | Library/Homebrew/test/test_options.rb | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Library/Homebrew/test/test_options.rb b/Library/Homebrew/test/test_options.rb new file mode 100644 index 000000000..065c00a82 --- /dev/null +++ b/Library/Homebrew/test/test_options.rb @@ -0,0 +1,90 @@ +require 'testing_env' +require 'options' + +class OptionTests < Test::Unit::TestCase + def setup + @option = Option.new("foo") + end + + def test_to_s + assert_equal "--foo", @option.to_s + end + + def test_to_str + assert_equal "--foo", @option.to_str + end + + def test_to_json + assert_equal %q{"--foo"}, @option.to_json + end + + def test_equality + foo = Option.new("foo") + bar = Option.new("bar") + assert_equal foo, @option + assert_not_equal bar, @option + assert @option.eql?(foo) + assert !@option.eql?(bar) + assert bar < foo + end + + def test_strips_leading_dashes + option = Option.new("--foo") + assert_equal "foo", option.name + assert_equal "--foo", option.flag + end + + def test_description + assert_empty @option.description + assert_equal "foo", Option.new("foo", "foo").description + end +end + +class OptionsTests < Test::Unit::TestCase + def setup + @options = Options.new + end + + def test_no_duplicate_options + @options << Option.new("foo") + @options << Option.new("foo") + assert @options.include? "--foo" + assert_equal 1, @options.count + end + + def test_include + @options << Option.new("foo") + assert @options.include? "--foo" + assert @options.include? "foo" + assert @options.include? Option.new("foo") + end + + def test_union_returns_options + assert_instance_of Options, (@options + Options.new) + end + + def test_difference_returns_options + assert_instance_of Options, (@options - Options.new) + end + + def test_shovel_returns_self + assert_same @options, (@options << Option.new("foo")) + end + + def test_as_flags + @options << Option.new("foo") + assert_equal %w{--foo}, @options.as_flags + end + + def test_to_a + option = Option.new("foo") + @options << option + assert_equal [option], @options.to_a + end + + def test_to_ary + option = Option.new("foo") + @options << option + assert_equal [option], @options.to_ary + end +end |
