require 'spec_helper' describe Rspreadsheet::Tools do it 'converts correctly cell adresses' do Rspreadsheet::Tools.convert_cell_address('A1')[0].should == 1 Rspreadsheet::Tools.convert_cell_address('A1')[1].should == 1 Rspreadsheet::Tools.convert_cell_address('C5')[0].should == 5 Rspreadsheet::Tools.convert_cell_address('C5')[1].should == 3 end end describe Rspreadsheet do it 'can open ods testfile and reads its content correctly' do book = Rspreadsheet.new($test_filename) s = book.worksheets[1] (1..10).each do |i| s[i,1].should === i end s[1,2].should === 'text' s[2,2].should === Date.new(2014,1,1) end it 'can open and save file, and saved file has same cells as original' do tmp_filename = '/tmp/testfile1.ods' # first delete temp file File.delete(tmp_filename) if File.exists?(tmp_filename) book = Rspreadsheet.new($test_filename) # than open test file book.save(tmp_filename) # and save it as temp file book1 = Rspreadsheet.new($test_filename) # now open both again book2 = Rspreadsheet.new(tmp_filename) @sheet1 = book1.worksheets[1] @sheet2 = book2.worksheets[1] @sheet1.nonemptycells.each do |cell| # and test identity @sheet2[cell.row,cell.col].should == cell.value end end it 'can open and save file, and saved file is exactly same as original' do tmp_filename = '/tmp/testfile1.ods' # first delete temp file File.delete(tmp_filename) if File.exists?(tmp_filename) book = Rspreadsheet.new($test_filename) # than open test file book.save(tmp_filename) # and save it as temp file # now compare them @content_xml1 = Zip::File.open($test_filename) do |zip| LibXML::XML::Document.io zip.get_input_stream('content.xml') end @content_xml2 = Zip::File.open(tmp_filename) do |zip| LibXML::XML::Document.io zip.get_input_stream('content.xml') end @content_xml2.root.first_diff(@content_xml1.root).should be_nil @content_xml1.root.first_diff(@content_xml2.root).should be_nil @content_xml1.root.equals?(@content_xml2.root).should == true end it 'when open and save file modified, than the file is different' do tmp_filename = '/tmp/testfile1.ods' # first delete temp file File.delete(tmp_filename) if File.exists?(tmp_filename) book = Rspreadsheet.new($test_filename) # than open test file book.worksheets[1].rows(1).cells(1).value.should_not == 'xyzxyz' book.worksheets[1].rows(1).cells(1).value ='xyzxyz' book.worksheets[1].rows(1).cells(1).value.should == 'xyzxyz' # book.worksheets[1][1,1].should_not == 'xyzxyz' # book.worksheets[1][1,1]='xyzxyz' # book.worksheets[1][1,1].should == 'xyzxyz' book.save(tmp_filename) # and save it as temp file # now compare them @content_doc1 = Zip::File.open($test_filename) do |zip| LibXML::XML::Document.io zip.get_input_stream('content.xml') end @content_doc2 = Zip::File.open(tmp_filename) do |zip| LibXML::XML::Document.io zip.get_input_stream('content.xml') end @content_doc1.eql?(@content_doc2).should == false end it 'can create file' do book = Rspreadsheet.new end it 'can create new worksheet' do book = Rspreadsheet.new book.create_worksheet end end