diff options
Diffstat (limited to 'spec/ruby/library/stringscanner')
43 files changed, 309 insertions, 453 deletions
diff --git a/spec/ruby/library/stringscanner/check_spec.rb b/spec/ruby/library/stringscanner/check_spec.rb index 235f2f22e9..232158b09e 100644 --- a/spec/ruby/library/stringscanner/check_spec.rb +++ b/spec/ruby/library/stringscanner/check_spec.rb @@ -33,33 +33,31 @@ describe "StringScanner#check" do it "returns nil when matching failed" do @s.check(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end context "when #check was called with a String pattern" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil when matching succeeded" do @s.check("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "raises IndexError when matching succeeded" do @s.check("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end it "returns nil when matching failed" do @s.check("2008") @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end it "returns a matching substring when given Integer index" do @@ -68,7 +66,6 @@ describe "StringScanner#check" do end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -77,10 +74,9 @@ describe "StringScanner#check" do @s.check("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -89,7 +85,7 @@ describe "StringScanner#check" do @s.check("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/check_until_spec.rb b/spec/ruby/library/stringscanner/check_until_spec.rb index 701a703ebe..b4ef35d9f7 100644 --- a/spec/ruby/library/stringscanner/check_until_spec.rb +++ b/spec/ruby/library/stringscanner/check_until_spec.rb @@ -24,7 +24,7 @@ describe "StringScanner#check_until" do it "raises TypeError if given a String" do -> { @s.check_until('T') - }.should raise_error(TypeError, 'wrong argument type String (expected Regexp)') + }.should.raise(TypeError, 'wrong argument type String (expected Regexp)') end end @@ -35,7 +35,6 @@ describe "StringScanner#check_until" do end # https://github.com/ruby/strscan/issues/131 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.1" it "sets the last match result if given a String" do @s.check_until("a") @@ -45,7 +44,6 @@ describe "StringScanner#check_until" do @s.post_match.should == " test" end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "sets the last match result if given a String" do @@ -69,34 +67,32 @@ describe "StringScanner#check_until" do it "returns nil when matching failed" do @s.check_until(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" context "when #check_until was called with a String pattern" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil when matching succeeded" do @s.check_until("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError when matching succeeded" do @s.check_until("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end it "returns nil when matching failed" do @s.check_until("2008") @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end it "returns a matching substring when given Integer index" do @@ -105,7 +101,6 @@ describe "StringScanner#check_until" do end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -114,10 +109,9 @@ describe "StringScanner#check_until" do @s.check_until("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -126,7 +120,7 @@ describe "StringScanner#check_until" do @s.check_until("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/clear_spec.rb b/spec/ruby/library/stringscanner/clear_spec.rb deleted file mode 100644 index 7ae089704a..0000000000 --- a/spec/ruby/library/stringscanner/clear_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/terminate' -require 'strscan' - -describe "StringScanner#clear" do - it_behaves_like :strscan_terminate, :clear - - it "warns in verbose mode that the method is obsolete" do - s = StringScanner.new("abc") - -> { - s.clear - }.should complain(/clear.*obsolete.*terminate/, verbose: true) - - -> { - s.clear - }.should_not complain(verbose: false) - end -end diff --git a/spec/ruby/library/stringscanner/dup_spec.rb b/spec/ruby/library/stringscanner/dup_spec.rb index 0fc52a1477..0118964526 100644 --- a/spec/ruby/library/stringscanner/dup_spec.rb +++ b/spec/ruby/library/stringscanner/dup_spec.rb @@ -15,7 +15,7 @@ describe "StringScanner#dup" do it "copies the passed StringScanner's position to self" do @orig_s.pos = 5 s = @orig_s.dup - s.pos.should eql(5) + s.pos.should.eql?(5) end it "copies previous match state" do @@ -34,6 +34,6 @@ describe "StringScanner#dup" do it "copies the passed StringScanner scan pointer to self" do @orig_s.terminate s = @orig_s.dup - s.eos?.should be_true + s.eos?.should == true end end diff --git a/spec/ruby/library/stringscanner/element_reference_spec.rb b/spec/ruby/library/stringscanner/element_reference_spec.rb index 91b6d86dc7..bcd48ede61 100644 --- a/spec/ruby/library/stringscanner/element_reference_spec.rb +++ b/spec/ruby/library/stringscanner/element_reference_spec.rb @@ -7,8 +7,8 @@ describe "StringScanner#[]" do end it "returns nil if there is no current match" do - @s[0].should be_nil - @s[:wday].should be_nil + @s[0].should == nil + @s[:wday].should == nil end it "returns the n-th subgroup in the most recent match" do @@ -35,24 +35,24 @@ describe "StringScanner#[]" do it "raises a TypeError if the given index is nil" do @s.scan(/(\w+) (\w+) (\d+) /) - -> { @s[nil]}.should raise_error(TypeError) + -> { @s[nil]}.should.raise(TypeError) end it "raises a TypeError when a Range is as argument" do @s.scan(/(\w+) (\w+) (\d+) /) - -> { @s[0..2]}.should raise_error(TypeError) + -> { @s[0..2]}.should.raise(TypeError) end it "raises a IndexError when there's no any named capture group in the regexp" do @s.scan(/(\w+) (\w+) (\d+) /) - -> { @s["wday"]}.should raise_error(IndexError) - -> { @s[:wday]}.should raise_error(IndexError) + -> { @s["wday"]}.should.raise(IndexError) + -> { @s[:wday]}.should.raise(IndexError) end it "raises a IndexError when given a not existing capture group name" do @s.scan(/(?<a>\w+) (?<b>\w+) (?<c>\d+) /) - -> { @s["wday"]}.should raise_error(IndexError) - -> { @s[:wday]}.should raise_error(IndexError) + -> { @s["wday"]}.should.raise(IndexError) + -> { @s[:wday]}.should.raise(IndexError) end it "returns named capture" do diff --git a/spec/ruby/library/stringscanner/empty_spec.rb b/spec/ruby/library/stringscanner/empty_spec.rb deleted file mode 100644 index d9449bea6e..0000000000 --- a/spec/ruby/library/stringscanner/empty_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/eos' -require 'strscan' - -describe "StringScanner#empty?" do - it_behaves_like :strscan_eos, :empty? - - it "warns in verbose mode that the method is obsolete" do - s = StringScanner.new("abc") - -> { - s.empty? - }.should complain(/empty?.*obsolete.*eos?/, verbose: true) - - -> { - s.empty? - }.should_not complain(verbose: false) - end -end diff --git a/spec/ruby/library/stringscanner/eos_spec.rb b/spec/ruby/library/stringscanner/eos_spec.rb index b58ee1e473..03c2804e5b 100644 --- a/spec/ruby/library/stringscanner/eos_spec.rb +++ b/spec/ruby/library/stringscanner/eos_spec.rb @@ -1,7 +1,20 @@ require_relative '../../spec_helper' -require_relative 'shared/eos' require 'strscan' describe "StringScanner#eos?" do - it_behaves_like :strscan_eos, :eos? + 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.should.eos? + + s = StringScanner.new('') + s.should.eos? + end + + it "returns false if the scan pointer is not at the end of the string" do + @s.should_not.eos? + end end diff --git a/spec/ruby/library/stringscanner/exist_spec.rb b/spec/ruby/library/stringscanner/exist_spec.rb index 3f40c7a5a5..2207622366 100644 --- a/spec/ruby/library/stringscanner/exist_spec.rb +++ b/spec/ruby/library/stringscanner/exist_spec.rb @@ -32,7 +32,7 @@ describe "StringScanner#exist?" do it "raises TypeError if given a String" do -> { @s.exist?('T') - }.should raise_error(TypeError, 'wrong argument type String (expected Regexp)') + }.should.raise(TypeError, 'wrong argument type String (expected Regexp)') end end @@ -57,34 +57,32 @@ describe "StringScanner#exist?" do it "returns nil when matching failed" do @s.exist?(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" context "when #exist? was called with a String pattern" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil when matching succeeded" do @s.exist?("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError when matching succeeded" do @s.exist?("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end it "returns nil when matching failed" do @s.exist?("2008") @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end it "returns a matching substring when given Integer index" do @@ -93,7 +91,6 @@ describe "StringScanner#exist?" do end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -102,10 +99,9 @@ describe "StringScanner#exist?" do @s.exist?("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -114,7 +110,7 @@ describe "StringScanner#exist?" do @s.exist?("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/get_byte_spec.rb b/spec/ruby/library/stringscanner/get_byte_spec.rb index 29e2f557de..b989b73883 100644 --- a/spec/ruby/library/stringscanner/get_byte_spec.rb +++ b/spec/ruby/library/stringscanner/get_byte_spec.rb @@ -1,7 +1,84 @@ +# encoding: binary require_relative '../../spec_helper' -require_relative 'shared/get_byte' require 'strscan' describe "StringScanner#get_byte" do - it_behaves_like :strscan_get_byte, :get_byte + it "scans one byte and returns it" do + s = StringScanner.new('abc5.') + s.get_byte.should == 'a' + s.get_byte.should == 'b' + s.get_byte.should == 'c' + s.get_byte.should == '5' + s.get_byte.should == '.' + end + + it "is not multi-byte character sensitive" do + s = StringScanner.new("\244\242") + s.get_byte.should == "\244" + s.get_byte.should == "\242" + end + + it "returns nil at the end of the string" do + # empty string case + s = StringScanner.new('') + s.get_byte.should == nil + s.get_byte.should == nil + + # non-empty string case + s = StringScanner.new('a') + s.get_byte # skip one + s.get_byte.should == nil + end + + describe "#[] successive call with a capture group name" do + # https://github.com/ruby/strscan/issues/139 + version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" + it "returns nil" do + s = StringScanner.new("This is a test") + s.get_byte + s.should.matched? + s[:a].should == nil + end + end + version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" + it "raises IndexError" do + s = StringScanner.new("This is a test") + s.get_byte + s.should.matched? + -> { s[:a] }.should.raise(IndexError) + end + end + + it "returns a matching character when given Integer index" do + s = StringScanner.new("This is a test") + s.get_byte + s[0].should == "T" + end + + # https://github.com/ruby/strscan/issues/135 + version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" + it "ignores the previous matching with Regexp" do + s = StringScanner.new("This is a test") + s.exist?(/(?<a>This)/) + s.should.matched? + s[:a].should == "This" + + s.get_byte + s.should.matched? + s[:a].should == nil + end + end + version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" + it "ignores the previous matching with Regexp" do + s = StringScanner.new("This is a test") + s.exist?(/(?<a>This)/) + s.should.matched? + s[:a].should == "This" + + s.get_byte + s.should.matched? + -> { s[:a] }.should.raise(IndexError) + end + end + end end diff --git a/spec/ruby/library/stringscanner/getbyte_spec.rb b/spec/ruby/library/stringscanner/getbyte_spec.rb deleted file mode 100644 index e0659a5829..0000000000 --- a/spec/ruby/library/stringscanner/getbyte_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/get_byte' -require_relative 'shared/extract_range' -require 'strscan' - -describe "StringScanner#getbyte" do - it_behaves_like :strscan_get_byte, :getbyte - - it "warns in verbose mode that the method is obsolete" do - s = StringScanner.new("abc") - -> { - s.getbyte - }.should complain(/getbyte.*obsolete.*get_byte/, verbose: true) - - -> { - s.getbyte - }.should_not complain(verbose: false) - end - - it_behaves_like :extract_range, :getbyte -end diff --git a/spec/ruby/library/stringscanner/getch_spec.rb b/spec/ruby/library/stringscanner/getch_spec.rb index ac43cf449d..cd41b4336a 100644 --- a/spec/ruby/library/stringscanner/getch_spec.rb +++ b/spec/ruby/library/stringscanner/getch_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'shared/extract_range' require 'strscan' @@ -11,6 +11,13 @@ describe "StringScanner#getch" do s.getch.should == "c" end + it "scans newlines too" do + s = StringScanner.new("a\nc") + s.getch.should == "a" + s.getch.should == "\n" + s.getch.should == "c" + end + it "is multi-byte character sensitive" do # Japanese hiragana "A" in EUC-JP src = "\244\242".dup.force_encoding("euc-jp") @@ -33,22 +40,20 @@ describe "StringScanner#getch" do describe "#[] successive call with a capture group name" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil" do s = StringScanner.new("This is a test") s.getch s.should.matched? - s[:a].should be_nil + s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError" do s = StringScanner.new("This is a test") s.getch s.should.matched? - -> { s[:a] }.should raise_error(IndexError) + -> { s[:a] }.should.raise(IndexError) end end @@ -59,7 +64,6 @@ describe "StringScanner#getch" do end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do s = StringScanner.new("This is a test") @@ -70,10 +74,9 @@ describe "StringScanner#getch" do s.getch s.should.matched? - s[:a].should be_nil + s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do s = StringScanner.new("This is a test") @@ -84,7 +87,7 @@ describe "StringScanner#getch" do s.getch s.should.matched? - -> { s[:a] }.should raise_error(IndexError) + -> { s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/initialize_spec.rb b/spec/ruby/library/stringscanner/initialize_spec.rb index 77f6084c1b..fdab4d381c 100644 --- a/spec/ruby/library/stringscanner/initialize_spec.rb +++ b/spec/ruby/library/stringscanner/initialize_spec.rb @@ -7,12 +7,12 @@ describe "StringScanner#initialize" do end it "is a private method" do - StringScanner.should have_private_instance_method(:initialize) + StringScanner.private_instance_methods(false).should.include?(:initialize) end it "returns an instance of StringScanner" do - @s.should be_kind_of(StringScanner) - @s.eos?.should be_false + @s.should.is_a?(StringScanner) + @s.eos?.should == false end it "converts the argument into a string using #to_str" do diff --git a/spec/ruby/library/stringscanner/inspect_spec.rb b/spec/ruby/library/stringscanner/inspect_spec.rb index ff6b97eb91..570d0b7b10 100644 --- a/spec/ruby/library/stringscanner/inspect_spec.rb +++ b/spec/ruby/library/stringscanner/inspect_spec.rb @@ -7,7 +7,7 @@ describe "StringScanner#inspect" do end it "returns a String object" do - @s.inspect.should be_kind_of(String) + @s.inspect.should.is_a?(String) end it "returns a string that represents the StringScanner object" do diff --git a/spec/ruby/library/stringscanner/match_spec.rb b/spec/ruby/library/stringscanner/match_spec.rb index a27bb51d72..c2bc49324b 100644 --- a/spec/ruby/library/stringscanner/match_spec.rb +++ b/spec/ruby/library/stringscanner/match_spec.rb @@ -45,7 +45,7 @@ describe "StringScanner#match?" do it "returns nil when matching failed" do @s.match?(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end end diff --git a/spec/ruby/library/stringscanner/matched_spec.rb b/spec/ruby/library/stringscanner/matched_spec.rb index c020bd3eae..95b57574c5 100644 --- a/spec/ruby/library/stringscanner/matched_spec.rb +++ b/spec/ruby/library/stringscanner/matched_spec.rb @@ -31,11 +31,11 @@ describe "StringScanner#matched?" do it "returns true if the last match was successful" do @s.match?(/\w+/) - @s.matched?.should be_true + @s.matched?.should == true end it "returns false if there's no match" do @s.match?(/\d+/) - @s.matched?.should be_false + @s.matched?.should == false end end diff --git a/spec/ruby/library/stringscanner/named_captures_spec.rb b/spec/ruby/library/stringscanner/named_captures_spec.rb index f35051b8cb..927784a6c4 100644 --- a/spec/ruby/library/stringscanner/named_captures_spec.rb +++ b/spec/ruby/library/stringscanner/named_captures_spec.rb @@ -1,32 +1,28 @@ require_relative '../../spec_helper' require 'strscan' -version_is StringScanner::Version, "3.0.5" do # ruby_version_is "3.2" - describe "StringScanner#named_captures" do - before do - @s = StringScanner.new('Fri Dec 12 1975 14:39') - end +describe "StringScanner#named_captures" do + before do + @s = StringScanner.new('Fri Dec 12 1975 14:39') + end - it "returns a hash of names and matched substrings for named capturing groups in a regular expression of the most recent matching" do - @s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\d+)/) - @s.named_captures.should == {"wday" => "Fri", "month" => "Dec", "day" => "12"} - end + it "returns a hash of names and matched substrings for named capturing groups in a regular expression of the most recent matching" do + @s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\d+)/) + @s.named_captures.should == {"wday" => "Fri", "month" => "Dec", "day" => "12"} + end - it "returns {} if there are no named capturing groups" do - @s.exist?(/(\w+) (\w+) (\d+)/) - @s.named_captures.should == {} - end + it "returns {} if there are no named capturing groups" do + @s.exist?(/(\w+) (\w+) (\d+)/) + @s.named_captures.should == {} + end - # https://github.com/ruby/strscan/issues/132 - ruby_bug "", "3.2"..."3.3" do # fixed in strscan v3.0.7 - it "returns {} if there is no any matching done" do - @s.named_captures.should == {} - end - end + # https://github.com/ruby/strscan/issues/132 fixed in strscan v3.0.7 + it "returns {} if there is no any matching done" do + @s.named_captures.should == {} + end - it "returns nil for an optional named capturing group if it doesn't match" do - @s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\s+)?/) - @s.named_captures.should == {"wday" => "Fri", "month" => "Dec", "day" => nil} - end + it "returns nil for an optional named capturing group if it doesn't match" do + @s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\s+)?/) + @s.named_captures.should == {"wday" => "Fri", "month" => "Dec", "day" => nil} end end diff --git a/spec/ruby/library/stringscanner/peek_spec.rb b/spec/ruby/library/stringscanner/peek_spec.rb index cbb5630ff9..5b54c6be0b 100644 --- a/spec/ruby/library/stringscanner/peek_spec.rb +++ b/spec/ruby/library/stringscanner/peek_spec.rb @@ -1,7 +1,42 @@ require_relative '../../spec_helper' -require_relative 'shared/peek' require 'strscan' describe "StringScanner#peek" do - it_behaves_like :strscan_peek, :peek + before :each do + @s = StringScanner.new('This is a test') + end + + it "returns at most the specified number of bytes from the current position" do + @s.peek(4).should == "This" + @s.pos.should == 0 + @s.pos = 5 + @s.peek(2).should == "is" + @s.peek(1000).should == "is a test" + + s = StringScanner.new("été") + s.peek(2).should == "é" + end + + it "returns an empty string when the passed argument is zero" do + @s.peek(0).should == "" + end + + it "raises a ArgumentError when the passed argument is negative" do + -> { @s.peek(-2) }.should.raise(ArgumentError) + end + + it "raises a RangeError when the passed argument is a Bignum" do + -> { @s.peek(bignum_value) }.should.raise(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.peek(1) + ch.should_not.is_a?(cls) + ch.should.instance_of?(String) + end end diff --git a/spec/ruby/library/stringscanner/peep_spec.rb b/spec/ruby/library/stringscanner/peep_spec.rb deleted file mode 100644 index bf6d579325..0000000000 --- a/spec/ruby/library/stringscanner/peep_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/peek' -require 'strscan' - -describe "StringScanner#peep" do - it_behaves_like :strscan_peek, :peep - - it "warns in verbose mode that the method is obsolete" do - s = StringScanner.new("abc") - -> { - s.peep(1) - }.should complain(/peep.*obsolete.*peek/, verbose: true) - - -> { - s.peep(1) - }.should_not complain(verbose: false) - end -end diff --git a/spec/ruby/library/stringscanner/rest_size_spec.rb b/spec/ruby/library/stringscanner/rest_size_spec.rb index e62e3a8f8c..65d3b50e08 100644 --- a/spec/ruby/library/stringscanner/rest_size_spec.rb +++ b/spec/ruby/library/stringscanner/rest_size_spec.rb @@ -1,7 +1,30 @@ require_relative '../../spec_helper' -require_relative 'shared/rest_size' require 'strscan' describe "StringScanner#rest_size" do - it_behaves_like :strscan_rest_size, :rest_size + before :each do + @s = StringScanner.new('This is a test') + end + + it "returns the length of the rest of the string" do + @s.rest_size.should == 14 + @s.scan(/This/) + @s.rest_size.should == 10 + @s.terminate + @s.rest_size.should == 0 + end + + it "is equivalent to rest.bytesize" do + @s.scan(/This/) + @s.rest_size.should == @s.rest.bytesize + + s = StringScanner.new('été') + s.rest_size.should == 5 + s.scan(/./) + s.rest_size.should == 3 + s.scan(/./) + s.rest_size.should == 2 + s.scan(/./) + s.rest_size.should == 0 + end end diff --git a/spec/ruby/library/stringscanner/rest_spec.rb b/spec/ruby/library/stringscanner/rest_spec.rb index 67072f880d..40f073058c 100644 --- a/spec/ruby/library/stringscanner/rest_spec.rb +++ b/spec/ruby/library/stringscanner/rest_spec.rb @@ -32,14 +32,14 @@ describe "StringScanner#rest?" do end it "returns true if there is more data in the string" do - @s.rest?.should be_true + @s.rest?.should == true @s.scan(/This/) - @s.rest?.should be_true + @s.rest?.should == true end it "returns false if there is no more data in the string" do @s.terminate - @s.rest?.should be_false + @s.rest?.should == false end it "is the opposite of eos?" do diff --git a/spec/ruby/library/stringscanner/restsize_spec.rb b/spec/ruby/library/stringscanner/restsize_spec.rb deleted file mode 100644 index 710520afae..0000000000 --- a/spec/ruby/library/stringscanner/restsize_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/rest_size' -require 'strscan' - -describe "StringScanner#restsize" do - it_behaves_like :strscan_rest_size, :restsize - - it "warns in verbose mode that the method is obsolete" do - s = StringScanner.new("abc") - -> { - s.restsize - }.should complain(/restsize.*obsolete.*rest_size/, verbose: true) - - -> { - s.restsize - }.should_not complain(verbose: false) - end -end diff --git a/spec/ruby/library/stringscanner/scan_byte_spec.rb b/spec/ruby/library/stringscanner/scan_byte_spec.rb index c60e22be4f..8fd77270e4 100644 --- a/spec/ruby/library/stringscanner/scan_byte_spec.rb +++ b/spec/ruby/library/stringscanner/scan_byte_spec.rb @@ -43,22 +43,20 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" describe "#[] successive call with a capture group name" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil" do s = StringScanner.new("abc") s.scan_byte s.should.matched? - s[:a].should be_nil + s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError" do s = StringScanner.new("abc") s.scan_byte s.should.matched? - -> { s[:a] }.should raise_error(IndexError) + -> { s[:a] }.should.raise(IndexError) end end @@ -69,7 +67,6 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do s = StringScanner.new("abc") @@ -83,7 +80,6 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do s = StringScanner.new("abc") @@ -94,7 +90,7 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" s.scan_byte s.should.matched? - -> { s[:a] }.should raise_error(IndexError) + -> { s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/scan_full_spec.rb b/spec/ruby/library/stringscanner/scan_full_spec.rb index 967313f5ca..7633c17860 100644 --- a/spec/ruby/library/stringscanner/scan_full_spec.rb +++ b/spec/ruby/library/stringscanner/scan_full_spec.rb @@ -38,7 +38,7 @@ describe "StringScanner#scan_full" do it "returns nil when matching failed" do @s.scan_full(/(?<a>2008)/, false, false) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end end diff --git a/spec/ruby/library/stringscanner/scan_integer_spec.rb b/spec/ruby/library/stringscanner/scan_integer_spec.rb index a0b3685bae..d83149344a 100644 --- a/spec/ruby/library/stringscanner/scan_integer_spec.rb +++ b/spec/ruby/library/stringscanner/scan_integer_spec.rb @@ -25,7 +25,7 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" end # https://github.com/ruby/strscan/issues/130 - ruby_bug "", "3.4"..."3.5" do # introduced in strscan v3.1.1 + ruby_bug "", "3.4"..."4.0" do # introduced in strscan v3.1.1 it "sets the last match result" do s = StringScanner.new("42abc") s.scan_integer @@ -44,7 +44,7 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" -> { s.scan_integer - }.should raise_error(Encoding::CompatibilityError, 'ASCII incompatible encoding: UTF-16BE') + }.should.raise(Encoding::CompatibilityError, /ASCII incompatible encoding: UTF-16BE|incompatible encoding regexp match/) end context "given base" do @@ -65,10 +65,9 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" it "raises ArgumentError when passed not supported base" do -> { StringScanner.new("42").scan_integer(base: 5) - }.should raise_error(ArgumentError, "Unsupported integer base: 5, expected 10 or 16") + }.should.raise(ArgumentError, "Unsupported integer base: 5, expected 10 or 16") end - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "does not match '0x' prefix on its own" do StringScanner.new("0x").scan_integer(base: 16).should == nil @@ -76,7 +75,6 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" StringScanner.new("+0x").scan_integer(base: 16).should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "matches '0' in a '0x' that is followed by non-hex characters" do @@ -96,7 +94,6 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" describe "#[] successive call with a capture group name" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil substring when matching succeeded" do s = StringScanner.new("42") @@ -105,13 +102,12 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError when matching succeeded" do s = StringScanner.new("42") s.scan_integer s.should.matched? - -> { s[:a] }.should raise_error(IndexError) + -> { s[:a] }.should.raise(IndexError) end end @@ -119,7 +115,7 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" s = StringScanner.new("a42") s.scan_integer s.should_not.matched? - s[:a].should be_nil + s[:a].should == nil end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" @@ -131,7 +127,6 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "does not ignore the previous matching with Regexp" do s = StringScanner.new("42") @@ -145,7 +140,6 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" s[:a].should == "42" end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "ignores the previous matching with Regexp" do s = StringScanner.new("42") @@ -156,7 +150,7 @@ version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" s.scan_integer s.should.matched? - -> { s[:a] }.should raise_error(IndexError) + -> { s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/scan_spec.rb b/spec/ruby/library/stringscanner/scan_spec.rb index 088c3419fb..ee8a9eb103 100644 --- a/spec/ruby/library/stringscanner/scan_spec.rb +++ b/spec/ruby/library/stringscanner/scan_spec.rb @@ -15,7 +15,7 @@ describe "StringScanner#scan" do it "treats ^ as matching from the beginning of the current position" do @s.scan(/\w+/).should == "This" - @s.scan(/^\d/).should be_nil + @s.scan(/^\d/).should == nil @s.scan(/^\s/).should == " " end @@ -26,7 +26,7 @@ describe "StringScanner#scan" do it "treats \\A as matching from the beginning of the current position" do @s.scan(/\w+/).should == "This" - @s.scan(/\A\d/).should be_nil + @s.scan(/\A\d/).should == nil @s.scan(/\A\s/).should == " " end @@ -41,24 +41,24 @@ describe "StringScanner#scan" do it "returns nil when there is no more to scan" do @s.scan(/[\w\s]+/).should == "This is a test" - @s.scan(/\w+/).should be_nil + @s.scan(/\w+/).should == nil end it "returns an empty string when the pattern matches empty" do @s.scan(/.*/).should == "This is a test" @s.scan(/.*/).should == "" - @s.scan(/./).should be_nil + @s.scan(/./).should == nil end it "treats String as the pattern itself" do - @s.scan("this").should be_nil + @s.scan("this").should == nil @s.scan("This").should == "This" end it "raises a TypeError if pattern isn't a Regexp nor String" do - -> { @s.scan(5) }.should raise_error(TypeError) - -> { @s.scan(:test) }.should raise_error(TypeError) - -> { @s.scan(mock('x')) }.should raise_error(TypeError) + -> { @s.scan(5) }.should.raise(TypeError) + -> { @s.scan(:test) }.should.raise(TypeError) + -> { @s.scan(mock('x')) }.should.raise(TypeError) end describe "#[] successive call with a capture group name" do @@ -71,7 +71,7 @@ describe "StringScanner#scan" do it "returns nil when matching failed" do @s.scan(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end end @@ -91,11 +91,11 @@ describe "StringScanner#scan with fixed_anchor: true" do it "treats ^ as matching from the beginning of line" do @s.scan(/\w+\n/).should == "This\n" @s.scan(/^\w/).should == "i" - @s.scan(/^\w/).should be_nil + @s.scan(/^\w/).should == nil end it "treats \\A as matching from the beginning of string" do @s.scan(/\A\w/).should == "T" - @s.scan(/\A\w/).should be_nil + @s.scan(/\A\w/).should == nil end end diff --git a/spec/ruby/library/stringscanner/scan_until_spec.rb b/spec/ruby/library/stringscanner/scan_until_spec.rb index 737d83a14c..df83f3916a 100644 --- a/spec/ruby/library/stringscanner/scan_until_spec.rb +++ b/spec/ruby/library/stringscanner/scan_until_spec.rb @@ -31,7 +31,7 @@ describe "StringScanner#scan_until" do it "raises TypeError if given a String" do -> { @s.scan_until('T') - }.should raise_error(TypeError, 'wrong argument type String (expected Regexp)') + }.should.raise(TypeError, 'wrong argument type String (expected Regexp)') end end @@ -41,7 +41,6 @@ describe "StringScanner#scan_until" do end # https://github.com/ruby/strscan/issues/131 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.1" it "sets the last match result if given a String" do @s.scan_until("a") @@ -51,7 +50,6 @@ describe "StringScanner#scan_until" do @s.post_match.should == " test" end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "sets the last match result if given a String" do @@ -75,34 +73,32 @@ describe "StringScanner#scan_until" do it "returns nil when matching failed" do @s.scan_until(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" context "when #scan_until was called with a String pattern" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil when matching succeeded" do @s.scan_until("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError when matching succeeded" do @s.scan_until("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end it "returns nil when matching failed" do @s.scan_until("2008") @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end it "returns a matching substring when given Integer index" do @@ -111,7 +107,6 @@ describe "StringScanner#scan_until" do end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -120,10 +115,9 @@ describe "StringScanner#scan_until" do @s.scan_until("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -132,7 +126,7 @@ describe "StringScanner#scan_until" do @s.scan_until("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/search_full_spec.rb b/spec/ruby/library/stringscanner/search_full_spec.rb index a089da2043..656884f46b 100644 --- a/spec/ruby/library/stringscanner/search_full_spec.rb +++ b/spec/ruby/library/stringscanner/search_full_spec.rb @@ -40,7 +40,7 @@ describe "StringScanner#search_full" do it "raises TypeError if given a String" do -> { @s.search_full('T', true, true) - }.should raise_error(TypeError, 'wrong argument type String (expected Regexp)') + }.should.raise(TypeError, 'wrong argument type String (expected Regexp)') end end @@ -50,7 +50,6 @@ describe "StringScanner#search_full" do end # https://github.com/ruby/strscan/issues/131 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.1" it "sets the last match result if given a String" do @s.search_full("is a", false, false) @@ -60,7 +59,6 @@ describe "StringScanner#search_full" do @s.post_match.should == " test" end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "sets the last match result if given a String" do @@ -84,34 +82,32 @@ describe "StringScanner#search_full" do it "returns nil when matching failed" do @s.search_full(/(?<a>2008)/, false, false) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" context "when #search_full was called with a String pattern" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil when matching succeeded" do @s.search_full("This", false, false) @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError when matching succeeded" do @s.search_full("This", false, false) @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end it "returns nil when matching failed" do @s.search_full("2008", false, false) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end it "returns a matching substring when given Integer index" do @@ -128,7 +124,7 @@ describe "StringScanner#search_full" do @s.search_full("This", false, false) @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/shared/bol.rb b/spec/ruby/library/stringscanner/shared/bol.rb index ebcdd7938f..ec5c2051b5 100644 --- a/spec/ruby/library/stringscanner/shared/bol.rb +++ b/spec/ruby/library/stringscanner/shared/bol.rb @@ -1,25 +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.send(@method).should == true s.scan(/This/) - s.send(@method).should be_false + s.send(@method).should == false s.terminate - s.send(@method).should be_false + s.send(@method).should == false s = StringScanner.new("hello\nworld") - s.bol?.should be_true + s.bol?.should == true s.scan(/\w+/) - s.bol?.should be_false + s.bol?.should == false s.scan(/\n/) - s.bol?.should be_true + s.bol?.should == true s.unscan - s.bol?.should be_false + 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 be_true + 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 index 1dbae11f7c..8138b0f8dc 100644 --- a/spec/ruby/library/stringscanner/shared/concat.rb +++ b/spec/ruby/library/stringscanner/shared/concat.rb @@ -3,28 +3,28 @@ describe :strscan_concat, shared: true do s = StringScanner.new(+"hello ") s.send(@method, 'world').should == s s.string.should == "hello world" - s.eos?.should be_false + 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_error(TypeError) - -> { StringScanner.new('hello').send(@method, mock('x')) }.should raise_error(TypeError) + -> { 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_error(TypeError) + -> { a.send(@method, 333) }.should.raise(TypeError) b = StringScanner.new("") - -> { b.send(@method, (256 * 3 + 64)) }.should raise_error(TypeError) - -> { b.send(@method, -200) }.should raise_error(TypeError) + -> { 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_error(TypeError) + -> { StringScanner.new("").send(@method, x) }.should.raise(TypeError) end end diff --git a/spec/ruby/library/stringscanner/shared/eos.rb b/spec/ruby/library/stringscanner/shared/eos.rb deleted file mode 100644 index ea04c764a2..0000000000 --- a/spec/ruby/library/stringscanner/shared/eos.rb +++ /dev/null @@ -1,17 +0,0 @@ -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/ruby/library/stringscanner/shared/extract_range.rb b/spec/ruby/library/stringscanner/shared/extract_range.rb index e7404fd0cb..c64cc41fa7 100644 --- a/spec/ruby/library/stringscanner/shared/extract_range.rb +++ b/spec/ruby/library/stringscanner/shared/extract_range.rb @@ -5,7 +5,7 @@ describe :extract_range, shared: true do s = StringScanner.new(sub) ch = s.send(@method) - ch.should_not be_kind_of(cls) - ch.should be_an_instance_of(String) + 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 index 070a132812..8a6349bec1 100644 --- a/spec/ruby/library/stringscanner/shared/extract_range_matched.rb +++ b/spec/ruby/library/stringscanner/shared/extract_range_matched.rb @@ -7,7 +7,7 @@ describe :extract_range_matched, shared: true do s.scan(/\w{1}/) ch = s.send(@method) - ch.should_not be_kind_of(cls) - ch.should be_an_instance_of(String) + ch.should_not.is_a?(cls) + ch.should.instance_of?(String) end end diff --git a/spec/ruby/library/stringscanner/shared/get_byte.rb b/spec/ruby/library/stringscanner/shared/get_byte.rb deleted file mode 100644 index f1b016905f..0000000000 --- a/spec/ruby/library/stringscanner/shared/get_byte.rb +++ /dev/null @@ -1,87 +0,0 @@ -# -*- encoding: binary -*- -require 'strscan' - -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 - - describe "#[] successive call with a capture group name" do - # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes - version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" - it "returns nil" do - s = StringScanner.new("This is a test") - s.send(@method) - s.should.matched? - s[:a].should be_nil - end - end - end - version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" - it "raises IndexError" do - s = StringScanner.new("This is a test") - s.send(@method) - s.should.matched? - -> { s[:a] }.should raise_error(IndexError) - end - end - - it "returns a matching character when given Integer index" do - s = StringScanner.new("This is a test") - s.send(@method) - s[0].should == "T" - end - - # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes - version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" - it "ignores the previous matching with Regexp" do - s = StringScanner.new("This is a test") - s.exist?(/(?<a>This)/) - s.should.matched? - s[:a].should == "This" - - s.send(@method) - s.should.matched? - s[:a].should be_nil - end - end - end - version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" - it "ignores the previous matching with Regexp" do - s = StringScanner.new("This is a test") - s.exist?(/(?<a>This)/) - s.should.matched? - s[:a].should == "This" - - s.send(@method) - s.should.matched? - -> { s[:a] }.should raise_error(IndexError) - end - end - end -end diff --git a/spec/ruby/library/stringscanner/shared/peek.rb b/spec/ruby/library/stringscanner/shared/peek.rb deleted file mode 100644 index 4c757866c1..0000000000 --- a/spec/ruby/library/stringscanner/shared/peek.rb +++ /dev/null @@ -1,39 +0,0 @@ -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 bytes 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" - - s = StringScanner.new("été") - s.send(@method, 2).should == "é" - 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 - -> { @s.send(@method, -2) }.should raise_error(ArgumentError) - end - - it "raises a RangeError when the passed argument is a Bignum" do - -> { @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 -end diff --git a/spec/ruby/library/stringscanner/shared/pos.rb b/spec/ruby/library/stringscanner/shared/pos.rb index 6d540881f2..91f80fdf08 100644 --- a/spec/ruby/library/stringscanner/shared/pos.rb +++ b/spec/ruby/library/stringscanner/shared/pos.rb @@ -22,6 +22,13 @@ describe :strscan_pos, shared: true 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 @@ -43,10 +50,10 @@ describe :strscan_pos_set, shared: true do it "raises a RangeError if position too far backward" do -> { @s.send(@method, -20) - }.should raise_error(RangeError) + }.should.raise(RangeError) end it "raises a RangeError when the passed argument is out of range" do - -> { @s.send(@method, 20) }.should raise_error(RangeError) + -> { @s.send(@method, 20) }.should.raise(RangeError) end end diff --git a/spec/ruby/library/stringscanner/shared/rest_size.rb b/spec/ruby/library/stringscanner/shared/rest_size.rb deleted file mode 100644 index 4c4f49e45c..0000000000 --- a/spec/ruby/library/stringscanner/shared/rest_size.rb +++ /dev/null @@ -1,18 +0,0 @@ -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/ruby/library/stringscanner/shared/terminate.rb b/spec/ruby/library/stringscanner/shared/terminate.rb deleted file mode 100644 index bf41d097e2..0000000000 --- a/spec/ruby/library/stringscanner/shared/terminate.rb +++ /dev/null @@ -1,8 +0,0 @@ -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 diff --git a/spec/ruby/library/stringscanner/skip_spec.rb b/spec/ruby/library/stringscanner/skip_spec.rb index 12f5b7781c..2b955b3172 100644 --- a/spec/ruby/library/stringscanner/skip_spec.rb +++ b/spec/ruby/library/stringscanner/skip_spec.rb @@ -26,7 +26,7 @@ describe "StringScanner#skip" do it "returns nil when matching failed" do @s.skip(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end end diff --git a/spec/ruby/library/stringscanner/skip_until_spec.rb b/spec/ruby/library/stringscanner/skip_until_spec.rb index f5be4b5ceb..508db285ba 100644 --- a/spec/ruby/library/stringscanner/skip_until_spec.rb +++ b/spec/ruby/library/stringscanner/skip_until_spec.rb @@ -27,7 +27,7 @@ describe "StringScanner#skip_until" do it "raises TypeError if given a String" do -> { @s.skip_until('T') - }.should raise_error(TypeError, 'wrong argument type String (expected Regexp)') + }.should.raise(TypeError, 'wrong argument type String (expected Regexp)') end end @@ -38,7 +38,6 @@ describe "StringScanner#skip_until" do end # https://github.com/ruby/strscan/issues/131 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.1" it "sets the last match result if given a String" do @s.skip_until("a") @@ -48,7 +47,6 @@ describe "StringScanner#skip_until" do @s.post_match.should == " test" end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "sets the last match result if given a String" do @@ -72,34 +70,32 @@ describe "StringScanner#skip_until" do it "returns nil when matching failed" do @s.skip_until(/(?<a>2008)/) @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end end version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4" context "when #skip_until was called with a String pattern" do # https://github.com/ruby/strscan/issues/139 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "returns nil when matching succeeded" do @s.skip_until("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3" it "raises IndexError when matching succeeded" do @s.skip_until("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end it "returns nil when matching failed" do @s.skip_until("2008") @s.should_not.matched? - @s[:a].should be_nil + @s[:a].should == nil end it "returns a matching substring when given Integer index" do @@ -108,7 +104,6 @@ describe "StringScanner#skip_until" do end # https://github.com/ruby/strscan/issues/135 - ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -117,10 +112,9 @@ describe "StringScanner#skip_until" do @s.skip_until("This") @s.should.matched? - @s[:a].should be_nil + @s[:a].should == nil end end - end version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4" it "ignores the previous matching with Regexp" do @s.exist?(/(?<a>This)/) @@ -129,7 +123,7 @@ describe "StringScanner#skip_until" do @s.skip_until("This") @s.should.matched? - -> { @s[:a] }.should raise_error(IndexError) + -> { @s[:a] }.should.raise(IndexError) end end end diff --git a/spec/ruby/library/stringscanner/string_spec.rb b/spec/ruby/library/stringscanner/string_spec.rb index cba6bd51dd..6cbbff4822 100644 --- a/spec/ruby/library/stringscanner/string_spec.rb +++ b/spec/ruby/library/stringscanner/string_spec.rb @@ -14,7 +14,7 @@ describe "StringScanner#string" do end it "returns the identical object passed in" do - @s.string.equal?(@string).should be_true + @s.string.equal?(@string).should == true end end diff --git a/spec/ruby/library/stringscanner/terminate_spec.rb b/spec/ruby/library/stringscanner/terminate_spec.rb index 7943efe0f9..3cff5c010c 100644 --- a/spec/ruby/library/stringscanner/terminate_spec.rb +++ b/spec/ruby/library/stringscanner/terminate_spec.rb @@ -1,7 +1,11 @@ require_relative '../../spec_helper' -require_relative 'shared/terminate' require 'strscan' describe "StringScanner#terminate" do - it_behaves_like :strscan_terminate, :terminate + it "set the scan pointer to the end of the string and clear matching data." do + s = StringScanner.new('This is a test') + s.terminate + s.should_not.bol? + s.should.eos? + end end diff --git a/spec/ruby/library/stringscanner/unscan_spec.rb b/spec/ruby/library/stringscanner/unscan_spec.rb index df0ea43367..f738778273 100644 --- a/spec/ruby/library/stringscanner/unscan_spec.rb +++ b/spec/ruby/library/stringscanner/unscan_spec.rb @@ -21,8 +21,8 @@ describe "StringScanner#unscan" do @s.pos.should == pos end - it "raises a ScanError when the previous match had failed" do - -> { @s.unscan }.should raise_error(ScanError) - -> { @s.scan(/\d/); @s.unscan }.should raise_error(ScanError) + it "raises a StringScanner::Error when the previous match had failed" do + -> { @s.unscan }.should.raise(StringScanner::Error) + -> { @s.scan(/\d/); @s.unscan }.should.raise(StringScanner::Error) end end diff --git a/spec/ruby/library/stringscanner/values_at_spec.rb b/spec/ruby/library/stringscanner/values_at_spec.rb index 14d4a5f6a7..b00cce0ffa 100644 --- a/spec/ruby/library/stringscanner/values_at_spec.rb +++ b/spec/ruby/library/stringscanner/values_at_spec.rb @@ -35,7 +35,7 @@ describe "StringScanner#captures" do -> { @s.values_at("foo") - }.should raise_error(IndexError, "undefined group name reference: foo") + }.should.raise(IndexError, "undefined group name reference: foo") end end @@ -54,7 +54,7 @@ describe "StringScanner#captures" do -> { @s.values_at([]) - }.should raise_error(TypeError, "no implicit conversion of Array into Integer") + }.should.raise(TypeError, "no implicit conversion of Array into Integer") end it "returns nil if the most recent matching fails" do |
