Skip to content

Commit

Permalink
more io updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sampersand committed Dec 26, 2023
1 parent 3284d3b commit 872d831
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
14 changes: 8 additions & 6 deletions core/io.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3278,7 +3278,7 @@ class IO
# f.read # => "BCDE012"
# f.close
#
def ungetbyte: (Integer | string | nil object) -> nil
def ungetbyte: (Integer | string | nil byte) -> nil

# <!--
# rdoc-file=io.c
Expand Down Expand Up @@ -3321,7 +3321,7 @@ class IO
# f.getc.ord # => 1090
# f.close
#
def ungetc: (Integer | string object) -> nil
def ungetc: (Integer | string char) -> nil

# <!--
# rdoc-file=io.c
Expand Down Expand Up @@ -3401,7 +3401,6 @@ class IO
def write_nonblock: (_ToS string, ?exception: true) -> Integer
| (_ToS string, exception: false) -> (Integer | :wait_writable)


# <!--
# rdoc-file=ext/io/wait/wait.c
# - io.wait(events, timeout) -> event mask, false or nil
Expand Down Expand Up @@ -3435,7 +3434,8 @@ class IO
# available.
#
%a{ruby:since:3.2.0}
def wait_readable: (?Time::_Timeout? timeout) -> (self | bool)?
def wait_readable: (?nil) -> (self | bool)
| (Time::_Timeout timeout) -> (self | bool)?

# <!--
# rdoc-file=ext/io/wait/wait.c
Expand All @@ -3446,8 +3446,10 @@ class IO
# times out.
#
%a{ruby:since:3.2.0}
def wait_writable: (?Time::_Timeout? timeout) -> (self | bool)?
def wait_writable: (?nil) -> (self | bool)
| (Time::_Timeout timeout) -> (self | bool)?

%a{ruby:since:3.2.0}
def wait_priority: (?Time::_Timeout? timeout) -> (self | bool)?
def wait_priority: (?nil) -> (self | bool)
| (Time::_Timeout timeout) -> (self | bool)?
end
18 changes: 13 additions & 5 deletions test/stdlib/IO_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ def test_wait
IO.pipe do |read, write|
with_int IO::READABLE do |events|
with_timeout(seconds: 0, nanoseconds: 500).and_nil do |timeout|
write.puts "Hello"
write.puts 'Hello'
assert_send_type '(int, Time::_Timeout?) -> Integer',
read, :wait, events, timeout
read.gets
Expand All @@ -1329,15 +1329,15 @@ def test_wait
%i[r read readable].each do |event|
with_timeout(seconds: 0, nanoseconds: 500) do |timeout|
assert_send_type '(*IO::wait_mode | Time::_Timeout) -> nil',
io, :wait, event, timeout, event
read, :wait, event, timeout, event
end
end

%i[r read readable w write writable rw read_write readable_writable].each do |event|
with_timeout(seconds: 0, nanoseconds: 500) do |timeout|
write.puts "hello"
write.puts 'hello'
assert_send_type '(*IO::wait_mode | Time::_Timeout) -> IO',
io, :wait, event, timeout, event
read, :wait, event, timeout, event
read.gets
end
end
Expand All @@ -1347,7 +1347,15 @@ def test_wait
# the signature as `bool`, not `true`.
read.ungetc '&'
assert_send_type '(*IO::wait_mode | Time::_Timeout) -> true',
io, :wait, :read, 0, :read
read, :wait, :read, 0, :read
end
end

def test_wait_writable
omit 'todo: test wait_writable'
end

def test_wait_priority
omit 'todo: test wait_priority'
end
end
6 changes: 4 additions & 2 deletions test/stdlib/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ def if_ruby31(&block)

module WithStdlibAliases
include RBS::UnitTest::Convertibles
include RBS::UnitTest::WithAliases

def with_timeout(seconds: 1, nanoseconds: 0)
unless block_given?
return WithAliases::WithEnum.new to_enum(__method__, seconds: seconds, nanoseconds: nanoseconds)
return WithEnum.new to_enum(__method__, seconds: seconds, nanoseconds: nanoseconds)
end

timeout_obj = BlankSlate.new.__with_object_methods(:define_singleton_method)
Expand All @@ -80,7 +82,7 @@ def with_timeout(seconds: 1, nanoseconds: 0)
end

def with_to_s(&block)
return WithAliases::WithEnum.new to_enum(__method__ || raise) unless block
return WithEnum.new to_enum(__method__ || raise) unless block
with_bool(&block)
[nil, 1, Object.new, ToS.new, "hello, world!"].each(&block)
end
Expand Down

0 comments on commit 872d831

Please sign in to comment.