diff options
author | jeg2 <jeg2@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-18 15:30:24 +0000 |
---|---|---|
committer | jeg2 <jeg2@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-18 15:30:24 +0000 |
commit | b7be78d774daf5b9a9fdd63e6d20e30309b9efa0 (patch) | |
tree | f7272b9cfeace2b5ee9a3a283c0085ad15b398a0 /test/csv/test_serialization.rb | |
parent | 54e2cb81799f0ff7e443758b50e906ea5b1833d3 (diff) | |
download | ruby-b7be78d774daf5b9a9fdd63e6d20e30309b9efa0.tar.gz |
* test/csv/*: Renamed tc_* files to test_* to fit in within Ruby's
testing process.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19843 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/csv/test_serialization.rb')
-rw-r--r-- | test/csv/test_serialization.rb | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/test/csv/test_serialization.rb b/test/csv/test_serialization.rb new file mode 100644 index 0000000000..878619f58a --- /dev/null +++ b/test/csv/test_serialization.rb @@ -0,0 +1,156 @@ +#!/usr/bin/env ruby -w +# encoding: UTF-8 + +# tc_serialization.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" + +# An example of how to provide custom CSV serialization. +class Hash + def self.csv_load( meta, headers, fields ) + self[*headers.zip(fields).to_a.flatten.map { |e| eval(e) }] + end + + def csv_headers + keys.map { |key| key.inspect } + end + + def csv_dump( headers ) + headers.map { |header| fetch(eval(header)).inspect } + end +end + +class TestSerialization < Test::Unit::TestCase + + ### Classes Used to Test Serialization ### + + class ReadOnlyName + def initialize( first, last ) + @first, @last = first, last + end + + attr_reader :first, :last + + def ==( other ) + %w{first last}.all? { |att| send(att) == other.send(att) } + end + end + + Name = Struct.new(:first, :last) + + class FullName < Name + def initialize( first, last, suffix = nil ) + super(first, last) + + @suffix = suffix + end + + attr_accessor :suffix + + def ==( other ) + %w{first last suffix}.all? { |att| send(att) == other.send(att) } + end + end + + ### Tests ### + + def test_class_dump + @names = [ %w{James Gray}, + %w{Dana Gray}, + %w{Greg Brown} ].map do |first, last| + ReadOnlyName.new(first, last) + end + + assert_nothing_raised(Exception) do + @data = CSV.dump(@names) + end + assert_equal(<<-END_CLASS_DUMP.gsub(/^\s*/, ""), @data) + class,TestSerialization::ReadOnlyName + @first,@last + James,Gray + Dana,Gray + Greg,Brown + END_CLASS_DUMP + end + + def test_struct_dump + @names = [ %w{James Gray}, + %w{Dana Gray}, + %w{Greg Brown} ].map do |first, last| + Name.new(first, last) + end + + assert_nothing_raised(Exception) do + @data = CSV.dump(@names) + end + assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data) + class,TestSerialization::Name + first=,last= + James,Gray + Dana,Gray + Greg,Brown + END_STRUCT_DUMP + end + + def test_inherited_struct_dump + @names = [ %w{James Gray II}, + %w{Dana Gray}, + %w{Greg Brown} ].map do |first, last, suffix| + FullName.new(first, last, suffix) + end + + assert_nothing_raised(Exception) do + @data = CSV.dump(@names) + end + assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data) + class,TestSerialization::FullName + @suffix,first=,last= + II,James,Gray + ,Dana,Gray + ,Greg,Brown + END_STRUCT_DUMP + end + + def test_load + %w{ test_class_dump + test_struct_dump + test_inherited_struct_dump }.each do |test| + send(test) + CSV.load(@data).each do |loaded| + assert_instance_of(@names.first.class, loaded) + assert_equal(@names.shift, loaded) + end + end + end + + def test_io + test_class_dump + + data_file = File.join(File.dirname(__FILE__), "temp_test_data.csv") + CSV.dump(@names, File.open(data_file, "w")) + + assert(File.exist?(data_file)) + assert_equal(<<-END_IO_DUMP.gsub(/^\s*/, ""), File.read(data_file)) + class,TestSerialization::ReadOnlyName + @first,@last + James,Gray + Dana,Gray + Greg,Brown + END_IO_DUMP + + assert_equal(@names, CSV.load(File.open(data_file))) + + File.unlink(data_file) + end + + def test_custom_dump_and_load + obj = {1 => "simple", test: Hash} + assert_equal(obj, CSV.load(CSV.dump([obj])).first) + end +end |