Skip to content

Instantly share code, notes, and snippets.

@shri-zz
Created April 9, 2009 21:28
Show Gist options
  • Save shri-zz/92753 to your computer and use it in GitHub Desktop.
Save shri-zz/92753 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes.rb'
# TODO - Should be moved to /fixtures/classes.rb. Shown here just for convinience for now
module StringSpecs
def self.split(s, string_pattern, limit = nil)
if limit then
result1 = s.split(string_pattern, limit)
result2 = s.split(eval("/#{string_pattern}/"), limit)
else
result1 = s.split(string_pattern)
result2 = s.split(eval("/#{string_pattern}/"))
end
result1.should == result2
result1
end
end
describe "String#split" do
it "returns an array of substrings based on splitting on the given string" do
StringSpecs.split("mellow yellow", "ello").should == ["m", "w y", "w"]
end
it "suppresses trailing empty fields when limit isn't given or 0" do
StringSpecs.split("1,2,,3,4,,", ',').should == ["1", "2", "", "3", "4"]
StringSpecs.split("1,2,,3,4,,", ',', 0).should == ["1", "2", "", "3", "4"]
StringSpecs.split(" a b c\nd ", " ").should == ["", "a", "b", "c\nd"]
StringSpecs.split("hai", "hai").should == []
StringSpecs.split(",", ",").should == []
StringSpecs.split(",", ",", 0).should == []
end
it "returns an array with one entry if limit is 1: the original string" do
StringSpecs.split("hai", "hai", 1).should == ["hai"]
StringSpecs.split("x.y.z", ".", 1).should == ["x.y.z"]
StringSpecs.split("hello world ", " ", 1).should == ["hello world "]
StringSpecs.split("hi!", "", 1).should == ["hi!"]
end
it "returns at most limit fields when limit > 1" do
StringSpecs.split("hai", "hai", 2).should == ["", ""]
StringSpecs.split("1,2,,3,4,,", ',', 2).should == ["1", "2,,3,4,,"]
StringSpecs.split("1,2,,3,4,,", ',', 3).should == ["1", "2", ",3,4,,"]
StringSpecs.split("1,2,,3,4,,", ',', 4).should == ["1", "2", "", "3,4,,"]
StringSpecs.split("1,2,,3,4,,", ',', 5).should == ["1", "2", "", "3", "4,,"]
StringSpecs.split("1,2,,3,4,,", ',', 6).should == ["1", "2", "", "3", "4", ","]
StringSpecs.split("x", 'x', 2).should == ["", ""]
StringSpecs.split("xx", 'x', 2).should == ["", "x"]
StringSpecs.split("xx", 'x', 3).should == ["", "", ""]
StringSpecs.split("xxx", 'x', 2).should == ["", "xx"]
StringSpecs.split("xxx", 'x', 3).should == ["", "", "x"]
StringSpecs.split("xxx", 'x', 4).should == ["", "", "", ""]
end
it "doesn't suppress or limit fields when limit is negative" do
StringSpecs.split("1,2,,3,4,,", ',', -1).should == ["1", "2", "", "3", "4", "", ""]
StringSpecs.split("1,2,,3,4,,", ',', -5).should == ["1", "2", "", "3", "4", "", ""]
StringSpecs.split(" a b c\nd ", " ", -1).should == ["", "a", "b", "c\nd", ""]
StringSpecs.split(",", ",", -1).should == ["", ""]
end
it "defaults to $; when string isn't given or nil" do
begin
old_fs = $;
[",", /,/, ":", /:/, "", //, "XY", /XY/, nil].each do |fs|
$; = fs
["x,y,z,,,", "1:2:", "aXYbXYcXY", ""].each do |str|
expected = str.split(fs || " ")
str.split(nil).should == expected
str.split.should == expected
str.split(nil, -1).should == str.split(fs || " ", -1)
str.split(nil, 0).should == str.split(fs || " ", 0)
str.split(nil, 2).should == str.split(fs || " ", 2)
end
end
ensure
$; = old_fs
end
end
it "tries converting limit to an integer via to_int" do
obj = mock('2')
def obj.to_int() 2 end
StringSpecs.split("1-2-3-4", "-", obj).should == ["1", "2-3-4"]
obj = mock('2')
obj.should_receive(:respond_to?).twice.with(:to_int).and_return(true)
obj.should_receive(:method_missing).twice.with(:to_int).and_return(2)
StringSpecs.split("1-2-3-4", "-", obj).should == ["1", "2-3-4"]
end
it "doesn't set $~" do
$~ = nil
"x.y.z".split(".")
$~.should == nil
end
it "returns subclass instances based on self" do
["", "x-y-z-", " x y "].each do |str|
["", //, "-", /-/, " ", / /].each do |pat|
[-1, 0, 1, 2].each do |limit|
StringSpecs::MyString.new(str).split(pat, limit).each do |x|
x.class.should == StringSpecs::MyString
end
end
end
end
end
it "does not call constructor on created subclass instances" do
# can't call should_not_receive on an object that doesn't yet exist
# so failure here is signalled by exception, not expectation failure
s = StringSpecs::StringWithRaisingConstructor.new('silly:string')
StringSpecs.split(s, ':').first.should == 'silly'
end
it "taints the resulting strings if self is tainted" do
["", "x-y-z-", " x y "].each do |str|
["", //, "-", /-/, " ", / /].each do |pat|
[-1, 0, 1, 2].each do |limit|
str.dup.taint.split(pat).each do |x|
x.tainted?.should == true
end
str.split(pat.dup.taint).each do |x|
x.tainted?.should == false
end
end
end
end
end
end
describe "String#split with String" do
it "ignores leading and continuous whitespace when string is a single space" do
" now's the time ".split(' ').should == ["now's", "the", "time"]
" now's the time ".split(' ', -1).should == ["now's", "the", "time", ""]
"\t\n a\t\tb \n\r\r\nc\v\vd\v ".split(' ').should == ["a", "b", "c", "d"]
"a\x00a b".split(' ').should == ["a\x00a", "b"]
end
it "splits between characters when its argument is an empty string" do
"hi!".split("").should == ["h", "i", "!"]
"hi!".split("", -1).should == ["h", "i", "!", ""]
"hi!".split("", 2).should == ["h", "i!"]
end
it "tries converting its pattern argument to a string via to_str" do
obj = mock('::')
def obj.to_str() "::" end
"hello::world".split(obj).should == ["hello", "world"]
obj = mock('::')
obj.should_receive(:respond_to?).with(:to_str).any_number_of_times.and_return(true)
obj.should_receive(:method_missing).with(:to_str).and_return("::")
"hello::world".split(obj).should == ["hello", "world"]
end
it "accepts String subtype as pattern" do
["", "x-y-z-", " x y "].each do |str|
["", "-", " "].each do |pat|
[-1, 0, 1, 2].each do |limit|
str.split(StringSpecs::MyString.new(pat), limit).each do |x|
x.class.should == String
end
end
end
end
end
end
describe "String#split with Regexp" do
# TODO - This will need to be filled in...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment