switches.rb 0.12.1 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43cb8cbe7416baef56a0b3da428ad241fcd8b4622d926e6c64e0db0aa311d439
4
- data.tar.gz: cbd74c3f9d883474c4e207ae1974f9f8271d2fb25865cf606c48794bc1498a61
3
+ metadata.gz: 3cbd3a1b71b73ff88bff3636515686728e4f7624f24d5f8256b205070099f36c
4
+ data.tar.gz: bcfbeb1a9792d4756ec1b0c1c6956f24a4397955f28a184cd63bd7f1e242d6ef
5
5
  SHA512:
6
- metadata.gz: 06ab95f84758ab089a5ce0665ed43e5177b9fd638a0a663ade5ab52c94d587447ccd25fa890c26cd126a44b9da801bbb4aa0d1b647cc3d3423d6095cebdd64e8
7
- data.tar.gz: bdc919b1bb6d903752337f606c91cd8cde56df35185c5b3b22a54aa5e7ef69bc30ac4553a03de60ff65773f4460bc905e6f5324c8ddf01d0deec0a8a7b55a29c
6
+ metadata.gz: 31b869491dc02f0464e872157d130bea31187226536b29cfba18f20a9b5c5fde41fd302b70f5e0eb775893e6ead89faaf482174513b20d90c172f5b7b78b50e4
7
+ data.tar.gz: 7331a051152ada9e4416c124d845a39938534397d4fb7782ca47878534cde203a9cd965451289664aec6c06859eab6022c82fe30448d3afaf3f8c8accb6ccb2c
data/CHANGELOG CHANGED
@@ -1,6 +1,31 @@
1
- # CHANGELOG
1
+ # switches/CHANGELOG
2
2
 
3
- ## 20260816
3
+ ## 20260921
4
+
5
+ 0.14.0: ~ lib/Switches.rb --> lib/switches.rb, so that it works on both case sensitive and case insensitive filesystems.
6
+
7
+ 1. ~ lib/Switches.rb --> lib/switches.rb.
8
+ 2. ~ spec/all.rb --> test/switches_test.rb, - spec/.
9
+ 3. + test/Switches/VERSION_test.rb, test/gemspec_test.rb, test/loading_test.rb.
10
+ 4. ~ Rakefile: FileList['test/**/*_test.rb'] for ['spec/all.rb'], /:spec/:test/, + a version task.
11
+ 5. ~ switches.rb.gemspec: /LICENSE.txt/LICENSE/, /Dir['spec/**\/*.rb']/Dir['test/**\/*_test.rb']/, + the heading comment.
12
+ 6. ~ LICENSE.txt --> LICENSE.
13
+ 7. ~ set_switches_with_defaults: if casting == Integer for case @castings[switch] when Integer. case/when asks whether the value is an instance of the class, and @castings holds the class itself, so Integer === Integer was false, no branch was taken, and every switch declared with a cast and a default answered nil. integer!, float!, array! and regexp! alike, with or without the bang.
14
+ 8. + 6 examples for a default upon a switch which casts, one per cast and two controls. The four cast examples fail against the previous form and the two controls do not, --mailbox having no cast and a supplied value never reaching the default.
15
+ 9. ~ README.md: + a License section.
16
+ 10. ~ Switches::VERSION: /0.13.0/0.14.0/
17
+
18
+ ## 20260817
19
+
20
+ 0.13.0: Convert the specs from RSpec to Minitest.
21
+
22
+ 1. ~ spec/all.rb: Minitest spec style throughout, + require 'minitest/autorun'. The 75 examples run as 75 runs and 92 assertions, a raise checked for its message becoming two. The substitutions are tabulated in ROADMAP.md, along with the four the mechanical pass got wrong.
23
+ 2. ~ Rakefile: Rake::TestTask in place of RSpec::Core::RakeTask, pointed at spec/all.rb.
24
+ 3. ~ switches.rb.gemspec: minitest and rake as the development dependencies, in place of rake and rspec.
25
+ 4. ~ spec/all.rb: the gemspec spec which asserts those dependencies, which the conversion otherwise fails against itself.
26
+ 5. ~ CHANGELOG: - item 5 of the 0.12.0 entry, which named ROADMAP.md and the header's Todo and Ideas sections. Both went out in 0.12.1 and 0.12.0 contains neither; it was left behind when the one commit was split into two.
27
+ 6. ~ ROADMAP.md: the conversion moves from Open questions into 0.13.0's section, and 0.12.0's section is dated 20260817, which is when it was made.
28
+ 7. ~ CHANGELOG: the date headings — 0.13.0, 0.12.1 and 0.12.0 under 20260817, and + 20260816 for 0.11.0, 0.10.5 and 0.10.4, which had been left under the one heading with them.
4
29
 
