|
2 | 2 | require 'strscan' |
3 | 3 |
|
4 | 4 | describe "StringScanner#check_until" do |
5 | | - before :each do |
| 5 | + before do |
6 | 6 | @s = StringScanner.new("This is a test") |
7 | 7 | end |
8 | 8 |
|
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 |
10 | 10 | @s.check_until(/a/).should == "This is a" |
11 | 11 | @s.pos.should == 0 |
12 | | - @s.matched.should == "a" |
13 | 12 | @s.check_until(/test/).should == "This is a test" |
14 | 13 | end |
15 | 14 |
|
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" |
17 | 24 | it "raises TypeError if given a String" do |
18 | 25 | -> { |
19 | 26 | @s.check_until('T') |
20 | 27 | }.should raise_error(TypeError, 'wrong argument type String (expected Regexp)') |
21 | 28 | end |
22 | 29 | 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 |
23 | 129 | end |
0 commit comments