test/helper.rb in write_xlsx-0.0.4 vs test/helper.rb in write_xlsx-0.51.0
- old
+ new
@@ -24,13 +24,12 @@
end
class Test::Unit::TestCase
def setup_dir_var
@test_dir = File.dirname(__FILE__)
- @expected_dir = File.join(@test_dir, 'expected_dir')
- @result_dir = File.join(@test_dir, 'result_dir')
@perl_output = File.join(@test_dir, 'perl_output')
+ @regression_output = File.join(@test_dir, 'regression', 'xlsx_files')
end
def expected_to_array(lines)
array = []
lines.each_line do |line|
@@ -39,68 +38,112 @@
end
array
end
def got_to_array(xml_str)
- str = xml_str.gsub(/[\r\n]/, '')
- str.gsub(/>[ \t\r\n]*</, ">\t<").split(/\t/)
+ xml_str.gsub(/[\r\n]/, '').
+ gsub(/ +\/>/, ' />').
+ gsub(/>[ \t]*</, ">\t<").
+ split("\t")
end
- def compare_xlsx(expected, result, xlsx)
- begin
- prepare_compare(expected, result, xlsx)
- expected_files = files(expected)
- result_files = files(result)
-
- not_exists = expected_files - result_files
- assert(not_exists.empty?, "These files does not exist: #{not_exists.to_s}")
-
- additional_exist = result_files - expected_files
- assert(additional_exist.empty?, "These files must not exist: #{additional_exist.to_s}")
-
- compare_files(expected, result)
- ensure
- cleanup(xlsx)
+ def vml_str_to_array(vml_str)
+ ret = ''
+ vml_str.split(/[\r\n]+/).each do |vml|
+ str = vml.sub(/^\s+/, ''). # Remove leading whitespace.
+ sub(/\s+$/, ''). # Remove trailing whitespace.
+ gsub(/\'/, '"'). # Convert VMLs attribute quotes.
+ gsub(/([^ ])\/>$/, '\1 />'). # Add space before element end like XML::Writer.
+ sub(/"$/, '" '). # Add space between attributes.
+ sub(/>$/, ">\n"). # Add newline after element end.
+ gsub(/></, ">\n<") # Split multiple elements.
+ str.chomp! if str == "<x:Anchor>\n" # Put all of Anchor on one line.
+ ret += str
end
+ ret.split(/\n/)
end
- def compare_files(expected, result)
- files(expected).each do |file|
- compare_file(expected, result, file)
- end
+ def entrys(xlsx)
+ result = []
+ Zip::ZipFile.foreach(xlsx) { |entry| result << entry }
+ result
end
- def compare_file(expected, result, file)
- ruby_19 do
- assert_equal(got_to_array(IO.read(File.join(expected, file), :encoding => 'UTF-8')),
- got_to_array(IO.read(File.join(result, file), :encoding => 'UTF-8')),
- "#{file} differs.")
- end
- ruby_18 do
- assert_equal(got_to_array(IO.read(File.join(expected, file))),
- got_to_array(IO.read(File.join(result, file))),
- "#{file} differs.")
- end
+ def compare_xlsx_for_regression(exp_filename, got_filename, ignore_members = nil, ignore_elements = nil)
+ compare_xlsx(exp_filename, got_filename, ignore_members, ignore_elements, true)
end
- def prepare_compare(expected, result, xlsx)
- prepare_xlsx(expected, File.join(@perl_output, xlsx))
- prepare_xlsx(result, xlsx)
- end
+ def compare_xlsx(exp_filename, got_filename, ignore_members = nil, ignore_elements = nil, regression = false)
+ # The zip "members" are the files in the XLSX container.
+ got_members = entrys(got_filename).sort_by {|member| member.name}
+ exp_members = entrys(exp_filename).sort_by {|member| member.name}
- def prepare_xlsx(dir, xlsx)
- Dir.mkdir(dir)
- system("unzip -q #{xlsx} -d #{dir}")
- end
+ # Ignore some test specific filenames.
+ if ignore_members
+ got_members.reject! {|member| ignore_members.include?(member.name) }
+ exp_members.reject! {|member| ignore_members.include?(member.name) }
+ end
- def files(dir)
- Dir.glob(File.join(dir, "**/*")).select { |f| File.file?(f) }.
- reject { |f| File.basename(f) =~ /(core|theme1)\.xml/ }.
- collect { |f| f.sub(Regexp.new("^#{dir}"), '') }
+ # Check that each XLSX container has the same file members.
+ assert_equal(
+ exp_members.collect {|member| member.name},
+ got_members.collect {|member| member.name},
+ "file members differs.")
+
+ # Compare each file in the XLSX containers.
+ exp_members.each_index do |i|
+ got_xml_str = got_members[i].get_input_stream.read.gsub(%r!(\S)/>!, '\1 />')
+ exp_xml_str = exp_members[i].get_input_stream.read.gsub(%r!(\S)/>!, '\1 />')
+
+ # Remove dates and user specific data from the core.xml data.
+ if exp_members[i].name == 'docProps/core.xml'
+ if regression
+ exp_xml_str = exp_xml_str.gsub(/ ?John/, '').gsub(/\d\d\d\d-\d\d-\d\dT\d\d\:\d\d:\d\dZ/,'')
+ else
+ exp_xml_str = exp_xml_str.gsub(/\d\d\d\d-\d\d-\d\dT\d\d\:\d\d:\d\dZ/,'')
+ end
+ got_xml_str = got_xml_str.gsub(/\d\d\d\d-\d\d-\d\dT\d\d\:\d\d:\d\dZ/,'')
+ end
+
+ if exp_members[i].name =~ %r!xl/worksheets/sheet\d.xml!
+ exp_xml_str = exp_xml_str.
+ sub(/horizontalDpi="200" /, '').
+ sub(/verticalDpi="200" /, '').
+ sub(/(<pageSetup.* )r:id="rId1"/, '\1').
+ sub(/ +\/>/, ' />')
+ end
+
+ if exp_members[i].name =~ /.vml$/
+ got_xml = got_to_array(got_xml_str)
+ exp_xml = vml_str_to_array(exp_xml_str)
+ else
+ got_xml = got_to_array(got_xml_str)
+ exp_xml = got_to_array(exp_xml_str)
+ end
+
+ # Ignore test specific XML elements for defined filenames.
+ if ignore_elements && ignore_elements[exp_members[i].name]
+ str = ignore_elements[exp_members[i].name].join('|')
+ regex = Regexp.new(str)
+
+ got_xml = got_xml.reject { |s| s =~ regex }
+ exp_xml = exp_xml.reject { |s| s =~ regex }
+ end
+
+ # Reorder the XML elements in the XLSX relationship files.
+ case exp_members[i].name
+ when '[Content_Types].xml', /.rels$/
+ got_xml = sort_rel_file_data(got_xml)
+ exp_xml = sort_rel_file_data(exp_xml)
+ end
+
+ # Comparison of the XML elements in each file.
+ assert_equal(exp_xml, got_xml, "#{exp_members[i].name} differs.")
+ end
end
- def cleanup(xlsx)
- Writexlsx::Utility.delete_files(xlsx) if File.exist?(xlsx)
- Writexlsx::Utility.delete_files(@expected_dir) if File.exist?(@expected_dir)
- Writexlsx::Utility.delete_files(@result_dir) if File.exist?(@result_dir)
+ def sort_rel_file_data(xml_array)
+ header = xml_array.shift
+ tail = xml_array.pop
+ xml_array.sort.unshift(header).push(tail)
end
end