diff options
author | jeg2 <jeg2@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-25 02:46:26 +0000 |
---|---|---|
committer | jeg2 <jeg2@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-25 02:46:26 +0000 |
commit | 29ba327bd16fd7eb7114050724e4784bdcaac83a (patch) | |
tree | 2779c5d0c96275f397aaaa286686479ef4f83649 /test/csv/tc_csv_writing.rb | |
parent | 0d570142ba2b7488804729b3cc1a9b4d87db1014 (diff) | |
download | ruby-29ba327bd16fd7eb7114050724e4784bdcaac83a.tar.gz |
* lib/csv.rb: Import the FasterCSV source as the new CSV class.
* test/csv/*: Added all applicable tests from FasterCSV.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14646 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/csv/tc_csv_writing.rb')
-rw-r--r-- | test/csv/tc_csv_writing.rb | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/test/csv/tc_csv_writing.rb b/test/csv/tc_csv_writing.rb new file mode 100644 index 0000000000..4677284306 --- /dev/null +++ b/test/csv/tc_csv_writing.rb @@ -0,0 +1,96 @@ +#!/usr/local/bin/ruby -w + +# tc_csv_writing.rb +# +# Created by James Edward Gray II on 2005-10-31. +# Copyright 2005 James Edward Gray II. You can redistribute or modify this code +# under the terms of Ruby's license. + +require "test/unit" + +require "csv" + +class TestCSVWriting < Test::Unit::TestCase + def test_writing + [ ["\t", ["\t"]], + ["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]], + ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]], + ["\"\"\"\n\",\"\"\"\n\"", ["\"\n", "\"\n"]], + ["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]], + ["\"\"", [""]], + ["foo,\"\"\"\",baz", ["foo", "\"", "baz"]], + ["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]], + ["foo,\"\r\",baz", ["foo", "\r", "baz"]], + ["foo,\"\",baz", ["foo", "", "baz"]], + ["\",\"", [","]], + ["foo", ["foo"]], + [",,", [nil, nil, nil]], + [",", [nil, nil]], + ["foo,\"\n\",baz", ["foo", "\n", "baz"]], + ["foo,,baz", ["foo", nil, "baz"]], + ["\"\"\"\r\",\"\"\"\r\"", ["\"\r", "\"\r"]], + ["\",\",\",\"", [",", ","]], + ["foo,bar,", ["foo", "bar", nil]], + [",foo,bar", [nil, "foo", "bar"]], + ["foo,bar", ["foo", "bar"]], + [";", [";"]], + ["\t,\t", ["\t", "\t"]], + ["foo,\"\r\n\r\",baz", ["foo", "\r\n\r", "baz"]], + ["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]], + ["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]], + [";,;", [";", ";"]], + ["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]], + ["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]], + ["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]], + ["\"\"", [""]], + ["foo,\"\"\"\",baz", ["foo", "\"", "baz"]], + ["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]], + ["foo,\"\r\",baz", ["foo", "\r", "baz"]], + ["foo,\"\",baz", ["foo", "", "baz"]], + ["foo", ["foo"]], + [",,", [nil, nil, nil]], + [",", [nil, nil]], + ["foo,\"\n\",baz", ["foo", "\n", "baz"]], + ["foo,,baz", ["foo", nil, "baz"]], + ["foo,bar", ["foo", "bar"]], + ["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]], + ["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]], + [%Q{a,b}, ["a", "b"]], + [%Q{a,"""b"""}, ["a", "\"b\""]], + [%Q{a,"""b"}, ["a", "\"b"]], + [%Q{a,"b"""}, ["a", "b\""]], + [%Q{a,"\nb"""}, ["a", "\nb\""]], + [%Q{a,"""\nb"}, ["a", "\"\nb"]], + [%Q{a,"""\nb\n"""}, ["a", "\"\nb\n\""]], + [%Q{a,"""\nb\n""",}, ["a", "\"\nb\n\"", nil]], + [%Q{a,,,}, ["a", nil, nil, nil]], + [%Q{,}, [nil, nil]], + [%Q{"",""}, ["", ""]], + [%Q{""""}, ["\""]], + [%Q{"""",""}, ["\"",""]], + [%Q{,""}, [nil,""]], + [%Q{,"\r"}, [nil,"\r"]], + [%Q{"\r\n,"}, ["\r\n,"]], + [%Q{"\r\n,",}, ["\r\n,", nil]] ].each do |test_case| + assert_equal(test_case.first + $/, CSV.generate_line(test_case.last)) + end + end + + def test_col_sep + assert_equal( "a;b;;c\n", CSV.generate_line( ["a", "b", nil, "c"], + :col_sep => ";" ) ) + assert_equal( "a\tb\t\tc\n", CSV.generate_line( ["a", "b", nil, "c"], + :col_sep => "\t" ) ) + end + + def test_row_sep + assert_equal( "a,b,,c\r\n", CSV.generate_line( ["a", "b", nil, "c"], + :row_sep => "\r\n" ) ) + end + + def test_force_quotes + assert_equal( %Q{"1","b","","already ""quoted"""\n}, + CSV.generate_line( [1, "b", nil, %Q{already "quoted"}], + :force_quotes => true ) ) + end +end |