class MultibyteCharsUTF8BehaviourTest

Public Instance Methods

setup() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 105
def setup
  @chars = UNICODE_STRING.dup.mb_chars
  # Ruby 1.9 only supports basic whitespace
  @whitespace = "\n\t "
end
test_acts_like_string() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 487
def test_acts_like_string
  assert "Bambi".mb_chars.acts_like_string?
end
test_capitalize_should_work_on_ascii_characters() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 461
def test_capitalize_should_work_on_ascii_characters
  assert_equal "", "".mb_chars.capitalize
  assert_equal "Abc", "abc".mb_chars.capitalize
end
test_center_should_count_characters_instead_of_bytes() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 327
def test_center_should_count_characters_instead_of_bytes
  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)
  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, "αη")
end
test_center_should_raise_argument_errors_on_bad_arguments() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 322
def test_center_should_raise_argument_errors_on_bad_arguments
  assert_raise(ArgumentError) { @chars.center(10, "") }
  assert_raise(ArgumentError) { @chars.center }
end
test_downcase_should_downcase_ascii_characters() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 451
def test_downcase_should_downcase_ascii_characters
  assert_equal "", "".mb_chars.downcase
  assert_equal "abc", "aBc".mb_chars.downcase
end
test_identity() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 148
def test_identity
  assert_equal @chars, @chars
  assert @chars.eql?(@chars)
  assert !@chars.eql?(UNICODE_STRING)
end
test_include_raises_when_nil_is_passed() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 225
def test_include_raises_when_nil_is_passed
  assert_raises TypeError, NoMethodError, "Expected chars.include?(nil) to raise TypeError or NoMethodError" do
    @chars.include?(nil)
  end
end
test_index_should_return_character_offset() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 231
def test_index_should_return_character_offset
  assert_nil @chars.index("u")
  assert_equal 0, @chars.index("こに")
  assert_equal 2, @chars.index("ち")
  assert_equal 2, @chars.index("ち", -2)
  assert_nil @chars.index("ち", -1)
  assert_equal 3, @chars.index("わ")
  assert_equal 5, "ééxééx".mb_chars.index("x", 4)
end
test_indexed_insert_should_raise_on_index_overflow() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 271
def test_indexed_insert_should_raise_on_index_overflow
  before = @chars.to_s
  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" }
  assert_equal before, @chars
end
test_indexed_insert_should_raise_on_range_overflow() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 280
def test_indexed_insert_should_raise_on_range_overflow
  before = @chars.to_s
  assert_raise(RangeError) { @chars[10..12] = "a" }
  assert_equal before, @chars
end
test_indexed_insert_should_take_character_offsets() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 250
def test_indexed_insert_should_take_character_offsets
  @chars[2] = "a"
  assert_equal "こにaわ", @chars
  @chars[2] = "ηη"
  assert_equal "こにηηわ", @chars
  @chars[3, 2] = "λλλ"
  assert_equal "こにηλλλ", @chars
  @chars[1, 0] = "λ"
  assert_equal "こλにηλλλ", @chars
  @chars[4..6] = "ηη"
  assert_equal "こλにηηη", @chars
  @chars[/ηη/] = "λλλ"
  assert_equal "こλにλλλη", @chars
  @chars[/(λλ)(.)/, 2] = "α"
  assert_equal "こλにλλαη", @chars
  @chars["α"] = "¢"
  assert_equal "こλにλλ¢η", @chars
  @chars["λλ"] = "ααα"
  assert_equal "こλにααα¢η", @chars
end
test_insert_should_be_destructive() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 207
def test_insert_should_be_destructive
  @chars.insert(1, "わ")
  assert_equal "こわにちわ", @chars
end
test_insert_throws_index_error() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 212
def test_insert_throws_index_error
  assert_raise(IndexError) { @chars.insert(-12, "わ") }
  assert_raise(IndexError) { @chars.insert(12, "わ") }
end
test_ljust_should_count_characters_instead_of_bytes() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 309
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)
  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, "αη")
