Skip to content

Commit 16b5a0a

Browse files
committed
Generalize VersionGuard and add #version_is to work with any version
* And not just the Ruby version.
1 parent 45ef991 commit 16b5a0a

File tree

4 files changed

+74
-44
lines changed

4 files changed

+74
-44
lines changed

lib/mspec/guards/bug.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
require 'mspec/guards/version'
22

33
class BugGuard < VersionGuard
4-
def initialize(bug, version)
4+
def initialize(bug, requirement)
55
@bug = bug
6-
if String === version
6+
if String === requirement
77
MSpec.deprecate "ruby_bug with a single version", 'an exclusive range ("2.1"..."2.3")'
8-
@version = SpecVersion.new version, true
8+
@requirement = SpecVersion.new requirement, true
9+
super(FULL_RUBY_VERSION, requirement)
910
else
10-
super(version)
11+
super(FULL_RUBY_VERSION, requirement)
1112
end
12-
@parameters = [@bug, @version]
1313
end
1414

1515
def match?
1616
return false if MSpec.mode? :no_ruby_bug
1717
return false unless PlatformGuard.standard?
18-
if Range === @version
18+
19+
if Range === @requirement
1920
super
2021
else
21-
FULL_RUBY_VERSION <= @version
22+
FULL_RUBY_VERSION <= @requirement
2223
end
2324
end
2425
end
2526

26-
def ruby_bug(bug, version, &block)
27-
BugGuard.new(bug, version).run_unless(:ruby_bug, &block)
27+
def ruby_bug(bug, requirement, &block)
28+
BugGuard.new(bug, requirement).run_unless(:ruby_bug, &block)
2829
end

lib/mspec/guards/version.rb

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,40 @@
55
class VersionGuard < SpecGuard
66
FULL_RUBY_VERSION = SpecVersion.new SpecGuard.ruby_version(:full)
77

8-
def initialize(version)
9-
case version
8+
def initialize(version, requirement)
9+
version = SpecVersion.new(version) unless SpecVersion === version
10+
@version = version
11+
12+
case requirement
1013
when String
11-
@version = SpecVersion.new version
14+
@requirement = SpecVersion.new requirement
1215
when Range
13-
MSpec.deprecate "an empty version range end", 'a specific version' if version.end.empty?
14-
a = SpecVersion.new version.begin
15-
b = SpecVersion.new version.end
16-
unless version.exclude_end?
16+
MSpec.deprecate "an empty version range end", 'a specific version' if requirement.end.empty?
17+
a = SpecVersion.new requirement.begin
18+
b = SpecVersion.new requirement.end
19+
unless requirement.exclude_end?
1720
MSpec.deprecate "ruby_version_is with an inclusive range", 'an exclusive range ("2.1"..."2.3")'
1821
end
19-
@version = version.exclude_end? ? a...b : a..b
22+
@requirement = requirement.exclude_end? ? a...b : a..b
2023
else
21-
raise "version must be a String or Range but was a #{version.class}"
24+
raise "version must be a String or Range but was a #{requirement.class}"
2225
end
23-
@parameters = [version]
26+
super(@version, @requirement)
2427
end
2528

2629
def match?
27-
if Range === @version
28-
@version.include? FULL_RUBY_VERSION
30+
if Range === @requirement
31+
@requirement.include? @version
2932
else
30-
FULL_RUBY_VERSION >= @version
33+
@version >= @requirement
3134
end
3235
end
3336
end
3437

35-
def ruby_version_is(*args, &block)
36-
VersionGuard.new(*args).run_if(:ruby_version_is, &block)
38+
def version_is(base_version, requirement, &block)
39+
VersionGuard.new(base_version, requirement).run_if(:version_is, &block)
40+
end
41+
42+
def ruby_version_is(requirement, &block)
43+
VersionGuard.new(VersionGuard::FULL_RUBY_VERSION, requirement).run_if(:ruby_version_is, &block)
3744
end

spec/guards/guard_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@
254254
end
255255

256256
it "allows to combine guards" do
257-
guard1 = VersionGuard.new 'x.x.x'
257+
guard1 = VersionGuard.new '1.2.3', 'x.x.x'
258258
VersionGuard.stub(:new).and_return(guard1)
259259
guard2 = PlatformGuard.new :dummy
260260
PlatformGuard.stub(:new).and_return(guard2)
@@ -360,7 +360,7 @@
360360
end
361361

362362
it "allows to combine guards" do
363-
guard1 = VersionGuard.new 'x.x.x'
363+
guard1 = VersionGuard.new '1.2.3', 'x.x.x'
364364
VersionGuard.stub(:new).and_return(guard1)
365365
guard2 = PlatformGuard.new :dummy
366366
PlatformGuard.stub(:new).and_return(guard2)

