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"
|
2014-07-19 15:49:52 -04:00
|
|
|
|
|
|
|
class MultibyteProxyText < ActiveSupport::TestCase
|
|
|
|
class AsciiOnlyEncoder
|
|
|
|
attr_reader :wrapped_string
|
|
|
|
alias to_s wrapped_string
|
|
|
|
|
|
|
|
def initialize(string)
|
2016-08-06 12:03:25 -04:00
|
|
|
@wrapped_string = string.gsub(/[^\u0000-\u007F]/, "?")
|
2014-07-19 15:49:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_custom_encoder(encoder)
|
|
|
|
original_proxy_class = ActiveSupport::Multibyte.proxy_class
|
|
|
|
|
|
|
|
begin
|
|
|
|
ActiveSupport::Multibyte.proxy_class = encoder
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Multibyte.proxy_class = original_proxy_class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "custom multibyte encoder" do
|
|
|
|
with_custom_encoder(AsciiOnlyEncoder) do
|
|
|
|
assert_equal "s?me string 123", "søme string 123".mb_chars.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "søme string 123", "søme string 123".mb_chars.to_s
|
|
|
|
end
|
|
|
|
end
|