-
-
Save jc00ke/4cac2b2d6d9de75552c0 to your computer and use it in GitHub Desktop.
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
require File.expand_path('../../../spec_helper', __FILE__) | |
require File.expand_path('../fixtures/classes', __FILE__) | |
require File.expand_path('../../string/fixtures/classes', __FILE__) | |
# FIXME: These methods exist on 1.9 only when the -n or -p option is passed to | |
# ruby, but we currently don't have a way of specifying that. | |
ruby_version_is ""..."1.9" do | |
describe "Kernel#chomp" do | |
it "is a private method" do | |
Kernel.should have_private_instance_method(:chomp) | |
end | |
end | |
describe "Kernel#chomp!" do | |
it "is a private method" do | |
Kernel.should have_private_instance_method(:chomp!) | |
end | |
end | |
describe "Kernel#chomp with separator" do | |
describe "with a non empty string separator" do | |
describe "and $_ does not repeat the separtor at the end" do | |
it "should return a new string with the record separator removed" do | |
$_ = "hello" | |
Kernel.chomp("llo").should == "he" | |
$_.should == "he" | |
end | |
end | |
describe "and $_ does repeat the separtor at the end" do | |
it "should return a new string with a single instance of the record separator removed" do | |
$_ = "hellollo" | |
Kernel.chomp("llo").should == "hello" | |
$_.should == "hello" | |
end | |
end | |
describe "separator is \\n" do | |
it "returns the string unchanged when $_ does not end with \\n" do | |
$_ = "hello" | |
Kernel.chomp("\n").should == "hello" | |
$_.should == "hello" | |
end | |
it "returns $_ without \\n when $_ ends with a single \\n" do | |
$_ = "hello\n" | |
Kernel.chomp("\n").should == "hello" | |
$_.should == "hello" | |
end | |
it "returns $_ without \\r\\n when $_ ends with a single \\r\\n" do | |
$_ = "hello\r\n" | |
Kernel.chomp("\n").should == "hello" | |
$_.should == "hello" | |
end | |
it "returns $_ without \\r when $_ ends with a single \\n\\r" do | |
$_ = "hello\n\r" | |
Kernel.chomp("\n").should == "hello\n" | |
$_.should == "hello\n" | |
end | |
it "returns $_ without \\r when $_ ends with a single \\r" do | |
$_ = "hello\r" | |
Kernel.chomp("\n").should == "hello" | |
$_.should == "hello" | |
end | |
it "should not remove \\n from the middle of $_" do | |
$_ = "hello \n there" | |
Kernel.chomp("\n").should == "hello \n there" | |
$_.should == "hello \n there" | |
end | |
it "should only remove a single \\r\\n pair when $_ ends with a sequence of \\n followed by \\r\\n" do | |
$_ = "hello\r\n\r\n\n\n\r\n" | |
Kernel.chomp("\n").should == "hello\r\n\r\n\n\n" | |
$_.should == "hello\r\n\r\n\n\n" | |
end | |
it "should only remove a single \\r when $_ ends with \\n\\r" do | |
$_ = "hello\n\r" | |
Kernel.chomp("\r").should == "hello\n" | |
$_.should == "hello\n" | |
end | |
it "should only remove \\r\\n when $_ ends with \\n\\r\\n" do | |
$_ = "hello\n\r\n" | |
Kernel.chomp("\r\n").should == "hello\n" | |
$_.should == "hello\n" | |
end | |
end | |
end | |
describe "with a nil separator" do | |
it "returns self" do | |
$_ = "hello\n\n" | |
Kernel.chomp(nil).should == "hello\n\n" | |
$_.should == "hello\n\n" | |
end | |
end | |
describe "with an empty separator" do | |
it "returns empty string when supplied an empty string" do | |
$_ = "" | |
Kernel.chomp("").should == "" | |
$_.should == "" | |
end | |
it "returns the string when $_ has no carriage returns" do | |
$_ = "hello" | |
Kernel.chomp("").should == "hello" | |
$_.should == "hello" | |
end | |
it "removes a single carriage return when $_ ends in a single carriage return" do | |
$_ = "hello\n" | |
Kernel.chomp("").should == "hello" | |
$_.should == "hello" | |
end | |
it "does not remove a carriage return from the middle of $_" do | |
$_ = "hello\nx" | |
Kernel.chomp("").should == "hello\nx" | |
$_.should == "hello\nx" | |
end | |
it "removes a \\r\\n pair when appearing at the end of $_" do | |
$_ = "hello\r\n" | |
Kernel.chomp("").should == "hello" | |
$_.should == "hello" | |
end | |
it "removes multiple instances of \\r\\n or \\n when appearing at the end of $_" do | |
$_ = "hello\r\n\r\n\n\n\r\n" | |
Kernel.chomp("").should == "hello" | |
$_.should == "hello" | |
end | |
it "does not remove a bare \\r at the end of $_" do | |
$_ = "hello\r" | |
Kernel.chomp("").should == "hello\r" | |
$_.should == "hello\r" | |
end | |
it "does not remove a \\n\\r pair at the end of $_" do | |
$_ = "hello\n\r" | |
Kernel.chomp("").should == "hello\n\r" | |
$_.should == "hello\n\r" | |
end | |
it "removes a \\r\\n pair at the end of $_ but not any remaining unpaired \\r" do | |
$_ = "hello\r\r\r\n" | |
Kernel.chomp("").should == "hello\r\r" | |
$_.should == "hello\r\r" | |
end | |
end | |
describe "when called with $_ being an empty string" do | |
it "returns an empty string if the separator is \\n" do | |
$_ = "" | |
Kernel.chomp("\n").should == "" | |
$_.should == "" | |
end | |
it "returns an empty string if the separator is \\r" do | |
$_ = "" | |
Kernel.chomp("\r").should == "" | |
$_.should == "" | |
end | |
it "returns an empty string if the separator is an empty string" do | |
$_ = "" | |
Kernel.chomp("").should == "" | |
$_.should == "" | |
end | |
it "returns an empty string if the separator is nil" do | |
$_ = "" | |
Kernel.chomp(nil).should == "" | |
$_.should == "" | |
end | |
end | |
it "calls #to_str to convert separator to a String" do | |
separator = mock('llo') | |
separator.stub!(:to_str).and_return("llo") | |
$_ = "hello" | |
Kernel.chomp(separator).should == "he" | |
$_.should == "he" | |
end | |
it "raises a TypeError if separator can't be converted to a string" do | |
lambda { $_ = "hello"; Kernel.chomp([]) }.should raise_error(TypeError) | |
end | |
it "returns subclass instances when called on a subclass" do | |
$_ = StringSpecs::MyString.new("hello\n") | |
Kernel.chomp.should be_kind_of(StringSpecs::MyString) | |
end | |
end | |
describe "Kernel#chomp without a separator" do | |
it "uses $/ as the separator" do | |
existing_separator = $/ | |
$_ = "helloxyz" | |
$/ = "z" | |
Kernel.chomp.should == "helloxy" | |
$_.should == "helloxy" | |
$/ = existing_separator | |
end | |
end | |
describe "Kernel#chomp! with separator" do | |
it "returns nil if no modifications were made" do | |
$_ = "four" | |
Kernel.chomp!.should == nil | |
$_.should == "four" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment