Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add objects methods #2063

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions lib/rbs/unit_test/with_aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
42 changes: 22 additions & 20 deletions sig/unit_test/with_aliases.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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
#
Expand All @@ -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
2 changes: 1 addition & 1 deletion test/stdlib/Kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/stdlib/Regexp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions test/stdlib/String_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
'%<a>d %<b>d', :%, named
end
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand Down