2017-05-07 12:04:49 +00:00
|
|
|
describe :strscan_peek, shared: true do
|
|
|
|
before :each do
|
|
|
|
@s = StringScanner.new('This is a test')
|
|
|
|
end
|
|
|
|
|
2017-07-27 12:10:41 +00:00
|
|
|
it "returns at most the specified number of bytes from the current position" do
|
2017-05-07 12:04:49 +00:00
|
|
|
@s.send(@method, 4).should == "This"
|
|
|
|
@s.pos.should == 0
|
|
|
|
@s.pos = 5
|
|
|
|
@s.send(@method, 2).should == "is"
|
|
|
|
@s.send(@method, 1000).should == "is a test"
|
2017-07-27 12:10:41 +00:00
|
|
|
|
|
|
|
s = StringScanner.new("été")
|
|
|
|
s.send(@method, 2).should == "é"
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an empty string when the passed argument is zero" do
|
|
|
|
@s.send(@method, 0).should == ""
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a ArgumentError when the passed argument is negative" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { @s.send(@method, -2) }.should raise_error(ArgumentError)
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a RangeError when the passed argument is a Bignum" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { @s.send(@method, bignum_value) }.should raise_error(RangeError)
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an instance of String when passed a String subclass" do
|
|
|
|
cls = Class.new(String)
|
|
|
|
sub = cls.new("abc")
|
|
|
|
|
|
|
|
s = StringScanner.new(sub)
|
|
|
|
|
|
|
|
ch = s.send(@method, 1)
|
|
|
|
ch.should_not be_kind_of(cls)
|
|
|
|
ch.should be_an_instance_of(String)
|
|
|
|
end
|
|
|
|
|
2019-09-24 20:59:12 -07:00
|
|
|
ruby_version_is ''...'2.7' do
|
|
|
|
it "taints the returned String if the input was tainted" do
|
|
|
|
str = 'abc'
|
|
|
|
str.taint
|
|
|
|
|
|
|
|
s = StringScanner.new(str)
|
|
|
|
s.send(@method, 1).tainted?.should be_true
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
end
|