5
30
  0.12.1: Move the reasoning out of the CHANGELOG and the header into a ROADMAP.
6
31
 
@@ -14,7 +39,9 @@
14
39
  2. + lib/Switches.rb: SwitchNameCollision < ArgumentError.
15
40
  3. ~ README.md: + a Notes entry for the refusal, and for the booleans and OptionParser names it does not cover.
16
41
  4. + spec/all.rb: the refusal through #set and #perform, the message, that nothing reaches the parser first, and the two legal cases.
17
- 5. + ROADMAP.md, and - the # Todo: and # Ideas: sections from the lib/Switches.rb header, which is where they were. Not listed in the gemspec, so it does not ship.
42
+
43
+
44
+ ## 20260816
18
45
 
19
46
  0.11.0: Forward to OptionParser, which #method_missing has never actually done.
20
47
 
@@ -42,6 +69,7 @@
42
69
  6. ~ lib/Switches.rb: the header's Dependencies section names ostruct, since the Standard Ruby Library no longer covers it.
43
70
  7. ~ .gitignore: + a header, + .claude, ~ /tmp/ to tmp/, and - /test/tmp/, which tmp/ now covers.
44
71
 
72
+
45
73
  ## 20260812
46
74
 
47
75
  0.10.3: Move the version into a constant, and add the standard version and gemspec tests.
@@ -78,6 +106,7 @@
78
106
  5. + LICENSE.txt: the MIT licence.
79
107
  6. + CHANGELOG: this file.
80
108
 
109
+
81
110
  ## 20180404
82
111
 
83
112
  0.9.16: Add a Gemfile.
data/README.md CHANGED
@@ -205,3 +205,7 @@ switches.class # => Hash
205
205
  3. Commit your changes (`git commit -am 'Add some feature'`)
206
206
  4. Push to the branch (`git push origin my-new-feature`)
207
207
  5. Create a new pull request
208
+
209
+ ## License
210
+
211
+ MIT
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
1
  # Rakefile
2
2
 
3
- require 'rspec/core/rake_task'
3
+ require 'rake/testtask'
4
4
 
5
- RSpec::Core::RakeTask.new(:spec) do |t|
6
- t.pattern = 'spec/all.rb'
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.test_files = FileList['test/**/*_test.rb']
7
7
  end
8
8
 
9
- task default: :spec
9
+ task default: :test
10
10
 
11
11
  desc "Show version"
12
12
  task :version do
@@ -2,5 +2,5 @@
2
2
  # Switches::VERSION
3
3
 
4
4
  class Switches
5
- VERSION = '0.12.1'
5
+ VERSION = '0.14.0'
6
6
  end
@@ -1,4 +1,4 @@
1
- # Switches.rb
1
+ # switches.rb
2
2
  # Switches
3
3
 
4
4
  # Description: Switches provides for a nice wrapper to OptionParser to also act as a store for switches supplied.
@@ -344,12 +344,16 @@ class Switches
344
344
  switches_with_defaults.each do |switch|
345
345
  if @settings.to_h[switch.to_sym].nil?
346
346
  if switches_with_castings.include?(switch)
347
+ casting = @castings[switch]
347
348
  cast_value = (
348
- case @castings[switch]
349
- when Integer; @defaults[switch].to_i
350
- when Float; @defaults[switch].to_f
351
- when Array; @defaults[switch].to_a
352
- when Regexp; Regexp.new(@defaults[switch])
349
+ if casting == Integer
350
+ @defaults[switch].to_i
351
+ elsif casting == Float
352
+ @defaults[switch].to_f
353
+ elsif casting == Array
354
+ @defaults[switch].to_a
355
+ elsif casting == Regexp
356
+ Regexp.new(@defaults[switch])
353
357
  end
354
358
  )
355
359
  @settings.send(switch + '=', cast_value)
data/switches.rb.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # switches.rb.gemspec
2
+
1
3
  require_relative './lib/Switches/VERSION'
2
4
 
3
5
  class Gem::Specification
@@ -26,14 +28,14 @@ Gem::Specification.new do |s|
26
28
  s.require_paths = ['lib']
27
29
 
28
30
  s.files = [
31
+ 'switches.rb.gemspec',
32
+ Dir['lib/**/*.rb'],
33
+ Dir['test/**/*_test.rb'],
29
34
  'CHANGELOG',
30
35
  'Gemfile',
31
- 'LICENSE.txt',
36
+ 'LICENSE',
32
37
  'Rakefile',
33
38
  'README.md',
34
- 'switches.rb.gemspec',
35
- Dir['lib/**/*.rb'],
36
- Dir['spec/**/*.rb'],
37
39
  ].flatten
