summaryrefslogtreecommitdiff
path: root/spec/rubyspec/library/stringscanner/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubyspec/library/stringscanner/shared')
-rw-r--r--spec/rubyspec/library/stringscanner/shared/bol.rb25
-rw-r--r--spec/rubyspec/library/stringscanner/shared/concat.rb30
-rw-r--r--spec/rubyspec/library/stringscanner/shared/eos.rb17
-rw-r--r--spec/rubyspec/library/stringscanner/shared/extract_range.rb22
-rw-r--r--spec/rubyspec/library/stringscanner/shared/extract_range_matched.rb22
-rw-r--r--spec/rubyspec/library/stringscanner/shared/get_byte.rb29
-rw-r--r--spec/rubyspec/library/stringscanner/shared/matched_size.rb21
-rw-r--r--spec/rubyspec/library/stringscanner/shared/peek.rb44
-rw-r--r--spec/rubyspec/library/stringscanner/shared/pos.rb52
-rw-r--r--spec/rubyspec/library/stringscanner/shared/rest_size.rb18
-rw-r--r--spec/rubyspec/library/stringscanner/shared/terminate.rb8
11 files changed, 288 insertions, 0 deletions
diff --git a/spec/rubyspec/library/stringscanner/shared/bol.rb b/spec/rubyspec/library/stringscanner/shared/bol.rb
new file mode 100644
index 0000000000..ebcdd7938f
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/bol.rb
@@ -0,0 +1,25 @@
+describe :strscan_bol, shared: true do
+ it "returns true if the scan pointer is at the beginning of the line, false otherwise" do
+ s = StringScanner.new("This is a test")
+ s.send(@method).should be_true
+ s.scan(/This/)
+ s.send(@method).should be_false
+ s.terminate
+ s.send(@method).should be_false
+
+ s = StringScanner.new("hello\nworld")
+ s.bol?.should be_true
+ s.scan(/\w+/)
+ s.bol?.should be_false
+ s.scan(/\n/)
+ s.bol?.should be_true
+ s.unscan
+ s.bol?.should be_false
+ end
+
+ it "returns true if the scan pointer is at the end of the line of an empty string." do
+ s = StringScanner.new('')
+ s.terminate
+ s.send(@method).should be_true
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/concat.rb b/spec/rubyspec/library/stringscanner/shared/concat.rb
new file mode 100644
index 0000000000..28788d3ff1
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/concat.rb
@@ -0,0 +1,30 @@
+describe :strscan_concat, shared: true do
+ it "concatenates the given argument to self and returns self" do
+ s = StringScanner.new("hello ")
+ s.send(@method, 'world').should == s
+ s.string.should == "hello world"
+ s.eos?.should be_false
+ end
+
+ it "raises a TypeError if the given argument can't be converted to a String" do
+ lambda { StringScanner.new('hello').send(@method, :world) }.should raise_error(TypeError)
+ lambda { StringScanner.new('hello').send(@method, mock('x')) }.should raise_error(TypeError)
+ end
+end
+
+describe :strscan_concat_fixnum, shared: true do
+ it "raises a TypeError" do
+ a = StringScanner.new("hello world")
+ lambda { a.send(@method, 333) }.should raise_error(TypeError)
+ b = StringScanner.new("")
+ lambda { b.send(@method, (256 * 3 + 64)) }.should raise_error(TypeError)
+ lambda { b.send(@method, -200) }.should raise_error(TypeError)
+ end
+
+ it "doesn't call to_int on the argument" do
+ x = mock('x')
+ x.should_not_receive(:to_int)
+
+ lambda { "".send(@method, x) }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/eos.rb b/spec/rubyspec/library/stringscanner/shared/eos.rb
new file mode 100644
index 0000000000..ea04c764a2
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/eos.rb
@@ -0,0 +1,17 @@
+describe :strscan_eos, shared: true do
+ before :each do
+ @s = StringScanner.new("This is a test")
+ end
+
+ it "returns true if the scan pointer is at the end of the string" do
+ @s.terminate
+ @s.send(@method).should be_true
+
+ s = StringScanner.new('')
+ s.send(@method).should be_true
+ end
+
+ it "returns false if the scan pointer is not at the end of the string" do
+ @s.send(@method).should be_false
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/extract_range.rb b/spec/rubyspec/library/stringscanner/shared/extract_range.rb
new file mode 100644
index 0000000000..7e98540b1a
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/extract_range.rb
@@ -0,0 +1,22 @@
+describe :extract_range, shared: true do
+ 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)
+ ch.should_not be_kind_of(cls)
+ ch.should be_an_instance_of(String)
+ end
+
+ it "taints the returned String if the input was tainted" do
+ str = 'abc'
+ str.taint
+
+ s = StringScanner.new(str)
+
+ s.send(@method).tainted?.should be_true
+ s.send(@method).tainted?.should be_true
+ s.send(@method).tainted?.should be_true
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/extract_range_matched.rb b/spec/rubyspec/library/stringscanner/shared/extract_range_matched.rb
new file mode 100644
index 0000000000..fe695e8ac1
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/extract_range_matched.rb
@@ -0,0 +1,22 @@
+describe :extract_range_matched, shared: true do
+ it "returns an instance of String when passed a String subclass" do
+ cls = Class.new(String)
+ sub = cls.new("abc")
+
+ s = StringScanner.new(sub)
+ s.scan(/\w{1}/)
+
+ ch = s.send(@method)
+ ch.should_not be_kind_of(cls)
+ ch.should be_an_instance_of(String)
+ end
+
+ it "taints the returned String if the input was tainted" do
+ str = 'abc'
+ str.taint
+
+ s = StringScanner.new(str)
+ s.scan(/\w{1}/)
+ s.send(@method).tainted?.should be_true
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/get_byte.rb b/spec/rubyspec/library/stringscanner/shared/get_byte.rb
new file mode 100644
index 0000000000..763ab6f4a4
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/get_byte.rb
@@ -0,0 +1,29 @@
+# -*- encoding: binary -*-
+describe :strscan_get_byte, shared: true do
+ it "scans one byte and returns it" do
+ s = StringScanner.new('abc5.')
+ s.send(@method).should == 'a'
+ s.send(@method).should == 'b'
+ s.send(@method).should == 'c'
+ s.send(@method).should == '5'
+ s.send(@method).should == '.'
+ end
+
+ it "is not multi-byte character sensitive" do
+ s = StringScanner.new("\244\242")
+ s.send(@method).should == "\244"
+ s.send(@method).should == "\242"
+ end
+
+ it "returns nil at the end of the string" do
+ # empty string case
+ s = StringScanner.new('')
+ s.send(@method).should == nil
+ s.send(@method).should == nil
+
+ # non-empty string case
+ s = StringScanner.new('a')
+ s.send(@method) # skip one
+ s.send(@method).should == nil
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/matched_size.rb b/spec/rubyspec/library/stringscanner/shared/matched_size.rb
new file mode 100644
index 0000000000..0d783dc92b
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/matched_size.rb
@@ -0,0 +1,21 @@
+describe :strscan_matched_size, shared: true do
+ before :each do
+ @s = StringScanner.new("This is a test")
+ end
+
+ it "returns the size of the most recent match" do
+ @s.check(/This/)
+ @s.send(@method).should == 4
+ @s.send(@method).should == 4
+ @s.scan(//)
+ @s.send(@method).should == 0
+ end
+
+ it "returns nil if there was no recent match" do
+ @s.send(@method).should == nil
+ @s.check(/\d+/)
+ @s.send(@method).should == nil
+ @s.terminate
+ @s.send(@method).should == nil
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/peek.rb b/spec/rubyspec/library/stringscanner/shared/peek.rb
new file mode 100644
index 0000000000..f414cc3b40
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/peek.rb
@@ -0,0 +1,44 @@
+describe :strscan_peek, shared: true do
+ before :each do
+ @s = StringScanner.new('This is a test')
+ end
+
+ it "returns at most the specified number of characters from the current position" do
+ @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"
+ 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
+ lambda { @s.send(@method, -2) }.should raise_error(ArgumentError)
+ end
+
+ it "raises a RangeError when the passed argument is a Bignum" do
+ lambda { @s.send(@method, bignum_value) }.should raise_error(RangeError)
+ 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
+
+ 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
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/pos.rb b/spec/rubyspec/library/stringscanner/shared/pos.rb
new file mode 100644
index 0000000000..80ded17b0f
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/pos.rb
@@ -0,0 +1,52 @@
+describe :strscan_pos, shared: true do
+ before :each do
+ @s = StringScanner.new("This is a test")
+ end
+
+ it "returns the position of the scan pointer" do
+ @s.send(@method).should == 0
+ @s.scan_until(/This is/)
+ @s.send(@method).should == 7
+ @s.get_byte
+ @s.send(@method).should == 8
+ @s.terminate
+ @s.send(@method).should == 14
+ end
+
+ it "returns 0 in the reset position" do
+ @s.reset
+ @s.send(@method).should == 0
+ end
+
+ it "returns the length of the string in the terminate position" do
+ @s.terminate
+ @s.send(@method).should == @s.string.length
+ end
+end
+
+describe :strscan_pos_set, shared: true do
+ before :each do
+ @s = StringScanner.new("This is a test")
+ end
+
+ it "modify the scan pointer" do
+ @s.send(@method, 5)
+ @s.rest.should == "is a test"
+ end
+
+ it "positions from the end if the argument is negative" do
+ @s.send(@method, -2)
+ @s.rest.should == "st"
+ @s.pos.should == 12
+ end
+
+ it "raises a RangeError if position too far backward" do
+ lambda {
+ @s.send(@method, -20)
+ }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError when the passed argument is out of range" do
+ lambda { @s.send(@method, 20) }.should raise_error(RangeError)
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/rest_size.rb b/spec/rubyspec/library/stringscanner/shared/rest_size.rb
new file mode 100644
index 0000000000..4c4f49e45c
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/rest_size.rb
@@ -0,0 +1,18 @@
+describe :strscan_rest_size, shared: true do
+ before :each do
+ @s = StringScanner.new('This is a test')
+ end
+
+ it "returns the length of the rest of the string" do
+ @s.send(@method).should == 14
+ @s.scan(/This/)
+ @s.send(@method).should == 10
+ @s.terminate
+ @s.send(@method).should == 0
+ end
+
+ it "is equivalent to rest.size" do
+ @s.scan(/This/)
+ @s.send(@method).should == @s.rest.size
+ end
+end
diff --git a/spec/rubyspec/library/stringscanner/shared/terminate.rb b/spec/rubyspec/library/stringscanner/shared/terminate.rb
new file mode 100644
index 0000000000..bf41d097e2
--- /dev/null
+++ b/spec/rubyspec/library/stringscanner/shared/terminate.rb
@@ -0,0 +1,8 @@
+describe :strscan_terminate, shared: true do
+ it "set the scan pointer to the end of the string and clear matching data." do
+ s = StringScanner.new('This is a test')
+ s.send(@method)
+ s.bol?.should be_false
+ s.eos?.should be_true
+ end
+end