bfts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ require 'test/unit'
2
+ require 'rubicon_testcase'
3
+
4
+ class TestNilClass < RubiconTestCase
5
+
6
+ def test_and
7
+ truth_table(nil.method("&"), false, false)
8
+ end
9
+
10
+ def test_carat
11
+ truth_table(nil.method("^"), false, true)
12
+ $global = 0
13
+ assert_equal(true, nil ^ util_side_effect)
14
+ assert_equal(1, $global)
15
+ end
16
+
17
+ def test_inspect
18
+ # TODO: raise NotImplementedError, 'Need to write test_inspect'
19
+ end
20
+
21
+ def test_nil_eh
22
+ assert(nil.nil?)
23
+ end
24
+
25
+ def test_or
26
+ truth_table(nil.method("|"), false, true)
27
+ $global = 0
28
+ assert_equal(true, nil | util_side_effect)
29
+ assert_equal(1, $global)
30
+ end
31
+
32
+ def test_to_a
33
+ assert_equal([], nil.to_a)
34
+ end
35
+
36
+ def test_to_f
37
+ assert_equal(0.0, nil.to_f)
38
+ end
39
+
40
+ def test_to_i
41
+ assert_equal(0, nil.to_i)
42
+ end
43
+
44
+ def test_to_s
45
+ assert_equal("", nil.to_s)
46
+ end
47
+
48
+ def util_side_effect
49
+ $global = 1
50
+ end
51
+ end
@@ -0,0 +1,281 @@
1
+ require 'test/unit'
2
+ require 'rubicon_testcase'
3
+
4
+ class TestRange < RubiconTestCase
5
+
6
+ ############################################################
7
+ # Test Utilities:
8
+
9
+ #
10
+ # Tests where the methods == and eql? behave the same.
11
+ # This is true when the class of the endpoints of
12
+ # the Range have == and eql? methods that are
13
+ # aliases. This in turn is true for most classes,
14
+ # for example for the class used here: Fixnum.
15
+ #
16
+ # TODO: write tests using an endpoint class where
17
+ # the methods == and eql? differ.
18
+ #
19
+ def util_test_equals(method)
20
+
21
+ r25 = Range.new(2, 5)
22
+ r34 = Range.new(3, 4)
23
+ r35 = Range.new(3, 5)
24
+ r36 = Range.new(3, 6)
25
+ r45 = Range.new(4, 5)
26
+ rx25 = Range.new(2, 5, true)
27
+ rx34 = Range.new(3, 4, true)
28
+ rx35 = Range.new(3, 5, true)
29
+ rx36 = Range.new(3, 6, true)
30
+ rx45 = Range.new(4, 5, true)
31
+
32
+ # closed interval
33
+ assert_equal(false, r35.send(method, r34))
34
+ assert_equal(true, r35.send(method, r35))
35
+ assert_equal(false, r35.send(method, r36))
36
+
37
+ assert_equal(false, r35.send(method, r25))
38
+ assert_equal(false, r35.send(method, r45))
39
+
40
+ # half-open interval
41
+ assert_equal(false, rx35.send(method, rx34))
42
+ assert_equal(true, rx35.send(method, rx35))
43
+ assert_equal(false, rx35.send(method, rx36))
44
+
45
+ assert_equal(false, rx35.send(method, rx25))
46
+ assert_equal(false, rx35.send(method, rx45))
47
+
48
+ # half-open / closed interval: never equal
49
+ assert_equal(false, rx35.send(method, r34))
50
+ assert_equal(false, rx35.send(method, r35))
51
+ assert_equal(false, rx35.send(method, r36))
52
+
53
+ assert_equal(false, rx35.send(method, r25))
54
+ assert_equal(false, rx35.send(method, r45))
55
+
56
+ # closed / half-open interval: never equal
57
+ assert_equal(false, r35.send(method, rx34))
58
+ assert_equal(false, r35.send(method, rx35))
59
+ assert_equal(false, r35.send(method, rx36))
60
+
61
+ assert_equal(false, r35.send(method, rx25))
62
+ assert_equal(false, r35.send(method, rx45))
63
+
64
+ # non-Range argument
65
+ assert_equal(false, r35.send(method, Object.new))
66
+ assert_equal(false, rx35.send(method, Object.new))
67
+
68
+ end
69
+
70
+ def util_test_end(msg)
71
+ assert_equal(10, Range.new(1, 10).send(msg))
72
+ assert_equal(11, Range.new(1, 11, true).send(msg))
73
+ assert_equal("z", Range.new("a", "z").send(msg))
74
+ assert_equal("A", Range.new("a", "A", true).send(msg))
75
+ end
76
+
77
+ def util_member(method)
78
+ is = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
79
+ xs = is.map {|x| 0.5 + x}
80
+ iss = ["aj", "ak", "al", "am", "an" ,"ao" "ap"]
81
+ xss = ["ajjj", "akkk", "alll", "ammm", "annn" ,"aooo" "appp"]
82
+
83
+ r47 = Range.new(4, 7)
84
+ rx47 = Range.new(4, 7, true)
85
+ r_akam = Range.new("ak", "am")
86
+ rx_akam = Range.new("ak", "am", true)
87
+
88
+ # as discrete range
89
+ assert_equal([4, 5, 6, 7 ], is.select {|i| r47.send(method, i) })
90
+ assert_equal([4, 5, 6], is.select {|i| rx47.send(method, i) })
91
+
92
+ assert_equal(["ak","al","am"], iss.select {|i| r_akam.send(method, i) })
93
+ assert_equal(["ak","al"], iss.select {|i| rx_akam.send(method, i) })
94
+
95
+ # as continuous range
96
+ assert_equal([4.5, 5.5, 6.5], xs.select {|x| r47.send(method, x) })
97
+ assert_equal([4.5, 5.5, 6.5], xs.select {|x| rx47.send(method, x) })
98
+
99
+ assert_equal(["akkk","alll"], xss.select {|i| r_akam.send(method, i) })
100
+ assert_equal(["akkk","alll"], xss.select {|i| rx_akam.send(method, i) })
101
+
102
+ # non-comparable argument
103
+ assert_equal(false, Range.new(5, 10) === Object.new)
104
+ assert_equal(false, Range.new(5, 10, true) === Object.new)
105
+ end
106
+
107
+ def util_step_tester(acc_facit, range, *step_args)
108
+ acc = []
109
+ res = range.step(*step_args) {|x| acc << x }
110
+ assert_equal(acc_facit, acc)
111
+ assert_same(range, res)
112
+ end
113
+
114
+ def util_each(first, last, exclude, expected)
115
+ index = first
116
+ count = 0
117
+ Range.new(first, last, exclude).each do |x|
118
+ assert_equal(index, x)
119
+ index = index.succ
120
+ count += 1
121
+ end
122
+ assert_equal(expected, count)
123
+ end
124
+
125
+ ############################################################
126
+ # Test Methods
127
+
128
+ # TODO: need to test new/initialize, esp w/ bad values
129
+
130
+ def test_begin
131
+ assert_equal(1, Range.new(1, 10).begin)
132
+ assert_equal("a", Range.new("a", "z").begin)
133
+ assert_equal(1, Range.new(1, 10, true).begin)
134
+ assert_equal("a", Range.new("a", "z", true).begin)
135
+ end
136
+
137
+ def test_each
138
+ util_each(1, 10, false, 10)
139
+ util_each(1, 10, true, 9)
140
+
141
+ util_each("A", "J", false, 10)
142
+ util_each("A", "J", true, 9)
143
+
144
+ # TODO: test something that has a .succ, but is neither int nor string
145
+ t1 = Time.at(1)
146
+ t10 = Time.at(10)
147
+ util_each(t1, t10, false, 10)
148
+ util_each(t1, t10, true, 9)
149
+
150
+ # test something that has no .succ:
151
+ # HACK:
152
+ # util_each(Object.new, Object.new, false, 10)
153
+ # util_each(Object.new, Object.new, true, 9)
154
+ end
155
+
156
+ def test_end
157
+ util_test_end(:end)
158
+ end
159
+
160
+ def test_eql_eh
161
+ util_test_equals(:eql?)
162
+ end
163
+
164
+ def test_equals2 # '=='
165
+ util_test_equals(:==)
166
+ end
167
+
168
+ def test_equals3 # '==='
169
+ util_member(:===)
170
+
171
+ # misc
172
+ gotit = false
173
+ case 52
174
+ when Range.new(0, 49)
175
+ fail("Shouldn't have matched")
176
+ when Range.new(50, 75)
177
+ gotit = true
178
+ else
179
+ fail("Shouldn't have matched")
180
+ end
181
+ assert_equal(true,gotit)
182
+
183
+ gotit = false
184
+ case 50
185
+ when Range.new(0, 49)
186
+ fail("Shouldn't have matched")
187
+ when Range.new(50, 75)
188
+ gotit = true
189
+ else
190
+ fail("Shouldn't have matched")
191
+ end
192
+ assert_equal(true,gotit)
193
+
194
+ gotit = false
195
+ case 75
196
+ when Range.new(0, 49)
197
+ fail("Shouldn't have matched")
198
+ when Range.new(50, 75)
199
+ gotit = true
200
+ else
201
+ fail("Shouldn't have matched")
202
+ end
203
+ assert_equal(true,gotit)
204
+ end
205
+
206
+ def test_exclude_end_eh
207
+ assert_equal(true, Range.new(1, 10, true).exclude_end?)
208
+ assert_equal(false,Range.new(1, 10).exclude_end?)
209
+ assert_equal(true, Range.new("A", "Z", true).exclude_end?)
210
+ assert_equal(false,Range.new("A", "Z").exclude_end?)
211
+ end
212
+
213
+ def test_first
214
+ assert_equal(1, Range.new(1, 10).first)
215
+ assert_equal("a", Range.new("a", "z").first)
216
+ assert_equal(1, Range.new(1, 10, true).first)
217
+ assert_equal("a", Range.new("a", "z", true).first)
218
+ end
219
+
220
+ def test_hash
221
+ assert_equal(Range.new(5, 9).hash, Range.new(5, 9).hash)
222
+ assert_equal(Range.new("A", "Z").hash, Range.new("A", "Z").hash)
223
+
224
+ assert_equal(Range.new(5, 9, true).hash, Range.new(5, 9, true).hash)
225
+ assert_equal(Range.new("A", "Z", true).hash, Range.new("A", "Z", true).hash)
226
+
227
+ assert_not_equal(Range.new(5, 9).hash, Range.new(5, 9, true).hash)
228
+ assert_not_equal(Range.new("A", "Z").hash, Range.new("A", "Z", true).hash)
229
+
230
+ assert_not_equal(Range.new(5, 9).hash, Range.new(5, 8).hash)
231
+ assert_not_equal(Range.new("A", "Z").hash, Range.new("a", "Z", true).hash)
232
+ end
233
+
234
+ def test_include_eh
235
+ util_member(:include?)
236
+ end
237
+
238
+ def test_inspect
239
+ assert_equal('1..10', Range.new(1, 10).inspect)
240
+ assert_equal('1...10', Range.new(1, 10, true).inspect)
241
+ assert_equal('"a".."z"', Range.new('a', 'z').inspect)
242
+ assert_equal('"a"..."z"', Range.new('a', 'z', true).inspect)
243
+ end
244
+
245
+ def test_last
246
+ util_test_end(:last)
247
+ end
248
+
249
+ def test_member_eh
250
+ util_member(:member?)
251
+ end
252
+
253
+ def test_step
254
+ # n=1 default in step(n)
255
+ util_step_tester([5,6,7,8,9], Range.new(5, 9))
256
+ util_step_tester([5,6,7,8], Range.new(5, 9, true))
257
+
258
+ # explicit n=1
259
+ util_step_tester([5,6,7,8,9], Range.new(5, 9), 1)
260
+ util_step_tester([5,6,7,8], Range.new(5, 9, true), 1)
261
+
262
+ # n=2
263
+ util_step_tester([5,7,9], Range.new(5, 9), 2)
264
+ util_step_tester([5,7], Range.new(5, 9, true), 2)
265
+
266
+ # n=3
267
+ util_step_tester([5,8], Range.new(5, 9), 3)
268
+ util_step_tester([5,8], Range.new(5, 9, true), 3)
269
+
270
+ # n=4
271
+ util_step_tester([5,9], Range.new(5, 9), 4)
272
+ util_step_tester([5], Range.new(5, 9, true), 4)
273
+ end
274
+
275
+ def test_to_s
276
+ assert_equal('1..10', Range.new(1, 10).to_s)
277
+ assert_equal('1...10', Range.new(1, 10, true).to_s)
278
+ assert_equal('a..z', Range.new('a', 'z').to_s)
279
+ assert_equal('a...z', Range.new('a', 'z', true).to_s)
280
+ end
281
+ end
@@ -0,0 +1,1793 @@
1
+ require 'test/unit'
2
+ require 'rubicon_testcase'
3
+
4
+ class TestStringSubclass < String; end # for test_to_s/test_to_str
5
+
6
+ # Helper class to test String#=~.
7
+ #
8
+ class MatchDefiner
9
+ def initialize(result)
10
+ @result = result
11
+ end
12
+
13
+ def =~(other)
14
+ [other, @result]
15
+ end
16
+ end
17
+
18
+ class TestString < RubiconTestCase
19
+
20
+ def initialize(*args)
21
+ @cls = String
22
+
23
+ begin
24
+ S("Foo")[/./, 1]
25
+ @aref_re_nth = true
26
+ rescue
27
+ @aref_re_nth = false
28
+ end
29
+ begin
30
+ S("Foo")[/Bar/] = S("")
31
+ @aref_re_silent = true
32
+ rescue IndexError
33
+ @aref_re_silent = false
34
+ end
35
+ begin
36
+ S("Foo").slice!(4)
37
+ @aref_slicebang_silent = true
38
+ rescue
39
+ @aref_slicebang_silent = false
40
+ end
41
+ super
42
+ end
43
+
44
+ def S(str)
45
+ @cls.new(str)
46
+ end
47
+
48
+ def casetest(a, b, rev=false) # TODO: rename
49
+ case a
50
+ when b
51
+ assert(!rev)
52
+ else
53
+ assert(rev)
54
+ end
55
+ end
56
+
57
+ def test_capitalize
58
+ assert_equal(S("Hello"), S("hello").capitalize)
59
+ assert_equal(S("Hello"), S("hELLO").capitalize)
60
+ assert_equal(S("Hello"), S("Hello").capitalize)
61
+ assert_equal(S("123abc"), S("123ABC").capitalize)
62
+ end
63
+
64
+ def test_capitalize_bang
65
+ a = S("hello")
66
+ assert_equal(S("Hello"), a.capitalize!)
67
+ assert_equal(S("Hello"), a)
68
+
69
+ a = S("Hello")
70
+ assert_nil a.capitalize!
71
+ assert_equal(S("Hello"), a)
72
+
73
+ a = S("hELLO")
74
+ assert_equal S("Hello"), a.capitalize!
75
+ assert_equal S("Hello"), a
76
+
77
+ a = S("123ABC")
78
+ assert_equal S("123abc"), a.capitalize!
79
+ assert_equal S("123abc"), a
80
+ end
81
+
82
+ def test_capitalize_bang_multibyte
83
+ # TODO: flunk "No capitalize! multibyte tests yet"
84
+ end
85
+
86
+ def test_casecmp
87
+ # 0
88
+ assert_equal(0, S("123abc").casecmp(S("123ABC")))
89
+ assert_equal(0, S("123AbC").casecmp(S("123aBc")))
90
+ assert_equal(0, S("123ABC").casecmp(S("123ABC")))
91
+ # 1
92
+ assert_equal(1, S("1X3ABC").casecmp(S("123ABC")))
93
+ assert_equal(1, S("123AXC").casecmp(S("123ABC")))
94
+ assert_equal(1, S("123ABX").casecmp(S("123ABC")))
95
+ assert_equal(1, S("123ABCX").casecmp(S("123ABC")))
96
+ # -1
97
+ assert_equal(-1, S("1#3ABC").casecmp(S("123ABC")))
98
+ assert_equal(-1, S("123A#C").casecmp(S("123ABC")))
99
+ assert_equal(-1, S("123AB#").casecmp(S("123ABC")))
100
+ assert_equal(-1, S("123AB").casecmp(S("123ABC")))
101
+
102
+ assert_raises TypeError do
103
+ 'foo'.casecmp Object.new
104
+ end
105
+ end
106
+
107
+ def test_center
108
+ s = S("")
109
+ assert_not_equal s.object_id, s.center(0).object_id
110
+
111
+ assert_equal S(""), S("") .center(0)
112
+ assert_equal S("@"), S("@").center(0)
113
+
114
+ assert_equal S(" "), S("") .center(1)
115
+ assert_equal S("@"), S("@").center(1)
116
+ assert_equal S("@ "), S("@").center(2)
117
+ assert_equal S(" @ "), S("@").center(3)
118
+ assert_equal S(" @ "), S("@").center(4)
119
+ assert_equal S(" @ "), S("@").center(5)
120
+
121
+ assert_equal S(" @@ "), S("@@").center(5)
122
+
123
+ assert_equal S(""), S("") .center(0, 'X')
124
+ assert_equal S("@"), S("@").center(0, 'X')
125
+ assert_equal S("X"), S("") .center(1, 'X')
126
+ assert_equal S("@"), S("@").center(1, 'X')
127
+ assert_equal S("@X"), S("@").center(2, 'X')
128
+ assert_equal S("X@X"), S("@").center(3, 'X')
129
+ assert_equal S("X@XX"), S("@").center(4, 'X')
130
+ assert_equal S("XX@XX"), S("@").center(5, 'X')
131
+
132
+ assert_equal S("X@XY"), S("@").center(4, 'XY')
133
+ assert_equal S("XY@XY"), S("@").center(5, 'XY')
134
+ assert_equal S("XY@XYX"), S("@").center(6, 'XY')
135
+ assert_equal S("XYX@XYX"), S("@").center(7, 'XY')
136
+
137
+ assert_raises ArgumentError, "Zero width padding not allowed" do
138
+ S("").center 0, S("")
139
+ end
140
+ end
141
+
142
+ def test_chomp
143
+ assert_equal(S("hello"), S("hello").chomp("\n"))
144
+ assert_equal(S("hello"), S("hello\n").chomp("\n"))
145
+
146
+ $/ = "\n"
147
+
148
+ assert_equal(S("hello"), S("hello").chomp)
149
+ assert_equal(S("hello"), S("hello\n").chomp)
150
+
151
+ $/ = "!"
152
+ assert_equal(S("hello"), S("hello").chomp)
153
+ assert_equal(S("hello"), S("hello!").chomp)
154
+ $/ = "\n"
155
+ end
156
+
157
+ def test_chomp_bang
158
+ a = S("")
159
+ a.chomp!
160
+ assert_equal('', a)
161
+
162
+ a = S("hello")
163
+ a.chomp!(S("\n"))
164
+
165
+ assert_equal(S("hello"), a)
166
+ assert_equal(nil, a.chomp!(S("\n")))
167
+
168
+ a = S("hello\n")
169
+ a.chomp!(S("\n"))
170
+ assert_equal(S("hello"), a)
171
+
172
+ $/ = "\n"
173
+ a = S("hello")
174
+ a.chomp!
175
+ assert_equal(S("hello"), a)
176
+
177
+ a = S("hello\n")
178
+ a.chomp!
179
+ assert_equal(S("hello"), a)
180
+
181
+ $/ = "!"
182
+ a = S("hello")
183
+ a.chomp!
184
+ assert_equal(S("hello"), a)
185
+
186
+ a="hello!"
187
+ a.chomp!
188
+ assert_equal(S("hello"), a)
189
+
190
+ $/ = "\n"
191
+
192
+ a = S("hello\n")
193
+ b = a.dup
194
+ assert_equal(S("hello"), a.chomp!)
195
+ assert_equal(S("hello\n"), b)
196
+ end
197
+
198
+ def test_chop
199
+ assert_equal(S(""), S("").chop)
200
+ assert_equal(S(""), S("h").chop)
201
+ assert_equal(S("hell"), S("hello").chop)
202
+ assert_equal(S("hello"), S("hello\r\n").chop)
203
+ assert_equal(S("hello\n"), S("hello\n\r").chop)
204
+ assert_equal(S(""), S("\r\n").chop)
205
+ end
206
+
207
+ def test_chop_bang
208
+ a = S("").chop!
209
+ assert_nil(a)
210
+
211
+ a = S("hello").chop!
212
+ assert_equal(S("hell"), a)
213
+
214
+ a = S("hello\r\n").chop!
215
+ assert_equal(S("hello"), a)
216
+
217
+ a = S("hello\n\r").chop!
218
+ assert_equal(S("hello\n"), a)
219
+
220
+ a = S("\r\n").chop!
221
+ assert_equal(S(""), a)
222
+ end
223
+
224
+ def test_class_new
225
+ assert_equal("RUBY", S("RUBY"))
226
+ end
227
+
228
+ def test_concat
229
+ assert_equal(S("world!"), S("world").concat(33))
230
+ assert_equal(S("world!"), S("world").concat(S('!')))
231
+ end
232
+
233
+ def test_count
234
+ a = S("hello world")
235
+ assert_equal(5, a.count(S("lo")))
236
+ assert_equal(0, a.count(S("lo"), S("h")))
237
+ assert_equal(2, a.count(S("lo"), S("o")))
238
+ assert_equal(8, a.count(S("^l")))
239
+ assert_equal(4, a.count(S("hello"), S("^l")))
240
+ assert_equal(4, a.count(S("ej-m")))
241
+ assert_equal(2, a.count(S("aeiou"), S("^e")))
242
+ end
243
+
244
+ def test_crypt
245
+ assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa")))
246
+ assert(S('aaGUC/JkO9/Sc') != S("mypassword").crypt(S("ab")))
247
+
248
+ # "salt" should be at least 2 characters
249
+ assert_raise(ArgumentError) { S("mypassword").crypt("a")}
250
+ end
251
+
252
+ def test_delete
253
+ a = S("hello")
254
+ assert_equal(S("heo"), a.delete(S("l"), S("lo")))
255
+ assert_equal(S("hello"), a.delete(S("lo"), S("h")))
256
+ assert_equal(S("he"), a.delete(S("lo")))
257
+ assert_equal(S("hell"), a.delete(S("aeiou"), S("^e")))
258
+ assert_equal(S("ho"), a.delete(S("ej-m")))
259
+ end
260
+
261
+ def test_delete_bang
262
+ a = S("hello")
263
+ a.delete!(S("l"), S("lo"))
264
+ assert_equal(S("heo"), a)
265
+
266
+ a = S("hello")
267
+ a.delete!(S("lo"))
268
+ assert_equal(S("he"), a)
269
+
270
+ a = S("hello")
271
+ a.delete!(S("aeiou"), S("^e"))
272
+ assert_equal(S("hell"), a)
273
+
274
+ a = S("hello")
275
+ a.delete!(S("ej-m"))
276
+ assert_equal(S("ho"), a)
277
+
278
+ a = S("hello")
279
+ assert_nil(a.delete!(S("z")))
280
+
281
+ a = S("hello")
282
+ b = a.dup
283
+ a.delete!(S("lo"))
284
+ assert_equal(S("he"), a)
285
+ assert_equal(S("hello"), b)
286
+ end
287
+
288
+ def test_downcase
289
+ assert_equal(S("hello"), S("helLO").downcase)
290
+ assert_equal(S("hello"), S("hello").downcase)
291
+ assert_equal(S("hello"), S("HELLO").downcase)
292
+ assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase)
293
+ end
294
+
295
+ def test_downcase_bang
296
+ a = S("helLO")
297
+ assert_equal(S("hello"), a.downcase!)
298
+ assert_equal(S("hello"), a)
299
+
300
+ a = S("hello")
301
+ assert_nil(a.downcase!)
302
+ assert_equal(S("hello"), a)
303
+ end
304
+
305
+ def test_downcase_bang_multibyte
306
+ # TODO: flunk "No downcase! multibyte tests yet"
307
+ end
308
+
309
+ def test_dump
310
+ a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10
311
+ assert_equal(S('"Test\\001\\002\\003\\t\\r\\n"'), a.dump)
312
+ end
313
+
314
+ def test_each
315
+ $/ = "\n"
316
+ res=[]
317
+ S("hello\nworld").each {|x| res << x}
318
+ assert_equal(S("hello\n"), res[0])
319
+ assert_equal(S("world"), res[1])
320
+
321
+ res=[]
322
+ S("hello\n\n\nworld").each(S('')) {|x| res << x}
323
+ assert_equal(S("hello\n\n\n"), res[0])
324
+ assert_equal(S("world"), res[1])
325
+
326
+ $/ = "!"
327
+ res=[]
328
+ S("hello!world").each {|x| res << x}
329
+ assert_equal(S("hello!"), res[0])
330
+ assert_equal(S("world"), res[1])
331
+
332
+ $/ = "\n"
333
+ end
334
+
335
+ def test_each_byte
336
+ res = []
337
+ S("ABC").each_byte {|x| res << x }
338
+ assert_equal(65, res[0])
339
+ assert_equal(66, res[1])
340
+ assert_equal(67, res[2])
341
+ end
342
+
343
+ def test_each_line
344
+ $/ = "\n"
345
+ res=[]
346
+ S("hello\nworld").each {|x| res << x}
347
+ assert_equal(S("hello\n"), res[0])
348
+ assert_equal(S("world"), res[1])
349
+
350
+ res=[]
351
+ S("hello\n\n\nworld").each(S('')) {|x| res << x}
352
+ assert_equal(S("hello\n\n\n"), res[0])
353
+ assert_equal(S("world"), res[1])
354
+
355
+ $/ = "!"
356
+ res=[]
357
+ S("hello!world").each {|x| res << x}
358
+ assert_equal(S("hello!"), res[0])
359
+ assert_equal(S("world"), res[1])
360
+
361
+ $/ = "\n"
362
+ end
363
+
364
+ def test_empty_eh
365
+ assert(S("").empty?)
366
+ assert(!S("not").empty?)
367
+ end
368
+
369
+ def test_eql_eh
370
+ a = S("hello")
371
+ assert a.eql?(S("hello"))
372
+ assert a.eql?(a)
373
+ end
374
+
375
+ def test_equals2
376
+ assert_equal(false, S("foo") == :foo)
377
+ assert_equal(false, S("foo") == :foo)
378
+
379
+ assert(S("abcdef") == S("abcdef"))
380
+
381
+ assert(S("CAT") != S('cat'))
382
+ assert(S("CaT") != S('cAt'))
383
+ end
384
+
385
+ def test_equals3
386
+ assert_equal(false, S("foo") === :foo)
387
+ casetest(S("abcdef"), S("abcdef"))
388
+ casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to
389
+ casetest(S("CaT"), S('cAt'), true) # find these in the case.
390
+ end
391
+
392
+ def test_equalstilde
393
+ # str =~ str
394
+ assert_raises TypeError do
395
+ assert_equal 10, S("FeeFieFoo-Fum") =~ S("Fum")
396
+ end
397
+
398
+ # "str =~ regexp" same as "regexp =~ str"
399
+ assert_equal 10, S("FeeFieFoo-Fum") =~ /Fum$/
400
+ assert_equal nil, S("FeeFieFoo-Fum") =~ /FUM$/
401
+
402
+ # "str =~ obj" calls "obj =~ str"
403
+ assert_equal ["aaa", 123], "aaa" =~ MatchDefiner.new(123)
404
+ assert_equal ["bbb", :foo], "bbb" =~ MatchDefiner.new(:foo)
405
+ assert_equal ["ccc", nil], "ccc" =~ MatchDefiner.new(nil)
406
+
407
+ # default Object#=~ method.
408
+ assert_equal false, "a string" =~ Object.new
409
+ end
410
+
411
+ def test_gsub
412
+ assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*')))
413
+ assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>')))
414
+ assert_equal(S("104 101 108 108 111 "),
415
+ S("hello").gsub(/./) { |s| s[0].to_s + S(' ')})
416
+ assert_equal(S("HELL-o"),
417
+ S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 })
418
+
419
+ a = S("hello")
420
+ a.taint
421
+ assert(a.gsub(/./, S('X')).tainted?)
422
+ end
423
+
424
+ def test_gsub_bang
425
+ a = S("hello")
426
+ b = a.dup
427
+ a.gsub!(/[aeiou]/, S('*'))
428
+ assert_equal(S("h*ll*"), a)
429
+ assert_equal(S("hello"), b)
430
+
431
+ a = S("hello")
432
+ a.gsub!(/([aeiou])/, S('<\1>'))
433
+ assert_equal(S("h<e>ll<o>"), a)
434
+
435
+ a = S("hello")
436
+ a.gsub!(/./) { |s| s[0].to_s + S(' ')}
437
+ assert_equal(S("104 101 108 108 111 "), a)
438
+
439
+ a = S("hello")
440
+ a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
441
+ assert_equal(S("HELL-o"), a)
442
+
443
+ r = S('X')
444
+ r.taint
445
+ a.gsub!(/./, r)
446
+ assert(a.tainted?)
447
+
448
+ a = S("hello")
449
+ assert_nil(a.sub!(S('X'), S('Y')))
450
+ end
451
+
452
+ def test_hash
453
+ assert_equal(S("hello").hash, S("hello").hash)
454
+ assert_not_equal(S("hello").hash, S("helLO").hash)
455
+ end
456
+
457
+ def test_hex
458
+ assert_equal(0, S("0").hex, "0")
459
+ assert_equal(0, S("0x0").hex, "0x0")
460
+ assert_equal(255, S("0xff").hex, "0xff")
461
+ assert_equal(-255, S("-0xff").hex, "-0xff")
462
+ assert_equal(255, S("0xFF").hex, "0xFF")
463
+ assert_equal(-255, S("-0xFF").hex, "-0xFF")
464
+ assert_equal(255, S("0Xff").hex, "0Xff")
465
+ assert_equal(255, S("ff").hex, "ff")
466
+ assert_equal(-255, S("-ff").hex, "-ff")
467
+ assert_equal(255, S("FF").hex, "FF")
468
+ assert_equal(-255, S("-FF").hex, "-FF")
469
+ assert_equal(0, S("-ralph").hex, '-ralph')
470
+ assert_equal(-15, S("-fred").hex, '-fred')
471
+ assert_equal(15, S("fred").hex, 'fred')
472
+ assert_equal(-15, S("-Fred").hex, '-Fred')
473
+ assert_equal(15, S("Fred").hex, 'Fred')
474
+ end
475
+
476
+ def test_include_eh
477
+ assert_equal true, S("foobar").include?(S("foo"))
478
+ assert_equal false, S("foobar").include?(S("baz"))
479
+
480
+ assert_equal true, S("foobar").include?(?f)
481
+ assert_equal false, S("foobar").include?(?z)
482
+
483
+ assert_raises TypeError do
484
+ S('').include? :junk
485
+ end
486
+ end
487
+
488
+ def test_index
489
+ assert_equal(65, S("AooBar")[0])
490
+ assert_equal(66, S("FooBaB")[-1])
491
+ assert_equal(nil, S("FooBar")[6])
492
+ assert_equal(nil, S("FooBar")[-7])
493
+
494
+ assert_equal(S("Foo"), S("FooBar")[0,3])
495
+ assert_equal(S("Bar"), S("FooBar")[-3,3])
496
+ assert_equal(S(""), S("FooBar")[6,2])
497
+ assert_equal(nil, S("FooBar")[-7,10])
498
+
499
+ assert_equal(S("Foo"), S("FooBar")[0..2])
500
+ assert_equal(S("Foo"), S("FooBar")[0...3])
501
+ assert_equal(S("Bar"), S("FooBar")[-3..-1])
502
+ assert_equal("", S("FooBar")[6..2])
503
+ assert_equal(nil, S("FooBar")[-10..-7])
504
+
505
+ assert_equal(S("Foo"), S("FooBar")[/^F../])
506
+ assert_equal(S("Bar"), S("FooBar")[/..r$/])
507
+ assert_equal(nil, S("FooBar")[/xyzzy/])
508
+ assert_equal(nil, S("FooBar")[/plugh/])
509
+
510
+ assert_equal(S("Foo"), S("FooBar")[S("Foo")])
511
+ assert_equal(S("Bar"), S("FooBar")[S("Bar")])
512
+ assert_equal(nil, S("FooBar")[S("xyzzy")])
513
+ assert_equal(nil, S("FooBar")[S("plugh")])
514
+
515
+ if @aref_re_nth
516
+ assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1])
517
+ assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2])
518
+ assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3])
519
+ assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
520
+ assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
521
+ assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
522
+ end
523
+
524
+ # TODO: figure out why there were two test_index's and how to consolidate
525
+
526
+ assert_equal 0, S("hello").index(?h)
527
+ assert_equal 3, S("hello").index(?l, 3)
528
+
529
+ assert_nil S("hello").index(?z)
530
+ assert_nil S("hello").index(?z, 3)
531
+
532
+ assert_equal 1, S("hello").index(S("ell"))
533
+ assert_equal 3, S("hello").index(S("l"), 3)
534
+
535
+ assert_nil S("hello").index(/z./)
536
+ assert_nil S("hello").index(S("z"), 3)
537
+
538
+ assert_equal 2, S("hello").index(/ll./)
539
+ assert_equal 3, S("hello").index(/l./, 3)
540
+
541
+ assert_nil S("hello").index(S("z"))
542
+ assert_nil S("hello").index(/z./, 3)
543
+
544
+ # flunk "No backref tests" # HACK uncomment
545
+ end
546
+
547
+ def test_index_equals
548
+ s = S("FooBar")
549
+ s[0] = S('A')
550
+ assert_equal(S("AooBar"), s)
551
+
552
+ s[-1]= S('B')
553
+ assert_equal(S("AooBaB"), s)
554
+ assert_raises(IndexError) { s[-7] = S("xyz") }
555
+ assert_equal(S("AooBaB"), s)
556
+ s[0] = S("ABC")
557
+ assert_equal(S("ABCooBaB"), s)
558
+
559
+ s = S("FooBar")
560
+ s[0,3] = S("A")
561
+ assert_equal(S("ABar"),s)
562
+ s[0] = S("Foo")
563
+ assert_equal(S("FooBar"), s)
564
+ s[-3,3] = S("Foo")
565
+ assert_equal(S("FooFoo"), s)
566
+ assert_raise(IndexError) { s[7,3] = S("Bar") }
567
+ assert_raise(IndexError) { s[-7,3] = S("Bar") }
568
+
569
+ s = S("FooBar")
570
+ s[0..2] = S("A")
571
+ assert_equal(S("ABar"), s)
572
+ s[1..3] = S("Foo")
573
+ assert_equal(S("AFoo"), s)
574
+ s[-4..-4] = S("Foo")
575
+ assert_equal(S("FooFoo"), s)
576
+ assert_raise(RangeError) { s[7..10] = S("Bar") }
577
+ assert_raise(RangeError) { s[-7..-10] = S("Bar") }
578
+
579
+ s = S("FooBar")
580
+ s[/^F../]= S("Bar")
581
+ assert_equal(S("BarBar"), s)
582
+ s[/..r$/] = S("Foo")
583
+ assert_equal(S("BarFoo"), s)
584
+ if @aref_re_silent
585
+ s[/xyzzy/] = S("None")
586
+ assert_equal(S("BarFoo"), s)
587
+ else
588
+ assert_raise(IndexError) { s[/xyzzy/] = S("None") }
589
+ end
590
+ if @aref_re_nth
591
+ s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo")
592
+ assert_equal(S("FooFoo"), s)
593
+ s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar")
594
+ assert_equal(S("FooBar"), s)
595
+ assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" }
596
+ s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo")
597
+ assert_equal(S("FooFoo"), s)
598
+ s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar")
599
+ assert_equal(S("BarFoo"), s)
600
+ assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" }
601
+ end
602
+
603
+ s = S("FooBar")
604
+ s[S("Foo")] = S("Bar")
605
+ assert_equal(S("BarBar"), s)
606
+
607
+ s = S("a string")
608
+ s[0..s.size] = S("another string")
609
+ assert_equal(S("another string"), s)
610
+ end
611
+
612
+ def test_insert
613
+ assert_equal S("BCAD"), S("AD").insert(0, S("BC"))
614
+ assert_equal S("ABCD"), S("AD").insert(1, S("BC"))
615
+ assert_equal S("ADBC"), S("AD").insert(2, S("BC"))
616
+
617
+ assert_raises(IndexError) { S("AD").insert(3, S("BC")) }
618
+
619
+ assert_equal S("ADBC"), S("AD").insert(-1, S("BC"))
620
+ assert_equal S("ABCD"), S("AD").insert(-2, S("BC"))
621
+ assert_equal S("BCAD"), S("AD").insert(-3, S("BC"))
622
+
623
+ assert_raises(IndexError) { S("AD").insert(-4, S("BC")) }
624
+
625
+ s = S("AD")
626
+ s.insert 0, S("BC")
627
+ assert_equal S("BCAD"), s
628
+ end
629
+
630
+ # Need to make sure that we get back exactly what we want, intern is safe
631
+ # for this. (test/unit calls inspect on results, which is useless for
632
+ # debugging this.)
633
+
634
+ def test_inspect
635
+ assert_equal :'""', S("").inspect.intern
636
+ assert_equal :'"string"', S("string").inspect.intern
637
+ assert_equal :'"\""', S("\"").inspect.intern
638
+ assert_equal :'"\\\\"', S("\\").inspect.intern
639
+ assert_equal :'"\n"', S("\n").inspect.intern
640
+ assert_equal :'"\r"', S("\r").inspect.intern
641
+ assert_equal :'"\t"', S("\t").inspect.intern
642
+ assert_equal :'"\f"', S("\f").inspect.intern
643
+ assert_equal :'"\001"', S("\001").inspect.intern
644
+ assert_equal :'"\010"', S("\010").inspect.intern
645
+ assert_equal :'"\177"', S("\177").inspect.intern
646
+ assert_equal :'"\377"', S("\377").inspect.intern
647
+
648
+ assert_equal :'"\\#{1}"', (S("#") + S("{1}")).inspect.intern
649
+
650
+ assert_equal :'"\\#$f"', (S("#") + S("$f")).inspect.intern
651
+ assert_equal :'"\\#@f"', (S("#") + S("@f")).inspect.intern
652
+ end
653
+
654
+ def test_intern
655
+ assert_equal(:koala, S("koala").intern)
656
+ assert(:koala != S("Koala").intern)
657
+
658
+ # error cases
659
+ assert_raise(ArgumentError) { S("").intern }
660
+ assert_raise(ArgumentError) { S("with\0null\0inside").intern }
661
+ end
662
+
663
+ def test_length
664
+ assert_equal(0, S("").length)
665
+ assert_equal(4, S("1234").length)
666
+ assert_equal(6, S("1234\r\n").length)
667
+ assert_equal(7, S("\0011234\r\n").length)
668
+ end
669
+
670
+ def test_ljust
671
+ assert_equal S(""), S("").ljust(-1)
672
+ assert_equal S(""), S("").ljust(0)
673
+ assert_equal S(" "), S("").ljust(1)
674
+ assert_equal S(" "), S("").ljust(2)
675
+
676
+ assert_equal S("@"), S("@").ljust(0)
677
+ assert_equal S("@"), S("@").ljust(1)
678
+ assert_equal S("@ "), S("@").ljust(2)
679
+ assert_equal S("@ "), S("@").ljust(3)
680
+
681
+ assert_equal S("@@"), S("@@").ljust(1)
682
+ assert_equal S("@@"), S("@@").ljust(2)
683
+ assert_equal S("@@ "), S("@@").ljust(3)
684
+ assert_equal S("@@ "), S("@@").ljust(4)
685
+
686
+ assert_equal(S("@X"), S("@").ljust(2, "X"))
687
+ assert_equal(S("@XX"), S("@").ljust(3, "X"))
688
+
689
+ assert_equal(S("@X"), S("@").ljust(2, "XY"))
690
+ assert_equal(S("@XY"), S("@").ljust(3, "XY"))
691
+ assert_equal(S("@XY"), S("@").ljust(3, "XY"))
692
+ assert_equal(S("@XYX"), S("@").ljust(4, "XY"))
693
+
694
+ assert_equal S("@@"), S("@@").ljust(1, "XY")
695
+ assert_equal S("@@"), S("@@").ljust(2, "XY")
696
+ assert_equal S("@@X"), S("@@").ljust(3, "XY")
697
+ assert_equal S("@@XY"), S("@@").ljust(4, "XY")
698
+
699
+ # zero width padding
700
+ assert_raises ArgumentError do
701
+ S("hi").ljust(0, "")
702
+ end
703
+ end
704
+
705
+ def test_lstrip
706
+ a = S(" hello")
707
+ assert_equal(S("hello"), a.lstrip)
708
+ assert_equal(S(" hello"), a)
709
+ assert_equal(S("hello "), S(" hello ").lstrip)
710
+ assert_equal(S("hello"), S("hello").lstrip)
711
+ end
712
+
713
+ def test_lstrip_bang
714
+ a = S(" abc")
715
+ b = a.dup
716
+ assert_equal(S("abc"), a.lstrip!)
717
+ assert_equal(S("abc"), a)
718
+ assert_equal(S(" abc"), b)
719
+ end
720
+
721
+ def test_lt2
722
+ assert_equal(S("world!"), S("world") << 33)
723
+ assert_equal(S("world!"), S("world") << S('!'))
724
+ end
725
+
726
+ def test_match
727
+ a = S("cruel world")
728
+
729
+ m = a.match(/\w+/)
730
+ assert_kind_of MatchData, m
731
+ assert_equal S("cruel"), m.to_s
732
+
733
+ m = a.match '\w+'
734
+ assert_kind_of MatchData, m
735
+ assert_equal S("cruel"), m.to_s
736
+
737
+ assert_raises TypeError do
738
+ a.match Object.new
739
+ end
740
+
741
+ o = Object.new
742
+ def o.to_str() return '\w+' end
743
+ m = a.match o
744
+ assert_kind_of MatchData, m
745
+ assert_equal S("cruel"), m.to_s
746
+ end
747
+
748
+ def test_next
749
+ assert_equal(S("abd"), S("abc").next)
750
+ assert_equal(S("z"), S("y").next)
751
+ assert_equal(S("aaa"), S("zz").next)
752
+
753
+ assert_equal(S("124"), S("123").next)
754
+ assert_equal(S("1000"), S("999").next)
755
+
756
+ assert_equal(S("2000aaa"), S("1999zzz").next)
757
+ assert_equal(S("AAAAA000"), S("ZZZZ999").next)
758
+
759
+ assert_equal(S("*+"), S("**").next)
760
+ end
761
+
762
+ def test_next_bang
763
+ a = S("abc")
764
+ b = a.dup
765
+ assert_equal(S("abd"), a.next!)
766
+ assert_equal(S("abd"), a)
767
+ assert_equal(S("abc"), b)
768
+
769
+ a = S("y")
770
+ assert_equal(S("z"), a.next!)
771
+ assert_equal(S("z"), a)
772
+
773
+ a = S("zz")
774
+ assert_equal(S("aaa"), a.next!)
775
+ assert_equal(S("aaa"), a)
776
+
777
+ a = S("123")
778
+ assert_equal(S("124"), a.next!)
779
+ assert_equal(S("124"), a)
780
+
781
+ a = S("999")
782
+ assert_equal(S("1000"), a.next!)
783
+ assert_equal(S("1000"), a)
784
+
785
+ a = S("1999zzz")
786
+ assert_equal(S("2000aaa"), a.next!)
787
+ assert_equal(S("2000aaa"), a)
788
+
789
+ a = S("ZZZZ999")
790
+ assert_equal(S("AAAAA000"), a.next!)
791
+ assert_equal(S("AAAAA000"), a)
792
+
793
+ a = S("**")
794
+ assert_equal(S("*+"), a.next!)
795
+ assert_equal(S("*+"), a)
796
+ end
797
+
798
+ def test_oct
799
+ assert_equal(0, S("0").oct, "0")
800
+ assert_equal(255, S("0377").oct, "0377")
801
+ assert_equal(-255, S("-0377").oct, "-0377")
802
+ assert_equal(255, S("377").oct, "377")
803
+ assert_equal(-255, S("-377").oct, "-377")
804
+ assert_equal(24, S("030X").oct, "030X")
805
+ assert_equal(-24, S("-030X").oct, "-030X")
806
+ assert_equal(0, S("ralph").oct, "ralph")
807
+ assert_equal(0, S("-ralph").oct, "-ralph")
808
+ end
809
+
810
+ def test_percent
811
+ assert_equal(S("00123"), S("%05d") % 123)
812
+ assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1])
813
+ x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") %
814
+ [S("hi"),
815
+ 123,
816
+ S("never seen"),
817
+ 456,
818
+ 0,
819
+ ?A,
820
+ 3.0999,
821
+ 11,
822
+ 171,
823
+ 171,
824
+ 11,
825
+ 171,
826
+ 171]
827
+
828
+ assert_equal(S(' hi 123 %foo 456 0x0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x)
829
+ end
830
+
831
+ def test_plus
832
+ s1 = S('')
833
+ s2 = S('')
834
+ s3 = s1 + s2
835
+ assert_equal S(''), s3
836
+ assert_not_equal s1.object_id, s3.object_id
837
+ assert_not_equal s2.object_id, s3.object_id
838
+
839
+ s1 = S('yo')
840
+ s2 = S('')
841
+ s3 = s1 + s2
842
+ assert_equal S('yo'), s3
843
+ assert_not_equal s1.object_id, s3.object_id
844
+
845
+ s1 = S('')
846
+ s2 = S('yo')
847
+ s3 = s1 + s2
848
+ assert_equal S('yo'), s3
849
+ assert_not_equal s2.object_id, s3.object_id
850
+
851
+ s1 = S('yo')
852
+ s2 = S('del')
853
+ s3 = s1 + s2
854
+ assert_equal S('yodel'), s3
855
+ assert_equal false, s3.tainted?
856
+
857
+ s1 = S('yo')
858
+ s2 = S('del')
859
+ s1.taint
860
+ s3 = s1 + s2
861
+ assert_equal true, s3.tainted?
862
+
863
+ s1 = S('yo')
864
+ s2 = S('del')
865
+ s2.taint
866
+ s3 = s1 + s2
867
+ assert_equal true, s3.tainted?
868
+
869
+ s1 = S('yo')
870
+ s2 = S('del')
871
+ s1.taint
872
+ s2.taint
873
+ s3 = s1 + s2
874
+ assert_equal true, s3.tainted?
875
+ end
876
+
877
+ def test_replace
878
+ a = S("foo")
879
+ assert_equal S("f"), a.replace(S("f"))
880
+
881
+ a = S("foo")
882
+ assert_equal S("foobar"), a.replace(S("foobar"))
883
+
884
+ a = S("foo")
885
+ a.taint
886
+ b = a.replace S("xyz")
887
+ assert_equal S("xyz"), b
888
+
889
+ assert b.tainted?, "Replaced string should be tainted"
890
+ end
891
+
892
+ def test_reverse
893
+ a = S("beta")
894
+ assert_equal(S("ateb"), a.reverse)
895
+ assert_equal(S("beta"), a)
896
+ end
897
+
898
+ def test_reverse_bang
899
+ a = S("beta")
900
+ assert_equal(S("ateb"), a.reverse!)
901
+ assert_equal(S("ateb"), a)
902
+ end
903
+
904
+ def test_rindex
905
+ # String
906
+ assert_equal 0, S('').rindex(S(''))
907
+ assert_equal 3, S('foo').rindex(S(''))
908
+ assert_nil S('').rindex(S('x'))
909
+
910
+ assert_equal 6, S("ell, hello").rindex(S("ell"))
911
+ assert_equal 3, S("hello,lo").rindex(S("l"), 3)
912
+
913
+ assert_nil S("hello").rindex(S("z"))
914
+ assert_nil S("hello").rindex(S("z"), 3)
915
+
916
+ # Fixnum
917
+ assert_nil S('').rindex(0)
918
+
919
+ assert_equal 3, S("hello").rindex(?l)
920
+ assert_equal 3, S("hello,lo").rindex(?l, 3)
921
+
922
+ assert_nil S("hello").rindex(?z)
923
+ assert_nil S("hello").rindex(?z, 3)
924
+
925
+ assert_nil S('').rindex(256)
926
+
927
+ assert_nil S('').rindex(-1)
928
+
929
+ # Regexp
930
+ assert_equal 0, S('').rindex(//)
931
+ assert_equal 5, S('hello').rindex(//)
932
+
933
+ assert_nil S('').rindex(/x/)
934
+
935
+ assert_equal 7, S("ell, hello").rindex(/ll./)
936
+ assert_equal 3, S("hello,lo").rindex(/l./, 3)
937
+
938
+ assert_nil S("hello").rindex(/z./, 3)
939
+ assert_nil S("hello").rindex(/z./)
940
+ end
941
+
942
+ def test_rjust
943
+ assert_equal S(""), S("").rjust(-1)
944
+ assert_equal S(""), S("").rjust(0)
945
+ assert_equal S(" "), S("").rjust(1)
946
+ assert_equal S(" "), S("").rjust(2)
947
+
948
+ assert_equal S("@"), S("@").rjust(0)
949
+ assert_equal S("@"), S("@").rjust(1)
950
+ assert_equal S(" @"), S("@").rjust(2)
951
+ assert_equal S(" @"), S("@").rjust(3)
952
+
953
+ assert_equal S("@@"), S("@@").rjust(1)
954
+ assert_equal S("@@"), S("@@").rjust(2)
955
+ assert_equal S(" @@"), S("@@").rjust(3)
956
+ assert_equal S(" @@"), S("@@").rjust(4)
957
+
958
+ assert_equal(S("X@"), S("@").rjust(2, "X"))
959
+ assert_equal(S("XX@"), S("@").rjust(3, "X"))
960
+
961
+ assert_equal(S("X@"), S("@").rjust(2, "XY"))
962
+ assert_equal(S("XY@"), S("@").rjust(3, "XY"))
963
+ assert_equal(S("XY@"), S("@").rjust(3, "XY"))
964
+ assert_equal(S("XYX@"), S("@").rjust(4, "XY"))
965
+
966
+ assert_equal S("@@"), S("@@").rjust(1, "XY")
967
+ assert_equal S("@@"), S("@@").rjust(2, "XY")
968
+ assert_equal S("X@@"), S("@@").rjust(3, "XY")
969
+ assert_equal S("XY@@"), S("@@").rjust(4, "XY")
970
+
971
+ # zero width padding
972
+ assert_raises ArgumentError do
973
+ S("hi").rjust(0, "")
974
+ end
975
+ end
976
+
977
+ def test_rstrip
978
+ a = S("hello ")
979
+ assert_equal(S("hello"), a.rstrip)
980
+ assert_equal(S("hello "), a)
981
+ assert_equal(S(" hello"), S(" hello ").rstrip)
982
+ assert_equal(S("hello"), S("hello").rstrip)
983
+ end
984
+
985
+ def test_rstrip_bang
986
+ a = S("abc ")
987
+ b = a.dup
988
+ assert_equal(S("abc"), a.rstrip!)
989
+ assert_equal(S("abc"), a)
990
+ assert_equal(S("abc "), b)
991
+ end
992
+
993
+ def test_scan
994
+ a = S("cruel world")
995
+ assert_equal([S("cruel"), S("world")],a.scan(/\w+/))
996
+ assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../))
997
+ assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/))
998
+
999
+ res = []
1000
+ a.scan(/\w+/) { |w| res << w }
1001
+ assert_equal([S("cruel"), S("world") ],res)
1002
+
1003
+ res = []
1004
+ a.scan(/.../) { |w| res << w }
1005
+ assert_equal([S("cru"), S("el "), S("wor")],res)
1006
+
1007
+ res = []
1008
+ a.scan(/(...)/) { |w| res << w }
1009
+ assert_equal([[S("cru")], [S("el ")], [S("wor")]],res)
1010
+ end
1011
+
1012
+ def test_size
1013
+ assert_equal(0, S("").size)
1014
+ assert_equal(4, S("1234").size)
1015
+ assert_equal(6, S("1234\r\n").size)
1016
+ assert_equal(7, S("\0011234\r\n").size)
1017
+ end
1018
+
1019
+ def test_slice
1020
+ assert_equal(65, S("AooBar").slice(0))
1021
+ assert_equal(66, S("FooBaB").slice(-1))
1022
+ assert_nil(S("FooBar").slice(6))
1023
+ assert_nil(S("FooBar").slice(-7))
1024
+
1025
+ assert_equal(S("Foo"), S("FooBar").slice(0,3))
1026
+ assert_equal(S(S("Bar")), S("FooBar").slice(-3,3))
1027
+ assert_nil(S("FooBar").slice(7,2)) # Maybe should be six?
1028
+ assert_nil(S("FooBar").slice(-7,10))
1029
+
1030
+ assert_equal(S("Foo"), S("FooBar").slice(0..2))
1031
+ assert_equal(S("Bar"), S("FooBar").slice(-3..-1))
1032
+ # Version.less_than("1.8.2") do
1033
+ # assert_nil(S("FooBar").slice(6..2))
1034
+ # end
1035
+ # Version.greater_or_equal("1.8.2") do
1036
+ assert_equal("", S("FooBar").slice(6..2))
1037
+ # end
1038
+ assert_nil(S("FooBar").slice(-10..-7))
1039
+
1040
+ assert_equal(S("Foo"), S("FooBar").slice(/^F../))
1041
+ assert_equal(S("Bar"), S("FooBar").slice(/..r$/))
1042
+ assert_nil(S("FooBar").slice(/xyzzy/))
1043
+ assert_nil(S("FooBar").slice(/plugh/))
1044
+
1045
+ assert_equal(S("Foo"), S("FooBar").slice(S("Foo")))
1046
+ assert_equal(S("Bar"), S("FooBar").slice(S("Bar")))
1047
+ assert_nil(S("FooBar").slice(S("xyzzy")))
1048
+ assert_nil(S("FooBar").slice(S("plugh")))
1049
+ end
1050
+
1051
+ def test_slice_bang
1052
+ a = S("AooBar")
1053
+ b = a.dup
1054
+ assert_equal(65, a.slice!(0))
1055
+ assert_equal(S("ooBar"), a)
1056
+ assert_equal(S("AooBar"), b)
1057
+
1058
+ a = S("FooBar")
1059
+ assert_equal(?r,a.slice!(-1))
1060
+ assert_equal(S("FooBa"), a)
1061
+
1062
+ a = S("FooBar")
1063
+ if @aref_slicebang_silent
1064
+ assert_nil( a.slice!(6) )
1065
+ else
1066
+ assert_raises(:IndexError) { a.slice!(6) }
1067
+ end
1068
+ assert_equal(S("FooBar"), a)
1069
+
1070
+ if @aref_slicebang_silent
1071
+ assert_nil( a.slice!(-7) )
1072
+ else
1073
+ assert_raises(:IndexError) { a.slice!(-7) }
1074
+ end
1075
+ assert_equal(S("FooBar"), a)
1076
+
1077
+ a = S("FooBar")
1078
+ assert_equal(S("Foo"), a.slice!(0,3))
1079
+ assert_equal(S("Bar"), a)
1080
+
1081
+ a = S("FooBar")
1082
+ assert_equal(S("Bar"), a.slice!(-3,3))
1083
+ assert_equal(S("Foo"), a)
1084
+
1085
+ a=S("FooBar")
1086
+ if @aref_slicebang_silent
1087
+ assert_nil(a.slice!(7,2)) # Maybe should be six?
1088
+ else
1089
+ assert_raises(:IndexError) {a.slice!(7,2)} # Maybe should be six?
1090
+ end
1091
+ assert_equal(S("FooBar"), a)
1092
+ if @aref_slicebang_silent
1093
+ assert_nil(a.slice!(-7,10))
1094
+ else
1095
+ assert_raises(:IndexError) {a.slice!(-7,10)}
1096
+ end
1097
+ assert_equal(S("FooBar"), a)
1098
+
1099
+ a=S("FooBar")
1100
+ assert_equal(S("Foo"), a.slice!(0..2))
1101
+ assert_equal(S("Bar"), a)
1102
+
1103
+ a=S("FooBar")
1104
+ assert_equal(S("Bar"), a.slice!(-3..-1))
1105
+ assert_equal(S("Foo"), a)
1106
+
1107
+ a=S("FooBar")
1108
+ if @aref_slicebang_silent
1109
+ # Version.less_than("1.8.2") do
1110
+ # assert_nil(a.slice!(6..2))
1111
+ # end
1112
+ # Version.greater_or_equal("1.8.2") do
1113
+ assert_equal("", a.slice!(6..2))
1114
+ # end
1115
+ else
1116
+ assert_raises(:RangeError) {a.slice!(6..2)}
1117
+ end
1118
+ assert_equal(S("FooBar"), a)
1119
+ if @aref_slicebang_silent
1120
+ assert_nil(a.slice!(-10..-7))
1121
+ else
1122
+ assert_raises(:RangeError) {a.slice!(-10..-7)}
1123
+ end
1124
+ assert_equal(S("FooBar"), a)
1125
+
1126
+ a=S("FooBar")
1127
+ assert_equal(S("Foo"), a.slice!(/^F../))
1128
+ assert_equal(S("Bar"), a)
1129
+
1130
+ a=S("FooBar")
1131
+ assert_equal(S("Bar"), a.slice!(/..r$/))
1132
+ assert_equal(S("Foo"), a)
1133
+
1134
+ a=S("FooBar")
1135
+ if @aref_slicebang_silent
1136
+ assert_nil(a.slice!(/xyzzy/))
1137
+ else
1138
+ assert_raises(:IndexError) {a.slice!(/xyzzy/)}
1139
+ end
1140
+ assert_equal(S("FooBar"), a)
1141
+ if @aref_slicebang_silent
1142
+ assert_nil(a.slice!(/plugh/))
1143
+ else
1144
+ assert_raises(:IndexError) {a.slice!(/plugh/)}
1145
+ end
1146
+ assert_equal(S("FooBar"), a)
1147
+
1148
+ a=S("FooBar")
1149
+ assert_equal(S("Foo"), a.slice!(S("Foo")))
1150
+ assert_equal(S("Bar"), a)
1151
+
1152
+ a=S("FooBar")
1153
+ assert_equal(S("Bar"), a.slice!(S("Bar")))
1154
+ assert_equal(S("Foo"), a)
1155
+ end
1156
+
1157
+ def test_spaceship
1158
+ assert_equal( 1, S("abcdef") <=> S("ABCDEF"))
1159
+ assert_equal(-1, S("ABCDEF") <=> S("abcdef"))
1160
+
1161
+ assert_equal( 1, S("abcdef") <=> S("abcde") )
1162
+ assert_equal( 0, S("abcdef") <=> S("abcdef"))
1163
+ assert_equal(-1, S("abcde") <=> S("abcdef"))
1164
+ end
1165
+
1166
+ def test_split
1167
+ original_dollar_semi = $;.nil? ? $; : $;.dup
1168
+
1169
+ assert_equal [], S("").split
1170
+ assert_equal [], S("").split(nil)
1171
+ assert_equal [], S("").split(' ')
1172
+ assert_equal [], S("").split(nil, 1)
1173
+ assert_equal [], S("").split(' ', 1)
1174
+
1175
+ str = S("a")
1176
+ arr = str.split(nil, 1)
1177
+ assert_equal ["a"], arr
1178
+ assert_equal str.object_id, arr.first.object_id
1179
+
1180
+ # Tests of #split's behavior with a pattern of ' ' or $; == nil
1181
+ $; = nil
1182
+ assert_equal [S("a"), S("b")], S("a b") .split
1183
+ assert_equal [S("a"), S("b")], S("a b") .split
1184
+ assert_equal [S("a"), S("b")], S(" a b") .split
1185
+ assert_equal [S("a"), S("b")], S(" a b ") .split
1186
+ assert_equal [S("a"), S("b")], S(" a b ") .split
1187
+ assert_equal [S("a"), S("b")], S(" a b ") .split
1188
+ assert_equal [S("a"), S("b")], S(" a b ") .split
1189
+ assert_equal [S("a"), S("b")], S(" a b ") .split
1190
+
1191
+ assert_equal [S("a"), S("b")], S("a\tb") .split
1192
+ assert_equal [S("a"), S("b")], S("a\t\tb") .split
1193
+ assert_equal [S("a"), S("b")], S("a\nb") .split
1194
+ assert_equal [S("a"), S("b")], S("a\n\nb") .split
1195
+ assert_equal [S("a"), S("b")], S("a\rb") .split
1196
+ assert_equal [S("a"), S("b")], S("a\r\rb") .split
1197
+
1198
+ assert_equal [S("a"), S("b")], S("a\t b") .split
1199
+ assert_equal [S("a"), S("b")], S("a\t b") .split
1200
+ assert_equal [S("a"), S("b")], S("a\t\nb") .split
1201
+ assert_equal [S("a"), S("b")], S("a\r\nb") .split
1202
+ assert_equal [S("a"), S("b")], S("a\r\n\r\nb").split
1203
+
1204
+ assert_equal [S("a"), S("b"), S("c")], S(" a b\t c ").split
1205
+
1206
+ assert_equal [S("a"), S("b")], S("a b") .split(S(" "))
1207
+ assert_equal [S("a"), S("b")], S("a b") .split(S(" "))
1208
+ assert_equal [S("a"), S("b")], S(" a b") .split(S(" "))
1209
+ assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1210
+ assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1211
+ assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1212
+ assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1213
+ assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1214
+
1215
+ assert_equal [S("a"), S("b")], S("a\tb") .split(S(" "))
1216
+ assert_equal [S("a"), S("b")], S("a\nb") .split(S(" "))
1217
+ assert_equal [S("a"), S("b")], S("a\rb") .split(S(" "))
1218
+ assert_equal [S("a"), S("b")], S("a\vb") .split(S(" "))
1219
+ assert_equal [S("a"), S("b")], S("a\t\tb") .split(S(" "))
1220
+ assert_equal [S("a"), S("b")], S("a\n\nb") .split(S(" "))
1221
+ assert_equal [S("a"), S("b")], S("a\r\rb") .split(S(" "))
1222
+ assert_equal [S("a"), S("b")], S("a\v\vb") .split(S(" "))
1223
+ assert_equal [S("a"), S("b")], S("a\v\vb") .split(S(" "))
1224
+
1225
+ assert_equal [S("a"), S("b")], S("a\t b") .split(S(" "))
1226
+ assert_equal [S("a"), S("b")], S("a\t b") .split(S(" "))
1227
+ assert_equal [S("a"), S("b")], S("a\t\nb") .split(S(" "))
1228
+ assert_equal [S("a"), S("b")], S("a\r\nb") .split(S(" "))
1229
+ assert_equal [S("a"), S("b")], S("a\r\n\r\nb").split(S(" "))
1230
+
1231
+ assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), -2)
1232
+ assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), -1)
1233
+ assert_equal [S("a"), S("b")], S(" a b ").split(S(" "), 0)
1234
+ assert_equal [S(" a b ")], S(" a b ").split(S(" "), 1)
1235
+ assert_equal [S("a"), S("b ")], S(" a b ").split(S(" "), 2)
1236
+ assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), 3)
1237
+ assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), 4)
1238
+
1239
+ assert_equal [S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" "))
1240
+
1241
+ assert_equal [S("a"), S("b")], S("a b").split(nil)
1242
+
1243
+ # These tests are all for various patterns.
1244
+
1245
+ assert_equal [S("a b")], S("a b") .split(S(" "))
1246
+ assert_equal [S("a"), S("b")], S("a b") .split(S(" "))
1247
+ assert_equal [S(" a b")], S(" a b") .split(S(" "))
1248
+ assert_equal [S(" a b ")], S(" a b ") .split(S(" "))
1249
+ assert_equal [S(""), S("a b ")], S(" a b ") .split(S(" "))
1250
+ assert_equal [S(""), S("a"), S("b ")], S(" a b ") .split(S(" "))
1251
+ assert_equal [S(""), S("a b")], S(" a b ") .split(S(" "))
1252
+ assert_equal [S(""), S("a"), S("b")], S(" a b ").split(S(" "))
1253
+
1254
+ $; = '|'
1255
+ assert_equal [S("a"), S("b")], S("a|b") .split
1256
+ assert_equal [S("a"), S(""), S("b")], S("a||b") .split
1257
+ assert_equal [S(""), S("a"), S("b")], S("|a|b") .split
1258
+ assert_equal [S(""), S("a"), S("b")], S("|a|b|") .split
1259
+ assert_equal [S(""), S(""), S("a"), S("b")], S("||a|b|") .split
1260
+ assert_equal [S(""), S(""), S("a"), S(""), S("b")], S("||a||b|") .split
1261
+ assert_equal [S(""), S(""), S("a"), S("b")], S("||a|b||") .split
1262
+ assert_equal [S(""), S(""), S("a"), S(""), S("b")], S("||a||b||").split
1263
+
1264
+ assert_equal [S("a"), S("b"), S("c")], S("a|b|c") .split(S("|"))
1265
+ assert_equal [S("a"), S(""), S("c")], S("a||c") .split(S("|"))
1266
+ assert_equal [S(""), S("a"), S("b"), S("c")], S("|a|b|c").split(S("|"))
1267
+
1268
+ assert_equal [S("a b")], S("a b").split(nil)
1269
+ assert_equal [S("a"), S("b")], S("a|b").split(nil)
1270
+
1271
+ # Regexp
1272
+
1273
+ assert_equal [S("a"), S("b"), S("c")], S("abc").split(//)
1274
+ assert_equal [S("a"), S("b"), S("c")], S("abc").split(//i)
1275
+
1276
+ assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), -1)
1277
+ assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 0)
1278
+ assert_equal [S("a|b|c")], S("a|b|c").split(S('|'), 1)
1279
+ assert_equal [S("a"), S("b|c")], S("a|b|c").split(S('|'), 2)
1280
+ assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3)
1281
+ assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 4)
1282
+
1283
+ assert_equal [S("a"), S("b")], S("a|b|").split(S('|'))
1284
+ assert_equal([S("a"), S("b"), S("")], S("a|b|").split(S('|'), -1))
1285
+
1286
+ assert_equal [S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|'))
1287
+ assert_equal([S("a"), S(""), S("b"), S("c"), S("")],
1288
+ S("a||b|c|").split(S('|'), -1))
1289
+
1290
+ assert_equal [S("a"), S("b")], S("a b") .split(/ /)
1291
+ assert_equal [S("a"), S(""), S("b")], S("a b") .split(/ /)
1292
+ assert_equal [S(""), S("a"), S("b")], S(" a b") .split(/ /)
1293
+ assert_equal [S(""), S("a"), S("b")], S(" a b ") .split(/ /)
1294
+ assert_equal [S(""), S(""), S("a"), S("b")], S(" a b ").split(/ /)
1295
+ assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1296
+ S(" a b ").split(/ /))
1297
+ assert_equal([S(""), S(""), S("a"), S("b")],
1298
+ S(" a b ").split(/ /))
1299
+ assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1300
+ S(" a b ").split(/ /))
1301
+
1302
+ assert_equal [S("a b")], S("a b") .split(/ /)
1303
+ assert_equal [S("a"), S("b")], S("a b") .split(/ /)
1304
+ assert_equal [S(" a b")], S(" a b") .split(/ /)
1305
+ assert_equal [S(" a b ")], S(" a b ") .split(/ /)
1306
+ assert_equal [S(""), S("a b ")], S(" a b ") .split(/ /)
1307
+ assert_equal [S(""), S("a"), S("b ")], S(" a b ") .split(/ /)
1308
+ assert_equal [S(""), S("a b")], S(" a b ") .split(/ /)
1309
+ assert_equal [S(""), S("a"), S("b")], S(" a b ").split(/ /)
1310
+
1311
+ assert_equal([S("a"), S("b")],
1312
+ S("a@b") .split(/@/))
1313
+ assert_equal([S("a"), S(""), S("b")],
1314
+ S("a@@b") .split(/@/))
1315
+ assert_equal([S(""), S("a"), S("b")],
1316
+ S("@a@b") .split(/@/))
1317
+ assert_equal([S(""), S("a"), S("b")],
1318
+ S("@a@b@") .split(/@/))
1319
+ assert_equal([S(""), S(""), S("a"), S("b")],
1320
+ S("@@a@b@") .split(/@/))
1321
+ assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1322
+ S("@@a@@b@") .split(/@/))
1323
+ assert_equal([S(""), S(""), S("a"), S("b")],
1324
+ S("@@a@b@@") .split(/@/))
1325
+ assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1326
+ S("@@a@@b@@").split(/@/))
1327
+
1328
+ assert_equal [S("a"), S("b"), S("c")], S("a@b@c") .split(/@/)
1329
+ assert_equal [S("a"), S(""), S("c")], S("a@@c") .split(/@/)
1330
+ assert_equal [S(""), S("a"), S("b"), S("c")], S("@a@b@c").split(/@/)
1331
+
1332
+ # grouping
1333
+
1334
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1335
+ S('ab1cd2ef').split(/(\d)/))
1336
+
1337
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1338
+ S('ab1cd2ef').split(/(\d)/, -2))
1339
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1340
+ S('ab1cd2ef').split(/(\d)/, -1))
1341
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1342
+ S('ab1cd2ef').split(/(\d)/, 0))
1343
+ assert_equal([S('ab1cd2ef')],
1344
+ S('ab1cd2ef').split(/(\d)/, 1))
1345
+ assert_equal([S('ab'), S('1'), S('cd2ef')],
1346
+ S('ab1cd2ef').split(/(\d)/, 2))
1347
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1348
+ S('ab1cd2ef').split(/(\d)/, 3))
1349
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1350
+ S('ab1cd2ef').split(/(\d)/, 4))
1351
+ assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1352
+ S('ab1cd2ef').split(/(\d)/, 5))
1353
+
1354
+ # mulitple grouping
1355
+
1356
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1357
+ S('ab12cd34ef').split(/(\d)(\d)/))
1358
+
1359
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1360
+ S('ab12cd34ef').split(/(\d)(\d)/, -2))
1361
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1362
+ S('ab12cd34ef').split(/(\d)(\d)/, -1))
1363
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1364
+ S('ab12cd34ef').split(/(\d)(\d)/, 0))
1365
+ assert_equal([S('ab12cd34ef')],
1366
+ S('ab12cd34ef').split(/(\d)(\d)/, 1))
1367
+ assert_equal([S('ab'), S('1'), S('2'), S('cd34ef')],
1368
+ S('ab12cd34ef').split(/(\d)(\d)/, 2))
1369
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1370
+ S('ab12cd34ef').split(/(\d)(\d)/, 3))
1371
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1372
+ S('ab12cd34ef').split(/(\d)(\d)/, 4))
1373
+ assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1374
+ S('ab12cd34ef').split(/(\d)(\d)/, 5))
1375
+
1376
+ # nested grouping (puke)
1377
+
1378
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1379
+ S('ab12cd34ef').split(/(\d(\d))/))
1380
+
1381
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1382
+ S('ab12cd34ef').split(/(\d(\d))/, -2))
1383
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1384
+ S('ab12cd34ef').split(/(\d(\d))/, -1))
1385
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1386
+ S('ab12cd34ef').split(/(\d(\d))/, 0))
1387
+ assert_equal([S('ab12cd34ef')],
1388
+ S('ab12cd34ef').split(/(\d(\d))/, 1))
1389
+ assert_equal([S('ab'), S('12'), S('2'), S('cd34ef')],
1390
+ S('ab12cd34ef').split(/(\d(\d))/, 2))
1391
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1392
+ S('ab12cd34ef').split(/(\d(\d))/, 3))
1393
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1394
+ S('ab12cd34ef').split(/(\d(\d))/, 4))
1395
+ assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1396
+ S('ab12cd34ef').split(/(\d(\d))/, 5))
1397
+
1398
+ # split any zero-length match
1399
+
1400
+ assert_equal [S('a'), S('b'), S('c')], S('abc').split(/[z]|/)
1401
+
1402
+ ensure
1403
+ $; = original_dollar_semi
1404
+ end
1405
+
1406
+ def test_split_icky1
1407
+ # HACK soo bizarre and ugly.
1408
+ assert_equal([S('a'), S('b'), S('c'), S('z')],
1409
+ S('abcz').split(/([z])|/),
1410
+ "S('abcz').split(/([z])|/)")
1411
+ end
1412
+
1413
+ def test_split_icky2
1414
+ assert_equal([S('c'), S('z'), S('a')],
1415
+ S('cza').split(/([z]|)/),
1416
+ "S('cz').split(/([z]|)/)")
1417
+ assert_equal([S('a'), S(''), S('b'), S(''), S('c'), S('z')],
1418
+ S('abcz').split(/([z]|)/),
1419
+ "S('abcz').split(/([z]|)/)")
1420
+ end
1421
+
1422
+ def test_squeeze
1423
+ assert_equal(S("abc"), S("aaabbbbccc").squeeze)
1424
+ assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" ")))
1425
+ assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z")))
1426
+ end
1427
+
1428
+ def test_squeeze_bang
1429
+ a = S("aaabbbbccc")
1430
+ b = a.dup
1431
+ assert_equal(S("abc"), a.squeeze!)
1432
+ assert_equal(S("abc"), a)
1433
+ assert_equal(S("aaabbbbccc"), b)
1434
+
1435
+ a = S("aa bb cc")
1436
+ assert_equal(S("aa bb cc"), a.squeeze!(S(" ")))
1437
+ assert_equal(S("aa bb cc"), a)
1438
+
1439
+ a = S("BxxxTyyyWzzzzz")
1440
+ assert_equal(S("BxTyWz"), a.squeeze!(S("a-z")))
1441
+ assert_equal(S("BxTyWz"), a)
1442
+
1443
+ a=S("The quick brown fox")
1444
+ assert_nil(a.squeeze!)
1445
+ end
1446
+
1447
+ def test_strip
1448
+ assert_equal(S("x"), S(" x ").strip)
1449
+ assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip)
1450
+ end
1451
+
1452
+ def test_strip_bang
1453
+ a = S(" x ")
1454
+ b = a.dup
1455
+ assert_equal(S("x") ,a.strip!)
1456
+ assert_equal(S("x") ,a)
1457
+ assert_equal(S(" x "), b)
1458
+
1459
+ a = S(" \n\r\t x \t\r\n\n ")
1460
+ assert_equal(S("x"), a.strip!)
1461
+ assert_equal(S("x"), a)
1462
+
1463
+ a = S("x")
1464
+ assert_nil(a.strip!)
1465
+ assert_equal(S("x") ,a)
1466
+ end
1467
+
1468
+ def test_sub
1469
+ assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*')))
1470
+ assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>')))
1471
+ assert_equal(S("104 ello"), S("hello").sub(/./) { |s| s[0].to_s + S(' ')})
1472
+ assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) {
1473
+ |s| $1.upcase + S('-') + $2
1474
+ })
1475
+
1476
+ assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\'))
1477
+ assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\'))
1478
+ assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1'))
1479
+ assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1'))
1480
+ assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1'))
1481
+ assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1'))
1482
+ assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1'))
1483
+
1484
+ assert_equal(S("a--ababababababababab"),
1485
+ S("abababababababababab").sub(/(b)/, '-\9-'))
1486
+ assert_equal(S("1-b-0"),
1487
+ S("1b2b3b4b5b6b7b8b9b0").
1488
+ sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-'))
1489
+ assert_equal(S("1-b-0"),
1490
+ S("1b2b3b4b5b6b7b8b9b0").
1491
+ sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-'))
1492
+ assert_equal(S("1-\\9-0"),
1493
+ S("1b2b3b4b5b6b7b8b9b0").
1494
+ sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-'))
1495
+ assert_equal(S("k"),
1496
+ S("1a2b3c4d5e6f7g8h9iAjBk").
1497
+ sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+'))
1498
+
1499
+ assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\'))
1500
+ assert_equal(S("ababa"), S("ababa").sub(/b/, '\&'))
1501
+ assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&'))
1502
+ assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&'))
1503
+ assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&'))
1504
+ assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&'))
1505
+
1506
+ a = S("hello")
1507
+ a.taint
1508
+ assert(a.sub(/./, S('X')).tainted?)
1509
+ end
1510
+
1511
+ def test_sub_bang
1512
+ a = S("hello")
1513
+ assert_nil a.sub!(/X/, S('Y'))
1514
+
1515
+ a = S("hello")
1516
+ assert_nil a.sub!(/X/) { |s| assert_nil s }
1517
+
1518
+ a = S("hello")
1519
+ a.sub!(/[aeiou]/, S('*'))
1520
+ assert_equal(S("h*llo"), a)
1521
+
1522
+ a = S("hello")
1523
+ a.sub!(/([aeiou])/, S('<\1>'))
1524
+ assert_equal(S("h<e>llo"), a)
1525
+
1526
+ a = S("hello")
1527
+
1528
+ a.sub!(/./) do |s|
1529
+ assert_equal S('h'), s
1530
+ s[0].to_s + S(' ')
1531
+ end
1532
+
1533
+ assert_equal(S("104 ello"), a)
1534
+
1535
+ a = S("hello")
1536
+ a.sub!(/(hell)(.)/) do |s|
1537
+ assert_equal S('hell'), $1
1538
+ assert_equal S('o'), $2
1539
+ $1.upcase + S('-') + $2
1540
+ end
1541
+ assert_equal(S("HELL-o"), a)
1542
+
1543
+ r = S('X')
1544
+ r.taint
1545
+ a.sub!(/./, r)
1546
+ assert(a.tainted?)
1547
+ end
1548
+
1549
+ def test_succ
1550
+ assert_equal(S("abd"), S("abc").succ)
1551
+ assert_equal(S("z"), S("y").succ)
1552
+ assert_equal(S("aaa"), S("zz").succ)
1553
+
1554
+ assert_equal(S("124"), S("123").succ)
1555
+ assert_equal(S("1000"), S("999").succ)
1556
+
1557
+ assert_equal(S("2000aaa"), S("1999zzz").succ)
1558
+ assert_equal(S("AAAAA000"), S("ZZZZ999").succ)
1559
+ assert_equal(S("*+"), S("**").succ)
1560
+
1561
+ assert_equal(S("\001\000"), S("\377").succ)
1562
+ end
1563
+
1564
+ def test_succ_bang
1565
+ a = S("abc")
1566
+ assert_equal(S("abd"), a.succ!)
1567
+ assert_equal(S("abd"), a)
1568
+
1569
+ a = S("y")
1570
+ assert_equal(S("z"), a.succ!)
1571
+ assert_equal(S("z"), a)
1572
+
1573
+ a = S("zz")
1574
+ assert_equal(S("aaa"), a.succ!)
1575
+ assert_equal(S("aaa"), a)
1576
+
1577
+ a = S("123")
1578
+ assert_equal(S("124"), a.succ!)
1579
+ assert_equal(S("124"), a)
1580
+
1581
+ a = S("999")
1582
+ assert_equal(S("1000"), a.succ!)
1583
+ assert_equal(S("1000"), a)
1584
+
1585
+ a = S("1999zzz")
1586
+ assert_equal(S("2000aaa"), a.succ!)
1587
+ assert_equal(S("2000aaa"), a)
1588
+
1589
+ a = S("ZZZZ999")
1590
+ assert_equal(S("AAAAA000"), a.succ!)
1591
+ assert_equal(S("AAAAA000"), a)
1592
+
1593
+ a = S("**")
1594
+ assert_equal(S("*+"), a.succ!)
1595
+ assert_equal(S("*+"), a)
1596
+
1597
+ a = S("\377")
1598
+ assert_equal(S("\001\000"), a.succ!)
1599
+ assert_equal(S("\001\000"), a)
1600
+ end
1601
+
1602
+ def test_sum
1603
+ n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001")
1604
+ assert_equal(15, n.sum)
1605
+ assert_equal(15, n.sum(32))
1606
+ n += S("\001")
1607
+ assert_equal(16, n.sum(17))
1608
+ assert_equal(16, n.sum(32))
1609
+ n[0] = 2
1610
+ assert(15 != n.sum)
1611
+
1612
+ # basic test of all reasonable "bit sizes"
1613
+ str = S("\xFF" * 257)
1614
+ 2.upto(32) do |bits|
1615
+ assert_equal(65535 % (2**bits), str.sum(bits))
1616
+ end
1617
+
1618
+ # with 16 bits the result is modulo 65536
1619
+ assert_equal( 255, S("\xFF").sum)
1620
+ assert_equal( 510, S("\xFF\xFF").sum)
1621
+ assert_equal(65535, S("\xFF" * 257).sum)
1622
+ assert_equal( 254, S("\xFF" * 258).sum)
1623
+
1624
+ # with 32 bits the result is modulo 2**32
1625
+ assert_equal( 255, S("\xFF").sum(32))
1626
+ assert_equal( 510, S("\xFF\xFF").sum(32))
1627
+ assert_equal(65535, S("\xFF" * 257).sum(32))
1628
+ assert_equal(65790, S("\xFF" * 258).sum(32))
1629
+ # the following case takes 16MB and takes a long time to compute,
1630
+ # so it is commented out.
1631
+ #assert_equal( 254, S("\xFF" * (257 * 65537 + 1)).sum(32))
1632
+ end
1633
+
1634
+ def test_swapcase
1635
+ assert_equal(S("hi&LOW"), S("HI&low").swapcase)
1636
+ end
1637
+
1638
+ def test_swapcase_bang
1639
+ a = S("hi&LOW")
1640
+ b = a.dup
1641
+ assert_equal(S("HI&low"), a.swapcase!)
1642
+ assert_equal(S("HI&low"), a)
1643
+ assert_equal(S("hi&LOW"), b)
1644
+
1645
+ a = S("$^#^%$#!!")
1646
+ assert_nil(a.swapcase!)
1647
+ assert_equal(S("$^#^%$#!!"), a)
1648
+ end
1649
+
1650
+ def test_swapcase_bang_multibyte
1651
+ # TODO: flunk "No multibyte tests yet"
1652
+ end
1653
+
1654
+ def test_times
1655
+ assert_equal(S(""), S("HO") * 0)
1656
+ assert_equal(S("HOHO"), S("HO") * 2)
1657
+ end
1658
+
1659
+ def test_times_negative
1660
+ assert_raises ArgumentError do
1661
+ S("str") * -1
1662
+ end
1663
+ end
1664
+
1665
+ def test_to_f
1666
+ assert_equal(344.3, S("344.3").to_f)
1667
+ assert_equal(5.9742e24, S("5.9742e24").to_f)
1668
+ assert_equal(98.6, S("98.6 degrees").to_f)
1669
+ assert_equal(0.0, S("degrees 100.0").to_f)
1670
+ end
1671
+
1672
+ def test_to_i
1673
+ assert_equal(1480, S("1480ft/sec").to_i)
1674
+ assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i)
1675
+ end
1676
+
1677
+ def test_to_s
1678
+ a = S("me")
1679
+ assert_equal("me", a.to_s)
1680
+ assert_equal(a.object_id, a.to_s.object_id)
1681
+
1682
+ b = TestStringSubclass.new("me")
1683
+ assert_equal("me", b.to_s)
1684
+ assert_not_equal(b.object_id, b.to_s.object_id)
1685
+ end
1686
+
1687
+ def test_to_str
1688
+ a = S("me")
1689
+ assert_equal("me", a.to_str)
1690
+ assert_equal(a.object_id, a.to_str.object_id)
1691
+
1692
+ b = TestStringSubclass.new("me")
1693
+ assert_equal("me", b.to_str)
1694
+ assert_not_equal(b.object_id, b.to_str.object_id)
1695
+ end
1696
+
1697
+ def test_to_sym
1698
+ assert_equal(:alpha, S("alpha").to_sym)
1699
+ assert_equal(:alp_ha, S("alp_ha").to_sym)
1700
+ assert_equal(:"9alpha", S("9alpha").to_sym)
1701
+ assert_equal(:"9al pha", S("9al pha").to_sym)
1702
+ end
1703
+
1704
+ def test_tr
1705
+ assert_equal S("hippo"), S("hello").tr(S("el"), S("ip"))
1706
+ assert_equal S("*e**o"), S("hello").tr(S("^aeiou"), S("*"))
1707
+ assert_equal S("hal"), S("ibm").tr(S("b-z"), S("a-z"))
1708
+
1709
+ assert_equal S("www"), S("abc").tr(S("a-c"), S("w"))
1710
+ assert_equal S("wxx"), S("abc").tr(S("a-c"), S("w-x"))
1711
+ assert_equal S("wxy"), S("abc").tr(S("a-c"), S("w-y"))
1712
+ assert_equal S("wxy"), S("abc").tr(S("a-c"), S("w-z"))
1713
+
1714
+ assert_equal S("wbc"), S("abc").tr(S("a"), S("w-x"))
1715
+ assert_equal S("wxc"), S("abc").tr(S("a-b"), S("w-y"))
1716
+ end
1717
+
1718
+ def test_tr_bang
1719
+ a = S("hello")
1720
+ b = a.dup
1721
+ assert_equal(S("hippo"), a.tr!(S("el"), S("ip")))
1722
+ assert_equal(S("hippo"), a)
1723
+ assert_equal(S("hello"),b)
1724
+
1725
+ a = S("hello")
1726
+ assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*")))
1727
+ assert_equal(S("*e**o"), a)
1728
+
1729
+ a = S("IBM")
1730
+ assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z")))
1731
+ assert_equal(S("HAL"), a)
1732
+
1733
+ a = S("ibm")
1734
+ assert_nil(a.tr!(S("B-Z"), S("A-Z")))
1735
+ assert_equal(S("ibm"), a)
1736
+ end
1737
+
1738
+ def test_tr_s
1739
+ assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
1740
+ assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*")))
1741
+ end
1742
+
1743
+ def test_tr_s_bang
1744
+ a = S("hello")
1745
+ b = a.dup
1746
+ assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp")))
1747
+ assert_equal(S("hypo"), a)
1748
+ assert_equal(S("hello"), b)
1749
+
1750
+ a = S("hello")
1751
+ assert_equal(S("h*o"), a.tr_s!(S("el"), S("*")))
1752
+ assert_equal(S("h*o"), a)
1753
+ end
1754
+
1755
+ def test_upcase
1756
+ assert_equal(S("HELLO"), S("hello").upcase)
1757
+ assert_equal(S("HELLO"), S("hello").upcase)
1758
+ assert_equal(S("HELLO"), S("HELLO").upcase)
1759
+ assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase)
1760
+ end
1761
+
1762
+ def test_upcase_bang
1763
+ a = S("hello")
1764
+ b = a.dup
1765
+ assert_equal(S("HELLO"), a.upcase!)
1766
+ assert_equal(S("HELLO"), a)
1767
+ assert_equal(S("hello"), b)
1768
+
1769
+ a = S("HELLO")
1770
+ assert_nil(a.upcase!)
1771
+ assert_equal(S("HELLO"), a)
1772
+ end
1773
+
1774
+ def test_upcase_bang_multibyte
1775
+ # TODO: flunk "No multibyte tests yet"
1776
+ end
1777
+
1778
+ def test_upto
1779
+ a = S("aa")
1780
+ start = S("aa")
1781
+ count = 0
1782
+ val = a.upto S("zz") do |s|
1783
+ assert_equal(start, s)
1784
+ start.succ!
1785
+ count += 1
1786
+ end
1787
+
1788
+ assert_equal(S("aa"), val)
1789
+ assert_equal(676, count)
1790
+ end
1791
+ end
1792
+
1793
+ require 'test/unit' if $0 == __FILE__