1# -*- encoding: utf-8 -*- 2require_relative '../../spec_helper' 3require_relative 'fixtures/classes' 4 5describe :kernel_chomp, shared: true do 6 it "removes the final newline of $_" do 7 KernelSpecs.chomp("abc\n", @method).should == "abc" 8 end 9 10 it "removes the final carriage return of $_" do 11 KernelSpecs.chomp("abc\r", @method).should == "abc" 12 end 13 14 it "removes the final carriage return, newline of $_" do 15 KernelSpecs.chomp("abc\r\n", @method).should == "abc" 16 end 17 18 it "removes only the final newline of $_" do 19 KernelSpecs.chomp("abc\n\n", @method).should == "abc\n" 20 end 21 22 it "removes the value of $/ from the end of $_" do 23 KernelSpecs.chomp("abcde", @method, "cde").should == "ab" 24 end 25end 26 27describe :kernel_chomp_private, shared: true do 28 it "is a private method" do 29 KernelSpecs.has_private_method(@method).should be_true 30 end 31end 32 33describe "Kernel.chomp" do 34 it_behaves_like :kernel_chomp, "Kernel.chomp" 35end 36 37describe "Kernel#chomp" do 38 it_behaves_like :kernel_chomp, "chomp" 39 40 it_behaves_like :kernel_chomp_private, :chomp 41end 42 43with_feature :encoding do 44 describe :kernel_chomp_encoded, shared: true do 45 before :each do 46 @external = Encoding.default_external 47 Encoding.default_external = Encoding::UTF_8 48 end 49 50 after :each do 51 Encoding.default_external = @external 52 end 53 54 it "removes the final carriage return, newline from a multi-byte $_" do 55 script = fixture __FILE__, "#{@method}.rb" 56 KernelSpecs.run_with_dash_n(script).should == "あれ" 57 end 58 end 59 60 describe "Kernel.chomp" do 61 it_behaves_like :kernel_chomp_encoded, "chomp" 62 end 63 64 describe "Kernel#chomp" do 65 it_behaves_like :kernel_chomp_encoded, "chomp_f" 66 end 67end 68