~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/csv/test_serialization.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
  def self.csv_load( meta, headers, fields )
17
17
    self[*headers.zip(fields).to_a.flatten.map { |e| eval(e) }]
18
18
  end
19
 
  
 
19
 
20
20
  def csv_headers
21
21
    keys.map { |key| key.inspect }
22
22
  end
23
 
  
 
23
 
24
24
  def csv_dump( headers )
25
25
    headers.map { |header| fetch(eval(header)).inspect }
26
26
  end
27
27
end
28
28
 
29
29
class TestSerialization < Test::Unit::TestCase
30
 
  
 
30
 
31
31
  ### Classes Used to Test Serialization ###
32
 
  
 
32
 
33
33
  class ReadOnlyName
34
34
    def initialize( first, last )
35
35
      @first, @last = first, last
36
36
    end
37
37
 
38
38
    attr_reader :first, :last
39
 
    
 
39
 
40
40
    def ==( other )
41
41
      %w{first last}.all? { |att| send(att) == other.send(att) }
42
42
    end
43
43
  end
44
44
 
45
45
  Name = Struct.new(:first, :last)
46
 
  
 
46
 
47
47
  class FullName < Name
48
48
    def initialize( first, last, suffix = nil )
49
49
      super(first, last)
50
 
      
 
50
 
51
51
      @suffix = suffix
52
52
    end
53
 
    
 
53
 
54
54
    attr_accessor :suffix
55
 
    
 
55
 
56
56
    def ==( other )
57
57
      %w{first last suffix}.all? { |att| send(att) == other.send(att) }
58
58
    end
59
59
  end
60
 
  
 
60
 
61
61
  ### Tests ###
62
62
 
63
63
  def test_class_dump
66
66
              %w{Greg Brown} ].map do |first, last|
67
67
      ReadOnlyName.new(first, last)
68
68
    end
69
 
    
70
 
    assert_nothing_raised(Exception) do 
 
69
 
 
70
    assert_nothing_raised(Exception) do
71
71
      @data = CSV.dump(@names)
72
72
    end
73
73
    assert_equal(<<-END_CLASS_DUMP.gsub(/^\s*/, ""), @data)
78
78
    Greg,Brown
79
79
    END_CLASS_DUMP
80
80
  end
81
 
  
 
81
 
82
82
  def test_struct_dump
83
83
    @names = [ %w{James Gray},
84
84
              %w{Dana Gray},
85
85
              %w{Greg Brown} ].map do |first, last|
86
86
      Name.new(first, last)
87
87
    end
88
 
    
89
 
    assert_nothing_raised(Exception) do 
 
88
 
 
89
    assert_nothing_raised(Exception) do
90
90
      @data = CSV.dump(@names)
91
91
    end
92
92
    assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
97
97
    Greg,Brown
98
98
    END_STRUCT_DUMP
99
99
  end
100
 
  
 
100
 
101
101
  def test_inherited_struct_dump
102
102
    @names = [ %w{James Gray II},
103
103
              %w{Dana Gray},
104
104
              %w{Greg Brown} ].map do |first, last, suffix|
105
105
      FullName.new(first, last, suffix)
106
106
    end
107
 
    
108
 
    assert_nothing_raised(Exception) do 
 
107
 
 
108
    assert_nothing_raised(Exception) do
109
109
      @data = CSV.dump(@names)
110
110
    end
111
111
    assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
116
116
    ,Greg,Brown
117
117
    END_STRUCT_DUMP
118
118
  end
119
 
  
 
119
 
120
120
  def test_load
121
121
    %w{ test_class_dump
122
122
        test_struct_dump
128
128
      end
129
129
    end
130
130
  end
131
 
  
 
131
 
132
132
  def test_io
133
133
    test_class_dump
134
 
    
 
134
 
135
135
    data_file = File.join(File.dirname(__FILE__), "temp_test_data.csv")
136
 
    CSV.dump(@names, File.open(data_file, "w"))
137
 
    
 
136
    CSV.dump(@names, File.open(data_file, "wb"))
 
137
 
138
138
    assert(File.exist?(data_file))
139
139
    assert_equal(<<-END_IO_DUMP.gsub(/^\s*/, ""), File.read(data_file))
140
140
    class,TestSerialization::ReadOnlyName
143
143
    Dana,Gray
144
144
    Greg,Brown
145
145
    END_IO_DUMP
146
 
    
 
146
 
147
147
    assert_equal(@names, CSV.load(File.open(data_file)))
148
 
    
 
148
 
149
149
    File.unlink(data_file)
150
150
  end
151
 
  
 
151
 
152
152
  def test_custom_dump_and_load
153
153
    obj = {1 => "simple", test: Hash}
154
154
    assert_equal(obj, CSV.load(CSV.dump([obj])).first)