forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added string or symbol helper for MSpec.
- Loading branch information
Brian Ford
committed
Feb 22, 2011
1 parent
26cd1fd
commit fe9bdb2
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'mspec/utils/version' | ||
require 'mspec/guards/guard' | ||
|
||
class Object | ||
# Accepts either a single argument or an Array of arguments. If RUBY_VERSION | ||
# is less than 1.9, converts the argument(s) to Strings; otherwise, converts | ||
# the argument(s) to Symbols. | ||
# | ||
# If one argument is passed, the converted argument is returned. If an Array | ||
# is passed, an Array is returned. | ||
# | ||
# For example, if RUBY_VERSION == 1.8.7 | ||
# | ||
# stasy(:some) => "some" | ||
# stasy("nom") => "nom" | ||
# | ||
# while if RUBY_VERSION == 1.9.0 | ||
# | ||
# stasy(:some) => :some | ||
# stasy("nom") => :nom | ||
|
||
def stasy(one, *rest) | ||
era = SpecVersion.new(SpecGuard.ruby_version) < "1.9" | ||
convert = era ? :to_s : :to_sym | ||
|
||
one = one.send convert | ||
if rest.empty? | ||
one | ||
else | ||
[one].concat rest.map { |x| x.send convert } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
require 'mspec/helpers/stasy' | ||
|
||
describe Object, "#stasy when RUBY_VERSION < 1.9" do | ||
before :all do | ||
@ruby_version = Object.const_get :RUBY_VERSION | ||
|
||
Object.const_set :RUBY_VERSION, "1.8.7" | ||
end | ||
|
||
after :all do | ||
Object.const_set :RUBY_VERSION, @ruby_version | ||
end | ||
|
||
it "returns a String when passed a String" do | ||
stasy("nom").should == "nom" | ||
end | ||
|
||
it "returns a String when passed a Symbol" do | ||
stasy(:some).should == "some" | ||
end | ||
|
||
it "returns an Array of Strings when passed an Array of Strings" do | ||
stasy("nom", "nom").should == ["nom", "nom"] | ||
end | ||
|
||
it "returns an Array of Strings when passed an Array of Symbols" do | ||
stasy(:some, :thing).should == ["some", "thing"] | ||
end | ||
end | ||
|
||
describe Object, "#stasy when RUBY_VERSION >= 1.9.0" do | ||
before :all do | ||
@ruby_version = Object.const_get :RUBY_VERSION | ||
|
||
Object.const_set :RUBY_VERSION, "1.9.0" | ||
end | ||
|
||
after :all do | ||
Object.const_set :RUBY_VERSION, @ruby_version | ||
end | ||
|
||
it "returns a Symbol when passed a String" do | ||
stasy("nom").should == :nom | ||
end | ||
|
||
it "returns a Symbol when passed a Symbol" do | ||
stasy(:some).should == :some | ||
end | ||
|
||
it "returns an Array of Symbols when passed an Array of Strings" do | ||
stasy("some", "thing").should == [:some, :thing] | ||
end | ||
|
||
it "returns an Array of Symbols when passed an Array of Symbols" do | ||
stasy(:nom, :nom).should == [:nom, :nom] | ||
end | ||
end |