tc_serialization.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/local/bin/ruby -w
  2. # tc_serialization.rb
  3. #
  4. # Created by James Edward Gray II on 2006-03-28.
  5. # Copyright 2012 Gray Productions. All rights reserved.
  6. require "test/unit"
  7. require "faster_csv"
  8. # An example of how to provide custom CSV serialization.
  9. class Hash
  10. def self.csv_load( meta, headers, fields )
  11. self[*headers.zip(fields).flatten.map { |e| eval(e) }]
  12. end
  13. def csv_headers
  14. keys.map { |key| key.inspect }
  15. end
  16. def csv_dump( headers )
  17. headers.map { |header| fetch(eval(header)).inspect }
  18. end
  19. end
  20. class TestSerialization < Test::Unit::TestCase
  21. ### Classes Used to Test Serialization ###
  22. class ReadOnlyName
  23. def initialize( first, last )
  24. @first, @last = first, last
  25. end
  26. attr_reader :first, :last
  27. def ==( other )
  28. %w{first last}.all? { |att| send(att) == other.send(att) }
  29. end
  30. end
  31. Name = Struct.new(:first, :last)
  32. class FullName < Name
  33. def initialize( first, last, suffix = nil )
  34. super(first, last)
  35. @suffix = suffix
  36. end
  37. attr_accessor :suffix
  38. def ==( other )
  39. %w{first last suffix}.all? { |att| send(att) == other.send(att) }
  40. end
  41. end
  42. ### Tests ###
  43. def test_class_dump
  44. @names = [ %w{James Gray},
  45. %w{Dana Gray},
  46. %w{Greg Brown} ].map do |first, last|
  47. ReadOnlyName.new(first, last)
  48. end
  49. assert_nothing_raised(Exception) do
  50. @data = FasterCSV.dump(@names)
  51. end
  52. assert_equal(<<-END_CLASS_DUMP.gsub(/^\s*/, ""), @data)
  53. class,TestSerialization::ReadOnlyName
  54. @first,@last
  55. James,Gray
  56. Dana,Gray
  57. Greg,Brown
  58. END_CLASS_DUMP
  59. end
  60. def test_struct_dump
  61. @names = [ %w{James Gray},
  62. %w{Dana Gray},
  63. %w{Greg Brown} ].map do |first, last|
  64. Name.new(first, last)
  65. end
  66. assert_nothing_raised(Exception) do
  67. @data = FasterCSV.dump(@names)
  68. end
  69. assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
  70. class,TestSerialization::Name
  71. first=,last=
  72. James,Gray
  73. Dana,Gray
  74. Greg,Brown
  75. END_STRUCT_DUMP
  76. end
  77. def test_inherited_struct_dump
  78. @names = [ %w{James Gray II},
  79. %w{Dana Gray},
  80. %w{Greg Brown} ].map do |first, last, suffix|
  81. FullName.new(first, last, suffix)
  82. end
  83. assert_nothing_raised(Exception) do
  84. @data = FasterCSV.dump(@names)
  85. end
  86. assert_equal(<<-END_STRUCT_DUMP.gsub(/^\s*/, ""), @data)
  87. class,TestSerialization::FullName
  88. @suffix,first=,last=
  89. II,James,Gray
  90. ,Dana,Gray
  91. ,Greg,Brown
  92. END_STRUCT_DUMP
  93. end
  94. def test_load
  95. %w{ test_class_dump
  96. test_struct_dump
  97. test_inherited_struct_dump }.each do |test|
  98. send(test)
  99. FasterCSV.load(@data).each do |loaded|
  100. assert_instance_of(@names.first.class, loaded)
  101. assert_equal(@names.shift, loaded)
  102. end
  103. end
  104. end
  105. def test_io
  106. test_class_dump
  107. data_file = File.join(File.dirname(__FILE__), "temp_test_data.csv")
  108. FasterCSV.dump(@names, File.open(data_file, "w"))
  109. assert(File.exist?(data_file))
  110. assert_equal(<<-END_IO_DUMP.gsub(/^\s*/, ""), File.read(data_file))
  111. class,TestSerialization::ReadOnlyName
  112. @first,@last
  113. James,Gray
  114. Dana,Gray
  115. Greg,Brown
  116. END_IO_DUMP
  117. assert_equal(@names, FasterCSV.load(File.open(data_file)))
  118. File.unlink(data_file)
  119. end
  120. def test_custom_dump_and_load
  121. obj = {1 => "simple", :test => Hash}
  122. assert_equal(obj, FasterCSV.load(FasterCSV.dump([obj])).first)
  123. end
  124. end