end
test_ljust_should_raise_argument_errors_on_bad_arguments() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 304
def test_ljust_should_raise_argument_errors_on_bad_arguments
  assert_raise(ArgumentError) { @chars.ljust(10, "") }
  assert_raise(ArgumentError) { @chars.ljust }
end
test_lstrip_strips_whitespace_from_the_left_of_the_string() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 345
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
test_method_works_for_proxyed_methods() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 478
def test_method_works_for_proxyed_methods
  assert_equal "ll", "hello".mb_chars.method(:slice).call(2..3) # Defined on Chars
  chars = "hello".mb_chars
  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
  assert_raise(NameError) { "".mb_chars.method(:undefined_method) } # Not defined
end
test_ord_should_return_unicode_value_for_first_character() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 442
def test_ord_should_return_unicode_value_for_first_character
  assert_equal 12371, @chars.ord
end
test_respond_to_knows_which_methods_the_proxy_responds_to() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 471
def test_respond_to_knows_which_methods_the_proxy_responds_to
  assert "".mb_chars.respond_to?(:slice) # Defined on Chars
  assert "".mb_chars.respond_to?(:capitalize!) # Defined on Chars
  assert "".mb_chars.respond_to?(:gsub) # Defined on String
  assert !"".mb_chars.respond_to?(:undefined_method) # Not defined
end
test_reverse_reverses_characters() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 378
def test_reverse_reverses_characters
  assert_equal "", "".mb_chars.reverse
  assert_equal "わちにこ", @chars.reverse
end
test_reverse_should_work_with_normalized_strings() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 383
def test_reverse_should_work_with_normalized_strings
  str = "bös"
  reversed_str = "söb"
  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
  assert_equal chars(reversed_str).decompose, chars(str).decompose.reverse
  assert_equal chars(reversed_str).compose, chars(str).compose.reverse
end
test_rindex_should_return_character_offset() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 241
def test_rindex_should_return_character_offset
  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)
end
test_rjust_should_count_characters_instead_of_bytes() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 291
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)
  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, "αη")
end
test_rjust_should_raise_argument_errors_on_bad_arguments() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 286
def test_rjust_should_raise_argument_errors_on_bad_arguments
  assert_raise(ArgumentError) { @chars.rjust(10, "") }
  assert_raise(ArgumentError) { @chars.rjust }
end
test_rstrip_strips_whitespace_from_the_right_of_the_string() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 351
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
test_should_be_equal_to_the_wrapped_string() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 175
def test_should_be_equal_to_the_wrapped_string
  assert_equal UNICODE_STRING, @chars
  assert_equal @chars, UNICODE_STRING
end
test_should_know_if_one_includes_the_other() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 217
def test_should_know_if_one_includes_the_other
  assert_includes @chars, ""
  assert_includes @chars, "ち"
  assert_includes @chars, "わ"
  assert_not_includes @chars, "こちわ"
  assert_not_includes @chars, "a"
end
test_should_not_be_equal_to_an_other_string() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 180
def test_should_not_be_equal_to_an_other_string
  assert_not_equal @chars, "other"
  assert_not_equal "other", @chars
end
test_should_return_character_offset_for_regexp_matches() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 190
def test_should_return_character_offset_for_regexp_matches
  assert_nil(@chars =~ /wrong/u)
  assert_equal 0, (@chars =~ /こ/u)
  assert_equal 0, (@chars =~ /こに/u)
  assert_equal 1, (@chars =~ /に/u)
  assert_equal 2, (@chars =~ /ち/u)
  assert_equal 3, (@chars =~ /わ/u)
end
test_should_use_character_offsets_for_insert_offsets() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 199
def test_should_use_character_offsets_for_insert_offsets
  assert_equal "", "".dup.mb_chars.insert(0, "")
  assert_equal "こわにちわ", @chars.insert(1, "わ")
  assert_equal "こわわわにちわ", @chars.insert(2, "わわ")
  assert_equal "わこわわわにちわ", @chars.insert(0, "わ")
  assert_equal "わこわわわにちわ", @chars.wrapped_string
end
test_size_returns_characters_instead_of_bytes() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 371
def test_size_returns_characters_instead_of_bytes
  assert_equal 0, "".mb_chars.size
  assert_equal 4, @chars.size
  assert_equal 4, @chars.length
  assert_equal 5, ASCII_STRING.mb_chars.size
