2017-07-09 08:06:36 -04:00
|
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
|
require "abstract_unit"
|
|
|
|
|
require "multibyte_test_helpers"
|
|
|
|
|
require "active_support/core_ext/string/multibyte"
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
|
class MultibyteCharsTest < ActiveSupport::TestCase
|
2008-09-21 11:37:38 -04:00
|
|
|
|
include MultibyteTestHelpers
|
2006-10-03 19:45:32 -04:00
|
|
|
|
|
|
|
|
|
def setup
|
2008-09-21 11:22:26 -04:00
|
|
|
|
@proxy_class = ActiveSupport::Multibyte::Chars
|
2014-08-15 21:19:42 -04:00
|
|
|
|
@chars = @proxy_class.new UNICODE_STRING.dup
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
|
|
|
|
def test_wraps_the_original_string
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.to_s
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.wrapped_string
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
|
|
|
|
def test_should_allow_method_calls_to_string
|
2016-08-06 12:03:25 -04:00
|
|
|
|
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing; "result"; end }
|
2014-08-15 18:51:13 -04:00
|
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
|
assert_nothing_raised do
|
2008-09-21 11:29:22 -04:00
|
|
|
|
@chars.__method_for_multibyte_testing
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
|
assert_raise NoMethodError do
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars.__unknown_method
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
|
|
|
|
def test_forwarded_method_calls_should_return_new_chars_instance
|
2016-08-06 12:03:25 -04:00
|
|
|
|
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing; "result"; end }
|
2014-08-15 18:51:13 -04:00
|
|
|
|
|
2010-05-19 15:14:51 -04:00
|
|
|
|
assert_kind_of @proxy_class, @chars.__method_for_multibyte_testing
|
2008-09-21 11:29:22 -04:00
|
|
|
|
assert_not_equal @chars.object_id, @chars.__method_for_multibyte_testing.object_id
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2012-01-05 09:51:10 -05:00
|
|
|
|
def test_forwarded_bang_method_calls_should_return_the_original_chars_instance_when_result_is_not_nil
|
2016-08-06 12:03:25 -04:00
|
|
|
|
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing!; "result"; end }
|
2014-08-15 18:51:13 -04:00
|
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
|
assert_kind_of @proxy_class, @chars.__method_for_multibyte_testing!
|
2008-09-21 11:29:22 -04:00
|
|
|
|
assert_equal @chars.object_id, @chars.__method_for_multibyte_testing!.object_id
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-01-05 09:51:10 -05:00
|
|
|
|
def test_forwarded_bang_method_calls_should_return_nil_when_result_is_nil
|
2014-08-15 18:51:13 -04:00
|
|
|
|
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing_that_returns_nil!; end }
|
|
|
|
|
|
2012-01-05 09:51:10 -05:00
|
|
|
|
assert_nil @chars.__method_for_multibyte_testing_that_returns_nil!
|
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_methods_are_forwarded_to_wrapped_string_for_byte_strings
|
2012-11-28 16:57:07 -05:00
|
|
|
|
assert_equal BYTE_STRING.length, BYTE_STRING.mb_chars.length
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-02-05 20:00:18 -05:00
|
|
|
|
def test_forwarded_method_with_non_string_result_should_be_returned_verbatim
|
2018-05-17 04:32:27 -04:00
|
|
|
|
str = +""
|
2014-08-15 18:51:13 -04:00
|
|
|
|
str.singleton_class.class_eval { def __method_for_multibyte_testing_with_integer_result; 1; end }
|
|
|
|
|
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing_with_integer_result; 1; end }
|
|
|
|
|
|
|
|
|
|
assert_equal str.__method_for_multibyte_testing_with_integer_result, @chars.__method_for_multibyte_testing_with_integer_result
|
2008-09-21 11:29:22 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_should_concatenate
|
2018-05-17 04:32:27 -04:00
|
|
|
|
mb_a = (+"a").mb_chars
|
|
|
|
|
mb_b = (+"b").mb_chars
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "ab", mb_a + "b"
|
|
|
|
|
assert_equal "ab", "a" + mb_b
|
|
|
|
|
assert_equal "ab", mb_a + mb_b
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "ab", mb_a << "b"
|
2018-05-17 04:32:27 -04:00
|
|
|
|
assert_equal "ab", (+"a") << mb_b
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "abb", mb_a << mb_b
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:22:26 -04:00
|
|
|
|
def test_consumes_utf8_strings
|
2018-10-15 02:29:56 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert @proxy_class.consumes?(UNICODE_STRING)
|
|
|
|
|
assert @proxy_class.consumes?(ASCII_STRING)
|
|
|
|
|
assert_not @proxy_class.consumes?(BYTE_STRING)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_consumes_is_deprecated
|
|
|
|
|
assert_deprecated { @proxy_class.consumes?(UNICODE_STRING) }
|
2008-09-21 11:22:26 -04:00
|
|
|
|
end
|
|
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
|
def test_concatenation_should_return_a_proxy_class_instance
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal ActiveSupport::Multibyte.proxy_class, ("a".mb_chars + "b").class
|
2018-05-17 04:32:27 -04:00
|
|
|
|
assert_equal ActiveSupport::Multibyte.proxy_class, ((+"a").mb_chars << "b").class
|
2010-05-10 09:46:37 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
|
def test_ascii_strings_are_treated_at_utf8_strings
|
|
|
|
|
assert_equal ActiveSupport::Multibyte.proxy_class, ASCII_STRING.mb_chars.class
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
|
def test_concatenate_should_return_proxy_instance
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert(("a".mb_chars + "b").kind_of?(@proxy_class))
|
|
|
|
|
assert(("a".mb_chars + "b".mb_chars).kind_of?(@proxy_class))
|
2018-05-17 04:32:27 -04:00
|
|
|
|
assert(((+"a").mb_chars << "b").kind_of?(@proxy_class))
|
|
|
|
|
assert(((+"a").mb_chars << "b".mb_chars).kind_of?(@proxy_class))
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2010-05-10 09:46:37 -04:00
|
|
|
|
|
2012-01-31 17:01:00 -05:00
|
|
|
|
def test_should_return_string_as_json
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.as_json
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
2007-12-21 06:21:20 -05:00
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
|
class MultibyteCharsUTF8BehaviourTest < ActiveSupport::TestCase
|
2008-09-21 11:37:38 -04:00
|
|
|
|
include MultibyteTestHelpers
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
|
|
|
|
def setup
|
|
|
|
|
@chars = UNICODE_STRING.dup.mb_chars
|
2011-12-20 12:22:21 -05:00
|
|
|
|
# Ruby 1.9 only supports basic whitespace
|
|
|
|
|
@whitespace = "\n\t "
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
|
def test_split_should_return_an_array_of_chars_instances
|
|
|
|
|
@chars.split(//).each do |character|
|
|
|
|
|
assert_kind_of ActiveSupport::Multibyte.proxy_class, character
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2010-05-10 09:46:37 -04:00
|
|
|
|
end
|
2007-12-21 06:21:20 -05:00
|
|
|
|
|
2012-01-06 10:06:26 -05:00
|
|
|
|
%w{capitalize downcase lstrip reverse rstrip swapcase upcase}.each do |method|
|
2012-07-17 12:16:55 -04:00
|
|
|
|
class_eval(<<-EOTESTS, __FILE__, __LINE__ + 1)
|
2012-01-05 09:51:10 -05:00
|
|
|
|
def test_#{method}_bang_should_return_self_when_modifying_wrapped_string
|
2017-06-19 05:03:10 -04:00
|
|
|
|
chars = ' él piDió Un bUen café '.dup
|
2012-01-05 09:51:10 -05:00
|
|
|
|
assert_equal chars.object_id, chars.send("#{method}!").object_id
|
2010-07-08 16:42:14 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_#{method}_bang_should_change_wrapped_string
|
2017-06-19 05:03:10 -04:00
|
|
|
|
original = ' él piDió Un bUen café '.dup
|
2010-07-08 16:42:14 -04:00
|
|
|
|
proxy = chars(original.dup)
|
|
|
|
|
proxy.send("#{method}!")
|
|
|
|
|
assert_not_equal original, proxy.to_s
|
|
|
|
|
end
|
|
|
|
|
EOTESTS
|
2010-05-10 09:46:37 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2010-07-08 16:42:14 -04:00
|
|
|
|
def test_tidy_bytes_bang_should_return_self
|
|
|
|
|
assert_equal @chars.object_id, @chars.tidy_bytes!.object_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_tidy_bytes_bang_should_change_wrapped_string
|
2018-05-17 04:32:27 -04:00
|
|
|
|
original = +" Un bUen café \x92"
|
2010-07-08 16:42:14 -04:00
|
|
|
|
proxy = chars(original.dup)
|
|
|
|
|
proxy.tidy_bytes!
|
|
|
|
|
assert_not_equal original, proxy.to_s
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2011-12-20 12:31:13 -05:00
|
|
|
|
def test_unicode_string_should_have_utf8_encoding
|
|
|
|
|
assert_equal Encoding::UTF_8, UNICODE_STRING.encoding
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:25:36 -04:00
|
|
|
|
def test_identity
|
|
|
|
|
assert_equal @chars, @chars
|
|
|
|
|
assert @chars.eql?(@chars)
|
2018-04-17 18:21:34 -04:00
|
|
|
|
assert_not @chars.eql?(UNICODE_STRING)
|
2008-09-21 11:25:36 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_string_methods_are_chainable
|
2018-05-17 04:32:27 -04:00
|
|
|
|
assert chars(+"").insert(0, "").kind_of?(ActiveSupport::Multibyte.proxy_class)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert chars("").rjust(1).kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").ljust(1).kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").center(1).kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").rstrip.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").lstrip.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").strip.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").reverse.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars(" ").slice(0).kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").limit(0).kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").upcase.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").downcase.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").capitalize.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
2018-10-12 11:40:29 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert chars("").normalize.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
end
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert chars("").decompose.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").compose.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").tidy_bytes.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
|
|
|
|
assert chars("").swapcase.kind_of?(ActiveSupport::Multibyte.proxy_class)
|
2008-09-21 11:25:36 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_should_be_equal_to_the_wrapped_string
|
|
|
|
|
assert_equal UNICODE_STRING, @chars
|
|
|
|
|
assert_equal @chars, UNICODE_STRING
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_should_not_be_equal_to_an_other_string
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_not_equal @chars, "other"
|
|
|
|
|
assert_not_equal "other", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:25:36 -04:00
|
|
|
|
def test_sortability
|
2014-10-27 12:28:53 -04:00
|
|
|
|
words = %w(builder armor zebra).sort_by(&:mb_chars)
|
2008-09-21 11:25:36 -04:00
|
|
|
|
assert_equal %w(armor builder zebra), words
|
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_should_return_character_offset_for_regexp_matches
|
|
|
|
|
assert_nil(@chars =~ /wrong/u)
|
|
|
|
|
assert_equal 0, (@chars =~ /こ/u)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
assert_equal 0, (@chars =~ /こに/u)
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal 1, (@chars =~ /に/u)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
assert_equal 2, (@chars =~ /ち/u)
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal 3, (@chars =~ /わ/u)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_should_use_character_offsets_for_insert_offsets
|
2018-05-17 04:32:27 -04:00
|
|
|
|
assert_equal "", (+"").mb_chars.insert(0, "")
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こわにちわ", @chars.insert(1, "わ")
|
|
|
|
|
assert_equal "こわわわにちわ", @chars.insert(2, "わわ")
|
|
|
|
|
assert_equal "わこわわわにちわ", @chars.insert(0, "わ")
|
|
|
|
|
assert_equal "わこわわわにちわ", @chars.wrapped_string
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_insert_should_be_destructive
|
2016-08-06 12:03:25 -04:00
|
|
|
|
@chars.insert(1, "わ")
|
|
|
|
|
assert_equal "こわにちわ", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_insert_throws_index_error
|
2016-08-16 03:30:11 -04:00
|
|
|
|
assert_raise(IndexError) { @chars.insert(-12, "わ") }
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_raise(IndexError) { @chars.insert(12, "わ") }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_should_know_if_one_includes_the_other
|
2016-09-16 12:44:05 -04:00
|
|
|
|
assert_includes @chars, ""
|
|
|
|
|
assert_includes @chars, "ち"
|
|
|
|
|
assert_includes @chars, "わ"
|
|
|
|
|
assert_not_includes @chars, "こちわ"
|
|
|
|
|
assert_not_includes @chars, "a"
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2009-11-09 00:55:43 -05:00
|
|
|
|
def test_include_raises_when_nil_is_passed
|
2013-12-19 06:03:39 -05:00
|
|
|
|
assert_raises TypeError, NoMethodError, "Expected chars.include?(nil) to raise TypeError or NoMethodError" do
|
|
|
|
|
@chars.include?(nil)
|
|
|
|
|
end
|
2006-10-03 19:45:32 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
|
|
|
|
def test_index_should_return_character_offset
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_nil @chars.index("u")
|
|
|
|
|
assert_equal 0, @chars.index("こに")
|
|
|
|
|
assert_equal 2, @chars.index("ち")
|
|
|
|
|
assert_equal 2, @chars.index("ち", -2)
|
2016-12-24 12:29:52 -05:00
|
|
|
|
assert_nil @chars.index("ち", -1)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal 3, @chars.index("わ")
|
|
|
|
|
assert_equal 5, "ééxééx".mb_chars.index("x", 4)
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2009-08-09 21:57:25 -04:00
|
|
|
|
def test_rindex_should_return_character_offset
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_nil @chars.rindex("u")
|
|
|
|
|
assert_equal 1, @chars.rindex("に")
|
|
|
|
|
assert_equal 2, @chars.rindex("ち", -2)
|
|
|
|
|
assert_nil @chars.rindex("ち", -3)
|
|
|
|
|
assert_equal 6, "Café périferôl".mb_chars.rindex("é")
|
|
|
|
|
assert_equal 13, "Café périferôl".mb_chars.rindex(/\w/u)
|
2009-08-09 21:57:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_indexed_insert_should_take_character_offsets
|
2016-08-06 12:03:25 -04:00
|
|
|
|
@chars[2] = "a"
|
|
|
|
|
assert_equal "こにaわ", @chars
|
|
|
|
|
@chars[2] = "ηη"
|
|
|
|
|
assert_equal "こにηηわ", @chars
|
|
|
|
|
@chars[3, 2] = "λλλ"
|
|
|
|
|
assert_equal "こにηλλλ", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars[1, 0] = "λ"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こλにηλλλ", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars[4..6] = "ηη"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こλにηηη", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars[/ηη/] = "λλλ"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こλにλλλη", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars[/(λλ)(.)/, 2] = "α"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こλにλλαη", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars["α"] = "¢"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こλにλλ¢η", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
@chars["λλ"] = "ααα"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こλにααα¢η", @chars
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_indexed_insert_should_raise_on_index_overflow
|
|
|
|
|
before = @chars.to_s
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_raise(IndexError) { @chars[10] = "a" }
|
|
|
|
|
assert_raise(IndexError) { @chars[10, 4] = "a" }
|
|
|
|
|
assert_raise(IndexError) { @chars[/ii/] = "a" }
|
|
|
|
|
assert_raise(IndexError) { @chars[/()/, 10] = "a" }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal before, @chars
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_indexed_insert_should_raise_on_range_overflow
|
|
|
|
|
before = @chars.to_s
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_raise(RangeError) { @chars[10..12] = "a" }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal before, @chars
|
2007-05-28 19:33:54 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
|
|
|
|
def test_rjust_should_raise_argument_errors_on_bad_arguments
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_raise(ArgumentError) { @chars.rjust(10, "") }
|
2009-03-08 16:11:58 -04:00
|
|
|
|
assert_raise(ArgumentError) { @chars.rjust }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_rjust_should_count_characters_instead_of_bytes
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.rjust(-3)
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.rjust(0)
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.rjust(4)
|
|
|
|
|
assert_equal " #{UNICODE_STRING}", @chars.rjust(5)
|
|
|
|
|
assert_equal " #{UNICODE_STRING}", @chars.rjust(7)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "---#{UNICODE_STRING}", @chars.rjust(7, "-")
|
|
|
|
|
assert_equal "ααα#{UNICODE_STRING}", @chars.rjust(7, "α")
|
|
|
|
|
assert_equal "aba#{UNICODE_STRING}", @chars.rjust(7, "ab")
|
|
|
|
|
assert_equal "αηα#{UNICODE_STRING}", @chars.rjust(7, "αη")
|
|
|
|
|
assert_equal "αηαη#{UNICODE_STRING}", @chars.rjust(8, "αη")
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_ljust_should_raise_argument_errors_on_bad_arguments
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_raise(ArgumentError) { @chars.ljust(10, "") }
|
2009-03-08 16:11:58 -04:00
|
|
|
|
assert_raise(ArgumentError) { @chars.ljust }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_ljust_should_count_characters_instead_of_bytes
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.ljust(-3)
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.ljust(0)
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.ljust(4)
|
|
|
|
|
assert_equal "#{UNICODE_STRING} ", @chars.ljust(5)
|
|
|
|
|
assert_equal "#{UNICODE_STRING} ", @chars.ljust(7)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "#{UNICODE_STRING}---", @chars.ljust(7, "-")
|
|
|
|
|
assert_equal "#{UNICODE_STRING}ααα", @chars.ljust(7, "α")
|
|
|
|
|
assert_equal "#{UNICODE_STRING}aba", @chars.ljust(7, "ab")
|
|
|
|
|
assert_equal "#{UNICODE_STRING}αηα", @chars.ljust(7, "αη")
|
|
|
|
|
assert_equal "#{UNICODE_STRING}αηαη", @chars.ljust(8, "αη")
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_center_should_raise_argument_errors_on_bad_arguments
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_raise(ArgumentError) { @chars.center(10, "") }
|
2009-03-08 16:11:58 -04:00
|
|
|
|
assert_raise(ArgumentError) { @chars.center }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
|
def test_center_should_count_characters_instead_of_bytes
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal UNICODE_STRING, @chars.center(-3)
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.center(0)
|
|
|
|
|
assert_equal UNICODE_STRING, @chars.center(4)
|
|
|
|
|
assert_equal "#{UNICODE_STRING} ", @chars.center(5)
|
|
|
|
|
assert_equal " #{UNICODE_STRING} ", @chars.center(6)
|
|
|
|
|
assert_equal " #{UNICODE_STRING} ", @chars.center(7)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "--#{UNICODE_STRING}--", @chars.center(8, "-")
|
|
|
|
|
assert_equal "--#{UNICODE_STRING}---", @chars.center(9, "-")
|
|
|
|
|
assert_equal "αα#{UNICODE_STRING}αα", @chars.center(8, "α")
|
|
|
|
|
assert_equal "αα#{UNICODE_STRING}ααα", @chars.center(9, "α")
|
|
|
|
|
assert_equal "a#{UNICODE_STRING}ab", @chars.center(7, "ab")
|
|
|
|
|
assert_equal "ab#{UNICODE_STRING}ab", @chars.center(8, "ab")
|
|
|
|
|
assert_equal "abab#{UNICODE_STRING}abab", @chars.center(12, "ab")
|
|
|
|
|
assert_equal "α#{UNICODE_STRING}αη", @chars.center(7, "αη")
|
|
|
|
|
assert_equal "αη#{UNICODE_STRING}αη", @chars.center(8, "αη")
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_lstrip_strips_whitespace_from_the_left_of_the_string
|
|
|
|
|
assert_equal UNICODE_STRING, UNICODE_STRING.mb_chars.lstrip
|
|
|
|
|
assert_equal UNICODE_STRING, (@whitespace + UNICODE_STRING).mb_chars.lstrip
|
|
|
|
|
assert_equal UNICODE_STRING + @whitespace, (@whitespace + UNICODE_STRING + @whitespace).mb_chars.lstrip
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_rstrip_strips_whitespace_from_the_right_of_the_string
|
|
|
|
|
assert_equal UNICODE_STRING, UNICODE_STRING.mb_chars.rstrip
|
|
|
|
|
assert_equal UNICODE_STRING, (UNICODE_STRING + @whitespace).mb_chars.rstrip
|
|
|
|
|
assert_equal @whitespace + UNICODE_STRING, (@whitespace + UNICODE_STRING + @whitespace).mb_chars.rstrip
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_strip_strips_whitespace
|
|
|
|
|
assert_equal UNICODE_STRING, UNICODE_STRING.mb_chars.strip
|
|
|
|
|
assert_equal UNICODE_STRING, (@whitespace + UNICODE_STRING).mb_chars.strip
|
|
|
|
|
assert_equal UNICODE_STRING, (UNICODE_STRING + @whitespace).mb_chars.strip
|
|
|
|
|
assert_equal UNICODE_STRING, (@whitespace + UNICODE_STRING + @whitespace).mb_chars.strip
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_stripping_whitespace_leaves_whitespace_within_the_string_intact
|
|
|
|
|
string_with_whitespace = UNICODE_STRING + @whitespace + UNICODE_STRING
|
|
|
|
|
assert_equal string_with_whitespace, string_with_whitespace.mb_chars.strip
|
|
|
|
|
assert_equal string_with_whitespace, string_with_whitespace.mb_chars.lstrip
|
|
|
|
|
assert_equal string_with_whitespace, string_with_whitespace.mb_chars.rstrip
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_size_returns_characters_instead_of_bytes
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal 0, "".mb_chars.size
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal 4, @chars.size
|
|
|
|
|
assert_equal 4, @chars.length
|
|
|
|
|
assert_equal 5, ASCII_STRING.mb_chars.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_reverse_reverses_characters
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.reverse
|
|
|
|
|
assert_equal "わちにこ", @chars.reverse
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2009-09-26 20:32:21 -04:00
|
|
|
|
def test_reverse_should_work_with_normalized_strings
|
2016-08-06 12:03:25 -04:00
|
|
|
|
str = "bös"
|
|
|
|
|
reversed_str = "söb"
|
2018-10-12 11:40:29 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert_equal chars(reversed_str).normalize(:kc), chars(str).normalize(:kc).reverse
|
|
|
|
|
assert_equal chars(reversed_str).normalize(:c), chars(str).normalize(:c).reverse
|
|
|
|
|
assert_equal chars(reversed_str).normalize(:d), chars(str).normalize(:d).reverse
|
|
|
|
|
assert_equal chars(reversed_str).normalize(:kd), chars(str).normalize(:kd).reverse
|
|
|
|
|
end
|
2009-09-26 20:32:21 -04:00
|
|
|
|
assert_equal chars(reversed_str).decompose, chars(str).decompose.reverse
|
|
|
|
|
assert_equal chars(reversed_str).compose, chars(str).compose.reverse
|
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_slice_should_take_character_offsets
|
2016-12-24 12:29:52 -05:00
|
|
|
|
assert_nil "".mb_chars.slice(0)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こ", @chars.slice(0)
|
|
|
|
|
assert_equal "わ", @chars.slice(3)
|
2016-12-24 12:29:52 -05:00
|
|
|
|
assert_nil "".mb_chars.slice(-1..1)
|
|
|
|
|
assert_nil "".mb_chars.slice(-1, 1)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.slice(0..10)
|
|
|
|
|
assert_equal "にちわ", @chars.slice(1..3)
|
|
|
|
|
assert_equal "にちわ", @chars.slice(1, 3)
|
|
|
|
|
assert_equal "こ", @chars.slice(0, 1)
|
|
|
|
|
assert_equal "ちわ", @chars.slice(2..10)
|
|
|
|
|
assert_equal "", @chars.slice(4..10)
|
|
|
|
|
assert_equal "に", @chars.slice(/に/u)
|
|
|
|
|
assert_equal "にち", @chars.slice(/に./u)
|
2016-12-24 12:29:52 -05:00
|
|
|
|
assert_nil @chars.slice(/unknown/u)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "にち", @chars.slice(/(にち)/u, 1)
|
2016-12-24 12:29:52 -05:00
|
|
|
|
assert_nil @chars.slice(/(にち)/u, 2)
|
|
|
|
|
assert_nil @chars.slice(7..6)
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-12-30 08:16:15 -05:00
|
|
|
|
def test_slice_bang_returns_sliced_out_substring
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "にち", @chars.slice!(1..2)
|
2008-12-30 08:16:15 -05:00
|
|
|
|
end
|
|
|
|
|
|
2015-06-29 16:20:50 -04:00
|
|
|
|
def test_slice_bang_returns_nil_on_out_of_bound_arguments
|
2016-12-24 12:29:52 -05:00
|
|
|
|
assert_nil @chars.mb_chars.slice!(9..10)
|
2015-06-29 16:20:50 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-12-30 08:16:15 -05:00
|
|
|
|
def test_slice_bang_removes_the_slice_from_the_receiver
|
2018-05-17 04:32:27 -04:00
|
|
|
|
chars = (+"úüù").mb_chars
|
2016-10-28 23:05:58 -04:00
|
|
|
|
chars.slice!(0, 2)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "ù", chars
|
2008-12-30 08:16:15 -05:00
|
|
|
|
end
|
|
|
|
|
|
2015-10-20 18:02:31 -04:00
|
|
|
|
def test_slice_bang_returns_nil_and_does_not_modify_receiver_if_out_of_bounds
|
2018-05-17 04:32:27 -04:00
|
|
|
|
string = +"úüù"
|
2015-10-20 18:02:31 -04:00
|
|
|
|
chars = string.mb_chars
|
|
|
|
|
assert_nil chars.slice!(4, 5)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "úüù", chars
|
|
|
|
|
assert_equal "úüù", string
|
2015-10-20 18:02:31 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_slice_should_throw_exceptions_on_invalid_arguments
|
|
|
|
|
assert_raise(TypeError) { @chars.slice(2..3, 1) }
|
|
|
|
|
assert_raise(TypeError) { @chars.slice(1, 2..3) }
|
|
|
|
|
assert_raise(ArgumentError) { @chars.slice(1, 1, 1) }
|
|
|
|
|
end
|
|
|
|
|
|
2008-11-26 09:16:28 -05:00
|
|
|
|
def test_ord_should_return_unicode_value_for_first_character
|
|
|
|
|
assert_equal 12371, @chars.ord
|
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_upcase_should_upcase_ascii_characters
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.upcase
|
|
|
|
|
assert_equal "ABC", "aBc".mb_chars.upcase
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_downcase_should_downcase_ascii_characters
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.downcase
|
|
|
|
|
assert_equal "abc", "aBc".mb_chars.downcase
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-01-06 10:06:26 -05:00
|
|
|
|
def test_swapcase_should_swap_ascii_characters
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.swapcase
|
|
|
|
|
assert_equal "AbC", "aBc".mb_chars.swapcase
|
2012-01-06 10:06:26 -05:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_capitalize_should_work_on_ascii_characters
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.capitalize
|
|
|
|
|
assert_equal "Abc", "abc".mb_chars.capitalize
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2010-05-28 15:36:22 -04:00
|
|
|
|
def test_titleize_should_work_on_ascii_characters
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", "".mb_chars.titleize
|
|
|
|
|
assert_equal "Abc Abc", "abc abc".mb_chars.titleize
|
2010-05-28 15:36:22 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_respond_to_knows_which_methods_the_proxy_responds_to
|
2018-01-24 21:14:10 -05:00
|
|
|
|
assert_respond_to "".mb_chars, :slice # Defined on Chars
|
|
|
|
|
assert_respond_to "".mb_chars, :capitalize! # Defined on Chars
|
|
|
|
|
assert_respond_to "".mb_chars, :gsub # Defined on String
|
|
|
|
|
assert_not_respond_to "".mb_chars, :undefined_method # Not defined
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-05-05 02:34:18 -04:00
|
|
|
|
def test_method_works_for_proxyed_methods
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "ll", "hello".mb_chars.method(:slice).call(2..3) # Defined on Chars
|
2018-10-11 05:00:46 -04:00
|
|
|
|
chars = +"hello".mb_chars
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "Hello", chars.method(:capitalize!).call # Defined on Chars
|
|
|
|
|
assert_equal "Hello", chars
|
|
|
|
|
assert_equal "jello", "hello".mb_chars.method(:gsub).call(/h/, "j") # Defined on String
|
2016-08-16 03:30:11 -04:00
|
|
|
|
assert_raise(NameError) { "".mb_chars.method(:undefined_method) } # Not defined
|
2012-05-05 02:34:18 -04:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-11 16:38:20 -04:00
|
|
|
|
def test_acts_like_string
|
2018-01-25 18:14:09 -05:00
|
|
|
|
assert_predicate "Bambi".mb_chars, :acts_like_string?
|
2008-09-11 16:38:20 -04:00
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
2008-09-11 16:38:20 -04:00
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
# The default Multibyte Chars proxy has more features than the normal string implementation. Tests
|
|
|
|
|
# for the implementation of these features should run on all Ruby versions and shouldn't be tested
|
|
|
|
|
# through the proxy methods.
|
2012-01-05 20:12:46 -05:00
|
|
|
|
class MultibyteCharsExtrasTest < ActiveSupport::TestCase
|
2008-09-21 11:37:38 -04:00
|
|
|
|
include MultibyteTestHelpers
|
2006-10-03 19:45:32 -04:00
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_upcase_should_be_unicode_aware
|
|
|
|
|
assert_equal "АБВГД\0F", chars("аБвгд\0f").upcase
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こにちわ", chars("こにちわ").upcase
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_downcase_should_be_unicode_aware
|
2012-01-06 10:06:26 -05:00
|
|
|
|
assert_equal "абвгд\0f", chars("аБвгд\0F").downcase
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こにちわ", chars("こにちわ").downcase
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-01-06 10:06:26 -05:00
|
|
|
|
def test_swapcase_should_be_unicode_aware
|
|
|
|
|
assert_equal "аaéÜ\0f", chars("АAÉü\0F").swapcase
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "こにちわ", chars("こにちわ").swapcase
|
2012-01-06 10:06:26 -05:00
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_capitalize_should_be_unicode_aware
|
2016-08-06 12:03:25 -04:00
|
|
|
|
{ "аБвг аБвг" => "Абвг абвг",
|
|
|
|
|
"аБвг АБВГ" => "Абвг абвг",
|
|
|
|
|
"АБВГ АБВГ" => "Абвг абвг",
|
2016-10-28 23:05:58 -04:00
|
|
|
|
"" => "" }.each do |f, t|
|
2016-08-06 13:55:02 -04:00
|
|
|
|
assert_equal t, chars(f).capitalize
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2010-05-28 15:36:22 -04:00
|
|
|
|
def test_titleize_should_be_unicode_aware
|
|
|
|
|
assert_equal "Él Que Se Enteró", chars("ÉL QUE SE ENTERÓ").titleize
|
|
|
|
|
assert_equal "Абвг Абвг", chars("аБвг аБвг").titleize
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_titleize_should_not_affect_characters_that_do_not_case_fold
|
|
|
|
|
assert_equal "日本語", chars("日本語").titleize
|
|
|
|
|
end
|
|
|
|
|
|
2009-11-01 11:18:27 -05:00
|
|
|
|
def test_limit_should_not_break_on_blank_strings
|
2016-08-06 12:03:25 -04:00
|
|
|
|
example = chars("")
|
2009-11-02 15:34:07 -05:00
|
|
|
|
assert_equal example, example.limit(0)
|
|
|
|
|
assert_equal example, example.limit(1)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_limit_should_work_on_a_multibyte_string
|
2009-11-02 15:34:07 -05:00
|
|
|
|
example = chars(UNICODE_STRING)
|
2012-01-05 10:25:27 -05:00
|
|
|
|
bytesize = UNICODE_STRING.bytesize
|
2010-04-07 15:21:41 -04:00
|
|
|
|
|
2009-11-02 15:34:07 -05:00
|
|
|
|
assert_equal UNICODE_STRING, example.limit(bytesize)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", example.limit(0)
|
|
|
|
|
assert_equal "", example.limit(1)
|
|
|
|
|
assert_equal "こ", example.limit(3)
|
|
|
|
|
assert_equal "こに", example.limit(6)
|
|
|
|
|
assert_equal "こに", example.limit(8)
|
|
|
|
|
assert_equal "こにち", example.limit(9)
|
|
|
|
|
assert_equal "こにちわ", example.limit(50)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_limit_should_work_on_an_ascii_string
|
2009-11-02 15:34:07 -05:00
|
|
|
|
ascii = chars(ASCII_STRING)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
assert_equal ASCII_STRING, ascii.limit(ASCII_STRING.length)
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_equal "", ascii.limit(0)
|
|
|
|
|
assert_equal "o", ascii.limit(1)
|
|
|
|
|
assert_equal "oh", ascii.limit(2)
|
|
|
|
|
assert_equal "ohay", ascii.limit(4)
|
|
|
|
|
assert_equal "ohayo", ascii.limit(50)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_limit_should_keep_under_the_specified_byte_limit
|
2009-11-02 15:34:07 -05:00
|
|
|
|
example = chars(UNICODE_STRING)
|
2009-11-01 11:18:27 -05:00
|
|
|
|
(1..UNICODE_STRING.length).each do |limit|
|
2009-11-02 15:34:07 -05:00
|
|
|
|
assert example.limit(limit).to_s.length <= limit
|
2009-11-01 11:18:27 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
2010-04-07 15:21:41 -04:00
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
def test_composition_exclusion_is_set_up_properly
|
|
|
|
|
# Normalization of DEVANAGARI LETTER QA breaks when composition exclusion isn't used correctly
|
2016-08-06 12:03:25 -04:00
|
|
|
|
qa = [0x915, 0x93c].pack("U*")
|
2018-10-12 11:40:29 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert_equal qa, chars(qa).normalize(:c)
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Test for the Public Review Issue #29, bad explanation of composition might lead to a
|
|
|
|
|
# bad implementation: http://www.unicode.org/review/pr-29.html
|
|
|
|
|
def test_normalization_C_pri_29
|
|
|
|
|
[
|
|
|
|
|
[0x0B47, 0x0300, 0x0B3E],
|
|
|
|
|
[0x1100, 0x0300, 0x1161]
|
2016-08-06 12:03:25 -04:00
|
|
|
|
].map { |c| c.pack("U*") }.each do |c|
|
2018-10-12 11:40:29 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert_equal_codepoints c, chars(c).normalize(:c)
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_normalization_shouldnt_strip_null_bytes
|
|
|
|
|
null_byte_str = "Test\0test"
|
|
|
|
|
|
2018-10-12 11:40:29 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert_equal null_byte_str, chars(null_byte_str).normalize(:kc)
|
|
|
|
|
assert_equal null_byte_str, chars(null_byte_str).normalize(:c)
|
|
|
|
|
assert_equal null_byte_str, chars(null_byte_str).normalize(:d)
|
|
|
|
|
assert_equal null_byte_str, chars(null_byte_str).normalize(:kd)
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal null_byte_str, chars(null_byte_str).decompose
|
|
|
|
|
assert_equal null_byte_str, chars(null_byte_str).compose
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_simple_normalization
|
|
|
|
|
comp_str = [
|
|
|
|
|
44, # LATIN CAPITAL LETTER D
|
|
|
|
|
307, # COMBINING DOT ABOVE
|
|
|
|
|
328, # COMBINING OGONEK
|
|
|
|
|
323 # COMBINING DOT BELOW
|
|
|
|
|
].pack("U*")
|
|
|
|
|
|
2018-10-12 11:40:29 -04:00
|
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
|
assert_equal_codepoints "", chars("").normalize
|
|
|
|
|
assert_equal_codepoints [44, 105, 106, 328, 323].pack("U*"), chars(comp_str).normalize(:kc).to_s
|
|
|
|
|
assert_equal_codepoints [44, 307, 328, 323].pack("U*"), chars(comp_str).normalize(:c).to_s
|
|
|
|
|
assert_equal_codepoints [44, 307, 110, 780, 78, 769].pack("U*"), chars(comp_str).normalize(:d).to_s
|
|
|
|
|
assert_equal_codepoints [44, 105, 106, 110, 780, 78, 769].pack("U*"), chars(comp_str).normalize(:kd).to_s
|
|
|
|
|
end
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_should_compute_grapheme_length
|
|
|
|
|
[
|
2016-08-06 12:03:25 -04:00
|
|
|
|
["", 0],
|
|
|
|
|
["abc", 3],
|
|
|
|
|
["こにちわ", 4],
|
|
|
|
|
[[0x0924, 0x094D, 0x0930].pack("U*"), 2],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# GB3
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(cr lf), 1],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# GB4
|
|
|
|
|
[%w(cr n), 2],
|
|
|
|
|
[%w(lf n), 2],
|
|
|
|
|
[%w(control n), 2],
|
|
|
|
|
[%w(cr extend), 2],
|
|
|
|
|
[%w(lf extend), 2],
|
|
|
|
|
[%w(control extend), 2],
|
|
|
|
|
# GB 5
|
|
|
|
|
[%w(n cr), 2],
|
|
|
|
|
[%w(n lf), 2],
|
|
|
|
|
[%w(n control), 2],
|
|
|
|
|
[%w(extend cr), 2],
|
|
|
|
|
[%w(extend lf), 2],
|
|
|
|
|
[%w(extend control), 2],
|
|
|
|
|
# GB 6
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(l l), 1],
|
|
|
|
|
[%w(l v), 1],
|
|
|
|
|
[%w(l lv), 1],
|
|
|
|
|
[%w(l lvt), 1],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# GB7
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(lv v), 1],
|
|
|
|
|
[%w(lv t), 1],
|
|
|
|
|
[%w(v v), 1],
|
|
|
|
|
[%w(v t), 1],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# GB8
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(lvt t), 1],
|
|
|
|
|
[%w(t t), 1],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# GB8a
|
|
|
|
|
[%w(r r), 1],
|
|
|
|
|
# GB9
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(n extend), 1],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# GB9a
|
|
|
|
|
[%w(n spacingmark), 1],
|
|
|
|
|
# GB10
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(n n), 2],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
# Other
|
2008-09-21 11:21:30 -04:00
|
|
|
|
[%w(n cr lf n), 3],
|
2013-11-13 17:58:40 -05:00
|
|
|
|
[%w(n l v t), 2],
|
|
|
|
|
[%w(cr extend n), 3],
|
2008-09-21 11:21:30 -04:00
|
|
|
|
].each do |input, expected_length|
|
|
|
|
|
if input.kind_of?(Array)
|
|
|
|
|
str = string_from_classes(input)
|
|
|
|
|
else
|
|
|
|
|
str = input
|
|
|
|
|
end
|
2013-11-13 17:58:40 -05:00
|
|
|
|
assert_equal expected_length, chars(str).grapheme_length, input.inspect
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_tidy_bytes_should_tidy_bytes
|
2010-04-07 15:21:41 -04:00
|
|
|
|
single_byte_cases = {
|
|
|
|
|
"\x21" => "!", # Valid ASCII byte, low
|
|
|
|
|
"\x41" => "A", # Valid ASCII byte, mid
|
|
|
|
|
"\x7E" => "~", # Valid ASCII byte, high
|
|
|
|
|
"\x80" => "€", # Continuation byte, low (cp125)
|
|
|
|
|
"\x94" => "”", # Continuation byte, mid (cp125)
|
|
|
|
|
"\x9F" => "Ÿ", # Continuation byte, high (cp125)
|
|
|
|
|
"\xC0" => "À", # Overlong encoding, start of 2-byte sequence, but codepoint < 128
|
|
|
|
|
"\xC1" => "Á", # Overlong encoding, start of 2-byte sequence, but codepoint < 128
|
|
|
|
|
"\xC2" => "Â", # Start of 2-byte sequence, low
|
|
|
|
|
"\xC8" => "È", # Start of 2-byte sequence, mid
|
|
|
|
|
"\xDF" => "ß", # Start of 2-byte sequence, high
|
|
|
|
|
"\xE0" => "à", # Start of 3-byte sequence, low
|
|
|
|
|
"\xE8" => "è", # Start of 3-byte sequence, mid
|
|
|
|
|
"\xEF" => "ï", # Start of 3-byte sequence, high
|
|
|
|
|
"\xF0" => "ð", # Start of 4-byte sequence
|
|
|
|
|
"\xF1" => "ñ", # Unused byte
|
|
|
|
|
"\xFF" => "ÿ", # Restricted byte
|
|
|
|
|
"\x00" => "\x00" # null char
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
single_byte_cases.each do |bad, good|
|
|
|
|
|
assert_equal good, chars(bad).tidy_bytes.to_s
|
|
|
|
|
assert_equal "#{good}#{good}", chars("#{bad}#{bad}").tidy_bytes
|
|
|
|
|
assert_equal "#{good}#{good}#{good}", chars("#{bad}#{bad}#{bad}").tidy_bytes
|
|
|
|
|
assert_equal "#{good}a", chars("#{bad}a").tidy_bytes
|
|
|
|
|
assert_equal "#{good}á", chars("#{bad}á").tidy_bytes
|
|
|
|
|
assert_equal "a#{good}a", chars("a#{bad}a").tidy_bytes
|
|
|
|
|
assert_equal "á#{good}á", chars("á#{bad}á").tidy_bytes
|
|
|
|
|
assert_equal "a#{good}", chars("a#{bad}").tidy_bytes
|
|
|
|
|
assert_equal "á#{good}", chars("á#{bad}").tidy_bytes
|
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
byte_string = "\270\236\010\210\245"
|
2016-08-06 12:03:25 -04:00
|
|
|
|
tidy_string = [0xb8, 0x17e, 0x8, 0x2c6, 0xa5].pack("U*")
|
2008-09-21 11:21:30 -04:00
|
|
|
|
assert_equal_codepoints tidy_string, chars(byte_string).tidy_bytes
|
2016-08-06 12:03:25 -04:00
|
|
|
|
assert_nothing_raised { chars(byte_string).tidy_bytes.to_s.unpack("U*") }
|
2008-09-21 11:21:30 -04:00
|
|
|
|
|
2010-04-07 15:21:41 -04:00
|
|
|
|
# UTF-8 leading byte followed by too few continuation bytes
|
|
|
|
|
assert_equal_codepoints "\xc3\xb0\xc2\xa5\xc2\xa4\x21", chars("\xf0\xa5\xa4\x21").tidy_bytes
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_tidy_bytes_should_forcibly_tidy_bytes_if_specified
|
|
|
|
|
byte_string = "\xF0\xA5\xA4\xA4" # valid as both CP-1252 and UTF-8, but with different interpretations.
|
|
|
|
|
assert_not_equal "𥤤", chars(byte_string).tidy_bytes
|
|
|
|
|
# Forcible conversion to UTF-8
|
|
|
|
|
assert_equal "𥤤", chars(byte_string).tidy_bytes(true)
|
2008-09-21 11:21:30 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-11-28 16:57:07 -05:00
|
|
|
|
def test_class_is_not_forwarded
|
|
|
|
|
assert_equal BYTE_STRING.dup.mb_chars.class, ActiveSupport::Multibyte::Chars
|
|
|
|
|
end
|
2010-04-07 15:21:41 -04:00
|
|
|
|
|
2018-10-12 11:40:29 -04:00
|
|
|
|
def test_unicode_normalize_deprecation
|
|
|
|
|
# String#unicode_normalize default form is `:nfc`, and
|
|
|
|
|
# different than Multibyte::Unicode default, `:nkfc`.
|
|
|
|
|
# Deprecation should suggest the right form if no params
|
|
|
|
|
# are given and default is used.
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfkc\)/) do
|
|
|
|
|
ActiveSupport::Multibyte::Unicode.normalize("")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfd\)/) do
|
|
|
|
|
ActiveSupport::Multibyte::Unicode.normalize("", :d)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_chars_normalize_deprecation
|
|
|
|
|
# String#unicode_normalize default form is `:nfc`, and
|
|
|
|
|
# different than Multibyte::Unicode default, `:nkfc`.
|
|
|
|
|
# Deprecation should suggest the right form if no params
|
|
|
|
|
# are given and default is used.
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfkc\)/) do
|
|
|
|
|
"".mb_chars.normalize
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfc\)/) { "".mb_chars.normalize(:c) }
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfd\)/) { "".mb_chars.normalize(:d) }
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfkc\)/) { "".mb_chars.normalize(:kc) }
|
|
|
|
|
assert_deprecated(/unicode_normalize\(:nfkd\)/) { "".mb_chars.normalize(:kd) }
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-08 04:57:18 -04:00
|
|
|
|
def test_unicode_deprecations
|
|
|
|
|
assert_deprecated { ActiveSupport::Multibyte::Unicode.downcase("") }
|
|
|
|
|
assert_deprecated { ActiveSupport::Multibyte::Unicode.upcase("") }
|
|
|
|
|
assert_deprecated { ActiveSupport::Multibyte::Unicode.swapcase("") }
|
|
|
|
|
end
|
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
|
private
|
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
|
def string_from_classes(classes)
|
|
|
|
|
# Characters from the character classes as described in UAX #29
|
|
|
|
|
character_from_class = {
|
|
|
|
|
l: 0x1100, v: 0x1160, t: 0x11A8, lv: 0xAC00, lvt: 0xAC01, cr: 0x000D, lf: 0x000A,
|
|
|
|
|
extend: 0x094D, n: 0x64, spacingmark: 0x0903, r: 0x1F1E6, control: 0x0001
|
|
|
|
|
}
|
|
|
|
|
classes.collect do |k|
|
|
|
|
|
character_from_class[k.intern]
|
|
|
|
|
end.pack("U*")
|
|
|
|
|
end
|
2007-12-21 06:21:43 -05:00
|
|
|
|
end
|