diff options
Diffstat (limited to 'spec/ruby/library/stringscanner')
| -rw-r--r-- | spec/ruby/library/stringscanner/append_spec.rb | 28 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/beginning_of_line_spec.rb | 25 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/bol_spec.rb | 5 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/concat_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/pointer_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/pos_spec.rb | 57 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/shared/bol.rb | 25 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/shared/concat.rb | 30 | ||||
| -rw-r--r-- | spec/ruby/library/stringscanner/shared/pos.rb | 59 |
9 files changed, 114 insertions, 133 deletions
diff --git a/spec/ruby/library/stringscanner/append_spec.rb b/spec/ruby/library/stringscanner/append_spec.rb index fef5dcf2bd..68747d52d7 100644 --- a/spec/ruby/library/stringscanner/append_spec.rb +++ b/spec/ruby/library/stringscanner/append_spec.rb @@ -1,11 +1,33 @@ require_relative '../../spec_helper' -require_relative 'shared/concat' require 'strscan' describe "StringScanner#<<" do - it_behaves_like :strscan_concat, :<< + it "concatenates the given argument to self and returns self" do + s = StringScanner.new(+"hello ") + (s. << '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') << :world }.should.raise(TypeError) + -> { StringScanner.new('hello') << mock('x') }.should.raise(TypeError) + end end describe "StringScanner#<< when passed an Integer" do - it_behaves_like :strscan_concat_fixnum, :<< + it "raises a TypeError" do + a = StringScanner.new("hello world") + -> { a << 333 }.should.raise(TypeError) + b = StringScanner.new("") + -> { b << (256 * 3 + 64) }.should.raise(TypeError) + -> { b << -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("") << x }.should.raise(TypeError) + end end diff --git a/spec/ruby/library/stringscanner/beginning_of_line_spec.rb b/spec/ruby/library/stringscanner/beginning_of_line_spec.rb index 3f6f0da75f..ae97f52fe0 100644 --- a/spec/ruby/library/stringscanner/beginning_of_line_spec.rb +++ b/spec/ruby/library/stringscanner/beginning_of_line_spec.rb @@ -1,7 +1,28 @@ require_relative '../../spec_helper' -require_relative 'shared/bol' require 'strscan' describe "StringScanner#beginning_of_line?" do - it_behaves_like :strscan_bol, :beginning_of_line? + 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.beginning_of_line?.should == true + s.scan(/This/) + s.beginning_of_line?.should == false + s.terminate + s.beginning_of_line?.should == false + + s = StringScanner.new("hello\nworld") + s.beginning_of_line?.should == true + s.scan(/\w+/) + s.beginning_of_line?.should == false + s.scan(/\n/) + s.beginning_of_line?.should == true + s.unscan + s.beginning_of_line?.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.beginning_of_line?.should == true + end end diff --git a/spec/ruby/library/stringscanner/bol_spec.rb b/spec/ruby/library/stringscanner/bol_spec.rb index d31766e0e2..1d10c8f7c0 100644 --- a/spec/ruby/library/stringscanner/bol_spec.rb +++ b/spec/ruby/library/stringscanner/bol_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/bol' require 'strscan' describe "StringScanner#bol?" do - it_behaves_like :strscan_bol, :bol? + it "is an alias of StringScanner#beginning_of_line?" do + StringScanner.instance_method(:bol?).should == StringScanner.instance_method(:beginning_of_line?) + end end diff --git a/spec/ruby/library/stringscanner/concat_spec.rb b/spec/ruby/library/stringscanner/concat_spec.rb index 4f790e2505..716268c956 100644 --- a/spec/ruby/library/stringscanner/concat_spec.rb +++ b/spec/ruby/library/stringscanner/concat_spec.rb @@ -1,11 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/concat' require 'strscan' describe "StringScanner#concat" do - it_behaves_like :strscan_concat, :concat -end - -describe "StringScanner#concat when passed an Integer" do - it_behaves_like :strscan_concat_fixnum, :concat + it "is an alias of StringScanner#<<" do + StringScanner.instance_method(:concat).should == StringScanner.instance_method(:<<) + end end diff --git a/spec/ruby/library/stringscanner/pointer_spec.rb b/spec/ruby/library/stringscanner/pointer_spec.rb index bc0c0c50b7..5fc6c8cdf3 100644 --- a/spec/ruby/library/stringscanner/pointer_spec.rb +++ b/spec/ruby/library/stringscanner/pointer_spec.rb @@ -1,11 +1,14 @@ require_relative '../../spec_helper' -require_relative 'shared/pos' require 'strscan' describe "StringScanner#pointer" do - it_behaves_like :strscan_pos, :pointer + it "is an alias of StringScanner#pos" do + StringScanner.instance_method(:pointer).should == StringScanner.instance_method(:pos) + end end describe "StringScanner#pointer=" do - it_behaves_like :strscan_pos_set, :pointer= + it "is an alias of StringScanner#pos=" do + StringScanner.instance_method(:pointer=).should == StringScanner.instance_method(:pos=) + end end diff --git a/spec/ruby/library/stringscanner/pos_spec.rb b/spec/ruby/library/stringscanner/pos_spec.rb index 275fecf0f3..bc3003ebdf 100644 --- a/spec/ruby/library/stringscanner/pos_spec.rb +++ b/spec/ruby/library/stringscanner/pos_spec.rb @@ -1,11 +1,62 @@ require_relative '../../spec_helper' -require_relative 'shared/pos' require 'strscan' describe "StringScanner#pos" do - it_behaves_like :strscan_pos, :pos + before :each do + @s = StringScanner.new("This is a test") + end + + it "returns the position of the scan pointer" do + @s.pos.should == 0 + @s.scan_until(/This is/) + @s.pos.should == 7 + @s.get_byte + @s.pos.should == 8 + @s.terminate + @s.pos.should == 14 + end + + it "returns 0 in the reset position" do + @s.reset + @s.pos.should == 0 + end + + it "returns the length of the string in the terminate position" do + @s.terminate + @s.pos.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 "StringScanner#pos=" do - it_behaves_like :strscan_pos_set, :pos= + before :each do + @s = StringScanner.new("This is a test") + end + + it "modify the scan pointer" do + @s.pos = 5 + @s.rest.should == "is a test" + end + + it "positions from the end if the argument is negative" do + @s.pos = -2 + @s.rest.should == "st" + @s.pos.should == 12 + end + + it "raises a RangeError if position too far backward" do + -> { + @s.pos = -20 + }.should.raise(RangeError) + end + + it "raises a RangeError when the passed argument is out of range" do + -> { @s.pos = 20 }.should.raise(RangeError) + end end diff --git a/spec/ruby/library/stringscanner/shared/bol.rb b/spec/ruby/library/stringscanner/shared/bol.rb deleted file mode 100644 index ec5c2051b5..0000000000 --- a/spec/ruby/library/stringscanner/shared/bol.rb +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index 8138b0f8dc..0000000000 --- a/spec/ruby/library/stringscanner/shared/concat.rb +++ /dev/null @@ -1,30 +0,0 @@ -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/pos.rb b/spec/ruby/library/stringscanner/shared/pos.rb deleted file mode 100644 index 91f80fdf08..0000000000 --- a/spec/ruby/library/stringscanner/shared/pos.rb +++ /dev/null @@ -1,59 +0,0 @@ -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 |