end
test_slice_bang_removes_the_slice_from_the_receiver() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 422
def test_slice_bang_removes_the_slice_from_the_receiver
  chars = "úüù".dup.mb_chars
  chars.slice!(0, 2)
  assert_equal "ù", chars
end
test_slice_bang_returns_nil_and_does_not_modify_receiver_if_out_of_bounds() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 428
def test_slice_bang_returns_nil_and_does_not_modify_receiver_if_out_of_bounds
  string = "úüù".dup
  chars = string.mb_chars
  assert_nil chars.slice!(4, 5)
  assert_equal "úüù", chars
  assert_equal "úüù", string
end
test_slice_bang_returns_nil_on_out_of_bound_arguments() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 418
def test_slice_bang_returns_nil_on_out_of_bound_arguments
  assert_nil @chars.mb_chars.slice!(9..10)
end
test_slice_bang_returns_sliced_out_substring() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 414
def test_slice_bang_returns_sliced_out_substring
  assert_equal "にち", @chars.slice!(1..2)
end
test_slice_should_take_character_offsets() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 394
def test_slice_should_take_character_offsets
  assert_nil "".mb_chars.slice(0)
  assert_equal "こ", @chars.slice(0)
  assert_equal "わ", @chars.slice(3)
  assert_nil "".mb_chars.slice(-1..1)
  assert_nil "".mb_chars.slice(-1, 1)
  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)
  assert_nil @chars.slice(/unknown/u)
  assert_equal "にち", @chars.slice(/(にち)/u, 1)
  assert_nil @chars.slice(/(にち)/u, 2)
  assert_nil @chars.slice(7..6)
end
test_slice_should_throw_exceptions_on_invalid_arguments() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 436
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
test_sortability() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 185
def test_sortability
  words = %w(builder armor zebra).sort_by(&:mb_chars)
  assert_equal %w(armor builder zebra), words
end
test_split_should_return_an_array_of_chars_instances() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 111
def test_split_should_return_an_array_of_chars_instances
  @chars.split(//).each do |character|
    assert_kind_of ActiveSupport::Multibyte.proxy_class, character
  end
end
test_string_methods_are_chainable() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 154
def test_string_methods_are_chainable
  assert chars("".dup).insert(0, "").kind_of?(ActiveSupport::Multibyte.proxy_class)
  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)
  assert chars("").normalize.kind_of?(ActiveSupport::Multibyte.proxy_class)
  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)
end
test_strip_strips_whitespace() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 357
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
test_stripping_whitespace_leaves_whitespace_within_the_string_intact() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 364
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
test_swapcase_should_swap_ascii_characters() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 456
def test_swapcase_should_swap_ascii_characters
  assert_equal "", "".mb_chars.swapcase
  assert_equal "AbC", "aBc".mb_chars.swapcase
end
test_tidy_bytes_bang_should_change_wrapped_string() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 137
def test_tidy_bytes_bang_should_change_wrapped_string
  original = " Un bUen café \x92".dup
  proxy = chars(original.dup)
  proxy.tidy_bytes!
  assert_not_equal original, proxy.to_s
end
test_tidy_bytes_bang_should_return_self() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 133
def test_tidy_bytes_bang_should_return_self
  assert_equal @chars.object_id, @chars.tidy_bytes!.object_id
end
test_titleize_should_work_on_ascii_characters() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 466
def test_titleize_should_work_on_ascii_characters
  assert_equal "", "".mb_chars.titleize
  assert_equal "Abc Abc", "abc abc".mb_chars.titleize
end
test_unicode_string_should_have_utf8_encoding() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 144
def test_unicode_string_should_have_utf8_encoding
  assert_equal Encoding::UTF_8, UNICODE_STRING.encoding
end
test_upcase_should_upcase_ascii_characters() click to toggle source
# File activesupport/test/multibyte_chars_test.rb, line 446
def test_upcase_should_upcase_ascii_characters
  assert_equal "", "".mb_chars.upcase
  assert_equal "ABC", "aBc".mb_chars.upcase
end