Skip to content

Commit

Permalink
Fixed SpecGuard#platform? to handle JRuby correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed May 19, 2008
1 parent 02ae720 commit f4234a3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 32 deletions.
24 changes: 20 additions & 4 deletions lib/mspec/guards/guard.rb
Expand Up @@ -87,17 +87,33 @@ def implementation?(*args)
end
end

def windows?(key)
!!key.match(/(mswin|mingw)/)
end

def platform?(*args)
args.any? do |platform|
case platform
when :windows
['mswin', 'mingw'].any? { |p| RUBY_PLATFORM.match p }
if platform != :java && RUBY_PLATFORM.match('java') && os?(platform)
true
else
RUBY_PLATFORM.match platform.to_s
RUBY_PLATFORM.match(platform.to_s) || windows?(RUBY_PLATFORM)
end
end
end

def wordsize?(size)
size == 8 * 1.size
end

def os?(*oses)
require 'rbconfig'
oses.any? do |os|
host_os = Config::CONFIG['host_os'] || RUBY_PLATFORM
host_os.downcase!
host_os.match(os.to_s) || windows?(host_os)
end
end

def match?
implementation?(*@args) or platform?(*@args)
end
Expand Down
13 changes: 0 additions & 13 deletions lib/mspec/guards/platform.rb
Expand Up @@ -9,19 +9,6 @@ def initialize(*args)
end
end

def wordsize?(size)
size == 8 * 1.size
end

def os?(*oses)
require 'rbconfig'
oses.any? do |os|
host_os = Config::CONFIG['host_os'] || RUBY_PLATFORM
host_os.downcase!
host_os.match os.to_s
end
end

def match?
match = @platforms.empty? ? true : platform?(*@platforms)
@options.each do |key, value|
Expand Down
98 changes: 98 additions & 0 deletions spec/guards/guard_spec.rb
@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'mspec/guards/guard'
require 'rbconfig'

describe SpecGuard, ".register" do
before :each do
Expand Down Expand Up @@ -177,6 +178,103 @@
end
end

describe SpecGuard, "#platform? on JRuby" do
before :all do
@verbose = $VERBOSE
$VERBOSE = nil
end

after :all do
$VERBOSE = @verbose
end

before :each do
@ruby_platform = Object.const_get :RUBY_PLATFORM
Object.const_set :RUBY_PLATFORM, 'java'
@guard = SpecGuard.new
end

after :each do
Object.const_set :RUBY_PLATFORM, @ruby_platform
end

it "returns true when arg is :java and RUBY_PLATFORM contains 'java'" do
@guard.platform?(:java).should == true
end

it "returns true when arg is :windows and RUBY_PLATFORM contains 'java' and os?(:windows) is true" do
Config::CONFIG.stub!(:[]).and_return('mswin32')
@guard.platform?(:windows).should == true
end

it "returns true when RUBY_PLATFORM contains 'java' and os?(argument) is true" do
Config::CONFIG.stub!(:[]).and_return('amiga')
@guard.platform?(:amiga).should == true
end
end

describe SpecGuard, "#wordsize?" do
before :each do
@guard = SpecGuard.new
end

it "returns true when arg is 32 and 1.size is 4" do
@guard.wordsize?(32).should == (1.size == 4)
end

it "returns true when arg is 64 and 1.size is 8" do
@guard.wordsize?(64).should == (1.size == 8)
end
end

describe SpecGuard, "#os?" do
before :each do
@guard = SpecGuard.new
Config::CONFIG.stub!(:[]).and_return('unreal')
end

it "returns true if argument matches Config::CONFIG['host_os']" do
@guard.os?(:unreal).should == true
end

it "returns true if any argument matches Config::CONFIG['host_os']" do
@guard.os?(:bsd, :unreal, :amiga).should == true
end

it "returns false if no argument matches Config::CONFIG['host_os']" do
@guard.os?(:bsd, :netbsd, :amiga, :msdos).should == false
end

it "returns false if argument does not match Config::CONFIG['host_os']" do
@guard.os?(:amiga).should == false
end

it "returns true when arg is :windows and Config::CONFIG['host_os'] contains 'mswin'" do
Config::CONFIG.stub!(:[]).and_return('mswin32')
@guard.os?(:windows).should == true
end

it "returns true when arg is :windows and Config::CONFIG['host_os'] contains 'mingw'" do
Config::CONFIG.stub!(:[]).and_return('mingw32')
@guard.os?(:windows).should == true
end
end

describe SpecGuard, "windows?" do
before :each do
@guard = SpecGuard.new
end

it "returns true if the key passed matches 'mswin' or 'mingw'" do
@guard.windows?('mswin32').should == true
@guard.windows?('i386-mingw32').should == true
end

it "returns false if the key passes matches neither 'mswin' nor 'mingw'" do
@guard.windows?('darwin9.0').should == false
end
end

describe SpecGuard, "#match?" do
before :each do
@guard = SpecGuard.new
Expand Down
15 changes: 0 additions & 15 deletions spec/guards/platform_spec.rb
@@ -1,21 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'mspec/guards/platform'

describe MSpec, ".wordsize?" do
before :each do
@guard = PlatformGuard.new
end

it "returns true when arg is 32 and 1.size is 4" do
@guard.wordsize?(32).should == (1.size == 4)
end

it "returns true when arg is 64 and 1.size is 8" do
@guard.wordsize?(64).should == (1.size == 8)
end
end


describe Object, "#platform_is" do
before :each do
@guard = PlatformGuard.new :dummy
Expand Down

0 comments on commit f4234a3

Please sign in to comment.