Skip to content

Commit

Permalink
Merge pull request #2099 from tk0miya/csv/Array.to_csv
Browse files Browse the repository at this point in the history
stdlib: csv: Add types for Array#to_csv and String#parse_csv
  • Loading branch information
soutaro authored Dec 19, 2024
2 parents da70891 + 153451c commit 55cede4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
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)
#
# ["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

0 comments on commit 55cede4

Please sign in to comment.