aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/test_options.rb
blob: 250360a3071311152fe02f0948ef1c991827a4e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'testing_env'
require 'options'

class OptionTests < Homebrew::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_equality
    foo = Option.new("foo")
    bar = Option.new("bar")
    assert_equal foo, @option
    refute_equal bar, @option
    assert_eql @option, foo
    refute_eql @option, bar
  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

  def test_preserves_short_options
    option = Option.new("-d")
    assert_equal "-d", option.flag
    assert_equal "d", option.name
  end
end

class OptionsTests < Homebrew::TestCase
  def setup
    @options = Options.new
  end

  def test_no_duplicate_options
    @options << Option.new("foo")
    @options << Option.new("foo")
    assert_includes @options, "--foo"
    assert_equal 1, @options.count
  end

  def test_preserves_existing_member_when_pushing_duplicate
    a = Option.new("foo", "bar")
    b = Option.new("foo", "qux")
    @options << a << b
    assert_equal 1, @options.count
    assert_same a, @options.first
    assert_equal a.description, @options.first.description
  end

  def test_include
    @options << Option.new("foo")
    assert_includes @options, "--foo"
    assert_includes @options, "foo"
    assert_includes @options, 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

  def test_concat_array
    option = Option.new("foo")
    @options.concat([option])
    assert_includes @options, option
    assert_equal [option], @options.to_a
  end

  def test_concat_options
    option = Option.new("foo")
    opts = Options.new
    opts << option
    @options.concat(opts)
    assert_includes @options, option
    assert_equal [option], @options.to_a
  end

  def test_concat_returns_self
    assert_same @options, (@options.concat([]))
  end

  def test_intersection
    foo, bar, baz = %w{foo bar baz}.map { |o| Option.new(o) }
    options = Options.new << foo << bar
    @options << foo << baz
    assert_equal [foo], (@options & options).to_a
  end

  def test_set_union
    foo, bar, baz = %w{foo bar baz}.map { |o| Option.new(o) }
    options = Options.new << foo << bar
    @options << foo << baz
    assert_equal [foo, bar, baz].sort, (@options | options).to_a.sort
  end

  def test_coerce_with_array
    array = %w{--foo --bar}
    option1 = Option.new("foo")
    option2 = Option.new("bar")
    assert_equal [option1, option2].sort, Options.coerce(array).to_a.sort
  end

  def test_coerce_raises_for_inappropriate_types
    assert_raises(TypeError) { Options.coerce(1) }
  end

  def test_coerce_splits_multiple_switches_with_single_dash
    array = %w{-vd}
    verbose = Option.new("-v")
    debug = Option.new("-d")
    assert_equal [verbose, debug].sort, Options.coerce(array).to_a.sort
  end

  def test_copies_do_not_share_underlying_collection
    copy = @options.dup << Option.new("foo")
    assert_empty @options
    assert_equal 1, copy.count
  end
end