| 1 |
# frozen_string_literal: false |
| 2 |
require 'test/unit' |
| 3 |
require '-test-/string' |
| 4 |
require_relative '../symbol/noninterned_name' |
| 5 |
|
| 6 |
class Test_String_Fstring < Test::Unit::TestCase |
| 7 |
include Test_Symbol::NonInterned |
| 8 |
|
| 9 |
def assert_fstring(str) |
| 10 |
fstr = Bug::String.fstring(str) |
| 11 |
yield str |
| 12 |
yield fstr |
| 13 |
end |
| 14 |
|
| 15 |
def test_rb_enc_interned_str_autoloaded_encoding |
| 16 |
assert_separately([], <<~RUBY) |
| 17 |
require '-test-/string' |
| 18 |
assert_include(Encoding::Windows_31J.inspect, 'autoload') |
| 19 |
Bug::String.rb_enc_interned_str(Encoding::Windows_31J) |
| 20 |
RUBY |
| 21 |
end |
| 22 |
|
| 23 |
def test_rb_enc_interned_str_null_encoding |
| 24 |
assert_equal Encoding::ASCII_8BIT, Bug::String.rb_enc_interned_str(nil).encoding |
| 25 |
end |
| 26 |
|
| 27 |
def test_rb_enc_str_new_autoloaded_encoding |
| 28 |
assert_separately([], <<~RUBY) |
| 29 |
require '-test-/string' |
| 30 |
assert_include(Encoding::Windows_31J.inspect, 'autoload') |
| 31 |
Bug::String.rb_enc_str_new(Encoding::Windows_31J) |
| 32 |
RUBY |
| 33 |
end |
| 34 |
|
| 35 |
def test_rb_enc_str_new_null_encoding |
| 36 |
assert_equal Encoding::ASCII_8BIT, Bug::String.rb_enc_str_new(nil).encoding |
| 37 |
end |
| 38 |
|
| 39 |
def test_instance_variable |
| 40 |
str = __method__.to_s * 3 |
| 41 |
str.instance_variable_set(:@test, 42) |
| 42 |
str.freeze |
| 43 |
assert_fstring(str) {|s| assert_send([s, :instance_variable_defined?, :@test])} |
| 44 |
end |
| 45 |
|
| 46 |
def test_singleton_method |
| 47 |
str = __method__.to_s * 3 |
| 48 |
def str.foo |
| 49 |
end |
| 50 |
str.freeze |
| 51 |
assert_fstring(str) {|s| assert_send([s, :respond_to?, :foo])} |
| 52 |
end |
| 53 |
|
| 54 |
def test_singleton_class |
| 55 |
str = noninterned_name |
| 56 |
fstr = Bug::String.fstring(str) |
| 57 |
assert_raise(TypeError) {fstr.singleton_class} |
| 58 |
end |
| 59 |
|
| 60 |
def test_fake_str |
| 61 |
assert_equal([*"a".."z"].join(""), Bug::String.fstring_fake_str) |
| 62 |
end |
| 63 |
|
| 64 |
class S < String |
| 65 |
end |
| 66 |
|
| 67 |
def test_subclass |
| 68 |
str = S.new(__method__.to_s * 3) |
| 69 |
str.freeze |
| 70 |
assert_fstring(str) {|s| assert_instance_of(S, s)} |
| 71 |
end |
| 72 |
end |