Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View chomp_spec.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
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
Something went wrong with that request. Please try again.