Created
June 12, 2010 20:35
-
-
Save gstark/436053 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| From ca8e700b8d9ccd0d6a462908a84caaf588d42639 Mon Sep 17 00:00:00 2001 | |
| From: Gavin Stark <[email protected]> | |
| Date: Sat, 12 Jun 2010 16:25:40 -0400 | |
| Subject: [PATCH] Filling in Kernel.chomp specs based on String specs | |
| --- | |
| spec/ruby/core/kernel/chomp_spec.rb | 180 ++++++++++++++++++++++++++++++++++- | |
| 1 files changed, 176 insertions(+), 4 deletions(-) | |
| diff --git a/spec/ruby/core/kernel/chomp_spec.rb b/spec/ruby/core/kernel/chomp_spec.rb | |
| index 029a74d..7b60a75 100644 | |
| --- a/spec/ruby/core/kernel/chomp_spec.rb | |
| +++ b/spec/ruby/core/kernel/chomp_spec.rb | |
| @@ -1,5 +1,6 @@ | |
| 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. | |
| @@ -16,11 +17,182 @@ ruby_version_is ""..."1.9" do | |
| end | |
| end | |
| - describe "Kernel.chomp" do | |
| - it "needs to be reviewed for spec completeness" | |
| + describe "Kernel#chomp with separator" do | |
| + it "returns a new string with the given record separator removed" do | |
| + $_ = "hello" | |
| + Kernel.chomp("llo").should == "he" | |
| + | |
| + $_ = "hellollo" | |
| + Kernel.chomp("llo").should == "hello" | |
| + $_.should == "hello" | |
| + end | |
| + | |
| + it "removes carriage return (except \\r) chars multiple times when separator is an empty string" do | |
| + $_ = "" | |
| + Kernel.chomp("").should == "" | |
| + $_.should == "" | |
| + | |
| + $_ = "hello" | |
| + Kernel.chomp("").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\n" | |
| + Kernel.chomp("").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\nx" | |
| + Kernel.chomp("").should == "hello\nx" | |
| + $_.should == "hello\nx" | |
| + | |
| + $_ = "hello\r\n" | |
| + Kernel.chomp("").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\r\n\r\n\n\n\r\n" | |
| + Kernel.chomp("").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\r" | |
| + Kernel.chomp("").should == "hello\r" | |
| + $_.should == "hello\r" | |
| + | |
| + $_ = "hello\n\r" | |
| + Kernel.chomp("").should == "hello\n\r" | |
| + $_.should == "hello\n\r" | |
| + | |
| + $_ = "hello\r\r\r\n" | |
| + Kernel.chomp("").should == "hello\r\r" | |
| + $_.should == "hello\r\r" | |
| + end | |
| + | |
| + it "removes carriage return chars (\\n, \\r, \\r\\n) when separator is \\n" do | |
| + $_ = "hello" | |
| + Kernel.chomp("\n").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\n" | |
| + Kernel.chomp("\n").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\r\n" | |
| + Kernel.chomp("\n").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello\n\r" | |
| + Kernel.chomp("\n").should == "hello\n" | |
| + $_.should == "hello\n" | |
| + | |
| + $_ = "hello\r" | |
| + Kernel.chomp("\n").should == "hello" | |
| + $_.should == "hello" | |
| + | |
| + $_ = "hello \n there" | |
| + Kernel.chomp("\n").should == "hello \n there" | |
| + $_.should == "hello \n there" | |
| + | |
| + $_ = "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" | |
| + | |
| + $_ = "hello\n\r" | |
| + Kernel.chomp("\r").should == "hello\n" | |
| + $_.should == "hello\n" | |
| + | |
| + $_ = "hello\n\r\n" | |
| + Kernel.chomp("\r\n").should == "hello\n" | |
| + $_.should == "hello\n" | |
| + end | |
| + | |
| + it "returns self if the separator is nil" do | |
| + $_ = "hello\n\n" | |
| + Kernel.chomp(nil).should == "hello\n\n" | |
| + $_.should == "hello\n\n" | |
| + end | |
| + | |
| + it "returns an empty string when called on an empty string" do | |
| + $_ = "" | |
| + Kernel.chomp("\n").should == "" | |
| + $_.should == "" | |
| + | |
| + $_ = "" | |
| + Kernel.chomp("\r").should == "" | |
| + $_.should == "" | |
| + | |
| + $_ = "" | |
| + Kernel.chomp("").should == "" | |
| + $_.should == "" | |
| + | |
| + $_ = "" | |
| + Kernel.chomp(nil).should == "" | |
| + $_.should == "" | |
| + end | |
| + | |
| + it "uses $/ as the separator when none is given" do | |
| + ["", "x", "x\n", "x\r", "x\r\n", "x\n\r\r\n", "hello"].each do |str| | |
| + ["", "llo", "\n", "\r", nil].each do |sep| | |
| + begin | |
| + $_ = str | |
| + expected = Kernel.chomp(sep) | |
| + | |
| + old_rec_sep, $/ = $/, sep | |
| + | |
| + $_ = str | |
| + Kernel.chomp.should == expected | |
| + $_.should == expected | |
| + ensure | |
| + $/ = old_rec_sep | |
| + end | |
| + end | |
| + end | |
| + end | |
| + | |
| + it "calls #to_str to convert separator to a String" do | |
| + separator = mock('llo') | |
| + separator.should_receive(: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(30.3) }.should raise_error(TypeError) | |
| + lambda { $_ = "hello"; Kernel.chomp([]) }.should raise_error(TypeError) | |
| + lambda { $_ = "hello"; Kernel.chomp(mock('x')) }.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) | |
| + | |
| + $_ = StringSpecs::MyString.new("hello") | |
| + Kernel.chomp.should be_kind_of(StringSpecs::MyString) | |
| + | |
| + $_ = StringSpecs::MyString.new("") | |
| + Kernel.chomp.should be_kind_of(StringSpecs::MyString) | |
| + end | |
| end | |
| - describe "Kernel.chomp!" do | |
| - it "needs to be reviewed for spec completeness" | |
| + describe "Kernel#chomp! with separator" do | |
| + it "returns nil if no modifications were made" do | |
| + $_ = "four" | |
| + Kernel.chomp!.should == nil | |
| + $_.should == "four" | |
| + | |
| + $_ = "" | |
| + Kernel.chomp!.should == nil | |
| + | |
| + $_ = "line" | |
| + Kernel.chomp!.should == nil | |
| + | |
| + $_ = "hello\n" | |
| + Kernel.chomp!("x").should == nil | |
| + | |
| + $_ = "hello" | |
| + Kernel.chomp!("").should == nil | |
| + | |
| + $_ = "hello" | |
| + Kernel.chomp!(nil).should == nil | |
| + end | |
| end | |
| end | |
| -- | |
| 1.7.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment