diff --git a/lib/rbs/unit_test/with_aliases.rb b/lib/rbs/unit_test/with_aliases.rb index 986ed09cd..d1b68fe21 100644 --- a/lib/rbs/unit_test/with_aliases.rb +++ b/lib/rbs/unit_test/with_aliases.rb @@ -43,62 +43,62 @@ def with(*args, &block) args.each(&block) end - def with_int(value = 3, &block) - return WithEnum.new to_enum(__method__ || raise, value) unless block + def with_int(value = 3, object: false, &block) + return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block yield value - yield ToInt.new(value) + yield object_it(ToInt.new(value), object) end - def with_float(value = 0.1) - return WithEnum.new to_enum(__method__ || raise, value) unless block_given? + def with_float(value = 0.1, object: false) + return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block_given? yield value - yield ToF.new(value) + yield object_it(ToF.new(value), object) end - def with_string(value = '') - return WithEnum.new to_enum(__method__ || raise, value) unless block_given? + def with_string(value = '', object: false) + return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block_given? yield value - yield ToStr.new(value) + yield object_it(ToStr.new(value), object) end - def with_array(*elements) - return WithEnum.new to_enum(__method__ || raise, *elements) unless block_given? + def with_array(*elements, object: false) + return WithEnum.new to_enum(__method__ || raise, *elements, object: object) unless block_given? yield _ = elements - yield ToArray.new(*elements) + yield object_it(ToArray.new(*elements), object) end - def with_hash(hash = {}) - return WithEnum.new to_enum(__method__ || raise, hash) unless block_given? + def with_hash(hash = {}, object: false) + return WithEnum.new to_enum(__method__ || raise, hash, object: object) unless block_given? yield _ = hash - yield ToHash.new(hash) + yield object_it(ToHash.new(hash), object) end - def with_io(io = $stdout) - return WithEnum.new to_enum(__method__ || raise, io) unless block_given? + def with_io(io = $stdout, object: false) + return WithEnum.new to_enum(__method__ || raise, io, object: object) unless block_given? yield io - yield ToIO.new(io) + yield object_it(ToIO.new(io), object) end - def with_path(path = "/tmp/foo.txt", &block) - return WithEnum.new to_enum(__method__ || raise, path) unless block + def with_path(path = "/tmp/foo.txt", object: false, &block) + return WithEnum.new to_enum(__method__ || raise, path, object: object) unless block with_string(path, &block) - block.call ToPath.new(path) + block.call object_it(ToPath.new(path), object) end - def with_encoding(encoding = Encoding::UTF_8, &block) - return WithEnum.new to_enum(__method__ || raise, encoding) unless block + def with_encoding(encoding = Encoding::UTF_8, object: false, &block) + return WithEnum.new to_enum(__method__ || raise, encoding, object: object) unless block block.call encoding - with_string(encoding.to_s, &block) + with_string(encoding.to_s, object: object, &block) end - def with_interned(value = :&, &block) - return WithEnum.new to_enum(__method__ || raise, value) unless block + def with_interned(value = :&, object: false, &block) + return WithEnum.new to_enum(__method__ || raise, value, object: object) unless block - with_string(value.to_s, &block) + with_string(value.to_s, object: object, &block) block.call value.to_sym end @@ -108,10 +108,10 @@ def with_bool(&block) yield false end - def with_boolish(&block) - return WithEnum.new to_enum(__method__ || raise) unless block + def with_boolish(object: false, &block) + return WithEnum.new to_enum(__method__ || raise, object: object) unless block with_bool(&block) - [nil, 1, Object.new, BlankSlate.new, "hello, world!"].each(&block) + [nil, 1, Object.new, object_it(BlankSlate.new, object), "hello, world!"].each(&block) end alias with_untyped with_boolish @@ -138,6 +138,14 @@ def lower.<=>(rhs) = :not_nil unless defined? lower.<=> end end end + + def object_it(value, make) + if make + value.__with_object_methods + else + value + end + end end end end diff --git a/sig/unit_test/with_aliases.rbs b/sig/unit_test/with_aliases.rbs index bc21f5d31..1679995c1 100644 --- a/sig/unit_test/with_aliases.rbs +++ b/sig/unit_test/with_aliases.rbs @@ -70,48 +70,48 @@ module RBS # Yields `::int` objects # - def with_int: (?Integer value) { (int) -> void } -> void - | (?Integer value) -> WithEnum[int] + def with_int: (?Integer value, ?object: bool) { (int) -> void } -> void + | (?Integer value, ?object: bool) -> WithEnum[int] # Yields `::float` objects # - def with_float: (?Float value) { (float) -> void } -> void - | (?Float value) -> WithEnum[float] + def with_float: (?Float value, ?object: bool) { (float) -> void } -> void + | (?Float value, ?object: bool) -> WithEnum[float] # Yields `::string` objects # - def with_string: (?String value) { (string) -> void } -> void - | (?String value) -> WithEnum[string] + def with_string: (?String value, ?object: bool) { (string) -> void } -> void + | (?String value, ?object: bool) -> WithEnum[string] # Yields `::array` objects # - def with_array: [T] (*T elements) { (array[T]) -> void } -> void - | [T] (*T elements) -> WithEnum[array[T]] + def with_array: [T] (*T elements, ?object: bool) { (array[T]) -> void } -> void + | [T] (*T elements, ?object: bool) -> WithEnum[array[T]] # Yields `::hash` objects # - def with_hash: [K, V] (?Hash[K, V] hash) { (hash[K, V]) -> void } -> void - | [K, V] (?Hash[K, V] hash) -> WithEnum[hash[K, V]] + def with_hash: [K, V] (?Hash[K, V] hash, ?object: bool) { (hash[K, V]) -> void } -> void + | [K, V] (?Hash[K, V] hash, ?object: bool) -> WithEnum[hash[K, V]] # Yields `::io` objects # - def with_io: (?untyped io) { (io) -> void } -> void - | (?untyped io) -> WithEnum[io] + def with_io: (?untyped io, ?object: bool) { (io) -> void } -> void + | (?untyped io, ?object: bool) -> WithEnum[io] # Yields `::path` objects # - def with_path: (?String path) { (path) -> void } -> void - | (?String path) -> WithEnum[path] + def with_path: (?String path, ?object: bool) { (path) -> void } -> void + | (?String path, ?object: bool) -> WithEnum[path] # Yields `::encoding` objects # - def with_encoding: (?untyped encoding) { (encoding) -> void } -> void - | (?untyped encoding) -> WithEnum[encoding] + def with_encoding: (?untyped encoding, ?object: bool) { (encoding) -> void } -> void + | (?untyped encoding, ?object: bool) -> WithEnum[encoding] # Yields `::interned` objects # - def with_interned: (?Symbol value) { (interned) -> void } -> void - | (?Symbol value) -> WithEnum[interned] + def with_interned: (?Symbol value, ?object: bool) { (interned) -> void } -> void + | (?Symbol value, ?object: bool) -> WithEnum[interned] # Yields `::bool` objects # @@ -120,8 +120,8 @@ module RBS # Yields `::boolish` objects # - def with_boolish: () { (boolish) -> void } -> void - | () -> WithEnum[boolish] + def with_boolish: (?object: bool) { (boolish) -> void } -> void + | (?object: bool) -> WithEnum[boolish] # Yields `::untyped` objects # @@ -131,6 +131,8 @@ module RBS # Yields `::range` objects # def with_range: (WithEnum[untyped] start, WithEnum[untyped] stop, ?bool exclude_end) { (range[untyped]) -> void } -> void + + private def object_it: [T < Convertibles::BlankSlate] (T value, bool make_it_object) -> T end end end diff --git a/test/stdlib/Kernel_test.rb b/test/stdlib/Kernel_test.rb index 0ac9b8198..01071ecf6 100644 --- a/test/stdlib/Kernel_test.rb +++ b/test/stdlib/Kernel_test.rb @@ -40,7 +40,7 @@ def test_Hash assert_send_type "([]) -> Hash[untyped, untyped]", Kernel, :Hash, [] - with_hash 'a' => 3 do |hash| + with_hash({ 'a' => 3 }) do |hash| assert_send_type "(::hash[String, Integer]) -> Hash[String, Integer]", Kernel, :Hash, hash end diff --git a/test/stdlib/Regexp_test.rb b/test/stdlib/Regexp_test.rb index 80b9f5cd8..52a4ad947 100644 --- a/test/stdlib/Regexp_test.rb +++ b/test/stdlib/Regexp_test.rb @@ -47,7 +47,7 @@ def test_compile assert_send_type '(string) -> Regexp', Regexp, :compile, pattern - with_int(Regexp::IGNORECASE).and with_string('i'), true, false, nil do |options| + with_int(Regexp::IGNORECASE, object: true).and with_string('i'), true, false, nil do |options| assert_send_type '(string, int | string | bool | nil) -> Regexp', Regexp, :compile, pattern, options @@ -118,7 +118,7 @@ def test_linear_time? Regexp, :linear_time?, regexp, timeout: timeout end - with_int(Regexp::IGNORECASE).and(with_string('i'), true, false, nil) do |options| + with_int(Regexp::IGNORECASE, object: true).and(with_string('i'), true, false, nil) do |options| assert_send_type '(string, int | string | bool | nil) -> bool', Regexp, :linear_time?, regexp, options @@ -229,7 +229,7 @@ def test_new assert_send_type '(string) -> Regexp', Regexp, :new, pattern - with_int(Regexp::IGNORECASE).and with_string('i'), true, false, nil do |options| + with_int(Regexp::IGNORECASE, object: true).and with_string('i'), true, false, nil do |options| assert_send_type '(string, int | string | bool | nil) -> Regexp', Regexp, :new, pattern, options diff --git a/test/stdlib/String_test.rb b/test/stdlib/String_test.rb index e6429a41a..d840230fb 100644 --- a/test/stdlib/String_test.rb +++ b/test/stdlib/String_test.rb @@ -103,7 +103,7 @@ def test_mod '%d %d', :%, ary end - with_hash a: 3, b: 4 do |named| + with_hash({ a: 3, b: 4 }) do |named| assert_send_type '(hash[Symbol, untyped]) -> String', '%d %d', :%, named end @@ -953,7 +953,7 @@ def test_gsub 'hello', :gsub, pattern, replacement end - with_hash 'l' => ToS.new('!') do |replacement| + with_hash({ 'l' => ToS.new('!') }) do |replacement| assert_send_type '(Regexp | string, hash[String, _ToS]) -> String', 'hello', :gsub, pattern, replacement end @@ -979,7 +979,7 @@ def test_gsub! +'heya', :gsub!, pattern, replacement end - with_hash 'l' => ToS.new('!') do |replacement| + with_hash({ 'l' => ToS.new('!') }) do |replacement| assert_send_type '(Regexp | string, hash[String, _ToS]) -> String', +'hello', :gsub!, pattern, replacement assert_send_type '(Regexp | string, hash[String, _ToS]) -> nil', @@ -1499,7 +1499,7 @@ def test_sub 'hello', :sub, pattern, replacement end - with_hash 'l' => ToS.new('!') do |replacement| + with_hash({ 'l' => ToS.new('!') }) do |replacement| assert_send_type '(Regexp | string, hash[String, _ToS]) -> String', 'hello', :sub, pattern, replacement end @@ -1520,7 +1520,7 @@ def test_sub! 'heya', :sub!, pattern, replacement end - with_hash 'l' => ToS.new('!') do |replacement| + with_hash({ 'l' => ToS.new('!') }) do |replacement| assert_send_type '(Regexp | string, hash[String, _ToS]) -> String', 'hello', :sub!, pattern, replacement assert_send_type '(Regexp | string, hash[String, _ToS]) -> nil',