Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 198 lines (157 sloc) 5.425 kb
24ce35c @brixen Added encoding comments for files containing utf-8 chars.
brixen authored
1 # -*- encoding: utf-8 -*-
957c2c9 @brixen Standardize require lines.
brixen authored
2 require File.expand_path('../../../spec_helper', __FILE__)
3 require File.expand_path('../fixtures/classes.rb', __FILE__)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
4
5 describe "String#scan" do
50aeea2 Add spec for String#scan + $KCODE
Evan Phoenix authored
6 before :each do
24ce35c @brixen Added encoding comments for files containing utf-8 chars.
brixen authored
7 @kcode = $KCODE
50aeea2 Add spec for String#scan + $KCODE
Evan Phoenix authored
8 end
9
10 after :each do
24ce35c @brixen Added encoding comments for files containing utf-8 chars.
brixen authored
11 $KCODE = @kcode
50aeea2 Add spec for String#scan + $KCODE
Evan Phoenix authored
12 end
13
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
14 it "returns an array containing all matches" do
15 "cruel world".scan(/\w+/).should == ["cruel", "world"]
16 "cruel world".scan(/.../).should == ["cru", "el ", "wor"]
326a538 @brixen Fixed use of guards in the specs.
brixen authored
17
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
18 # Edge case
19 "hello".scan(//).should == ["", "", "", "", "", ""]
20 "".scan(//).should == [""]
21 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
22
50aeea2 Add spec for String#scan + $KCODE
Evan Phoenix authored
23 it "respects $KCODE when the pattern collapses to nothing" do
24 str = "こにちわ"
25 reg = %r!!
26
24ce35c @brixen Added encoding comments for files containing utf-8 chars.
brixen authored
27 $KCODE = "utf-8"
50aeea2 Add spec for String#scan + $KCODE
Evan Phoenix authored
28
29 str.scan(reg).should == ["", "", "", "", ""]
30 end
31
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
32 it "stores groups as arrays in the returned arrays" do
33 "hello".scan(/()/).should == [[""]] * 6
34 "hello".scan(/()()/).should == [["", ""]] * 6
35 "cruel world".scan(/(...)/).should == [["cru"], ["el "], ["wor"]]
36 "cruel world".scan(/(..)(..)/).should == [["cr", "ue"], ["l ", "wo"]]
37 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
38
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
39 it "scans for occurrences of the string if pattern is a string" do
40 "one two one two".scan('one').should == ["one", "one"]
41 "hello.".scan('.').should == ['.']
42 end
43
44 it "sets $~ to MatchData of last match and nil when there's none" do
45 'hello.'.scan(/.(.)/)
46 $~[0].should == 'o.'
47
48 'hello.'.scan(/not/)
49 $~.should == nil
50
51 'hello.'.scan('l')
52 $~.begin(0).should == 3
53 $~[0].should == 'l'
54
55 'hello.'.scan('not')
56 $~.should == nil
57 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
58
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
59 it "supports \\G which matches the end of the previous match / string start for first match" do
60 "one two one two".scan(/\G\w+/).should == ["one"]
61 "one two one two".scan(/\G\w+\s*/).should == ["one ", "two ", "one ", "two"]
62 "one two one two".scan(/\G\s*\w+/).should == ["one", " two", " one", " two"]
63 end
64
65 it "tries to convert pattern to a string via to_str" do
66 obj = mock('o')
67 obj.should_receive(:to_str).and_return("o")
68 "o_o".scan(obj).should == ["o", "o"]
69 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
70
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
71 it "raises a TypeError if pattern isn't a Regexp and can't be converted to a String" do
72 lambda { "cruel world".scan(5) }.should raise_error(TypeError)
73 lambda { "cruel world".scan(:test) }.should raise_error(TypeError)
74 lambda { "cruel world".scan(mock('x')) }.should raise_error(TypeError)
75 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
76
dc4cce3 @brixen Fixed some String specs.
brixen authored
77 ruby_bug "#4087", "1.9.2.135" do
78 it "taints the results if the String argument is tainted" do
cb591a5 @nurse Follow MRI r29932 [ruby-core:33338]
nurse authored
79 a = "hello hello hello".scan("hello".taint)
dc4cce3 @brixen Fixed some String specs.
brixen authored
80 a.each { |m| m.tainted?.should be_true }
cb591a5 @nurse Follow MRI r29932 [ruby-core:33338]
nurse authored
81 end
82 end
83
dc4cce3 @brixen Fixed some String specs.
brixen authored
84 it "taints the results when passed a String argument if self is tainted" do
85 a = "hello hello hello".taint.scan("hello")
86 a.each { |m| m.tainted?.should be_true }
87 end
cb591a5 @nurse Follow MRI r29932 [ruby-core:33338]
nurse authored
88
dc4cce3 @brixen Fixed some String specs.
brixen authored
89 it "taints the results if the Regexp argument is tainted" do
90 a = "hello".scan(/./.taint)
91 a.each { |m| m.tainted?.should be_true }
92 end
cb591a5 @nurse Follow MRI r29932 [ruby-core:33338]
nurse authored
93
dc4cce3 @brixen Fixed some String specs.
brixen authored
94 it "taints the results when passed a Regexp argument if self is tainted" do
95 a = "hello".taint.scan(/./)
96 a.each { |m| m.tainted?.should be_true }
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
97 end
98 end
99
100 describe "String#scan with pattern and block" do
101 it "returns self" do
102 s = "foo"
5a1271e @arthurschreiber Use 'obj.should equal(other_obj)' instead of 'obj.equal?(other_obj).shou...
arthurschreiber authored
103 s.scan(/./) {}.should equal(s)
104 s.scan(/roar/) {}.should equal(s)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
105 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
106
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
107 it "passes each match to the block as one argument: an array" do
108 a = []
109 "cruel world".scan(/\w+/) { |*w| a << w }
110 a.should == [["cruel"], ["world"]]
111 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
112
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
113 it "passes groups to the block as one argument: an array" do
114 a = []
115 "cruel world".scan(/(..)(..)/) { |w| a << w }
116 a.should == [["cr", "ue"], ["l ", "wo"]]
117 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
118
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
119 it "sets $~ for access from the block" do
120 str = "hello"
121
122 matches = []
123 offsets = []
326a538 @brixen Fixed use of guards in the specs.
brixen authored
124
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
125 str.scan(/([aeiou])/) do
126 md = $~
127 md.string.should == str
128 matches << md.to_a
129 offsets << md.offset(0)
130 str
131 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
132
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
133 matches.should == [["e", "e"], ["o", "o"]]
134 offsets.should == [[1, 2], [4, 5]]
135
136 matches = []
137 offsets = []
326a538 @brixen Fixed use of guards in the specs.
brixen authored
138
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
139 str.scan("l") do
140 md = $~
141 md.string.should == str
142 matches << md.to_a
143 offsets << md.offset(0)
144 str
145 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
146
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
147 matches.should == [["l"], ["l"]]
148 offsets.should == [[2, 3], [3, 4]]
149 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
150
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
151 it "restores $~ after leaving the block" do
152 [/./, "l"].each do |pattern|
153 old_md = nil
154 "hello".scan(pattern) do
155 old_md = $~
156 "ok".match(/./)
157 "x"
158 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
159
7ed5bfc @runpaint String#scan: Fix 1.9 spec failure involving $~
runpaint authored
160 $~[0].should == old_md[0]
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
161 $~.string.should == "hello"
162 end
163 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
164
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
165 it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
166 'hello.'.scan('l') { 'x' }
167 $~.begin(0).should == 3
168 $~[0].should == 'l'
169
170 'hello.'.scan('not') { 'x' }
171 $~.should == nil
172
173 'hello.'.scan(/.(.)/) { 'x' }
174 $~[0].should == 'o.'
175
176 'hello.'.scan(/not/) { 'x' }
177 $~.should == nil
178 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
179
dc4cce3 @brixen Fixed some String specs.
brixen authored
180 ruby_bug "#4087", "1.9.2.135" do
181 it "taints the results if the String argument is tainted" do
182 "hello hello hello".scan("hello".taint).each { |m| m.tainted?.should be_true }
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
183 end
cb591a5 @nurse Follow MRI r29932 [ruby-core:33338]
nurse authored
184 end
185
dc4cce3 @brixen Fixed some String specs.
brixen authored
186 it "taints the results when passed a String argument if self is tainted" do
187 "hello hello hello".taint.scan("hello").each { |m| m.tainted?.should be_true }
188 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
189
dc4cce3 @brixen Fixed some String specs.
brixen authored
190 it "taints the results if the Regexp argument is tainted" do
191 "hello".scan(/./.taint).each { |m| m.tainted?.should be_true }
192 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
193
dc4cce3 @brixen Fixed some String specs.
brixen authored
194 it "taints the results when passed a Regexp argument if self is tainted" do
195 "hello".taint.scan(/./).each { |m| m.tainted?.should be_true }
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
196 end
197 end
Something went wrong with that request. Please try again.