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

#getss, #readlines and #readliness *also* accept chomp: true #2212

Merged
merged 5 commits into from
Feb 27, 2025
Merged
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
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ end

task :stdlib_test => :compile do
test_files = FileList["test/stdlib/**/*_test.rb"].reject do |path|
path =~ %r{Ractor} || path =~ %r{Encoding}
path =~ %r{Ractor} || path =~ %r{Encoding} || path =~ %r{CGI_test}
end

if ENV["RANDOMIZE_STDLIB_TEST_ORDER"] == "true"
Expand All @@ -121,6 +121,7 @@ task :stdlib_test => :compile do

sh "#{ruby} -Ilib #{bin}/test_runner.rb #{test_files.join(' ')}"
# TODO: Ractor tests need to be run in a separate process
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/CGI_test.rb"
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Ractor_test.rb"
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Encoding_test.rb"
end
Expand Down
4 changes: 2 additions & 2 deletions core/kernel.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ module Kernel : BasicObject
# The style of programming using `$_` as an implicit parameter is gradually
# losing favor in the Ruby community.
#
def self?.gets: (?String arg0, ?Integer arg1) -> String?
def self?.gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?

# <!--
# rdoc-file=eval.c
Expand Down Expand Up @@ -1486,7 +1486,7 @@ module Kernel : BasicObject
# Optional keyword argument `chomp` specifies whether line separators are to be
# omitted.
#
def self?.readline: (?String arg0, ?Integer arg1) -> String
def self?.readline: (?String arg0, ?Integer arg1, ?chomp: boolish) -> String

# <!--
# rdoc-file=io.c
Expand Down
6 changes: 3 additions & 3 deletions core/rbs/unnamed/argf.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ module RBS
# See IO.readlines for details about getline_args.
#
%a{annotate:rdoc:copy:ARGF#gets}
def gets: (?String sep, ?Integer limit) -> String?
def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?

# <!--
# rdoc-file=io.c
Expand Down Expand Up @@ -1024,7 +1024,7 @@ module RBS
# An EOFError is raised at the end of the file.
#
%a{annotate:rdoc:copy:ARGF#readline}
def readline: (?String sep, ?Integer limit) -> String
def readline: (?String sep, ?Integer limit, ?chomp: boolish) -> String

# <!--
# rdoc-file=io.c
Expand All @@ -1044,7 +1044,7 @@ module RBS
# See `IO.readlines` for a full description of all options.
#
%a{annotate:rdoc:copy:ARGF#readlines}
def readlines: (?String sep, ?Integer limit) -> ::Array[String]
def readlines: (?String sep, ?Integer limit, ?chomp: boolish) -> ::Array[String]

# <!--
# rdoc-file=io.c
Expand Down
2 changes: 1 addition & 1 deletion stdlib/openssl/0/openssl.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ module OpenSSL
#
# Unlike IO#gets the separator must be provided if a limit is provided.
#
def gets: (?String | Regexp eol, ?Integer limit) -> String?
def gets: (?String | Regexp eol, ?Integer limit, ?chomp: boolish) -> String?

# <!--
# rdoc-file=ext/openssl/lib/openssl/buffering.rb
Expand Down
2 changes: 1 addition & 1 deletion stdlib/stringio/0/stringio.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class StringIO

def readchar: () -> String

def readline: (?String sep, ?Integer limit) -> String
def readline: (?String sep, ?Integer limit, ?chomp: boolish) -> String

# <!--
# rdoc-file=ext/stringio/stringio.c
Expand Down
12 changes: 12 additions & 0 deletions test/stdlib/ARGF_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def test_gets
ARGF.class.new(__FILE__), :gets, "\n"
assert_send_type "(::String sep, ::Integer limit) -> ::String",
ARGF.class.new(__FILE__), :gets, "\n", 1
assert_send_type "(chomp: boolish) -> ::String",
ARGF.class.new(__FILE__), :gets, chomp: true
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::String",
ARGF.class.new(__FILE__), :gets, "\n", 1, chomp: true
assert_send_type "() -> nil",
ARGF.class.new(Tempfile.new), :gets
end
Expand Down Expand Up @@ -60,6 +64,10 @@ def test_readline
ARGF.class.new(__FILE__), :readline, "\n"
assert_send_type "(::String sep, ::Integer limit) -> ::String",
ARGF.class.new(__FILE__), :readline, "\n", 1
assert_send_type "(chomp: boolish) -> ::String",
ARGF.class.new(__FILE__), :readline, chomp: true
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::String",
ARGF.class.new(__FILE__), :readline, "\n", 1, chomp: true
end

def test_readlines
Expand All @@ -69,6 +77,10 @@ def test_readlines
ARGF.class.new(__FILE__), :readlines, "\n"
assert_send_type "(::String sep, ::Integer limit) -> ::Array[::String]",
ARGF.class.new(__FILE__), :readlines, "\n", 1
assert_send_type "(chomp: boolish) -> ::Array[::String]",
ARGF.class.new(__FILE__), :readlines, chomp: true
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::Array[::String]",
ARGF.class.new(__FILE__), :readlines, "\n", 1, chomp: true
end

def test_inspect
Expand Down
21 changes: 21 additions & 0 deletions test/stdlib/Kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,25 @@ def test_raise
end
end
end

def test_readlines
$stdin = File.open(__FILE__)

assert_send_type(
"() -> Array[String]",
JustKernel.new, :readlines
)

with_int(3) do |limit|
with_string(",") do |separator|
$stdin = File.open(__FILE__)
assert_send_type(
"(string, int, chomp: bool) -> Array[String]",
JustKernel.new, :readlines, ",", 3, chomp: true
)
end
end
ensure
$stdin = STDIN
end
end
26 changes: 26 additions & 0 deletions test/stdlib/StringIO_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,30 @@ def test_truncate
io, :truncate, 10
)
end

def test_readline
assert_send_type "() -> ::String",
StringIO.new("\n"), :readline
assert_send_type "(::String sep) -> ::String",
StringIO.new("\n"), :readline, "\n"
assert_send_type "(::String sep, ::Integer limit) -> ::String",
StringIO.new("\n"), :readline, "\n", 1
assert_send_type "(chomp: boolish) -> ::String",
StringIO.new("\n"), :readline, chomp: true
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::String",
StringIO.new("\n"), :readline, "\n", 1, chomp: true
end

def test_readlines
assert_send_type "() -> ::Array[::String]",
StringIO.new("\n"), :readlines
assert_send_type "(::String sep) -> ::Array[::String]",
StringIO.new("\n"), :readlines, "\n"
assert_send_type "(::String sep, ::Integer limit) -> ::Array[::String]",
StringIO.new("\n"), :readlines, "\n", 1
assert_send_type "(chomp: boolish) -> ::Array[::String]",
StringIO.new("\n"), :readlines, chomp: true
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::Array[::String]",
StringIO.new("\n"), :readlines, "\n", 1, chomp: true
end
end