38
40
 
39
41
  s.dependencies = [
@@ -41,8 +43,8 @@ Gem::Specification.new do |s|
41
43
  ]
42
44
 
43
45
  s.development_dependencies = [
44
- 'rake',
45
- 'rspec'
46
+ 'minitest',
47
+ 'rake'
46
48
  ]
47
49
 
48
50
  s.metadata = {
@@ -0,0 +1,22 @@
1
+ # test/Switches/VERSION_test.rb
2
+
3
+ require 'minitest/autorun'
4
+
5
+ require_relative '../../lib/switches'
6
+
7
+ describe Switches do
8
+ describe "VERSION" do
9
+ it "is a string" do
10
+ _(Switches::VERSION).must_be_kind_of String
11
+ end
12
+
13
+ it "is three numbers separated by dots" do
14
+ _(Switches::VERSION).must_match(/\A\d+\.\d+\.\d+\z/)
15
+ end
16
+
17
+ it "matches the newest entry in the CHANGELOG" do
18
+ changelog = File.read(File.expand_path('../../CHANGELOG', __dir__))
19
+ _(changelog[/^(\d+\.\d+\.\d+):/, 1]).must_equal Switches::VERSION
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ # test/gemspec_test.rb
2
+
3
+ require 'minitest/autorun'
4
+
5
+ require_relative '../lib/switches'
6
+
7
+ describe 'switches.rb.gemspec' do
8
+ let(:spec){Gem::Specification.load(File.expand_path('../switches.rb.gemspec', __dir__))}
9
+
10
+ it "is a valid specification" do
11
+ _(spec.validate).must_equal true
12
+ end
13
+
14
+ it "does not pin a date" do
15
+ _(spec.date).must_equal Gem::Specification.new.date
16
+ end
17
+
18
+ it "takes its version from Switches::VERSION" do
19
+ _(spec.version.to_s).must_equal Switches::VERSION
20
+ end
21
+
22
+ it "declares ostruct as its only runtime dependency" do
23
+ _(spec.runtime_dependencies.map(&:name).sort).must_equal %w{ostruct}
24
+ end
25
+
26
+ it "declares its development dependencies" do
27
+ _(spec.development_dependencies.map(&:name).sort).must_equal %w{minitest rake}
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ # test/loading_test.rb
2
+
3
+ require 'minitest/autorun'
4
+
5
+ require_relative '../lib/switches'
6
+
7
+ # In its own process, since this one has loaded the file directly and so could
8
+ # not tell what requiring the gem by its own name would bring.
9
+ describe 'loading' do
10
+ def ruby(source)
11
+ lib = File.expand_path('../lib', __dir__)
12
+ IO.popen(['ruby', "-I#{lib}", '-e', source], err: [:child, :out]){|io| io.read}.strip
13
+ end
14
+
15
+ # The gem is switches.rb, so require 'switches.rb' reads lib/switches.rb. A
16
+ # capitalised lib/Switches.rb answers that only on a case-insensitive
17
+ # filesystem, which is to say on the machine it was written on and not on
18
+ # Linux. Dir.entries reports the name as stored, so this asks exactly what a
19
+ # case-sensitive filesystem would ask.
20
+ it "carries the lowercase file the gem's name asks for" do
21
+ _(Dir.entries(File.expand_path('../lib', __dir__))).must_include 'switches.rb'
22
+ end
23
+
24
+ it "defines Switches upon requiring the gem by name" do
25
+ _(ruby('require "switches.rb"; print defined?(Switches)')).must_equal 'constant'
26
+ end
27
+
28
+ it "defines the version too" do
29
+ _(ruby('require "switches.rb"; print Switches::VERSION')).must_equal Switches::VERSION
30
+ end
31
+
32
+ it "answers to the name without its extension as well" do
33
+ _(ruby('require "switches"; print defined?(Switches)')).must_equal 'constant'
34
+ end
35
+ end
@@ -1,11 +1,13 @@
1
- # spec/all.rb
1
+ # test/switches_test.rb
2
2
 
3
- require_relative '../lib/Switches'
3
+ require 'minitest/autorun'
4
+
5
+ require_relative '../lib/switches'
4
6
 
5
7
  describe Switches do
6
8
  describe ".new" do
7
9
  it "returns an object of class Switches" do
8
- expect(Switches.new.class).to eq(Switches)
10
+ _(Switches.new.class).must_equal Switches
9
11
  end
10
12
  end
11
13
 
@@ -21,40 +23,40 @@ describe Switches do
21
23
  end
22
24
 
23
25
  it "sets a short boolean switch to true when supplied with a short switch" do
24
- expect(build('-v').v?).to eq(true)
26
+ _(build('-v').v?).must_equal true
25
27
  end
26
28
 
27
29
  it "sets a long boolean switch to true when supplied with a short switch" do
28
- expect(build('-v').verbose?).to eq(true)
30
+ _(build('-v').verbose?).must_equal true
29
31
  end
30
32
 
31
33
  it "sets a short boolean switch to true when supplied with a long switch" do
32
- expect(build('--verbose').v?).to eq(true)
34
+ _(build('--verbose').v?).must_equal true
33
35
  end
34
36
 
35
37
  it "sets a long boolean switch to true when supplied with a long switch" do
36
- expect(build('--verbose').verbose?).to eq(true)
38
+ _(build('--verbose').verbose?).must_equal true
37
39
  end
38
40
 
39
41
  it "sets a short switch to the value supplied when supplied with a short switch" do
40
- expect(build('-p 22').p).to eq('22')
42
+ _(build('-p 22').p).must_equal '22'
41
43
  end
42
44
 
43
45
  it "sets a long switch to the value supplied when supplied with a short switch" do
44
- expect(build('-p 22').port).to eq('22')
46
+ _(build('-p 22').port).must_equal '22'
45
47
  end
46
48
 
47
49
  it "sets a short switch to the value supplied when supplied with a long switch" do
48
- expect(build('--port 22').p).to eq('22')
50
+ _(build('--port 22').p).must_equal '22'
49
51
  end
50
52
 
51
53
  it "sets a long switch to the value supplied when supplied with a long switch" do
52
- expect(build('--port 22').port).to eq('22')
54
+ _(build('--port 22').port).must_equal '22'
53
55
  end
54
56
 
55
57
  it "doesn't matter in which order the short and long switches are given" do
56
- expect(build('-h example.com').h).to eq('example.com')
57
- expect(build('--host example.com').h).to eq('example.com')
58
+ _(build('-h example.com').h).must_equal 'example.com'
59
+ _(build('--host example.com').h).must_equal 'example.com'
58
60
  end
59
61
  end
60
62
 
@@ -68,27 +70,27 @@ describe Switches do
68
70
  end
69
71
 
70
72
  it "sets a short switch to the argument supplied with a short switch" do
71
- expect(build('-p 22').p).to eq('22')
73
+ _(build('-p 22').p).must_equal '22'
72
74
  end
73
75
 
74
76
  it "sets a long switch to the argument supplied with a short switch" do
75
- expect(build('-p 22').port).to eq('22')
77
+ _(build('-p 22').port).must_equal '22'
76
78
  end
77
79
 
78
80
  it "sets a short switch to the argument supplied with a long switch" do
79
- expect(build('--port 22').p).to eq('22')
81
+ _(build('--port 22').p).must_equal '22'
80
82
  end
81
83
 
82
84
  it "sets a long switch to the argument supplied with a long switch" do
83
- expect(build('--port 22').port).to eq('22')
85
+ _(build('--port 22').port).must_equal '22'
84
86
  end
85
87
 
86
88
  it "raises OptionParser::MissingArgument when no argument is supplied to a short switch" do
87
- expect{build('-p')}.to raise_error(OptionParser::MissingArgument)
89
+ _{build('-p')}.must_raise OptionParser::MissingArgument
88
90
  end
89
91
 
90
92
  it "raises OptionParser::MissingArgument when no argument is supplied to a long switch" do
91
- expect{build('--port')}.to raise_error(OptionParser::MissingArgument)
93
+ _{build('--port')}.must_raise OptionParser::MissingArgument
92
94
  end
93
95
  end
94
96
 
@@ -102,27 +104,28 @@ describe Switches do
102
104
  end
103
105
 
104
106
  it "sets a short switch to the argument supplied with a short switch" do
105
- expect(build('-p 22').p).to eq('22')
107
+ _(build('-p 22').p).must_equal '22'
106
108
  end
107
109
 
108
110
  it "sets a long switch to the argument supplied with a short switch" do
109
- expect(build('-p 22').port).to eq('22')
111
+ _(build('-p 22').port).must_equal '22'
110
112
  end
111
113
 
112
114
  it "sets a short switch to the argument supplied with a long switch" do
113
- expect(build('--port 22').p).to eq('22')
115
+ _(build('--port 22').p).must_equal '22'
114
116
  end
115
117
 
116
118
  it "sets a long switch to the argument supplied with a long switch" do
117
- expect(build('--port 22').port).to eq('22')
119
+ _(build('--port 22').port).must_equal '22'
118
120
  end
119
121
 
120
122
  it "raises RequiredSwitchMissing when no switch is supplied" do
121
- expect{build}.to raise_error(RequiredSwitchMissing)
123
+ _{build}.must_raise RequiredSwitchMissing
122
124
  end
123
125
 
124
126
  it "names every missing switch in the RequiredSwitchMissing message" do
125
- expect{build}.to raise_error(RequiredSwitchMissing, /-p.*--port/m)
127
+ error = _{build}.must_raise RequiredSwitchMissing
128
+ _(error.message).must_match(/-p.*--port/m)
126
129
  end
127
130
  end
128
131
 
@@ -136,31 +139,31 @@ describe Switches do
136
139
  end
137
140
 
138
141
  it "sets a short switch to the argument supplied with a short switch" do
139
- expect(build('-p 22').p).to eq('22')
142
+ _(build('-p 22').p).must_equal '22'
140
143
  end
141
144
 
142
145
  it "sets a long switch to the argument supplied with a short switch" do
143
- expect(build('-p 22').port).to eq('22')
146
+ _(build('-p 22').port).must_equal '22'
144
147
  end
145
148
 
146
149
  it "sets a short switch to the argument supplied with a long switch" do
147
- expect(build('--port 22').p).to eq('22')
150
+ _(build('--port 22').p).must_equal '22'
148
151
  end
149
152
 
150
153
  it "sets a long switch to the argument supplied with a long switch" do
151
- expect(build('--port 22').port).to eq('22')
154
+ _(build('--port 22').port).must_equal '22'
152
155
  end
153
156
 
154
157
  it "raises OptionParser::MissingArgument when no argument is supplied to a short switch" do
155
- expect{build('-p')}.to raise_error(OptionParser::MissingArgument)
158
+ _{build('-p')}.must_raise OptionParser::MissingArgument
156
159
  end
157
160
 
158
161
  it "raises OptionParser::MissingArgument when no argument is supplied to a long switch" do
159
- expect{build('--port')}.to raise_error(OptionParser::MissingArgument)
162
+ _{build('--port')}.must_raise OptionParser::MissingArgument
160
163
  end
161
164
 
162
165
  it "raises RequiredSwitchMissing when no switch is supplied" do
163
- expect{build}.to raise_error(RequiredSwitchMissing)
166
+ _{build}.must_raise RequiredSwitchMissing
164
167
  end
165
168
  end
166
169
 
@@ -171,41 +174,44 @@ describe Switches do
171
174
  end
172
175
 
173
176
  it "refuses a switch named after a core method" do
174
- expect{build{|s| s.set(:tap)}}.to raise_error(SwitchNameCollision, /Kernel#tap/)
177
+ error = _{build{|s| s.set(:tap)}}.must_raise SwitchNameCollision
178
+ _(error.message).must_match(/Kernel#tap/)
175
179
  end
176
180
 
177
181
  it "refuses a switch named after a Switches method" do
178
- expect{build{|s| s.set(:settings)}}.to raise_error(SwitchNameCollision, /Switches#settings/)
182
+ error = _{build{|s| s.set(:settings)}}.must_raise SwitchNameCollision
183
+ _(error.message).must_match(/Switches#settings/)
179
184
  end
180
185
 
181
186
  it "refuses a colliding name declared through #perform" do
182
- expect{build{|s| s.perform(:display){}}}.to raise_error(SwitchNameCollision)
187
+ _{build{|s| s.perform(:display){}}}.must_raise SwitchNameCollision
183
188
  end
184
189
 
185
190
  it "names the switch in the form it would be supplied in" do
186
- expect{build{|s| s.set(:instance_variable_get)}}.to raise_error(SwitchNameCollision, /--instance_variable_get/)
191
+ error = _{build{|s| s.set(:instance_variable_get)}}.must_raise SwitchNameCollision
192
+ _(error.message).must_match(/--instance_variable_get/)
187
193
  end
188
194
 
189
195
  it "raises before the switch is declared to the parser" do
190
196
  switches = Switches.new
191
197
  begin; switches.set(:tap); rescue SwitchNameCollision; end
192
- expect(switches.to_h).to eq({})
198
+ _(switches.to_h).must_equal({})
193
199
  end
194
200
 
195
201
  it "accepts the name as a boolean, the accessor then carrying a question mark" do
196
202
  ARGV.clear
197
203
  ARGV << '--tap'
198
- expect(Switches.new{|s| s.boolean(:tap)}.tap?).to eq(true)
204
+ _(Switches.new{|s| s.boolean(:tap)}.tap?).must_equal true
199
205
  end
200
206
 
201
207
  it "accepts a name OptionParser has, a declared switch winning over the parser" do
202
208
  ARGV.clear
203
209
  %w{--banner text}.each{|a| ARGV << a}
204
- expect(Switches.new{|s| s.set(:banner)}.banner).to eq('text')
210
+ _(Switches.new{|s| s.set(:banner)}.banner).must_equal 'text'
205
211
  end
206
212
 
207
213
  it "raises a SwitchNameCollision, which is an ArgumentError" do
208
- expect(SwitchNameCollision.ancestors).to include(ArgumentError)
214
+ _(SwitchNameCollision.ancestors).must_include ArgumentError
209
215
  end
210
216
  end
211
217
 
@@ -216,47 +222,47 @@ describe Switches do
216
222
  end
217
223
 
218
224
  it "forwards a writer to the OptionParser" do
219
- expect(build{|s| s.banner = 'Usage: tap-audit [options]'}.help).to match(/\AUsage: tap-audit \[options\]/)
225
+ _(build{|s| s.banner = 'Usage: tap-audit [options]'}.help).must_match(/\AUsage: tap-audit \[options\]/)
220
226
  end
221
227
 
222
228
  it "forwards a method taking an argument to the OptionParser" do
223
- expect(build{|s| s.separator 'Options:'}.help).to match(/^Options:$/)
229
+ _(build{|s| s.separator 'Options:'}.help).must_match(/^Options:$/)
224
230
  end
225
231
 
226
232
  it "forwards a reader to the OptionParser" do
227
233
  switches = build{|s| s.program_name = 'tap-audit'}
228
- expect(switches.program_name).to eq('tap-audit')
229
- expect(switches.help).to match(/\AUsage: tap-audit/)
234
+ _(switches.program_name).must_equal 'tap-audit'
235
+ _(switches.help).must_match(/\AUsage: tap-audit/)
230
236
  end
231
237
 
232
238
  it "prefers a declared switch to the OptionParser method of the same name" do
233
239
  ARGV.clear
234
240
  %w{--version 2.1}.each{|a| ARGV << a}
235
- expect(Switches.new{|s| s.set(:version)}.version).to eq('2.1')
241
+ _(Switches.new{|s| s.set(:version)}.version).must_equal '2.1'
236
242
  end
237
243
 
238
244
  it "prefers a declared switch when it is written to as well" do
239
245
  switches = build{|s| s.set(:banner)}
240
246
  switches.banner = 'a setting, not the banner'
241
- expect(switches.to_h[:banner]).to eq('a setting, not the banner')
247
+ _(switches.to_h[:banner]).must_equal 'a setting, not the banner'
242
248
  end
243
249
 
244
250
  it "falls through to the settings for a name which is neither" do
245
- expect(build{|s| s.set(:port)}.nonesuch).to eq(nil)
251
+ _(build{|s| s.set(:port)}.nonesuch).must_be_nil
246
252
  end
247
253
 
248
254
  it "does not forward #to_a, which OptionParser answers with its help" do
249
- expect(build{|s| s.set(:port)}.to_a).to eq(nil)
255
+ _(build{|s| s.set(:port)}.to_a).must_be_nil
250
256
  end
251
257
 
252
258
  it "leaves Array() with the switches rather than the parser's help" do
253
259
  switches = build{|s| s.set(:port)}
254
- expect(Array(switches)).to eq([switches])
260
+ _(Array(switches)).must_equal [switches]
255
261
  end
256
262
 
257
263
  it "leaves the splat with the switches rather than the parser's help" do
258
264
  switches = build{|s| s.set(:port)}
259
- expect([*switches]).to eq([switches])
265
+ _([*switches]).must_equal [switches]
260
266
  end
261
267
  end
262
268
 
@@ -271,23 +277,23 @@ describe Switches do
271
277
  end
272
278
 
273
279
  it "sets a boolean switch supplied in its underscored form" do
274
- expect(build('--update_template').update_template?).to eq(true)
280
+ _(build('--update_template').update_template?).must_equal true
275
281
  end
276
282
 
277
283
  it "sets a boolean switch supplied in its hyphenated form" do
278
- expect(build('--update-template').update_template?).to eq(true)
284
+ _(build('--update-template').update_template?).must_equal true
279
285
  end
280
286
 
281
287
  it "sets a switch to the argument supplied in its underscored form" do
282
- expect(build('--access_token abc123').access_token).to eq('abc123')
288
+ _(build('--access_token abc123').access_token).must_equal 'abc123'
283
289
  end
284
290
 
285
291
  it "sets a switch to the argument supplied in its hyphenated form" do
286
- expect(build('--access-token abc123').access_token).to eq('abc123')
292
+ _(build('--access-token abc123').access_token).must_equal 'abc123'
287
293
  end
288
294
 
289
295
  it "names the setting for the symbol declared, whichever form is supplied" do
290
- expect(build('--update-template --access-token abc123').to_h).to eq({update_template?: true, access_token: 'abc123'})
296
+ _(build('--update-template --access-token abc123').to_h).must_equal({update_template?: true, access_token: 'abc123'})
291
297
  end
292
298
  end
293
299
 
@@ -301,19 +307,19 @@ describe Switches do
301
307
  end
302
308
 
303
309
  it "sets the switch to the argument supplied in its underscored form" do
304
- expect(build('--access_token abc123').access_token).to eq('abc123')
310
+ _(build('--access_token abc123').access_token).must_equal 'abc123'
305
311
  end
306
312
 
307
313
  it "sets the switch to the argument supplied in its hyphenated form" do
308
- expect(build('--access-token abc123').access_token).to eq('abc123')
314
+ _(build('--access-token abc123').access_token).must_equal 'abc123'
309
315
  end
310
316
 
311
317
  it "raises OptionParser::MissingArgument when no argument is supplied to the underscored form" do
312
- expect{build('--access_token')}.to raise_error(OptionParser::MissingArgument)
318
+ _{build('--access_token')}.must_raise OptionParser::MissingArgument
313
319
  end
314
320
 
315
321
  it "raises OptionParser::MissingArgument when no argument is supplied to the hyphenated form" do
316
- expect{build('--access-token')}.to raise_error(OptionParser::MissingArgument)
322
+ _{build('--access-token')}.must_raise OptionParser::MissingArgument
317
323
  end
318
324
  end
319
325
 
@@ -327,19 +333,20 @@ describe Switches do
327
333
  end
328
334
 
329
335
  it "counts the underscored form as supplied" do
330
- expect(build('--access_token abc123').access_token).to eq('abc123')
336
+ _(build('--access_token abc123').access_token).must_equal 'abc123'
331
337
  end
332
338
 
333
339
  it "counts the hyphenated form as supplied" do
334
- expect(build('--access-token abc123').access_token).to eq('abc123')
340
+ _(build('--access-token abc123').access_token).must_equal 'abc123'
335
341
  end
336
342
 
337
343
  it "raises RequiredSwitchMissing when neither form is supplied" do
338
- expect{build}.to raise_error(RequiredSwitchMissing)
344
+ _{build}.must_raise RequiredSwitchMissing
339
345
  end
340
346
 
341
347
  it "names the hyphenated form in the RequiredSwitchMissing message" do
342
- expect{build}.to raise_error(RequiredSwitchMissing, /--access-token/)
348
+ error = _{build}.must_raise RequiredSwitchMissing
349
+ _(error.message).must_match(/--access-token/)
343
350
  end
344
351
  end
345
352
 
@@ -353,23 +360,23 @@ describe Switches do
353
360
  it "calls the block when supplied with a short switch" do
354
361
  called = false
355
362
  build('-a'){called = true}
356
- expect(called).to eq(true)
363
+ _(called).must_equal true
357
364
  end
358
365
 
359
366
  it "calls the block when supplied with a long switch" do
360
367
  called = false
361
368
  build('--action'){called = true}
362
- expect(called).to eq(true)
369
+ _(called).must_equal true
363
370
  end
364
371
 
365
372
  it "doesn't call the block when the switch isn't supplied" do
366
373
  called = false
367
374
  build{called = true}
368
- expect(called).to eq(false)
375
+ _(called).must_equal false
369
376
  end
370
377
 
371
378
  it "doesn't require an argument" do
372
- expect{build('-a'){}}.not_to raise_error
379
+ _(build('-a'){}).must_be_kind_of Switches
373
380
  end
374
381
  end
375
382
 
@@ -383,64 +390,60 @@ describe Switches do
383
390
  it "passes the argument to the block when supplied with a short switch" do
384
391
  argument = nil
385
392
  build('-a value'){|o| argument = o}
386
- expect(argument).to eq('value')
393
+ _(argument).must_equal 'value'
387
394
  end
388
395
 
389
396
  it "passes the argument to the block when supplied with a long switch" do
390
397
  argument = nil
391
398
  build('--action value'){|o| argument = o}
392
- expect(argument).to eq('value')
399
+ _(argument).must_equal 'value'
393
400
  end
394
401
 
395
402
  it "doesn't call the block when the switch isn't supplied" do
396
403
  called = false
397
404
  build{called = true}
398
- expect(called).to eq(false)
405
+ _(called).must_equal false
399
406
  end
400
407
 
401
408
  it "raises OptionParser::MissingArgument when no argument is supplied" do
402
- expect{build('-a'){}}.to raise_error(OptionParser::MissingArgument)
409
+ _{build('-a'){}}.must_raise OptionParser::MissingArgument
403
410
  end
404
411
  end
405
- end
406
-
407
- describe Switches do
408
- describe "VERSION" do
409
- it "is a string" do
410
- expect(Switches::VERSION).to be_a(String)
412
+ describe "a default upon a switch which casts" do
413
+ def build(switches_string = '')
414
+ ARGV.clear
415
+ switches_string.split.each{|a| ARGV << a}
416
+ Switches.new do |s|
417
+ s.integer!(:lines, default: '10')
418
+ s.float!(:ratio, default: '1.5')
419
+ s.array!(:hosts, default: ['a', 'b'])
420
+ s.regexp!(:pattern, default: 'ab+c')
421
+ s.optional!(:mailbox, default: 'INBOX')
422
+ end
411
423
  end
412
424
 
413
- it "is three numbers separated by dots" do
414
- expect(Switches::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
425
+ it "casts an integer default rather than answering nil" do
426
+ _(build.lines).must_equal 10
415
427
  end
416
428
 
417
- it "matches the newest entry in the CHANGELOG" do
418
- changelog = File.read(File.expand_path('../CHANGELOG', __dir__))
419
- expect(changelog[/^(\d+\.\d+\.\d+):/, 1]).to eq(Switches::VERSION)
429
+ it "casts a float default rather than answering nil" do
430
+ _(build.ratio).must_equal 1.5
420
431
  end
421
- end
422
- end
423
432
 
424
- describe 'switches.rb.gemspec' do
425
- let(:spec){Gem::Specification.load(File.expand_path('../switches.rb.gemspec', __dir__))}
426
-
427
- it "is a valid specification" do
428
- expect(spec.validate).to eq(true)
429
- end
430
-
431
- it "does not pin a date" do
432
- expect(spec.date).to eq(Gem::Specification.new.date)
433
- end
433
+ it "carries an array default rather than answering nil" do
434
+ _(build.hosts).must_equal ['a', 'b']
435
+ end
434
436
 
435
- it "takes its version from Switches::VERSION" do
436
- expect(spec.version.to_s).to eq(Switches::VERSION)
437
- end
437
+ it "casts a regexp default rather than answering nil" do
438
+ _(build.pattern).must_equal(/ab+c/)
439
+ end
438
440
 
439
- it "declares ostruct as its only runtime dependency" do
440
- expect(spec.runtime_dependencies.map(&:name).sort).to eq(%w{ostruct})
441
- end
441
+ it "leaves a default upon a switch which does not cast alone" do
442
+ _(build.mailbox).must_equal 'INBOX'
443
+ end
442
444
 
443
- it "declares its development dependencies" do
444
- expect(spec.development_dependencies.map(&:name).sort).to eq(%w{rake rspec})
445
+ it "prefers a supplied value to the default" do
446
+ _(build('--lines 3').lines).must_equal 3
447
+ end
445
448
  end
446
449
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switches.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.1
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -24,7 +24,7 @@ dependencies:
24
24
  - !ruby/object:Gem::Version
25
25
  version: '0'
26
26
  - !ruby/object:Gem::Dependency
27
- name: rake
27
+ name: minitest
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - ">="
@@ -38,7 +38,7 @@ dependencies:
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  - !ruby/object:Gem::Dependency
41
- name: rspec
41
+ name: rake
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - ">="
@@ -60,13 +60,16 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - CHANGELOG
62
62
  - Gemfile
63
- - LICENSE.txt
63
+ - LICENSE
64
64
  - README.md
65
65
  - Rakefile
66
- - lib/Switches.rb
67
66
  - lib/Switches/VERSION.rb
68
- - spec/all.rb
67
+ - lib/switches.rb
69
68
  - switches.rb.gemspec
69
+ - test/Switches/VERSION_test.rb
70
+ - test/gemspec_test.rb
71
+ - test/loading_test.rb
72
+ - test/switches_test.rb
70
73
  homepage: https://github.com/thoran/switches
71
74
  licenses:
72
75
  - MIT
@@ -89,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
92
  - !ruby/object:Gem::Version
90
93
  version: '0'
91
94
  requirements: []
92
- rubygems_version: 4.0.18
95
+ rubygems_version: 4.0.20
93
96
  specification_version: 4
94
97
  summary: The easiest way to provide switches to a Ruby program.
95
98
  test_files: []
File without changes