Run each .xson/.bson test as a separate test method.

This commit is contained in:
Jim Menard 2009-01-12 11:41:39 -05:00
parent 4258f6f200
commit 921030c0f8
1 changed files with 27 additions and 23 deletions

View File

@ -33,6 +33,27 @@ class RoundTripTest < Test::Unit::TestCase
assert true
end
def self.create_test_for_round_trip_files_in_dir(dir)
names = Dir[File.join(dir, '*.xson')].collect {|f| File.basename(f).sub(/\.xson$/, '') }
names.each { |name|
eval <<EOS
def test_#{name}_#{dir.gsub(/[^a-zA-Z0-9_]/, '_')}
one_round_trip("#{dir}", "#{name}")
end
EOS
}
end
# Dynamically generate one test for each test file. This way, if one test
# fails the others will still run.
create_test_for_round_trip_files_in_dir(File.join(HERE, 'data'))
mongo_qa_dir = File.join(HERE, '..', 'mongo-qa/modules/bson_tests')
if File.exist?(mongo_qa_dir)
%w(basic_types complex single_types).each { |subdir_name|
create_test_for_round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
}
end
# Round-trip comparisons of Ruby-to-BSON and back.
# * Take the objects that were read from XML
# * Turn them into BSON bytes
@ -41,21 +62,6 @@ class RoundTripTest < Test::Unit::TestCase
# * Turn them back into BSON bytes
# * Compare that with the BSON files we have (or the bytes that were already
# generated)
def test_round_trip
round_trip_files_in_dir(File.join(HERE, 'data'))
mongo_qa_dir = File.join(HERE, '..', 'mongo-qa/modules/bson_tests')
if File.exist?(mongo_qa_dir)
%w(basic_types complex single_types).each { |subdir_name|
round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
}
end
end
def round_trip_files_in_dir(dir)
names = Dir[File.join(dir, '*.xson')].collect {|f| File.basename(f).sub(/\.xson$/, '') }
names.each { |name| one_round_trip(dir, name) }
end
def one_round_trip(dir, name)
obj = File.open(File.join(dir, "#{name}.xson")) { |f|
XMLToRuby.new.xml_to_ruby(f)
@ -78,10 +84,9 @@ class RoundTripTest < Test::Unit::TestCase
assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby
rescue => ex
$stderr.puts "failure while round-tripping (A) #{dir}/#{name}" # DEBUG
File.open(File.join(dir, "#{name}_out_a.bson"), 'wb') { |f| # DEBUG
bson_from_ruby.each { |b| f.putc(b) }
}
# File.open(File.join(dir, "#{name}_out_a.bson"), 'wb') { |f| # DEBUG
# bson_from_ruby.each { |b| f.putc(b) }
# }
raise ex
end
@ -100,10 +105,9 @@ class RoundTripTest < Test::Unit::TestCase
assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby
rescue => ex
$stderr.puts "failure while round-tripping (B) #{dir}/#{name}" # DEBUG
File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| # DEBUG
bson_from_ruby.each { |b| f.putc(b) }
}
# File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| DEBUG
# bson_from_ruby.each { |b| f.putc(b) }
# }
raise ex
end
}