spec/guards/version_spec.rb

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,44 @@
1414
describe VersionGuard, "#match?" do
1515
before :each do
1616
hide_deprecation_warnings
17-
stub_const "VersionGuard::FULL_RUBY_VERSION", SpecVersion.new('1.8.6')
17+
@current = '1.8.6'
1818
end
1919

2020
it "returns true when the argument is equal to RUBY_VERSION" do
21-
VersionGuard.new('1.8.6').match?.should == true
21+
VersionGuard.new(@current, '1.8.6').match?.should == true
2222
end
2323

2424
it "returns true when the argument is less than RUBY_VERSION" do
25-
VersionGuard.new('1.8').match?.should == true
26-
VersionGuard.new('1.8.5').match?.should == true
25+
VersionGuard.new(@current, '1.8').match?.should == true
26+
VersionGuard.new(@current, '1.8.5').match?.should == true
2727
end
2828

2929
it "returns false when the argument is greater than RUBY_VERSION" do
30-
VersionGuard.new('1.8.7').match?.should == false
31-
VersionGuard.new('1.9.2').match?.should == false
30+
VersionGuard.new(@current, '1.8.7').match?.should == false
31+
VersionGuard.new(@current, '1.9.2').match?.should == false
3232
end
3333

3434
it "returns true when the argument range includes RUBY_VERSION" do
35-
VersionGuard.new('1.8.5'..'1.8.7').match?.should == true
36-
VersionGuard.new('1.8'..'1.9').match?.should == true
37-
VersionGuard.new('1.8'...'1.9').match?.should == true
38-
VersionGuard.new('1.8'..'1.8.6').match?.should == true
39-
VersionGuard.new('1.8.5'..'1.8.6').match?.should == true
40-
VersionGuard.new(''...'1.8.7').match?.should == true
35+
VersionGuard.new(@current, '1.8.5'..'1.8.7').match?.should == true
36+
VersionGuard.new(@current, '1.8'..'1.9').match?.should == true
37+
VersionGuard.new(@current, '1.8'...'1.9').match?.should == true
38+
VersionGuard.new(@current, '1.8'..'1.8.6').match?.should == true
39+
VersionGuard.new(@current, '1.8.5'..'1.8.6').match?.should == true
40+
VersionGuard.new(@current, ''...'1.8.7').match?.should == true
4141
end
4242

4343
it "returns false when the argument range does not include RUBY_VERSION" do
44-
VersionGuard.new('1.8.7'..'1.8.9').match?.should == false
45-
VersionGuard.new('1.8.4'..'1.8.5').match?.should == false
46-
VersionGuard.new('1.8.4'...'1.8.6').match?.should == false
47-
VersionGuard.new('1.8.5'...'1.8.6').match?.should == false
48-
VersionGuard.new(''...'1.8.6').match?.should == false
44+
VersionGuard.new(@current, '1.8.7'..'1.8.9').match?.should == false
45+
VersionGuard.new(@current, '1.8.4'..'1.8.5').match?.should == false
46+
VersionGuard.new(@current, '1.8.4'...'1.8.6').match?.should == false
47+
VersionGuard.new(@current, '1.8.5'...'1.8.6').match?.should == false
48+
VersionGuard.new(@current, ''...'1.8.6').match?.should == false
4949
end
5050
end
5151

5252
describe Object, "#ruby_version_is" do
5353
before :each do
54-
@guard = VersionGuard.new 'x.x.x'
54+
@guard = VersionGuard.new '1.2.3', 'x.x.x'
5555
VersionGuard.stub(:new).and_return(@guard)
5656
ScratchPad.clear
5757
end
@@ -88,3 +88,25 @@
8888
end.should raise_error(Exception)
8989
end
9090
end
91+
92+
describe Object, "#version_is" do
93+
before :each do
94+
hide_deprecation_warnings
95+
end
96+
97+
it "returns the expected values" do
98+
version_is('1.2.3', '1.2.2').should == true
99+
version_is('1.2.3', '1.2.3').should == true
100+
version_is('1.2.3', '1.2.4').should == false
101+
102+
version_is('1.2.3', '1').should == true
103+
version_is('1.2.3', '1.0').should == true
104+
version_is('1.2.3', '2').should == false
105+
version_is('1.2.3', '2.0').should == false
106+
107+
version_is('1.2.3', '1.2.2'..'1.2.4').should == true
108+
version_is('1.2.3', '1.2.2'..'1.2.3').should == true
109+
version_is('1.2.3', '1.2.2'...'1.2.3').should == false
110+
version_is('1.2.3', '1.2.3'..'1.2.4').should == true
111+
end
112+
end

0 commit comments

Comments
 (0)