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

Add CSV::TSV class for tab-separated values #319

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,12 @@ def initialize(data,
writer if @writer_options[:write_headers]
end

class TSV < CSV
def initialize(data, **options)
super(data, **({col_sep: "\t"}.merge(options)))
end
end

# :call-seq:
# csv.col_sep -> string
#
Expand Down
26 changes: 25 additions & 1 deletion run-test.rb
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to change this file?
I think that we don't need to change this file but if you think that we need to change this file, could you open a separated PR?

Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
#!/usr/bin/env ruby

# Disable Ractor experimental warning
Warning[:experimental] = false

$VERBOSE = true

$LOAD_PATH.unshift("test")
$LOAD_PATH.unshift("test/lib")
$LOAD_PATH.unshift("lib")

require "test/unit"

# Configure test-unit for better stability
Test::Unit::AutoRunner.need_auto_run = false

# Track test execution status
failed_tests = []

Dir.glob("test/csv/**/*test_*.rb") do |test_rb|
# Ensure we only load syntax that we can handle
next if RUBY_VERSION < "2.7" && test_rb.end_with?("test_patterns.rb")

require File.expand_path(test_rb)
begin
require File.expand_path(test_rb)
rescue => e
puts "Error loading #{test_rb}: #{e.message}"
puts e.backtrace
failed_tests << test_rb
end
end

# Run tests with custom configuration
runner = Test::Unit::AutoRunner.new(true)
runner.process_args([])

# Exit with failure if any tests failed
exit(failed_tests.empty? && runner.run ? 0 : 1)
32 changes: 32 additions & 0 deletions test/csv/test_tsv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative "helper"

class TestTSV < Test::Unit::TestCase
def test_default_separator
tsv = TSV.new(String.new)
jsxs0 marked this conversation as resolved.
Show resolved Hide resolved
assert_equal("\t", tsv.col_sep)
end

def test_override_separator
tsv = TSV.new(String.new, col_sep: ",")
assert_equal(",", tsv.col_sep)
end

def test_read_tsv_data
data = "a\tb\tc\n1\t2\t3"
result = TSV.parse(data)
assert_equal([["a", "b", "c"], ["1", "2", "3"]], result.to_a)
end

def test_write_tsv_data
output = String.new
TSV.generate(output) do |tsv|
tsv << ["a", "b", "c"]
tsv << ["1", "2", "3"]
end
assert_equal("a\tb\tc\n1\t2\t3\n", output)
end

def test_inheritance
assert_kind_of(CSV, TSV.new(String.new))
end
end
Loading