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

stdlib: csv: Add types for Array#to_csv and String#parse_csv #2099

Merged
merged 1 commit into from
Dec 19, 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
18 changes: 18 additions & 0 deletions stdlib/csv/0/csv.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3756,3 +3756,21 @@ class CSV::Table[out Elem] < Object
#
def values_at: (*untyped indices_or_headers) -> untyped
end

%a{annotate:rdoc:skip}
class Array[unchecked out Elem] < Object
# Equivalent to CSV::generate_line(self, options)
Copy link
Member

Choose a reason for hiding this comment

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

The source code has docs, but opted out. Opened a PR to include them to the doc. ruby/csv#322

#
# ["CSV", "data"].to_csv
# #=> "CSV,data\n"
def to_csv: (**untyped options) -> String
end

%a{annotate:rdoc:skip}
class String
# Equivalent to CSV::parse_line(self, options)
#
# "CSV,data".parse_csv
# #=> ["CSV", "data"]
def parse_csv: (**untyped options) -> ::Array[String?]?
end
28 changes: 28 additions & 0 deletions test/stdlib/CSV_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,31 @@ def test_headers
csv, :headers
end
end

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

library "csv"
testing "Array[untyped]"

def test_to_csv_with_array
assert_send_type "() -> String",
[1, 2, 3], :to_csv
assert_send_type "(**untyped) -> String",
[1, 2, 3], :to_csv, col_sep: '\t'
end
end

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

library "csv"
testing "String"

def test_parse_csv_with_string
assert_send_type "() -> Array[String?]",
"1,2,3", :parse_csv
assert_send_type "(**untyped) -> Array[String?]",
"1,2,3", :parse_csv, col_sep: '\t'
end
end
Loading