summaryrefslogtreecommitdiff
path: root/spec/ruby/library/stringscanner/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/stringscanner/shared')
-rw-r--r--spec/ruby/library/stringscanner/shared/bol.rb25
-rw-r--r--spec/ruby/library/stringscanner/shared/concat.rb30
-rw-r--r--spec/ruby/library/stringscanner/shared/extract_range.rb11
-rw-r--r--spec/ruby/library/stringscanner/shared/extract_range_matched.rb13
-rw-r--r--spec/ruby/library/stringscanner/shared/pos.rb59
5 files changed, 138 insertions, 0 deletions
diff --git a/spec/ruby/library/stringscanner/shared/bol.rb b/spec/ruby/library/stringscanner/shared/bol.rb
new file mode 100644
index 0000000000..ec5c2051b5
--- /dev/null
+++ b/spec/ruby/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 == true
+ s.scan(/This/)
+ s.send(@method).should == false
+ s.terminate
+ s.send(@method).should == false
+
+ s = StringScanner.new("hello\nworld")
+ s.bol?.should == true
+ s.scan(/\w+/)
+ s.bol?.should == false
+ s.scan(/\n/)
+ s.bol?.should == true
+ s.unscan
+ s.bol?.should == 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 == true
+ end
+end
diff --git a/spec/ruby/library/stringscanner/shared/concat.rb b/spec/ruby/library/stringscanner/shared/concat.rb
new file mode 100644
index 0000000000..8138b0f8dc
--- /dev/null
+++ b/spec/ruby/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 == false
+ end
+
+ it "raises a TypeError if the given argument can't be converted to a String" do
+ -> { StringScanner.new('hello').send(@method, :world) }.should.raise(TypeError)
+ -> { StringScanner.new('hello').send(@method, mock('x')) }.should.raise(TypeError)
+ end
+end
+
+describe :strscan_concat_fixnum, shared: true do
+ it "raises a TypeError" do
+ a = StringScanner.new("hello world")
+ -> { a.send(@method, 333) }.should.raise(TypeError)
+ b = StringScanner.new("")
+ -> { b.send(@method, (256 * 3 + 64)) }.should.raise(TypeError)
+ -> { b.send(@method, -200) }.should.raise(TypeError)
+ end
+
+ it "doesn't call to_int on the argument" do
+ x = mock('x')
+ x.should_not_receive(:to_int)
+
+ -> { StringScanner.new("").send(@method, x) }.should.raise(TypeError)
+ end
+end
diff --git a/spec/ruby/library/stringscanner/shared/extract_range.rb b/spec/ruby/library/stringscanner/shared/extract_range.rb
new file mode 100644
index 0000000000..c64cc41fa7
--- /dev/null
+++ b/spec/ruby/library/stringscanner/shared/extract_range.rb
@@ -0,0 +1,11 @@
+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.is_a?(cls)
+ ch.should.instance_of?(String)
+ end
+end
diff --git a/spec/ruby/library/stringscanner/shared/extract_range_matched.rb b/spec/ruby/library/stringscanner/shared/extract_range_matched.rb
new file mode 100644
index 0000000000..8a6349bec1
--- /dev/null
+++ b/spec/ruby/library/stringscanner/shared/extract_range_matched.rb
@@ -0,0 +1,13 @@
+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.is_a?(cls)
+ ch.should.instance_of?(String)
+ end
+end
diff --git a/spec/ruby/library/stringscanner/shared/pos.rb b/spec/ruby/library/stringscanner/shared/pos.rb
new file mode 100644
index 0000000000..91f80fdf08
--- /dev/null
+++ b/spec/ruby/library/stringscanner/shared/pos.rb
@@ -0,0 +1,59 @@
+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
+
+ it "is not multi-byte character sensitive" do
+ s = StringScanner.new("abcädeföghi")
+
+ s.scan_until(/ö/)
+ s.pos.should == 10
+ 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
+ -> {
+ @s.send(@method, -20)
+ }.should.raise(RangeError)
+ end
+
+ it "raises a RangeError when the passed argument is out of range" do
+ -> { @s.send(@method, 20) }.should.raise(RangeError)
+ end
+end