From 153451c0aff0a419db08586295949bfba0da772c Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 26 Nov 2024 20:51:22 +0900 Subject: [PATCH] stdlib: csv: Add types for Array#to_csv and String#parse_csv refs: * https://www.rubydoc.info/gems/csv/Array#to_csv-instance_method * https://www.rubydoc.info/gems/csv/String#parse_csv-instance_method --- stdlib/csv/0/csv.rbs | 18 ++++++++++++++++++ test/stdlib/CSV_test.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/stdlib/csv/0/csv.rbs b/stdlib/csv/0/csv.rbs index 6baa34312..d522a001a 100644 --- a/stdlib/csv/0/csv.rbs +++ b/stdlib/csv/0/csv.rbs @@ -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 diff --git a/test/stdlib/CSV_test.rb b/test/stdlib/CSV_test.rb index 3f3271139..dc0c3fb01 100644 --- a/test/stdlib/CSV_test.rb +++ b/test/stdlib/CSV_test.rb @@ -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