2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-01-05 14:14:24 -05:00
|
|
|
require 'test/unit'
|
2014-05-18 03:43:45 -04:00
|
|
|
require 'etc'
|
2008-02-29 23:12:13 -05:00
|
|
|
require_relative 'allpairs'
|
2008-01-05 14:14:24 -05:00
|
|
|
|
|
|
|
class TestM17NComb < Test::Unit::TestCase
|
|
|
|
def assert_encoding(encname, actual, message=nil)
|
|
|
|
assert_equal(Encoding.find(encname), actual, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
module AESU
|
2013-07-20 05:10:17 -04:00
|
|
|
def a(str) str.dup.force_encoding(Encoding::US_ASCII) end
|
|
|
|
def b(str) str.b end
|
|
|
|
def e(str) str.dup.force_encoding(Encoding::EUC_JP) end
|
|
|
|
def s(str) str.dup.force_encoding(Encoding::SJIS) end
|
|
|
|
def u(str) str.dup.force_encoding(Encoding::UTF_8) end
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
include AESU
|
|
|
|
extend AESU
|
|
|
|
|
|
|
|
def assert_strenc(bytes, enc, actual, message=nil)
|
|
|
|
assert_instance_of(String, actual, message)
|
|
|
|
enc = Encoding.find(enc) if String === enc
|
|
|
|
assert_equal(enc, actual.encoding, message)
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(bytes), b(actual), message)
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
STRINGS = [
|
2013-07-20 05:10:17 -04:00
|
|
|
b(""), e(""), s(""), u(""),
|
|
|
|
b("a"), e("a"), s("a"), u("a"),
|
|
|
|
b("."), e("."), s("."), u("."),
|
2008-01-05 14:14:24 -05:00
|
|
|
|
|
|
|
# single character
|
2013-07-20 05:10:17 -04:00
|
|
|
b("\x80"), b("\xff"),
|
2008-01-05 14:14:24 -05:00
|
|
|
e("\xa1\xa1"), e("\xfe\xfe"),
|
|
|
|
e("\x8e\xa1"), e("\x8e\xfe"),
|
|
|
|
e("\x8f\xa1\xa1"), e("\x8f\xfe\xfe"),
|
|
|
|
s("\x81\x40"), s("\xfc\xfc"),
|
|
|
|
s("\xa1"), s("\xdf"),
|
|
|
|
u("\xc2\x80"), u("\xf4\x8f\xbf\xbf"),
|
|
|
|
|
|
|
|
# same byte sequence
|
2013-07-20 05:10:17 -04:00
|
|
|
b("\xc2\xa1"), e("\xc2\xa1"), s("\xc2\xa1"), u("\xc2\xa1"),
|
2008-01-05 14:14:24 -05:00
|
|
|
|
|
|
|
s("\x81A"), # mutibyte character which contains "A"
|
|
|
|
s("\x81a"), # mutibyte character which contains "a"
|
|
|
|
|
|
|
|
# invalid
|
|
|
|
e("\xa1"), e("\x80"),
|
|
|
|
s("\x81"), s("\x80"),
|
|
|
|
u("\xc2"), u("\x80"),
|
|
|
|
|
|
|
|
# for transitivity test
|
2008-09-24 08:29:01 -04:00
|
|
|
u("\xe0\xa0\xa1"), e("\xe0\xa0\xa1"), s("\xe0\xa0\xa1"), # [ruby-dev:32693]
|
2013-07-20 05:10:17 -04:00
|
|
|
e("\xa1\xa1"), b("\xa1\xa1"), s("\xa1\xa1"), # [ruby-dev:36484]
|
2013-07-19 23:14:09 -04:00
|
|
|
]
|
2008-01-23 04:25:17 -05:00
|
|
|
|
2013-07-19 23:14:09 -04:00
|
|
|
WSTRINGS = [
|
|
|
|
"aa".force_encoding("utf-16be"),
|
|
|
|
"aaaa".force_encoding("utf-32be"),
|
|
|
|
"aaa".force_encoding("utf-32be"),
|
2008-01-05 14:14:24 -05:00
|
|
|
]
|
|
|
|
|
2008-02-29 23:12:13 -05:00
|
|
|
def combination(*args, &b)
|
|
|
|
AllPairs.each(*args, &b)
|
2008-10-02 07:46:40 -04:00
|
|
|
#AllPairs.exhaustive_each(*args, &b)
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def encdump(str)
|
2008-01-23 04:25:17 -05:00
|
|
|
d = str.dump
|
|
|
|
if /\.force_encoding\("[A-Za-z0-9.:_+-]*"\)\z/ =~ d
|
|
|
|
d
|
|
|
|
else
|
|
|
|
"#{d}.force_encoding(#{str.encoding.name.dump})"
|
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def encdumpargs(args)
|
|
|
|
r = '('
|
|
|
|
args.each_with_index {|a, i|
|
|
|
|
r << ',' if 0 < i
|
|
|
|
if String === a
|
|
|
|
r << encdump(a)
|
|
|
|
else
|
|
|
|
r << a.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
r << ')'
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2014-07-22 00:33:38 -04:00
|
|
|
def encdumpcall(recv, meth, *args, &block)
|
2008-01-23 04:25:17 -05:00
|
|
|
desc = ''
|
|
|
|
if String === recv
|
|
|
|
desc << encdump(recv)
|
|
|
|
else
|
|
|
|
desc << recv.inspect
|
|
|
|
end
|
|
|
|
desc << '.' << meth.to_s
|
|
|
|
if !args.empty?
|
|
|
|
desc << '('
|
|
|
|
args.each_with_index {|a, i|
|
|
|
|
desc << ',' if 0 < i
|
|
|
|
if String === a
|
|
|
|
desc << encdump(a)
|
|
|
|
else
|
|
|
|
desc << a.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
desc << ')'
|
|
|
|
end
|
|
|
|
if block
|
|
|
|
desc << ' {}'
|
|
|
|
end
|
2014-07-22 00:33:38 -04:00
|
|
|
desc
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_enccall(recv, meth, *args, &block)
|
|
|
|
desc = encdumpcall(recv, meth, *args, &block)
|
2008-01-23 04:25:17 -05:00
|
|
|
result = nil
|
|
|
|
assert_nothing_raised(desc) {
|
|
|
|
result = recv.send(meth, *args, &block)
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
2013-07-19 23:14:09 -04:00
|
|
|
alias enccall assert_enccall
|
2008-01-23 04:25:17 -05:00
|
|
|
|
2008-01-05 14:14:24 -05:00
|
|
|
def assert_str_enc_propagation(t, s1, s2)
|
|
|
|
if !s1.ascii_only?
|
|
|
|
assert_equal(s1.encoding, t.encoding)
|
|
|
|
elsif !s2.ascii_only?
|
|
|
|
assert_equal(s2.encoding, t.encoding)
|
|
|
|
else
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include([s1.encoding, s2.encoding], t.encoding)
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_same_result(expected_proc, actual_proc)
|
|
|
|
e = nil
|
|
|
|
begin
|
|
|
|
t = expected_proc.call
|
|
|
|
rescue
|
|
|
|
e = $!
|
|
|
|
end
|
|
|
|
if e
|
|
|
|
assert_raise(e.class) { actual_proc.call }
|
|
|
|
else
|
|
|
|
assert_equal(t, actual_proc.call)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_slice_call
|
|
|
|
combination(STRINGS, -2..2) {|s, nth|
|
|
|
|
yield s, nth
|
|
|
|
}
|
|
|
|
combination(STRINGS, -2..2, 0..2) {|s, nth, len|
|
|
|
|
yield s, nth, len
|
|
|
|
}
|
|
|
|
combination(STRINGS, STRINGS) {|s, substr|
|
|
|
|
yield s, substr
|
|
|
|
}
|
|
|
|
combination(STRINGS, -2..2, 0..2) {|s, first, last|
|
|
|
|
yield s, first..last
|
|
|
|
yield s, first...last
|
|
|
|
}
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if !s2.valid_encoding?
|
|
|
|
next
|
|
|
|
end
|
|
|
|
yield s1, Regexp.new(Regexp.escape(s2))
|
|
|
|
}
|
|
|
|
combination(STRINGS, STRINGS, 0..2) {|s1, s2, nth|
|
|
|
|
if !s2.valid_encoding?
|
|
|
|
next
|
|
|
|
end
|
|
|
|
yield s1, Regexp.new(Regexp.escape(s2)), nth
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-01-26 21:38:10 -05:00
|
|
|
ASCII_INCOMPATIBLE_ENCODINGS = %w[
|
|
|
|
UTF-16BE
|
|
|
|
UTF-16LE
|
|
|
|
UTF-32BE
|
|
|
|
UTF-32LE
|
|
|
|
]
|
2008-01-05 14:14:24 -05:00
|
|
|
def str_enc_compatible?(*strs)
|
|
|
|
encs = []
|
2008-01-26 21:38:10 -05:00
|
|
|
ascii_incompatible_encodings = {}
|
|
|
|
has_ascii_compatible = false
|
2008-01-05 14:14:24 -05:00
|
|
|
strs.each {|s|
|
|
|
|
encs << s.encoding if !s.ascii_only?
|
2008-01-26 21:38:10 -05:00
|
|
|
if /\A#{Regexp.union ASCII_INCOMPATIBLE_ENCODINGS}\z/o =~ s.encoding.name
|
|
|
|
ascii_incompatible_encodings[s.encoding] = true
|
|
|
|
else
|
|
|
|
has_ascii_compatible = true
|
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
2008-01-26 21:38:10 -05:00
|
|
|
if ascii_incompatible_encodings.empty?
|
|
|
|
encs.uniq!
|
|
|
|
encs.length <= 1
|
|
|
|
else
|
|
|
|
!has_ascii_compatible && ascii_incompatible_encodings.size == 1
|
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# tests start
|
|
|
|
|
|
|
|
def test_str_new
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = String.new(s)
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_strenc(b(s), s.encoding, t)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_plus
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if s1.encoding != s2.encoding && !s1.ascii_only? && !s2.ascii_only?
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1 + s2 }
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :+, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(s1) + b(s2), b(t))
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_str_enc_propagation(t, s1, s2)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_times
|
|
|
|
STRINGS.each {|s|
|
|
|
|
[0,1,2].each {|n|
|
|
|
|
t = s * n
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s.valid_encoding?
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_strenc(b(s) * n, s.encoding, t)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sprintf_s
|
|
|
|
STRINGS.each {|s|
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_strenc(b(s), s.encoding, "%s".force_encoding(s.encoding) % s)
|
2008-01-05 14:14:24 -05:00
|
|
|
if !s.empty? # xxx
|
2013-07-20 05:10:17 -04:00
|
|
|
t = enccall(b("%s"), :%, s)
|
|
|
|
assert_strenc(b(s), (b('')+s).encoding, t)
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_eq_reflexive
|
|
|
|
STRINGS.each {|s|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(s, s, "#{encdump s} == #{encdump s}")
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_eq_symmetric
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if s1 == s2
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(s2, s1, "#{encdump s2} == #{encdump s1}")
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_equal(s2, s1, "!(#{encdump s2} == #{encdump s1})")
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_eq_transitive
|
|
|
|
combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
|
|
|
|
if s1 == s2 && s2 == s3
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(s1, s3, "transitive: #{encdump s1} == #{encdump s2} == #{encdump s3}")
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_eq
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
desc_eq = "#{encdump s1} == #{encdump s2}"
|
2013-07-20 05:10:17 -04:00
|
|
|
if b(s1) == b(s2) and
|
2008-09-18 09:17:41 -04:00
|
|
|
(s1.ascii_only? && s2.ascii_only? or
|
2008-10-05 23:22:56 -04:00
|
|
|
s1.encoding == s2.encoding) then
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(s1, :==, s2, desc_eq)
|
|
|
|
assert_not_operator(s1, :!=, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(0, s1 <=> s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(s1, :eql?, s2, desc_eq)
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_operator(s1, :==, s2, "!(#{desc_eq})")
|
|
|
|
assert_operator(s1, :!=, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_not_equal(0, s1 <=> s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_operator(s1, :eql?, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_concat
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
s = s1.dup
|
|
|
|
if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
|
|
|
|
s << s2
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(s, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(s), b(s1) + b(s2))
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_str_enc_propagation(s, s1, s2)
|
|
|
|
else
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s << s2 }
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_aref
|
|
|
|
STRINGS.each {|s|
|
2008-01-26 21:38:10 -05:00
|
|
|
t = ''.force_encoding(s.encoding)
|
2008-01-05 14:14:24 -05:00
|
|
|
0.upto(s.length-1) {|i|
|
|
|
|
u = s[i]
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(u, :valid_encoding?) if s.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
t << u
|
|
|
|
}
|
|
|
|
assert_equal(t, s)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_aref_len
|
|
|
|
STRINGS.each {|s|
|
2008-01-26 21:38:10 -05:00
|
|
|
t = ''.force_encoding(s.encoding)
|
2008-01-05 14:14:24 -05:00
|
|
|
0.upto(s.length-1) {|i|
|
|
|
|
u = s[i,1]
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(u, :valid_encoding?) if s.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
t << u
|
|
|
|
}
|
|
|
|
assert_equal(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
STRINGS.each {|s|
|
2008-01-26 21:38:10 -05:00
|
|
|
t = ''.force_encoding(s.encoding)
|
2008-01-05 14:14:24 -05:00
|
|
|
0.step(s.length-1, 2) {|i|
|
|
|
|
u = s[i,2]
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(u, :valid_encoding?) if s.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
t << u
|
|
|
|
}
|
|
|
|
assert_equal(t, s)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_aref_substr
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :[], s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
if t != nil
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s2, t)
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_match(/#{Regexp.escape(b(s2))}/, b(s1))
|
2008-01-05 14:14:24 -05:00
|
|
|
if s1.valid_encoding?
|
|
|
|
assert_match(/#{Regexp.escape(s2)}/, s1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1[s2] }
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_aref_range2
|
|
|
|
combination(STRINGS, -2..2, -2..2) {|s, first, last|
|
2008-01-23 04:25:17 -05:00
|
|
|
desc = "#{encdump s}[#{first}..#{last}]"
|
2008-01-05 14:14:24 -05:00
|
|
|
t = s[first..last]
|
|
|
|
if first < 0
|
|
|
|
first += s.length
|
|
|
|
if first < 0
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_nil(t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if s.length < first
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_nil(t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
if last < 0
|
|
|
|
last += s.length
|
|
|
|
end
|
|
|
|
t2 = ''
|
|
|
|
first.upto(last) {|i|
|
|
|
|
c = s[i]
|
|
|
|
t2 << c if c
|
|
|
|
}
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_equal(t2, t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_aref_range3
|
|
|
|
combination(STRINGS, -2..2, -2..2) {|s, first, last|
|
2008-01-23 04:25:17 -05:00
|
|
|
desc = "#{encdump s}[#{first}..#{last}]"
|
2008-01-05 14:14:24 -05:00
|
|
|
t = s[first...last]
|
|
|
|
if first < 0
|
|
|
|
first += s.length
|
|
|
|
if first < 0
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_nil(t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if s.length < first
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_nil(t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if last < 0
|
|
|
|
last += s.length
|
|
|
|
end
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
t2 = ''
|
|
|
|
first.upto(last-1) {|i|
|
|
|
|
c = s[i]
|
|
|
|
t2 << c if c
|
|
|
|
}
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_equal(t2, t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_assign
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
(-2).upto(2) {|i|
|
|
|
|
t = s1.dup
|
|
|
|
if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
|
|
|
|
if i < -s1.length || s1.length < i
|
|
|
|
assert_raise(IndexError) { t[i] = s2 }
|
|
|
|
else
|
|
|
|
t[i] = s2
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_send([b(t), :index, b(s2)])
|
2008-01-05 14:14:24 -05:00
|
|
|
if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
if i == s1.length && s2.empty?
|
|
|
|
assert_nil(t[i])
|
|
|
|
elsif i < 0
|
|
|
|
assert_equal(s2, t[i-s2.length+1,s2.length],
|
|
|
|
"t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i-s2.length+1},#{s2.length}]")
|
|
|
|
else
|
|
|
|
assert_equal(s2, t[i,s2.length],
|
|
|
|
"t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { t[i] = s2 }
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_assign_len
|
|
|
|
combination(STRINGS, -2..2, 0..2, STRINGS) {|s1, i, len, s2|
|
|
|
|
t = s1.dup
|
|
|
|
if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
|
|
|
|
if i < -s1.length || s1.length < i
|
|
|
|
assert_raise(IndexError) { t[i,len] = s2 }
|
|
|
|
else
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
t[i,len] = s2
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_send([b(t), :index, b(s2)])
|
2008-01-05 14:14:24 -05:00
|
|
|
if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
if i == s1.length && s2.empty?
|
|
|
|
assert_nil(t[i])
|
|
|
|
elsif i < 0
|
|
|
|
if -i < len
|
|
|
|
len = -i
|
|
|
|
end
|
|
|
|
assert_equal(s2, t[i-s2.length+len,s2.length],
|
|
|
|
"t = #{encdump(s1)}; t[#{i},#{len}] = #{encdump(s2)}; t[#{i-s2.length+len},#{s2.length}]")
|
|
|
|
else
|
|
|
|
assert_equal(s2, t[i,s2.length],
|
|
|
|
"t = #{encdump(s1)}; t[#{i},#{len}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { t[i,len] = s2 }
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_assign_substr
|
|
|
|
combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
|
|
|
|
t = s1.dup
|
|
|
|
encs = [
|
|
|
|
!s1.ascii_only? ? s1.encoding : nil,
|
|
|
|
!s2.ascii_only? ? s2.encoding : nil,
|
|
|
|
!s3.ascii_only? ? s3.encoding : nil].uniq.compact
|
|
|
|
if 1 < encs.length
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, IndexError) { t[s2] = s3 }
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
|
|
|
if encs.empty?
|
|
|
|
encs = [
|
|
|
|
s1.encoding,
|
|
|
|
s2.encoding,
|
|
|
|
s3.encoding].uniq.reject {|e| e == Encoding.find("ASCII-8BIT") }
|
|
|
|
if encs.empty?
|
|
|
|
encs = [Encoding.find("ASCII-8BIT")]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !t[s2]
|
|
|
|
else
|
2008-01-23 04:25:17 -05:00
|
|
|
enccall(t, :[]=, s2, s3)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding? && s3.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_assign_range2
|
|
|
|
combination(STRINGS, -2..2, -2..2, STRINGS) {|s1, first, last, s2|
|
|
|
|
t = s1.dup
|
|
|
|
if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
|
|
|
|
if first < -s1.length || s1.length < first
|
|
|
|
assert_raise(RangeError) { t[first..last] = s2 }
|
|
|
|
else
|
2008-01-23 04:25:17 -05:00
|
|
|
enccall(t, :[]=, first..last, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_send([b(t), :index, b(s2)])
|
2008-01-05 14:14:24 -05:00
|
|
|
if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
if first < 0
|
|
|
|
assert_equal(s2, t[s1.length+first, s2.length])
|
|
|
|
else
|
|
|
|
assert_equal(s2, t[first, s2.length])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, RangeError,
|
2008-01-05 14:14:24 -05:00
|
|
|
"t=#{encdump(s1)};t[#{first}..#{last}]=#{encdump(s2)}") {
|
|
|
|
t[first..last] = s2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_assign_range3
|
|
|
|
combination(STRINGS, -2..2, -2..2, STRINGS) {|s1, first, last, s2|
|
|
|
|
t = s1.dup
|
|
|
|
if s1.ascii_only? || s2.ascii_only? || s1.encoding == s2.encoding
|
|
|
|
if first < -s1.length || s1.length < first
|
|
|
|
assert_raise(RangeError) { t[first...last] = s2 }
|
|
|
|
else
|
2008-01-23 04:25:17 -05:00
|
|
|
enccall(t, :[]=, first...last, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_send([b(t), :index, b(s2)])
|
2008-01-05 14:14:24 -05:00
|
|
|
if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
if first < 0
|
|
|
|
assert_equal(s2, t[s1.length+first, s2.length])
|
|
|
|
else
|
|
|
|
assert_equal(s2, t[first, s2.length])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, RangeError,
|
2008-01-05 14:14:24 -05:00
|
|
|
"t=#{encdump(s1)};t[#{first}...#{last}]=#{encdump(s2)}") {
|
|
|
|
t[first...last] = s2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_cmp
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
desc = "#{encdump s1} <=> #{encdump s2}"
|
|
|
|
r = s1 <=> s2
|
|
|
|
if s1 == s2
|
|
|
|
assert_equal(0, r, desc)
|
|
|
|
else
|
|
|
|
assert_not_equal(0, r, desc)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_capitalize
|
|
|
|
STRINGS.each {|s|
|
|
|
|
begin
|
|
|
|
t1 = s.capitalize
|
|
|
|
rescue ArgumentError
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_predicate(s, :valid_encoding?)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t1, :valid_encoding?) if s.valid_encoding?
|
|
|
|
assert_operator(t1, :casecmp, s)
|
2008-01-05 14:14:24 -05:00
|
|
|
t2 = s.dup
|
|
|
|
t2.capitalize!
|
|
|
|
assert_equal(t1, t2)
|
2008-01-23 04:25:17 -05:00
|
|
|
r = s.downcase
|
2013-07-20 05:10:17 -04:00
|
|
|
r = enccall(r, :sub, /\A[a-z]/) {|ch| b(ch).upcase }
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_equal(r, t1)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_casecmp
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
#puts "#{encdump(s1)}.casecmp(#{encdump(s2)})"
|
2011-02-15 02:08:15 -05:00
|
|
|
next unless s1.valid_encoding? && s2.valid_encoding? && Encoding.compatible?(s1, s2)
|
|
|
|
r = s1.casecmp(s2)
|
|
|
|
assert_equal(s1.upcase <=> s2.upcase, r)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-24 01:55:22 -05:00
|
|
|
def test_str_casecmp?
|
|
|
|
strings = STRINGS.dup
|
|
|
|
strings.push(
|
|
|
|
# prevent wrong single byte optimization
|
|
|
|
"\xC0".force_encoding("ISO-8859-1"),
|
|
|
|
"\xE0".force_encoding("ISO-8859-1"),
|
|
|
|
)
|
|
|
|
combination(strings, strings) {|s1, s2|
|
|
|
|
#puts "#{encdump(s1)}.casecmp(#{encdump(s2)})"
|
|
|
|
next unless s1.valid_encoding? && s2.valid_encoding? && Encoding.compatible?(s1, s2)
|
|
|
|
r = s1.casecmp?(s2)
|
|
|
|
assert_equal(s1.downcase(:fold) == s2.downcase(:fold), r)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-01-05 14:14:24 -05:00
|
|
|
def test_str_center
|
|
|
|
combination(STRINGS, [0,1,2,3,10]) {|s1, width|
|
|
|
|
t = s1.center(width)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_send([b(t), :index, b(s1)])
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
combination(STRINGS, [0,1,2,3,10], STRINGS) {|s1, width, s2|
|
|
|
|
if s2.empty?
|
|
|
|
assert_raise(ArgumentError) { s1.center(width, s2) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.center(width, s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :center, width, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_send([b(t), :index, b(s1)])
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_str_enc_propagation(t, s1, s2) if (t != s1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_ljust
|
|
|
|
combination(STRINGS, [0,1,2,3,10]) {|s1, width|
|
|
|
|
t = s1.ljust(width)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_send([b(t), :index, b(s1)])
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
combination(STRINGS, [0,1,2,3,10], STRINGS) {|s1, width, s2|
|
|
|
|
if s2.empty?
|
|
|
|
assert_raise(ArgumentError) { s1.ljust(width, s2) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.ljust(width, s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :ljust, width, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_send([b(t), :index, b(s1)])
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_str_enc_propagation(t, s1, s2) if (t != s1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_rjust
|
|
|
|
combination(STRINGS, [0,1,2,3,10]) {|s1, width|
|
|
|
|
t = s1.rjust(width)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_send([b(t), :index, b(s1)])
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
combination(STRINGS, [0,1,2,3,10], STRINGS) {|s1, width, s2|
|
|
|
|
if s2.empty?
|
|
|
|
assert_raise(ArgumentError) { s1.rjust(width, s2) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.rjust(width, s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :rjust, width, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_send([b(t), :index, b(s1)])
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_str_enc_propagation(t, s1, s2) if (t != s1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_chomp
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
2008-02-29 09:57:04 -05:00
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && !Encoding.compatible?(s1,s2)
|
2009-03-05 22:56:38 -05:00
|
|
|
if s1.bytesize > s2.bytesize
|
2015-02-23 19:47:12 -05:00
|
|
|
assert_raise(Encoding::CompatibilityError, "#{encdump(s1)}.chomp(#{encdump(s2)})") do
|
|
|
|
s1.chomp(s2)
|
|
|
|
end
|
2008-02-29 09:57:04 -05:00
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :chomp, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?, "#{encdump(s1)}.chomp(#{encdump(s2)})") if s1.valid_encoding? && s2.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s1.encoding, t.encoding)
|
|
|
|
t2 = s1.dup
|
|
|
|
t2.chomp!(s2)
|
|
|
|
assert_equal(t, t2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-02-23 19:47:12 -05:00
|
|
|
def test_str_smart_chomp
|
|
|
|
bug10893 = '[ruby-core:68258] [Bug #10893]'
|
|
|
|
encodings = Encoding.list.select {|enc| !enc.dummy?}
|
|
|
|
combination(encodings, encodings) do |e1, e2|
|
|
|
|
expected = "abc".encode(e1)
|
|
|
|
combination(["abc\n", "abc\r\n"], ["", "\n"]) do |str, rs|
|
|
|
|
assert_equal(expected, str.encode(e1).chomp(rs.encode(e2)), bug10893)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-05 14:14:24 -05:00
|
|
|
def test_str_chop
|
|
|
|
STRINGS.each {|s|
|
|
|
|
s = s.dup
|
|
|
|
desc = "#{encdump s}.chop"
|
|
|
|
t = nil
|
|
|
|
assert_nothing_raised(desc) { t = s.chop }
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?) if s.valid_encoding?
|
|
|
|
assert_send([b(s), :index, b(t)])
|
2008-01-05 14:14:24 -05:00
|
|
|
t2 = s.dup
|
|
|
|
t2.chop!
|
|
|
|
assert_equal(t, t2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_clear
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.dup
|
|
|
|
t.clear
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?)
|
|
|
|
assert_empty(t)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_clone
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.clone
|
|
|
|
assert_equal(s, t)
|
|
|
|
assert_equal(s.encoding, t.encoding)
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(s), b(t))
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_dup
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.dup
|
|
|
|
assert_equal(s, t)
|
|
|
|
assert_equal(s.encoding, t.encoding)
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(s), b(t))
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_count
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
2014-07-22 00:33:38 -04:00
|
|
|
desc = proc {encdumpcall(s1, :count, s2)}
|
2008-01-05 14:14:24 -05:00
|
|
|
if !s1.valid_encoding? || !s2.valid_encoding?
|
2014-07-22 00:33:38 -04:00
|
|
|
assert_raise(ArgumentError, Encoding::CompatibilityError, desc) { s1.count(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2014-07-22 00:33:38 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.count(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
n = enccall(s1, :count, s2)
|
2013-07-20 05:10:17 -04:00
|
|
|
n0 = b(s1).count(b(s2))
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_operator(n, :<=, n0)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-23 23:07:36 -04:00
|
|
|
def crypt_supports_des_crypt?
|
|
|
|
/openbsd/ !~ RUBY_PLATFORM
|
|
|
|
end
|
|
|
|
|
2015-04-21 14:44:10 -04:00
|
|
|
# glibc 2.16 or later denies salt contained other than [0-9A-Za-z./] #7312
|
|
|
|
# we use this check to test strict and non-strict behavior separately #11045
|
|
|
|
strict_crypt = if defined? Etc::CS_GNU_LIBC_VERSION
|
2020-01-22 22:14:19 -05:00
|
|
|
begin
|
|
|
|
confstr = Etc.confstr(Etc::CS_GNU_LIBC_VERSION)
|
|
|
|
rescue Errno::EINVAL
|
|
|
|
false
|
|
|
|
else
|
|
|
|
glibcver = confstr.scan(/\d+/).map(&:to_i)
|
|
|
|
(glibcver <=> [2, 16]) >= 0
|
|
|
|
end
|
2015-04-21 14:44:10 -04:00
|
|
|
end
|
2012-11-11 03:15:53 -05:00
|
|
|
|
2015-04-21 14:44:10 -04:00
|
|
|
def test_str_crypt
|
2008-01-05 14:14:24 -05:00
|
|
|
combination(STRINGS, STRINGS) {|str, salt|
|
2015-04-21 14:44:10 -04:00
|
|
|
# skip input other than [0-9A-Za-z./] to confirm strict behavior
|
|
|
|
next unless salt.ascii_only? && /\A[0-9a-zA-Z.\/]+\z/ =~ salt
|
|
|
|
|
|
|
|
confirm_crypt_result(str, salt)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-23 23:07:36 -04:00
|
|
|
if !strict_crypt && /openbsd/ !~ RUBY_PLATFORM
|
2015-04-21 14:44:10 -04:00
|
|
|
def test_str_crypt_nonstrict
|
|
|
|
combination(STRINGS, STRINGS) {|str, salt|
|
|
|
|
# only test input other than [0-9A-Za-z./] to confirm non-strict behavior
|
|
|
|
next if salt.ascii_only? && /\A[0-9a-zA-Z.\/]+\z/ =~ salt
|
|
|
|
|
|
|
|
confirm_crypt_result(str, salt)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private def confirm_crypt_result(str, salt)
|
2019-05-23 23:07:36 -04:00
|
|
|
if crypt_supports_des_crypt?
|
|
|
|
if b(salt).length < 2
|
|
|
|
assert_raise(ArgumentError) { str.crypt(salt) }
|
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return if b(salt).length < 2
|
|
|
|
salt = "$2a$04$0WVaz0pV3jzfZ5G5tpmH#{salt}"
|
2015-04-21 14:44:10 -04:00
|
|
|
end
|
|
|
|
t = str.crypt(salt)
|
|
|
|
assert_equal(b(str).crypt(b(salt)), t, "#{encdump(str)}.crypt(#{encdump(salt)})")
|
|
|
|
assert_encoding('ASCII-8BIT', t.encoding)
|
|
|
|
end
|
|
|
|
|
2008-01-05 14:14:24 -05:00
|
|
|
def test_str_delete
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
2008-05-07 12:15:45 -04:00
|
|
|
if s1.empty?
|
|
|
|
assert_equal(s1, s1.delete(s2))
|
|
|
|
next
|
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
if !s1.valid_encoding? || !s2.valid_encoding?
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.delete(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.delete(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :delete, s2)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(t.encoding, s1.encoding)
|
|
|
|
assert_operator(t.length, :<=, s1.length)
|
|
|
|
t2 = s1.dup
|
|
|
|
t2.delete!(s2)
|
|
|
|
assert_equal(t, t2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_downcase
|
|
|
|
STRINGS.each {|s|
|
|
|
|
if !s.valid_encoding?
|
2016-06-01 21:24:52 -04:00
|
|
|
assert_raise(ArgumentError, "Offending string: #{s.inspect}, encoding: #{s.encoding}") { s.downcase }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
t = s.downcase
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(t.encoding, s.encoding)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(t, :casecmp, s)
|
2008-01-05 14:14:24 -05:00
|
|
|
t2 = s.dup
|
|
|
|
t2.downcase!
|
|
|
|
assert_equal(t, t2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_dump
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.dump
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?)
|
|
|
|
assert_predicate(t, :ascii_only?)
|
2008-01-05 14:14:24 -05:00
|
|
|
u = eval(t)
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(s), b(u))
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_each_line
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.each_line(s2) {} }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
lines = []
|
2008-01-23 04:25:17 -05:00
|
|
|
enccall(s1, :each_line, s2) {|line|
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s1.encoding, line.encoding)
|
|
|
|
lines << line
|
|
|
|
}
|
|
|
|
next if lines.size == 0
|
|
|
|
s2 = lines.join('')
|
|
|
|
assert_equal(s1.encoding, s2.encoding)
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_each_byte
|
|
|
|
STRINGS.each {|s|
|
|
|
|
bytes = []
|
|
|
|
s.each_byte {|b|
|
|
|
|
bytes << b
|
|
|
|
}
|
2013-07-20 05:10:17 -04:00
|
|
|
b(s).split(//).each_with_index {|ch, i|
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(ch.ord, bytes[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_empty?
|
|
|
|
STRINGS.each {|s|
|
|
|
|
if s.length == 0
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_empty(s)
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_empty(s)
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_hex
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.hex
|
2013-07-20 05:10:17 -04:00
|
|
|
t2 = b(s)[/\A[0-9a-fA-Fx]*/].hex
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(t2, t)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_include?
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.include?(s2) }
|
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.index(s2) }
|
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.rindex(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :include?, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
if t
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(b(s1), b(s2))
|
|
|
|
assert_send([s1, :index, s2])
|
|
|
|
assert_send([s1, :rindex, s2])
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_send([s1, :index, s2])
|
|
|
|
assert_not_send([s1, :rindex, s2], "!#{encdump(s1)}.rindex(#{encdump(s2)})")
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
if s2.empty?
|
|
|
|
assert_equal(true, t)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding? || !s2.valid_encoding?
|
|
|
|
assert_equal(false, t, "#{encdump s1}.include?(#{encdump s2})")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if t && s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
assert_match(/#{Regexp.escape(s2)}/, s1)
|
|
|
|
else
|
|
|
|
assert_no_match(/#{Regexp.escape(s2)}/, s1)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_index
|
|
|
|
combination(STRINGS, STRINGS, -2..2) {|s1, s2, pos|
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.index(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :index, s2, pos)
|
2008-01-05 14:14:24 -05:00
|
|
|
if s2.empty?
|
|
|
|
if pos < 0 && pos+s1.length < 0
|
|
|
|
assert_equal(nil, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
|
|
|
|
elsif pos < 0
|
|
|
|
assert_equal(s1.length+pos, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
|
|
|
|
elsif s1.length < pos
|
|
|
|
assert_equal(nil, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
|
|
|
|
else
|
|
|
|
assert_equal(pos, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding? || !s2.valid_encoding?
|
|
|
|
assert_equal(nil, t, "#{encdump s1}.index(#{encdump s2}, #{pos})");
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if t
|
|
|
|
re = /#{Regexp.escape(s2)}/
|
|
|
|
assert(re.match(s1, pos))
|
|
|
|
assert_equal($`.length, t, "#{encdump s1}.index(#{encdump s2}, #{pos})")
|
|
|
|
else
|
|
|
|
assert_no_match(/#{Regexp.escape(s2)}/, s1[pos..-1])
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_rindex
|
|
|
|
combination(STRINGS, STRINGS, -2..2) {|s1, s2, pos|
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.rindex(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :rindex, s2, pos)
|
2008-01-05 14:14:24 -05:00
|
|
|
if s2.empty?
|
|
|
|
if pos < 0 && pos+s1.length < 0
|
|
|
|
assert_equal(nil, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
|
|
|
|
elsif pos < 0
|
|
|
|
assert_equal(s1.length+pos, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
|
|
|
|
elsif s1.length < pos
|
|
|
|
assert_equal(s1.length, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
|
|
|
|
else
|
|
|
|
assert_equal(pos, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding? || !s2.valid_encoding?
|
|
|
|
assert_equal(nil, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if t
|
|
|
|
#puts "#{encdump s1}.rindex(#{encdump s2}, #{pos}) => #{t}"
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_send([b(s1), :index, b(s2)])
|
2008-01-05 14:14:24 -05:00
|
|
|
pos2 = pos
|
|
|
|
pos2 += s1.length if pos < 0
|
|
|
|
re = /\A(.{0,#{pos2}})#{Regexp.escape(s2)}/m
|
2008-01-23 10:34:31 -05:00
|
|
|
m = enccall(re, :match, s1)
|
|
|
|
assert(m, "#{re.inspect}.match(#{encdump(s1)})")
|
|
|
|
assert_equal(m[1].length, t, "#{encdump s1}.rindex(#{encdump s2}, #{pos})")
|
2008-01-05 14:14:24 -05:00
|
|
|
else
|
|
|
|
re = /#{Regexp.escape(s2)}/
|
|
|
|
n = re =~ s1
|
|
|
|
if n
|
|
|
|
if pos < 0
|
|
|
|
assert_operator(n, :>, s1.length+pos)
|
|
|
|
else
|
|
|
|
assert_operator(n, :>, pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_insert
|
|
|
|
combination(STRINGS, 0..2, STRINGS) {|s1, nth, s2|
|
|
|
|
t1 = s1.dup
|
|
|
|
t2 = s1.dup
|
|
|
|
begin
|
|
|
|
t1[nth, 0] = s2
|
2008-09-18 04:57:33 -04:00
|
|
|
rescue Encoding::CompatibilityError, IndexError => e1
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
begin
|
|
|
|
t2.insert(nth, s2)
|
2008-09-18 04:57:33 -04:00
|
|
|
rescue Encoding::CompatibilityError, IndexError => e2
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
assert_equal(t1, t2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t")
|
|
|
|
assert_equal(e1.class, e2.class, "begin #{encdump s1}.insert(#{nth},#{encdump s2}); rescue ArgumentError, IndexError => e; e end")
|
|
|
|
}
|
|
|
|
combination(STRINGS, -2..-1, STRINGS) {|s1, nth, s2|
|
|
|
|
next if s1.length + nth < 0
|
|
|
|
next unless s1.valid_encoding?
|
|
|
|
next unless s2.valid_encoding?
|
|
|
|
t1 = s1.dup
|
|
|
|
begin
|
|
|
|
t1.insert(nth, s2)
|
|
|
|
slen = s2.length
|
|
|
|
assert_equal(t1[nth-slen+1,slen], s2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t")
|
2011-02-15 02:08:15 -05:00
|
|
|
rescue Encoding::CompatibilityError, IndexError
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_intern
|
|
|
|
STRINGS.each {|s|
|
2013-07-20 05:10:17 -04:00
|
|
|
if /\0/ =~ b(s)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_raise(ArgumentError) { s.intern }
|
2010-03-16 12:26:04 -04:00
|
|
|
elsif s.valid_encoding?
|
2008-01-05 14:14:24 -05:00
|
|
|
sym = s.intern
|
2008-01-23 04:25:17 -05:00
|
|
|
assert_equal(s, sym.to_s, "#{encdump s}.intern.to_s")
|
2008-01-26 17:48:12 -05:00
|
|
|
assert_equal(sym, s.to_sym)
|
2010-03-16 12:26:04 -04:00
|
|
|
else
|
|
|
|
assert_raise(EncodingError) { s.intern }
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_length
|
|
|
|
STRINGS.each {|s|
|
|
|
|
assert_operator(s.length, :<=, s.bytesize)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_oct
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.oct
|
2013-07-20 05:10:17 -04:00
|
|
|
t2 = b(s)[/\A[0-9a-fA-FxX]*/].oct
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(t2, t)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_replace
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
t = s1.dup
|
|
|
|
t.replace s2
|
|
|
|
assert_equal(s2, t)
|
|
|
|
assert_equal(s2.encoding, t.encoding)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_reverse
|
|
|
|
STRINGS.each {|s|
|
|
|
|
t = s.reverse
|
|
|
|
assert_equal(s.bytesize, t.bytesize)
|
|
|
|
if !s.valid_encoding?
|
|
|
|
assert_operator(t.length, :<=, s.length)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal(s, t.reverse)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_scan
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
2014-03-27 05:08:51 -04:00
|
|
|
desc = proc {"#{s1.dump}.scan(#{s2.dump})"}
|
2008-01-05 14:14:24 -05:00
|
|
|
if !s2.valid_encoding?
|
2014-03-27 05:08:51 -04:00
|
|
|
assert_raise(RegexpError, desc) { s1.scan(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-12-12 23:45:56 -05:00
|
|
|
if s1.valid_encoding?
|
2014-03-27 05:08:51 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.scan(s2) }
|
2008-12-12 23:45:56 -05:00
|
|
|
else
|
2014-03-27 05:08:51 -04:00
|
|
|
assert_raise_with_message(ArgumentError, /invalid byte sequence/, desc) { s1.scan(s2) }
|
2008-12-12 23:45:56 -05:00
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding?
|
2014-03-27 05:08:51 -04:00
|
|
|
assert_raise(ArgumentError, desc) { s1.scan(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
r = enccall(s1, :scan, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
r.each {|t|
|
2014-03-27 05:08:51 -04:00
|
|
|
assert_equal(s2, t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_slice
|
|
|
|
each_slice_call {|obj, *args|
|
|
|
|
assert_same_result(lambda { obj[*args] }, lambda { obj.slice(*args) })
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_slice!
|
|
|
|
each_slice_call {|s, *args|
|
2008-07-05 08:33:55 -04:00
|
|
|
desc_slice = "#{encdump s}.slice#{encdumpargs args}"
|
|
|
|
desc_slice_bang = "#{encdump s}.slice!#{encdumpargs args}"
|
2008-01-05 14:14:24 -05:00
|
|
|
t = s.dup
|
|
|
|
begin
|
|
|
|
r = t.slice!(*args)
|
|
|
|
rescue
|
|
|
|
e = $!
|
|
|
|
end
|
|
|
|
if e
|
2008-07-05 08:33:55 -04:00
|
|
|
assert_raise(e.class, desc_slice) { s.slice(*args) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !r
|
2008-07-05 08:33:55 -04:00
|
|
|
assert_nil(s.slice(*args), desc_slice)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-07-05 08:33:55 -04:00
|
|
|
assert_equal(s.slice(*args), r, desc_slice_bang)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s.bytesize, r.bytesize + t.bytesize)
|
|
|
|
if args.length == 1 && String === args[0]
|
|
|
|
assert_equal(args[0].encoding, r.encoding,
|
|
|
|
"#{encdump s}.slice!#{encdumpargs args}.encoding")
|
|
|
|
else
|
|
|
|
assert_equal(s.encoding, r.encoding,
|
|
|
|
"#{encdump s}.slice!#{encdumpargs args}.encoding")
|
|
|
|
end
|
|
|
|
if [s, *args].all? {|o| !(String === o) || o.valid_encoding? }
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(r, :valid_encoding?)
|
|
|
|
assert_predicate(t, :valid_encoding?)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s.length, r.length + t.length)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_split
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if !s2.valid_encoding?
|
* string.c ():
* string.c ():
* string.c (single_byte_optimizable): make function inline. based
on a patch from Michael Selig <michael.selig at fs.com.au> in
[ruby-core:18532].
* string.c (str_modify_keep_cr): new function act as
rb_str_modify(), but don't clear coderange
* string.c (rb_str_casecmp): specialized for single byte strings.
* string.c (rb_str_splice): preserve coderange.
* string.c (rb_str_slice_bang, rb_str_reverse_bang,
rb_str_upcase_bang, rb_str_downcase_bang, tr_trans,
rb_str_capitalize_bang, rb_str_swapcase_bang,
rb_str_delete_bang, rb_str_chop_bang, rb_str_chomp_bang,
rb_str_lstrip_bang, rb_str_rstrip_bang): ditto.
* string.c (rb_str_clear): preset coderange.
* string.c (rb_str_split_m): specialized for splitting with a
string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-15 20:47:20 -04:00
|
|
|
assert_raise(ArgumentError, RegexpError) { s1.split(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.split(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding?
|
|
|
|
assert_raise(ArgumentError) { s1.split(s2) }
|
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :split, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
t.each {|r|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(b(s1), b(r))
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s1.encoding, r.encoding)
|
|
|
|
}
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(b(s1), t.map {|u| b(u) }.join(b(s2)))
|
2008-01-05 14:14:24 -05:00
|
|
|
if s1.valid_encoding? && s2.valid_encoding?
|
|
|
|
t.each {|r|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(r, :valid_encoding?)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_squeeze
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if !s1.valid_encoding? || !s2.valid_encoding?
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(ArgumentError, Encoding::CompatibilityError, "#{encdump s1}.squeeze(#{encdump s2})") { s1.squeeze(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError) { s1.squeeze(s2) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :squeeze, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_operator(t.length, :<=, s1.length)
|
|
|
|
t2 = s1.dup
|
|
|
|
t2.squeeze!(s2)
|
|
|
|
assert_equal(t, t2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_strip
|
|
|
|
STRINGS.each {|s|
|
|
|
|
if !s.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, "#{encdump s}.strip") { s.strip }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
t = s.strip
|
|
|
|
l = s.lstrip
|
|
|
|
r = s.rstrip
|
|
|
|
assert_operator(l.length, :<=, s.length)
|
|
|
|
assert_operator(r.length, :<=, s.length)
|
|
|
|
assert_operator(t.length, :<=, l.length)
|
|
|
|
assert_operator(t.length, :<=, r.length)
|
|
|
|
t2 = s.dup
|
|
|
|
t2.strip!
|
|
|
|
assert_equal(t, t2)
|
|
|
|
l2 = s.dup
|
|
|
|
l2.lstrip!
|
|
|
|
assert_equal(l, l2)
|
|
|
|
r2 = s.dup
|
|
|
|
r2.rstrip!
|
|
|
|
assert_equal(r, r2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_sum
|
|
|
|
STRINGS.each {|s|
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(b(s).sum, s.sum)
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_swapcase
|
|
|
|
STRINGS.each {|s|
|
|
|
|
if !s.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, "#{encdump s}.swapcase") { s.swapcase }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
t1 = s.swapcase
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t1, :valid_encoding?) if s.valid_encoding?
|
|
|
|
assert_operator(t1, :casecmp, s)
|
2008-01-05 14:14:24 -05:00
|
|
|
t2 = s.dup
|
|
|
|
t2.swapcase!
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
t3 = t1.swapcase
|
|
|
|
assert_equal(s, t3);
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_str_to_f
|
|
|
|
STRINGS.each {|s|
|
|
|
|
assert_nothing_raised { s.to_f }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_to_i
|
|
|
|
STRINGS.each {|s|
|
|
|
|
assert_nothing_raised { s.to_i }
|
|
|
|
2.upto(36) {|radix|
|
|
|
|
assert_nothing_raised { s.to_i(radix) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_to_s
|
|
|
|
STRINGS.each {|s|
|
|
|
|
assert_same(s, s.to_s)
|
|
|
|
assert_same(s, s.to_str)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tr
|
|
|
|
combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
|
|
|
|
desc = "#{encdump s1}.tr(#{encdump s2}, #{encdump s3})"
|
|
|
|
if s1.empty?
|
|
|
|
assert_equal(s1, s1.tr(s2, s3), desc)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !str_enc_compatible?(s1, s2, s3)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.tr(s2, s3) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, desc) { s1.tr(s2, s3) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if s2.empty?
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :tr, s2, s3)
|
|
|
|
assert_equal(s1, t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s2.valid_encoding? || !s3.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, desc) { s1.tr(s2, s3) }
|
|
|
|
next
|
|
|
|
end
|
2008-01-23 09:37:20 -05:00
|
|
|
t = enccall(s1, :tr, s2, s3)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_operator(s1.length, :>=, t.length, desc)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-05-20 09:32:16 -04:00
|
|
|
def test_tr_sjis
|
|
|
|
expected = "\x83}\x83~\x83\x80\x83\x81\x83\x82".force_encoding(Encoding::SJIS)
|
|
|
|
source = "\xCF\xD0\xD1\xD2\xD3".force_encoding(Encoding::SJIS)
|
|
|
|
from = "\xCF-\xD3".force_encoding(Encoding::SJIS)
|
|
|
|
to = "\x83}-\x83\x82".force_encoding(Encoding::SJIS)
|
|
|
|
assert_equal(expected, source.tr(from, to))
|
|
|
|
|
|
|
|
expected = "\x84}\x84~\x84\x80\x84\x81\x84\x82".force_encoding(Encoding::SJIS)
|
|
|
|
source = "\x84M\x84N\x84O\x84P\x84Q".force_encoding(Encoding::SJIS)
|
|
|
|
from = "\x84@-\x84`".force_encoding(Encoding::SJIS)
|
|
|
|
to = "\x84p-\x84\x91".force_encoding(Encoding::SJIS)
|
|
|
|
assert_equal(expected, source.tr(from, to))
|
|
|
|
end
|
|
|
|
|
2008-01-05 14:14:24 -05:00
|
|
|
def test_tr_s
|
|
|
|
combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
|
|
|
|
desc = "#{encdump s1}.tr_s(#{encdump s2}, #{encdump s3})"
|
|
|
|
if s1.empty?
|
|
|
|
assert_equal(s1, s1.tr_s(s2, s3), desc)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding?
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(ArgumentError, Encoding::CompatibilityError, desc) { s1.tr_s(s2, s3) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !str_enc_compatible?(s1, s2, s3)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.tr(s2, s3) }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if s2.empty?
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :tr_s, s2, s3)
|
|
|
|
assert_equal(s1, t, desc)
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s2.valid_encoding? || !s3.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, desc) { s1.tr_s(s2, s3) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2008-01-23 04:25:17 -05:00
|
|
|
t = enccall(s1, :tr_s, s2, s3)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_operator(s1.length, :>=, t.length, desc)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_upcase
|
|
|
|
STRINGS.each {|s|
|
|
|
|
desc = "#{encdump s}.upcase"
|
|
|
|
if !s.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, desc) { s.upcase }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
t1 = s.upcase
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t1, :valid_encoding?)
|
|
|
|
assert_operator(t1, :casecmp, s)
|
2008-01-05 14:14:24 -05:00
|
|
|
t2 = s.dup
|
|
|
|
t2.upcase!
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_succ
|
|
|
|
STRINGS.each {|s0|
|
|
|
|
next if s0.empty?
|
|
|
|
s = s0.dup
|
2009-11-04 19:57:57 -05:00
|
|
|
n = 300
|
2008-01-05 14:14:24 -05:00
|
|
|
h = {}
|
|
|
|
n.times {|i|
|
|
|
|
if h[s]
|
2008-01-27 03:21:24 -05:00
|
|
|
assert(false, "#{encdump s} cycle with succ #{i-h[s]} times")
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
h[s] = i
|
2008-01-27 03:21:24 -05:00
|
|
|
assert_operator(s.length, :<=, s0.length + Math.log2(i+1) + 1, "#{encdump s0} succ #{i} times => #{encdump s}")
|
|
|
|
#puts encdump(s)
|
|
|
|
t = s.succ
|
|
|
|
if s.valid_encoding?
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(t, :valid_encoding?, "#{encdump s}.succ.valid_encoding?")
|
2008-01-27 03:21:24 -05:00
|
|
|
end
|
|
|
|
s = t
|
2008-01-05 14:14:24 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 23:14:09 -04:00
|
|
|
|
|
|
|
Encoding.list.each do |enc|
|
|
|
|
next if enc.dummy?
|
|
|
|
{"A"=>"B", "A1"=>"A2", "A9"=>"B0", "9"=>"10", "Z"=>"AA"}.each do |orig, expected|
|
|
|
|
s = orig.encode(enc)
|
|
|
|
assert_strenc(expected.encode(enc), enc, s.succ, proc {"#{orig.dump}.encode(#{enc}).succ"})
|
|
|
|
end
|
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|
|
|
|
|
2013-07-20 05:10:12 -04:00
|
|
|
def test_str_succ2
|
2013-07-20 05:10:17 -04:00
|
|
|
assert_equal(a("\x01\x00"), a("\x7f").succ)
|
|
|
|
assert_equal(b("\x01\x00"), b("\xff").succ)
|
2013-07-20 05:10:12 -04:00
|
|
|
end
|
|
|
|
|
2008-01-05 14:14:24 -05:00
|
|
|
def test_str_hash
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
if s1.eql?(s2)
|
|
|
|
assert_equal(s1.hash, s2.hash, "#{encdump s1}.hash == #{encdump s2}.dump")
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_marshal
|
|
|
|
STRINGS.each {|s|
|
|
|
|
m = Marshal.dump(s)
|
|
|
|
t = Marshal.load(m)
|
|
|
|
assert_equal(s, t)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_sub
|
|
|
|
combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
|
|
|
|
if !s2.valid_encoding?
|
|
|
|
assert_raise(RegexpError) { Regexp.new(Regexp.escape(s2)) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
r2 = Regexp.new(Regexp.escape(s2))
|
|
|
|
[
|
|
|
|
[
|
|
|
|
"#{encdump s1}.sub(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { s1.sub(r2, s3) },
|
|
|
|
false
|
2008-01-05 14:14:24 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"#{encdump s1}.sub(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { s1.sub(r2) { s3 } },
|
|
|
|
false
|
2008-01-05 14:14:24 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"#{encdump s1}.gsub(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { s1.gsub(r2, s3) },
|
|
|
|
true
|
2008-01-05 14:14:24 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"#{encdump s1}.gsub(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { s1.gsub(r2) { s3 } },
|
|
|
|
true
|
2008-01-05 14:14:24 -05:00
|
|
|
]
|
2008-10-02 07:46:40 -04:00
|
|
|
].each {|desc, doit, g|
|
2008-01-05 14:14:24 -05:00
|
|
|
if !s1.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, desc) { doit.call }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !str_enc_compatible?(s1, s2)
|
2008-12-12 23:45:56 -05:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { doit.call }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
if !enccall(s1, :include?, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal(s1, doit.call)
|
|
|
|
next
|
|
|
|
end
|
2008-10-02 07:46:40 -04:00
|
|
|
if !str_enc_compatible?(g ? s1.gsub(r2, '') : s1.sub(r2, ''), s3)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { doit.call }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
t = nil
|
|
|
|
assert_nothing_raised(desc) {
|
|
|
|
t = doit.call
|
|
|
|
}
|
|
|
|
if s2 == s3
|
|
|
|
assert_equal(s1, t, desc)
|
|
|
|
else
|
|
|
|
assert_not_equal(s1, t, desc)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_sub!
|
|
|
|
combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
|
|
|
|
if !s2.valid_encoding?
|
|
|
|
assert_raise(RegexpError) { Regexp.new(Regexp.escape(s2)) }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
r2 = Regexp.new(Regexp.escape(s2))
|
|
|
|
[
|
|
|
|
[
|
|
|
|
"t=#{encdump s1}.dup;t.sub!(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { t=s1.dup; [t, t.sub!(r2, s3)] },
|
|
|
|
false
|
2008-01-05 14:14:24 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"t=#{encdump s1}.dup;t.sub!(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { t=s1.dup; [t, t.sub!(r2) { s3 }] },
|
|
|
|
false
|
2008-01-05 14:14:24 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"t=#{encdump s1}.dup;t.gsub!(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { t=s1.dup; [t, t.gsub!(r2, s3)] },
|
|
|
|
true
|
2008-01-05 14:14:24 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"t=#{encdump s1}.dup;t.gsub!(Regexp.new(#{encdump s2}), #{encdump s3})",
|
2008-10-02 07:46:40 -04:00
|
|
|
lambda { t=s1.dup; [t, t.gsub!(r2) { s3 }] },
|
|
|
|
true
|
2008-01-05 14:14:24 -05:00
|
|
|
]
|
2008-10-02 07:46:40 -04:00
|
|
|
].each {|desc, doit, g|
|
2008-01-05 14:14:24 -05:00
|
|
|
if !s1.valid_encoding?
|
|
|
|
assert_raise(ArgumentError, desc) { doit.call }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !str_enc_compatible?(s1, s2)
|
2008-12-12 23:45:56 -05:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { doit.call }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-23 04:25:17 -05:00
|
|
|
if !enccall(s1, :include?, s2)
|
2008-01-05 14:14:24 -05:00
|
|
|
assert_equal([s1, nil], doit.call)
|
|
|
|
next
|
|
|
|
end
|
2008-10-02 07:46:40 -04:00
|
|
|
if !str_enc_compatible?(g ? s1.gsub(r2, '') : s1.sub(r2, ''), s3)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { doit.call }
|
2008-01-05 14:14:24 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
t = ret = nil
|
|
|
|
assert_nothing_raised(desc) {
|
|
|
|
t, ret = doit.call
|
|
|
|
}
|
|
|
|
assert(ret)
|
|
|
|
if s2 == s3
|
|
|
|
assert_equal(s1, t, desc)
|
|
|
|
else
|
|
|
|
assert_not_equal(s1, t, desc)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-01-26 17:48:12 -05:00
|
|
|
def test_str_bytes
|
|
|
|
STRINGS.each {|s1|
|
|
|
|
ary = []
|
|
|
|
s1.bytes.each {|b|
|
|
|
|
ary << b
|
|
|
|
}
|
|
|
|
assert_equal(s1.unpack("C*"), ary)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_bytesize
|
|
|
|
STRINGS.each {|s1|
|
|
|
|
assert_equal(s1.unpack("C*").length, s1.bytesize)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_chars
|
|
|
|
STRINGS.each {|s1|
|
|
|
|
ary = []
|
|
|
|
s1.chars.each {|c|
|
|
|
|
ary << c
|
|
|
|
}
|
|
|
|
expected = []
|
|
|
|
s1.length.times {|i|
|
|
|
|
expected << s1[i]
|
|
|
|
}
|
|
|
|
assert_equal(expected, ary)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_chr
|
|
|
|
STRINGS.each {|s1|
|
|
|
|
if s1.empty?
|
|
|
|
assert_equal("", s1.chr)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal(s1[0], s1.chr)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_end_with?
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
desc = "#{encdump s1}.end_with?(#{encdump s2})"
|
|
|
|
if !str_enc_compatible?(s1, s2)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.end_with?(s2) }
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if s1.length < s2.length
|
2008-01-26 21:38:10 -05:00
|
|
|
assert_equal(false, enccall(s1, :end_with?, s2), desc)
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if s1[s1.length-s2.length, s2.length] == s2
|
2008-01-26 21:38:10 -05:00
|
|
|
assert_equal(true, enccall(s1, :end_with?, s2), desc)
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-26 21:38:10 -05:00
|
|
|
assert_equal(false, enccall(s1, :end_with?, s2), desc)
|
2008-01-26 17:48:12 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_start_with?
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
desc = "#{encdump s1}.start_with?(#{encdump s2})"
|
|
|
|
if !str_enc_compatible?(s1, s2)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.start_with?(s2) }
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
2015-12-14 01:40:55 -05:00
|
|
|
s1 = s1.b
|
|
|
|
s2 = s2.b
|
2008-01-26 17:48:12 -05:00
|
|
|
if s1.length < s2.length
|
2008-01-26 21:38:10 -05:00
|
|
|
assert_equal(false, enccall(s1, :start_with?, s2), desc)
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
if s1[0, s2.length] == s2
|
2008-01-26 21:38:10 -05:00
|
|
|
assert_equal(true, enccall(s1, :start_with?, s2), desc)
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-26 21:38:10 -05:00
|
|
|
assert_equal(false, enccall(s1, :start_with?, s2), desc)
|
2008-01-26 17:48:12 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_ord
|
|
|
|
STRINGS.each {|s1|
|
|
|
|
if s1.empty?
|
|
|
|
assert_raise(ArgumentError) { s1.ord }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if !s1.valid_encoding?
|
|
|
|
assert_raise(ArgumentError) { s1.ord }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal(s1[0].ord, s1.ord)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_partition
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
desc = "#{encdump s1}.partition(#{encdump s2})"
|
|
|
|
if !str_enc_compatible?(s1, s2)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.partition(s2) }
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-26 21:38:10 -05:00
|
|
|
i = enccall(s1, :index, s2)
|
2008-01-26 17:48:12 -05:00
|
|
|
if !i
|
|
|
|
assert_equal([s1, "", ""], s1.partition(s2), desc)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal([s1[0,i], s2, s1[(i+s2.length)..-1]], s1.partition(s2), desc)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str_rpartition
|
|
|
|
combination(STRINGS, STRINGS) {|s1, s2|
|
|
|
|
desc = "#{encdump s1}.rpartition(#{encdump s2})"
|
|
|
|
if !str_enc_compatible?(s1, s2)
|
2008-09-18 04:57:33 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, desc) { s1.rpartition(s2) }
|
2008-01-26 17:48:12 -05:00
|
|
|
next
|
|
|
|
end
|
2008-01-26 21:38:10 -05:00
|
|
|
i = enccall(s1, :rindex, s2)
|
2008-01-26 17:48:12 -05:00
|
|
|
if !i
|
|
|
|
assert_equal(["", "", s1], s1.rpartition(s2), desc)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal([s1[0,i], s2, s1[(i+s2.length)..-1]], s1.rpartition(s2), desc)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-10-04 02:18:46 -04:00
|
|
|
def test_bug11486
|
|
|
|
bug11486 = '[Bug #11486]'
|
2015-10-04 03:25:56 -04:00
|
|
|
assert_nil ("\u3042"*19+"\r"*19+"\u3042"*20+"\r"*20).encode(Encoding::EUC_JP).gsub!(/xxx/i, ""), bug11486
|
|
|
|
assert_match Regexp.new("ABC\uff41".encode(Encoding::EUC_JP), Regexp::IGNORECASE), "abc\uFF21".encode(Encoding::EUC_JP), bug11486
|
2015-10-04 02:18:46 -04:00
|
|
|
end
|
2008-01-05 14:14:24 -05:00
|
|
|
end
|