Skip to content

Commit e50e94a

Browse files
committed
Update stringscanner specs
Newer stringscanner needs newer specs for deprecated methods removed in recent releases.
1 parent 84e3fb9 commit e50e94a

39 files changed

Lines changed: 1333 additions & 233 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require_relative '../../spec_helper'
2+
require 'strscan'
3+
4+
describe "StringScanner#captures" do
5+
before do
6+
@s = StringScanner.new('Fri Dec 12 1975 14:39')
7+
end
8+
9+
it "returns the array of captured values of the most recent matching" do
10+
@s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\d+)/)
11+
@s.captures.should == ["Fri", "Dec", "12"]
12+
end
13+
14+
it "returns nil if the last match fails" do
15+
@s.scan(/nope/)
16+
@s.captures.should == nil
17+
end
18+
19+
it "returns nil if there is no any match done" do
20+
@s.captures.should == nil
21+
end
22+
23+
version_is StringScanner::Version, ""..."3.0.8" do # ruby_version_is ""..."3.3.3"
24+
it "returns '' for an optional capturing group if it doesn't match" do
25+
@s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\s+)?/)
26+
@s.captures.should == ["Fri", "Dec", ""]
27+
end
28+
end
29+
30+
version_is StringScanner::Version, "3.0.8" do # ruby_version_is "3.3.3"
31+
it "returns nil for an optional capturing group if it doesn't match" do
32+
@s.exist?(/(?<wday>\w+) (?<month>\w+) (?<day>\s+)?/)
33+
@s.captures.should == ["Fri", "Dec", nil]
34+
end
35+
end
36+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require_relative '../../spec_helper'
2+
require 'strscan'
3+
4+
describe "StringScanner#charpos" do
5+
it "returns character index corresponding to the current position" do
6+
s = StringScanner.new("abc")
7+
8+
s.scan_until(/b/)
9+
s.charpos.should == 2
10+
end
11+
12+
it "is multi-byte character sensitive" do
13+
s = StringScanner.new("abcädeföghi")
14+
15+
s.scan_until(/ö/)
16+
s.charpos.should == 8
17+
end
18+
end

spec/ruby/library/stringscanner/check_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,72 @@
2222
@s.matched.should == nil
2323
end
2424

25+
describe "#[] successive call with a capture group name" do
26+
context "when #check was called with a Regexp pattern" do
27+
it "returns matched substring when matching succeeded" do
28+
@s.check(/(?<a>This)/)
29+
@s.should.matched?
30+
@s[:a].should == "This"
31+
end
32+
33+
it "returns nil when matching failed" do
34+
@s.check(/(?<a>2008)/)
35+
@s.should_not.matched?
36+
@s[:a].should be_nil
37+
end
38+
end
39+
40+
context "when #check was called with a String pattern" do
41+
# https://github.com/ruby/strscan/issues/139
42+
version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
43+
it "returns nil when matching succeeded" do
44+
@s.check("This")
45+
@s.should.matched?
46+
@s[:a].should be_nil
47+
end
48+
end
49+
version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4"
50+
it "raises IndexError when matching succeeded" do
51+
@s.check("This")
52+
@s.should.matched?
53+
-> { @s[:a] }.should raise_error(IndexError)
54+
end
55+
end
56+
57+
it "returns nil when matching failed" do
58+
@s.check("2008")
59+
@s.should_not.matched?
60+
@s[:a].should be_nil
61+
end
62+
63+
it "returns a matching substring when given Integer index" do
64+
@s.check("This")
65+
@s[0].should == "This"
66+
end
67+
68+
# https://github.com/ruby/strscan/issues/135
69+
version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
70+
it "ignores the previous matching with Regexp" do
71+
@s.exist?(/(?<a>This)/)
72+
@s.should.matched?
73+
@s[:a].should == "This"
74+
75+
@s.check("This")
76+
@s.should.matched?
77+
@s[:a].should be_nil
78+
end
79+
end
80+
version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
81+
it "ignores the previous matching with Regexp" do
82+
@s.exist?(/(?<a>This)/)
83+
@s.should.matched?
84+
@s[:a].should == "This"
85+
86+
@s.check("This")
87+
@s.should.matched?
88+
-> { @s[:a] }.should raise_error(IndexError)
89+
end
90+
end
91+
end
92+
end
2593
end

spec/ruby/library/stringscanner/check_until_spec.rb

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,128 @@
22
require 'strscan'
33

44
describe "StringScanner#check_until" do
5-
before :each do
5+
before do
66
@s = StringScanner.new("This is a test")
77
end
88

9-
it "returns the same value of scan_until, but don't advances the scan pointer" do
9+
it "returns the same value of #scan_until, but don't advances the scan pointer" do
1010
@s.check_until(/a/).should == "This is a"
1111
@s.pos.should == 0
12-
@s.matched.should == "a"
1312
@s.check_until(/test/).should == "This is a test"
1413
end
1514

