Methods
S
T
Included Modules
Instance Public methods
setup()
# File activesupport/test/multibyte_chars_test.rb, line 103
def setup
  @chars = UNICODE_STRING.dup.mb_chars
  # Ruby 1.9 only supports basic whitespace
  @whitespace = "\n\t "
end
test_acts_like_string()
# File activesupport/test/multibyte_chars_test.rb, line 485
def test_acts_like_string
  assert 'Bambi'.mb_chars.acts_like_string?
end
test_capitalize_should_work_on_ascii_characters()
# File activesupport/test/multibyte_chars_test.rb, line 459
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()
# File activesupport/test/multibyte_chars_test.rb, line 325
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()
# File activesupport/test/multibyte_chars_test.rb, line 320
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()
# File activesupport/test/multibyte_chars_test.rb, line 449
def test_downcase_should_downcase_ascii_characters
  assert_equal '', ''.mb_chars.downcase
  assert_equal 'abc', 'aBc'.mb_chars.downcase
end
test_identity()
# File activesupport/test/multibyte_chars_test.rb, line 146
def test_identity
  assert_equal @chars, @chars
  assert @chars.eql?(@chars)
  assert !@chars.eql?(UNICODE_STRING)
end
test_include_raises_when_nil_is_passed()
# File activesupport/test/multibyte_chars_test.rb, line 223
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()
# File activesupport/test/multibyte_chars_test.rb, line 229
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_equal 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()
# File activesupport/test/multibyte_chars_test.rb, line 269
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()
# File activesupport/test/multibyte_chars_test.rb, line 278
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()
# File activesupport/test/multibyte_chars_test.rb, line 248
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()
# File activesupport/test/multibyte_chars_test.rb, line 205
def test_insert_should_be_destructive
  @chars.insert(1, 'わ')
  assert_equal 'こわにちわ', @chars
end
test_insert_throws_index_error()
# File activesupport/test/multibyte_chars_test.rb, line 210
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()
# File activesupport/test/multibyte_chars_test.rb, line 307
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()
# File activesupport/test/multibyte_chars_test.rb, line 302
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()
# File activesupport/test/multibyte_chars_test.rb, line 343
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()
# File activesupport/test/multibyte_chars_test.rb, line 476
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()
# File activesupport/test/multibyte_chars_test.rb, line 440
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()
# File activesupport/test/multibyte_chars_test.rb, line 469
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()
# File activesupport/test/multibyte_chars_test.rb, line 376
def test_reverse_reverses_characters
  assert_equal '', ''.mb_chars.reverse
  assert_equal 'わちにこ', @chars.reverse
end
test_reverse_should_work_with_normalized_strings()
# File activesupport/test/multibyte_chars_test.rb, line 381
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()
# File activesupport/test/multibyte_chars_test.rb, line 239
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()
# File activesupport/test/multibyte_chars_test.rb, line 289
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()
# File activesupport/test/multibyte_chars_test.rb, line 284
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()
# File activesupport/test/multibyte_chars_test.rb, line 349
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()
# File activesupport/test/multibyte_chars_test.rb, line 173
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()
# File activesupport/test/multibyte_chars_test.rb, line 215
def test_should_know_if_one_includes_the_other
  assert @chars.include?('')
  assert @chars.include?('ち')
  assert @chars.include?('わ')
  assert !@chars.include?('こちわ')
  assert !@chars.include?('a')
end
test_should_not_be_equal_to_an_other_string()
# File activesupport/test/multibyte_chars_test.rb, line 178
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()
# File activesupport/test/multibyte_chars_test.rb, line 188
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()
# File activesupport/test/multibyte_chars_test.rb, line 197
def test_should_use_character_offsets_for_insert_offsets
  assert_equal '', ''.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()
# File activesupport/test/multibyte_chars_test.rb, line 369
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()
# File activesupport/test/multibyte_chars_test.rb, line 420
def test_slice_bang_removes_the_slice_from_the_receiver
  chars = 'úüù'.mb_chars
  chars.slice!(0,2)
  assert_equal 'ù', chars
end
test_slice_bang_returns_nil_and_does_not_modify_receiver_if_out_of_bounds()
# File activesupport/test/multibyte_chars_test.rb, line 426
def test_slice_bang_returns_nil_and_does_not_modify_receiver_if_out_of_bounds
  string = 'úüù'
  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()
# File activesupport/test/multibyte_chars_test.rb, line 416
def test_slice_bang_returns_nil_on_out_of_bound_arguments
  assert_equal nil, @chars.mb_chars.slice!(9..10)
end
test_slice_bang_returns_sliced_out_substring()
# File activesupport/test/multibyte_chars_test.rb, line 412
def test_slice_bang_returns_sliced_out_substring
  assert_equal 'にち', @chars.slice!(1..2)
end
test_slice_should_take_character_offsets()
# File activesupport/test/multibyte_chars_test.rb, line 392
def test_slice_should_take_character_offsets
  assert_equal nil, ''.mb_chars.slice(0)
  assert_equal 'こ', @chars.slice(0)
  assert_equal 'わ', @chars.slice(3)
  assert_equal nil, ''.mb_chars.slice(-1..1)
  assert_equal 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_equal nil, @chars.slice(/unknown/u)
  assert_equal 'にち', @chars.slice(/(にち)/u, 1)
  assert_equal nil, @chars.slice(/(にち)/u, 2)
  assert_equal nil, @chars.slice(7..6)
end
test_slice_should_throw_exceptions_on_invalid_arguments()
# File activesupport/test/multibyte_chars_test.rb, line 434
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()
# File activesupport/test/multibyte_chars_test.rb, line 183
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()
# File activesupport/test/multibyte_chars_test.rb, line 109
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()
# File activesupport/test/multibyte_chars_test.rb, line 152
def test_string_methods_are_chainable
  assert chars('').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()
# File activesupport/test/multibyte_chars_test.rb, line 355
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()
# File activesupport/test/multibyte_chars_test.rb, line 362
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()
# File activesupport/test/multibyte_chars_test.rb, line 454
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()
# File activesupport/test/multibyte_chars_test.rb, line 135
def test_tidy_bytes_bang_should_change_wrapped_string
  original = " Un bUen café \x92"
  proxy = chars(original.dup)
  proxy.tidy_bytes!
  assert_not_equal original, proxy.to_s
end
test_tidy_bytes_bang_should_return_self()
# File activesupport/test/multibyte_chars_test.rb, line 131
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()
# File activesupport/test/multibyte_chars_test.rb, line 464
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()
# File activesupport/test/multibyte_chars_test.rb, line 142
def test_unicode_string_should_have_utf8_encoding
  assert_equal Encoding::UTF_8, UNICODE_STRING.encoding
end
test_upcase_should_upcase_ascii_characters()
# File activesupport/test/multibyte_chars_test.rb, line 444
def test_upcase_should_upcase_ascii_characters
  assert_equal '', ''.mb_chars.upcase
  assert_equal 'ABC', 'aBc'.mb_chars.upcase
end