1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/ruby/core/string/lstrip_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.3 KiB
Ruby
Raw Permalink Normal View History

require_relative '../../spec_helper'
require_relative 'fixtures/classes'
2021-10-05 13:41:44 -04:00
require_relative 'shared/strip'
describe "String#lstrip" do
2021-10-05 13:41:44 -04:00
it_behaves_like :string_strip, :lstrip
it "returns a copy of self with leading whitespace removed" do
" hello ".lstrip.should == "hello "
" hello world ".lstrip.should == "hello world "
"\n\r\t\n\v\r hello world ".lstrip.should == "hello world "
"hello".lstrip.should == "hello"
2022-07-27 11:18:25 -04:00
" こにちわ".lstrip.should == "こにちわ"
end
it "works with lazy substrings" do
" hello "[1...-1].lstrip.should == "hello "
" hello world "[1...-1].lstrip.should == "hello world "
"\n\r\t\n\v\r hello world "[1...-1].lstrip.should == "hello world "
" こにちわ "[1...-1].lstrip.should == "こにちわ"
end
2022-03-28 11:47:04 -04:00
ruby_version_is '3.0' do
it "strips leading \\0" do
"\x00hello".lstrip.should == "hello"
"\000 \000hello\000 \000".lstrip.should == "hello\000 \000"
end
end
end
describe "String#lstrip!" do
it "modifies self in place and returns self" do
a = " hello "
a.lstrip!.should equal(a)
a.should == "hello "
end
2022-07-27 11:18:25 -04:00
it "returns nil if no modifications were made" do
a = "hello"
a.lstrip!.should == nil
a.should == "hello"
end
it "makes a string empty if it is only whitespace" do
"".lstrip!.should == nil
" ".lstrip.should == ""
" ".lstrip.should == ""
end
2022-03-28 11:47:04 -04:00
ruby_version_is '3.0' do
2022-07-27 11:18:25 -04:00
it "removes leading NULL bytes and whitespace" do
a = "\000 \000hello\000 \000"
a.lstrip!
a.should == "hello\000 \000"
end
end
it "raises a FrozenError on a frozen instance that is modified" do
-> { " hello ".freeze.lstrip! }.should raise_error(FrozenError)
end
# see [ruby-core:23657]
it "raises a FrozenError on a frozen instance that would not be modified" do
-> { "hello".freeze.lstrip! }.should raise_error(FrozenError)
-> { "".freeze.lstrip! }.should raise_error(FrozenError)
end
2022-06-26 08:50:14 -04:00
2022-07-27 11:18:25 -04:00
it "raises an ArgumentError if the first non-space codepoint is invalid" do
2022-06-26 08:50:14 -04:00
s = "\xDFabc".force_encoding(Encoding::UTF_8)
s.valid_encoding?.should be_false
-> { s.lstrip! }.should raise_error(ArgumentError)
2022-07-27 11:18:25 -04:00
s = " \xDFabc".force_encoding(Encoding::UTF_8)
s.valid_encoding?.should be_false
-> { s.lstrip! }.should raise_error(ArgumentError)
2022-06-26 08:50:14 -04:00
end
end