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

Update type for CSV.foreach #1738

Merged
merged 3 commits into from
Jan 29, 2024
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gem "rbs-amber", path: "test/assets/test-gem"

# Bundled gems
gem "net-smtp"
gem 'csv'

group :minitest do
gem "minitest"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GEM
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.6)
csv (3.2.8)
Copy link
Contributor Author

@m11o m11o Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add csv gem to Gemfile because it is changed from default gem to bundled gem in ruby 3.4.
ref: #1734

dbm (1.1.0)
diff-lcs (1.5.0)
digest (3.1.1)
Expand Down Expand Up @@ -102,6 +103,7 @@ DEPENDENCIES
abbrev
base64
bigdecimal
csv
dbm
digest
fileutils
Expand Down
5 changes: 4 additions & 1 deletion stdlib/csv/0/csv.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,10 @@ class CSV < Object
# would read `UTF-32BE` data from the file but transcode it to `UTF-8`
# before parsing.
#
def self.foreach: [U] (String | IO | StringIO path, ?::Hash[Symbol, U] options) { (::Array[String?] arg0) -> void } -> void
def self.foreach: (String | IO path, ?String mode, headers: true, **untyped options) { (::CSV::Row arg0) -> void } -> void
Copy link
Contributor Author

@m11o m11o Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accurately, the headers keyword argument can't be only received true, but also truthy value, for example, String and Symbol, etc.

However, As headers argument is often provided true, I defined true only in this methods.
Please let me know if not good. Thanks.

| (String | IO path, ?String mode, headers: true, **untyped options) -> Enumerator[::CSV::Row, void]
| (String | IO path, ?String mode, **untyped options) { (::Array[String?] arg0) -> void } -> void
| (String | IO path, ?String mode, **untyped options) -> Enumerator[::Array[String?], void]

# <!--
# rdoc-file=lib/csv.rb
Expand Down
38 changes: 38 additions & 0 deletions test/stdlib/CSV_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require_relative "test_helper"
require "csv"

class CSVSingletonTest < Test::Unit::TestCase
include TestHelper

library 'csv'
testing "singleton(::CSV)"

def test_foreach
tmpdir = Dir.mktmpdir
path = File.join(tmpdir, "example.csv")
File.write(path, "a,b,c\n1,2,3\n")

string_array_block = ->(row) { row.size }
assert_send_type "(String path) { (Array[String?]) -> void } -> void",
CSV, :foreach, path, &string_array_block
assert_send_type "(IO path) { (Array[String?]) -> void } -> void",
CSV, :foreach, File.open(path), &string_array_block

csv_row_array_block = ->(row) { row.fields }
assert_send_type "(String path, headers: true) { (CSV::Row) -> void } -> void",
CSV, :foreach, path, headers: true, &csv_row_array_block
assert_send_type "(String path, headers: false) { (Array[String?]) -> void } -> void",
CSV, :foreach, path, headers: false, &string_array_block
assert_send_type "(IO path, headers: bool) { (CSV::Row) -> void } -> void",
CSV, :foreach, File.open(path), headers: true, &csv_row_array_block

assert_send_type "(String path, **untyped) -> Enumerator[Array[String?], void]",
CSV, :foreach, path, encoding: 'UTF-8'
assert_send_type "(String path, headers: bool) -> Enumerator[CSV::Row, void]",
CSV, :foreach, path, headers: true
assert_send_type "(String path, headers: bool, **untyped) -> Enumerator[CSV::Row, void]",
CSV, :foreach, path, headers: true, encoding: 'UTF-8'
end
end