16-
ruby_version_is ""..."3.4" do
15+
it "sets the last match result" do
16+
@s.check_until(/a/)
17+
18+
@s.pre_match.should == "This is "
19+
@s.matched.should == "a"
20+
@s.post_match.should == " test"
21+
end
22+
23+
version_is StringScanner::Version, ""..."3.1.1" do # ruby_version_is ""..."3.4"
1724
it "raises TypeError if given a String" do
1825
-> {
1926
@s.check_until('T')
2027
}.should raise_error(TypeError, 'wrong argument type String (expected Regexp)')
2128
end
2229
end
30+
31+
version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4"
32+
it "searches a substring in the rest part of a string if given a String" do
33+
@s.check_until("a").should == "This is a"
34+
@s.pos.should == 0
35+
end
36+
37+
# https://github.com/ruby/strscan/issues/131
38+
version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.1"
39+
it "sets the last match result if given a String" do
40+
@s.check_until("a")
41+
42+
@s.pre_match.should == ""
43+
@s.matched.should == "This is a"
44+
@s.post_match.should == " test"
45+
end
46+
end
47+
48+
version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4"
49+
it "sets the last match result if given a String" do
50+
@s.check_until("a")
51+
52+
@s.pre_match.should == "This is "
53+
@s.matched.should == "a"
54+
@s.post_match.should == " test"
55+
end
56+
end
57+
end
58+
59+
describe "#[] successive call with a capture group name" do
60+
context "when #check_until was called with a Regexp pattern" do
61+
it "returns matched substring when matching succeeded" do
62+
@s.check_until(/(?<a>This)/)
63+
@s.should.matched?
64+
@s[:a].should == "This"
65+
end
66+
67+
it "returns nil when matching failed" do
68+
@s.check_until(/(?<a>2008)/)
69+
@s.should_not.matched?
70+
@s[:a].should be_nil
71+
end
72+
end
73+
74+
version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4"
75+
context "when #check_until was called with a String pattern" do
76+
# https://github.com/ruby/strscan/issues/139
77+
version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
78+
it "returns nil when matching succeeded" do
79+
@s.check_until("This")
80+
@s.should.matched?
81+
@s[:a].should be_nil
82+
end
83+
end
84+
version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3"
85+
it "raises IndexError when matching succeeded" do
86+
@s.check_until("This")
87+
@s.should.matched?
88+
-> { @s[:a] }.should raise_error(IndexError)
89+
end
90+
end
91+
92+
it "returns nil when matching failed" do
93+
@s.check_until("2008")
94+
@s.should_not.matched?
95+
@s[:a].should be_nil
96+
end
97+
98+
it "returns a matching substring when given Integer index" do
99+
@s.check_until("This")
100+
@s[0].should == "This"
101+
end
102+
103+
# https://github.com/ruby/strscan/issues/135
104+
version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
105+
it "ignores the previous matching with Regexp" do
106+
@s.exist?(/(?<a>This)/)
107+
@s.should.matched?
108+
@s[:a].should == "This"
109+
110+
@s.check_until("This")
111+
@s.should.matched?
112+
@s[:a].should be_nil
113+
end
114+
end
115+
version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
116+
it "ignores the previous matching with Regexp" do
117+
@s.exist?(/(?<a>This)/)
118+
@s.should.matched?
119+
@s[:a].should == "This"
120+
121+
@s.check_until("This")
122+
@s.should.matched?
123+
-> { @s[:a] }.should raise_error(IndexError)
124+
end
125+
end
126+
end
127+
end
128+
end
23129
end

spec/ruby/library/stringscanner/clear_spec.rb

Lines changed: 0 additions & 18 deletions
This file was deleted.

spec/ruby/library/stringscanner/element_reference_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
it "returns nil if there is no current match" do
1010
@s[0].should be_nil
11+
@s[:wday].should be_nil
1112
end
1213

1314
it "returns the n-th subgroup in the most recent match" do
@@ -42,12 +43,18 @@
4243
-> { @s[0..2]}.should raise_error(TypeError)
4344
end
4445

45-
it "raises a IndexError when there's no named capture" do
46+
it "raises a IndexError when there's no any named capture group in the regexp" do
4647
@s.scan(/(\w+) (\w+) (\d+) /)
4748
-> { @s["wday"]}.should raise_error(IndexError)
4849
-> { @s[:wday]}.should raise_error(IndexError)
4950
end
5051

52+
it "raises a IndexError when given a not existing capture group name" do
53+
@s.scan(/(?<a>\w+) (?<b>\w+) (?<c>\d+) /)
54+
-> { @s["wday"]}.should raise_error(IndexError)
55+
-> { @s[:wday]}.should raise_error(IndexError)
56+
end
57+
5158
it "returns named capture" do
5259
@s.scan(/(?<wday>\w+) (?<month>\w+) (?<day>\d+) /)
5360
@s["wday"].should == "Fri"

spec/ruby/library/stringscanner/empty_spec.rb

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/eos'
32
require 'strscan'
43

54
describe "StringScanner#eos?" do
6-
it_behaves_like :strscan_eos, :eos?
5+
before :each do
6+
@s = StringScanner.new("This is a test")
7+
end
8+
9+
it "returns true if the scan pointer is at the end of the string" do
10+
@s.terminate
11+
@s.should.eos?
12+
13+
s = StringScanner.new('')
14+
s.should.eos?
15+
end
16+
17+
it "returns false if the scan pointer is not at the end of the string" do
18+
@s.should_not.eos?
19+
end
720
end

0 commit comments

Comments
 (0)