Skip to content

Commit

Permalink
Merge pull request #2002 from tk0miya/csv_read
Browse files Browse the repository at this point in the history
stdlib: Update signature of CSV.read
  • Loading branch information
soutaro committed Sep 13, 2024
2 parents ff96e83 + 7e86e10 commit 102d95c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stdlib/csv/0/csv.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,8 @@ class CSV < Object
# File.write(path, string)
# CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
#
def self.read: (String path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]
def self.read: (String | IO path, headers: true | :first_row | Array[untyped] | String, **untyped options) -> ::CSV::Table[CSV::Row]
| (String | IO path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]

# <!--
# rdoc-file=lib/csv.rb
Expand Down
28 changes: 28 additions & 0 deletions test/stdlib/CSV_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,32 @@ def test_foreach
assert_send_type "(String path, headers: bool, **untyped) -> Enumerator[CSV::Row, void]",
CSV, :foreach, path, headers: true, encoding: 'UTF-8'
end

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

assert_send_type "(String path, headers: true) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: true
assert_send_type "(IO path, headers: true) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: true
assert_send_type "(String path, headers: :first_row) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: :first_row
assert_send_type "(IO path, headers: :first_row) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: :first_row
assert_send_type "(String path, headers: Array[String]) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: %w[foo bar baz]
assert_send_type "(IO path, headers: Array[String]) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: %w[foo bar baz]
assert_send_type "(String path, headers: String) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: "foo,bar,baz"
assert_send_type "(IO path, headers: String) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: "foo,bar,baz"

assert_send_type "(String path) -> Array[Array[String?]]",
CSV, :read, path
assert_send_type "(IO path) -> Array[Array[String?]]",
CSV, :read, File.open(path)
end
end

0 comments on commit 102d95c

Please sign in to comment.