Skip to content

Commit

Permalink
Added string or symbol helper for MSpec.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Feb 22, 2011
1 parent 26cd1fd commit fe9bdb2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions mspec/lib/mspec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
require 'mspec/helpers/numeric'
require 'mspec/helpers/ruby_exe'
require 'mspec/helpers/scratch'
require 'mspec/helpers/stasy'
require 'mspec/helpers/tmp'
33 changes: 33 additions & 0 deletions mspec/lib/mspec/helpers/stasy.rb
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
58 changes: 58 additions & 0 deletions mspec/spec/helpers/stasy_spec.rb
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

0 comments on commit fe9bdb2

Please sign in to comment.