From dddfa903d2b856146f05ffb4415c31d6127bb5bf Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Mon, 14 Jan 2019 01:40:56 -0500 Subject: New upstream version 2.8.0 --- spec/lib/roo/base_spec.rb | 48 +++++++++- spec/lib/roo/excelx_spec.rb | 156 +++++++++++++++++++++++++------ spec/lib/roo/strict_spec.rb | 43 +++++++++ spec/lib/roo/utils_spec.rb | 15 ++- spec/lib/roo/weak_instance_cache_spec.rb | 92 ++++++++++++++++++ spec/lib/roo_spec.rb | 0 6 files changed, 317 insertions(+), 37 deletions(-) create mode 100644 spec/lib/roo/strict_spec.rb create mode 100644 spec/lib/roo/weak_instance_cache_spec.rb create mode 100644 spec/lib/roo_spec.rb (limited to 'spec') diff --git a/spec/lib/roo/base_spec.rb b/spec/lib/roo/base_spec.rb index d00025d..76cefcc 100644 --- a/spec/lib/roo/base_spec.rb +++ b/spec/lib/roo/base_spec.rb @@ -127,10 +127,22 @@ describe Roo::Base do end end - describe '#row' do - it 'should return the specified row' do + describe "#row" do + it "should return the specified row" do expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil]) - expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', 'forty-three', 'forty-four', 'forty-five', nil, nil]) + expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil]) + end + + it "should return the specified row if default_sheet is set by a string" do + spreadsheet.default_sheet = "my_sheet" + expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil]) + expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil]) + end + + it "should return the specified row if default_sheet is set by an integer" do + spreadsheet.default_sheet = 0 + expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil]) + expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil]) end end @@ -146,6 +158,11 @@ describe Roo::Base do expect { spreadsheet.row_with([/Missing Header/]) }.to \ raise_error(Roo::HeaderRowNotFoundError) end + + it 'returns missing headers' do + expect { spreadsheet.row_with([/Header/, /Missing Header 1/, /Missing Header 2/]) }.to \ + raise_error(Roo::HeaderRowNotFoundError, '[/Missing Header 1/, /Missing Header 2/]') + end end end @@ -173,6 +190,31 @@ describe Roo::Base do end end + describe "#default_sheet=" do + it "should correctly set the default sheet if passed a string" do + spreadsheet.default_sheet = "my_sheet" + expect(spreadsheet.default_sheet).to eq("my_sheet") + end + + it "should correctly set the default sheet if passed an integer" do + spreadsheet.default_sheet = 0 + expect(spreadsheet.default_sheet).to eq("my_sheet") + end + + it "should correctly set the default sheet if passed an integer for the second sheet" do + spreadsheet.default_sheet = 1 + expect(spreadsheet.default_sheet).to eq("blank sheet") + end + + it "should raise an error if passed a sheet that does not exist as an integer" do + expect { spreadsheet.default_sheet = 10 }.to raise_error RangeError + end + + it "should raise an error if passed a sheet that does not exist as a string" do + expect { spreadsheet.default_sheet = "does_not_exist" }.to raise_error RangeError + end + end + describe '#to_yaml' do it 'should convert the spreadsheet to yaml' do expect(spreadsheet.to_yaml({}, 5, 1, 5, 1)).to eq("--- \n" + yaml_entry(5, 1, 'date', '1961-11-21')) diff --git a/spec/lib/roo/excelx_spec.rb b/spec/lib/roo/excelx_spec.rb index 0ef0f19..6c2289f 100755 --- a/spec/lib/roo/excelx_spec.rb +++ b/spec/lib/roo/excelx_spec.rb @@ -151,6 +151,22 @@ describe Roo::Excelx do it 'returns the expected result' do expect(subject.sheet_for("Tabelle1").instance_variable_get("@name")).to eq "Tabelle1" end + + it 'returns the expected result when passed a number' do + expect(subject.sheet_for(0).instance_variable_get("@name")).to eq "Tabelle1" + end + + it 'returns the expected result when passed a number that is not the first sheet' do + expect(subject.sheet_for(1).instance_variable_get("@name")).to eq "Name of Sheet 2" + end + + it "should raise an error if passed a sheet that does not exist as an integer" do + expect { subject.sheet_for(10) }.to raise_error RangeError + end + + it "should raise an error if passed a sheet that does not exist as a string" do + expect { subject.sheet_for("does_not_exist") }.to raise_error RangeError + end end describe '#row' do @@ -304,6 +320,18 @@ describe Roo::Excelx do end end + describe '#row' do + context 'integers with leading zero' + let(:path) { 'test/files/number_with_zero_prefix.xlsx' } + + it 'returns base 10 integer' do + (1..50).each do |row_index| + range_start = (row_index - 1) * 20 + 1 + expect(subject.row(row_index)).to eq (range_start..(range_start+19)).to_a + end + end + end + describe '#excelx_format' do let(:path) { 'test/files/style.xlsx' } @@ -354,11 +382,22 @@ describe Roo::Excelx do end describe '#hyperlink' do - let(:path) { 'test/files/link.xlsx' } + context 'without location' do + let(:path) { 'test/files/link.xlsx' } - it 'returns the expected result' do - expect(subject.hyperlink(1, 1)).to eq "http://www.google.com" - expect(subject.hyperlink(1, 2)).to eq nil + it 'returns the expected result' do + expect(subject.hyperlink(1, 1)).to eq "http://www.google.com" + expect(subject.hyperlink(1, 2)).to eq nil + end + end + + context 'with location' do + let(:path) { 'test/files/link_with_location.xlsx' } + + it 'returns the expected result' do + expect(subject.hyperlink(1, 1)).to eq "http://www.google.com/#hey" + expect(subject.hyperlink(1, 2)).to eq nil + end end end @@ -480,34 +519,36 @@ describe Roo::Excelx do end describe '#html_strings' do - let(:path) { 'test/files/html_strings_formatting.xlsx' } + describe "HTML Parsing Enabling" do + let(:path) { 'test/files/html_strings_formatting.xlsx' } - it 'returns the expected result' do - expect(subject.excelx_value(1, 1, "Sheet1")).to eq "This has no formatting." - expect(subject.excelx_value(2, 1, "Sheet1")).to eq "This has bold formatting." - expect(subject.excelx_value(2, 2, "Sheet1")).to eq "This has italics formatting." - expect(subject.excelx_value(2, 3, "Sheet1")).to eq "This has underline format." - expect(subject.excelx_value(2, 4, "Sheet1")).to eq "Superscript. x123" - expect(subject.excelx_value(2, 5, "Sheet1")).to eq "SubScript. Tj" - - expect(subject.excelx_value(3, 1, "Sheet1")).to eq "Bold, italics together." - expect(subject.excelx_value(3, 2, "Sheet1")).to eq "Bold, Underline together." - expect(subject.excelx_value(3, 3, "Sheet1")).to eq "Bold, Superscript. xN" - expect(subject.excelx_value(3, 4, "Sheet1")).to eq "Bold, Subscript. Tabc" - expect(subject.excelx_value(3, 5, "Sheet1")).to eq "Italics, Underline together." - expect(subject.excelx_value(3, 6, "Sheet1")).to eq "Italics, Superscript. Xabc" - expect(subject.excelx_value(3, 7, "Sheet1")).to eq "Italics, Subscript. Befg" - expect(subject.excelx_value(4, 1, "Sheet1")).to eq "Bold, italics underline, together." - expect(subject.excelx_value(4, 2, "Sheet1")).to eq "Bold, italics, superscript. Xabc123" - expect(subject.excelx_value(4, 3, "Sheet1")).to eq "Bold, Italics, subscript. Mgha2" - expect(subject.excelx_value(4, 4, "Sheet1")).to eq "Bold, Underline, superscript. ABC123" - expect(subject.excelx_value(4, 5, "Sheet1")).to eq "Bold, Underline, subscript. GoodXYZ" - expect(subject.excelx_value(4, 6, "Sheet1")).to eq "Italics, Underline, superscript. Upswing" - expect(subject.excelx_value(4, 7, "Sheet1")).to eq "Italics, Underline, subscript. Tswing" - expect(subject.excelx_value(5, 1, "Sheet1")).to eq "Bold, italics, underline, superscript. GHJK1904" - expect(subject.excelx_value(5, 2, "Sheet1")).to eq "Bold, italics, underline, subscript. Mikedrop" - expect(subject.excelx_value(6, 1, "Sheet1")).to eq "See that regular html tags do not create html tags.\n
    \n
  1. Denver Broncos
  2. \n
  3. Carolina Panthers
  4. \n
  5. New England Patriots
  6. \n
  7. Arizona Panthers
  8. \n
" - expect(subject.excelx_value(7, 1, "Sheet1")).to eq "Does create html tags when formatting is used..\n
    \n
  1. Denver Broncos
  2. \n
  3. Carolina Panthers
  4. \n
  5. New England Patriots
  6. \n
  7. Arizona Panthers
  8. \n
" + it 'returns the expected result' do + expect(subject.excelx_value(1, 1, "Sheet1")).to eq("This has no formatting.") + expect(subject.excelx_value(2, 1, "Sheet1")).to eq("This has bold formatting.") + expect(subject.excelx_value(2, 2, "Sheet1")).to eq("This has italics formatting.") + expect(subject.excelx_value(2, 3, "Sheet1")).to eq("This has underline format.") + expect(subject.excelx_value(2, 4, "Sheet1")).to eq("Superscript. x123") + expect(subject.excelx_value(2, 5, "Sheet1")).to eq("SubScript. Tj") + + expect(subject.excelx_value(3, 1, "Sheet1")).to eq("Bold, italics together.") + expect(subject.excelx_value(3, 2, "Sheet1")).to eq("Bold, Underline together.") + expect(subject.excelx_value(3, 3, "Sheet1")).to eq("Bold, Superscript. xN") + expect(subject.excelx_value(3, 4, "Sheet1")).to eq("Bold, Subscript. Tabc") + expect(subject.excelx_value(3, 5, "Sheet1")).to eq("Italics, Underline together.") + expect(subject.excelx_value(3, 6, "Sheet1")).to eq("Italics, Superscript. Xabc") + expect(subject.excelx_value(3, 7, "Sheet1")).to eq("Italics, Subscript. Befg") + expect(subject.excelx_value(4, 1, "Sheet1")).to eq("Bold, italics underline, together.") + expect(subject.excelx_value(4, 2, "Sheet1")).to eq("Bold, italics, superscript. Xabc123") + expect(subject.excelx_value(4, 3, "Sheet1")).to eq("Bold, Italics, subscript. Mgha2") + expect(subject.excelx_value(4, 4, "Sheet1")).to eq("Bold, Underline, superscript. ABC123") + expect(subject.excelx_value(4, 5, "Sheet1")).to eq("Bold, Underline, subscript. GoodXYZ") + expect(subject.excelx_value(4, 6, "Sheet1")).to eq("Italics, Underline, superscript. Upswing") + expect(subject.excelx_value(4, 7, "Sheet1")).to eq("Italics, Underline, subscript. Tswing") + expect(subject.excelx_value(5, 1, "Sheet1")).to eq("Bold, italics, underline, superscript. GHJK1904") + expect(subject.excelx_value(5, 2, "Sheet1")).to eq("Bold, italics, underline, subscript. Mikedrop") + expect(subject.excelx_value(6, 1, "Sheet1")).to eq("See that regular html tags do not create html tags.\n
    \n
  1. Denver Broncos
  2. \n
  3. Carolina Panthers
  4. \n
  5. New England Patriots
  6. \n
  7. Arizona Panthers
  8. \n
") + expect(subject.excelx_value(7, 1, "Sheet1")).to eq("Does create html tags when formatting is used..\n
    \n
  1. Denver Broncos
  2. \n
  3. Carolina Panthers
  4. \n
  5. New England Patriots
  6. \n
  7. Arizona Panthers
  8. \n
") + end end end @@ -534,4 +575,57 @@ describe Roo::Excelx do expect(subject.sheet(0).excelx_format(2,1)).to eq 'm/d/yyyy" "h:mm:ss" "AM/PM' end end + + describe 'images' do + let(:path) { 'test/files/images.xlsx' } + + it 'returns array of images from default sheet' do + expect(subject.images).to be_kind_of(Array) + expect(subject.images.size).to eql(19) + end + + it 'returns empty array if there is no images on the sheet' do + expect(subject.images("Sheet2")).to eql([]) + end + end end + +describe 'Roo::Excelx with options set' do + subject(:xlsx) do + Roo::Excelx.new(path, disable_html_wrapper: true) + end + + describe '#html_strings' do + describe "HTML Parsing Disabled" do + let(:path) { 'test/files/html_strings_formatting.xlsx' } + + it 'returns the expected result' do + expect(subject.excelx_value(1, 1, "Sheet1")).to eq("This has no formatting.") + expect(subject.excelx_value(2, 1, "Sheet1")).to eq("This has bold formatting.") + expect(subject.excelx_value(2, 2, "Sheet1")).to eq("This has italics formatting.") + expect(subject.excelx_value(2, 3, "Sheet1")).to eq("This has underline format.") + expect(subject.excelx_value(2, 4, "Sheet1")).to eq("Superscript. x123") + expect(subject.excelx_value(2, 5, "Sheet1")).to eq("SubScript. Tj") + + expect(subject.excelx_value(3, 1, "Sheet1")).to eq("Bold, italics together.") + expect(subject.excelx_value(3, 2, "Sheet1")).to eq("Bold, Underline together.") + expect(subject.excelx_value(3, 3, "Sheet1")).to eq("Bold, Superscript. xN") + expect(subject.excelx_value(3, 4, "Sheet1")).to eq("Bold, Subscript. Tabc") + expect(subject.excelx_value(3, 5, "Sheet1")).to eq("Italics, Underline together.") + expect(subject.excelx_value(3, 6, "Sheet1")).to eq("Italics, Superscript. Xabc") + expect(subject.excelx_value(3, 7, "Sheet1")).to eq("Italics, Subscript. Befg") + expect(subject.excelx_value(4, 1, "Sheet1")).to eq("Bold, italics underline, together.") + expect(subject.excelx_value(4, 2, "Sheet1")).to eq("Bold, italics, superscript. Xabc123") + expect(subject.excelx_value(4, 3, "Sheet1")).to eq("Bold, Italics, subscript. Mgha2") + expect(subject.excelx_value(4, 4, "Sheet1")).to eq("Bold, Underline, superscript. ABC123") + expect(subject.excelx_value(4, 5, "Sheet1")).to eq("Bold, Underline, subscript. GoodXYZ") + expect(subject.excelx_value(4, 6, "Sheet1")).to eq("Italics, Underline, superscript. Upswing") + expect(subject.excelx_value(4, 7, "Sheet1")).to eq("Italics, Underline, subscript. Tswing") + expect(subject.excelx_value(5, 1, "Sheet1")).to eq("Bold, italics, underline, superscript. GHJK1904") + expect(subject.excelx_value(5, 2, "Sheet1")).to eq("Bold, italics, underline, subscript. Mikedrop") + expect(subject.excelx_value(6, 1, "Sheet1")).to eq("See that regular html tags do not create html tags.\n
    \n
  1. Denver Broncos
  2. \n
  3. Carolina Panthers
  4. \n
  5. New England Patriots
  6. \n
  7. Arizona Panthers
  8. \n
") + expect(subject.excelx_value(7, 1, "Sheet1")).to eq("Does create html tags when formatting is used..\n
    \n
  1. Denver Broncos
  2. \n
  3. Carolina Panthers
  4. \n
  5. New England Patriots
  6. \n
  7. Arizona Panthers
  8. \n
") + end + end + end +end \ No newline at end of file diff --git a/spec/lib/roo/strict_spec.rb b/spec/lib/roo/strict_spec.rb new file mode 100644 index 0000000..811ee51 --- /dev/null +++ b/spec/lib/roo/strict_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe Roo::Excelx do + subject { Roo::Excelx.new('test/files/strict.xlsx') } + + example '#sheets' do + expect(subject.sheets).to eq %w(Sheet1 Sheet2) + end + + example '#sheet' do + expect(subject.sheet('Sheet1')).to be_a(Roo::Excelx) + end + + example '#cell' do + expect(subject.cell(1, 1)).to eq 'Sheet 1' + expect(subject.cell(1, 1, 'Sheet2')).to eq 'Sheet 2' + end + + example '#row' do + expect(subject.row(1)).to eq ['Sheet 1'] + expect(subject.row(1, 'Sheet2')).to eq ['Sheet 2'] + end + + example '#first_row' do + expect(subject.first_row).to eq 1 + expect(subject.first_row('Sheet2')).to eq 1 + end + + example '#last_row' do + expect(subject.last_row).to eq 1 + expect(subject.last_row('Sheet2')).to eq 1 + end + + example '#first_column' do + expect(subject.first_column).to eq 1 + expect(subject.first_column('Sheet2')).to eq 1 + end + + example '#last_column' do + expect(subject.last_column).to eq 1 + expect(subject.last_column('Sheet2')).to eq 1 + end +end diff --git a/spec/lib/roo/utils_spec.rb b/spec/lib/roo/utils_spec.rb index ffe93d4..8f322d4 100644 --- a/spec/lib/roo/utils_spec.rb +++ b/spec/lib/roo/utils_spec.rb @@ -52,6 +52,15 @@ RSpec.describe ::Roo::Utils do end end + context '.extract_coordinate' do + it "returns the expected result" do + expect(described_class.extract_coordinate('A1')).to eq [1, 1] + expect(described_class.extract_coordinate('B2')).to eq [2, 2] + expect(described_class.extract_coordinate('R2')).to eq [2, 18] + expect(described_class.extract_coordinate('AR31')).to eq [31, 18 + 26] + end + end + context '.split_coord' do it "returns the expected result" do expect(described_class.split_coord('A1')).to eq ["A", 1] @@ -86,21 +95,21 @@ RSpec.describe ::Roo::Utils do expect(described_class.load_xml('test/files/sheet1.xml')).to be_a(Nokogiri::XML::Document) expect(described_class.load_xml('test/files/sheet1.xml'). remove_namespaces!.xpath("/worksheet/dimension").map do |dim| - dim.attributes["ref"].value end.first).to eq "A1:B11" + dim["ref"] end.first).to eq "A1:B11" end end context '.each_element' do it 'returns the expected result' do described_class.each_element('test/files/sheet1.xml', 'dimension') do |dim| - expect(dim.attributes["ref"].value).to eq "A1:B11" + expect(dim["ref"]).to eq "A1:B11" end rows = [] described_class.each_element('test/files/sheet1.xml', 'row') do |row| rows << row end expect(rows.size).to eq 11 - expect(rows[2].attributes["r"].value).to eq "3" + expect(rows[2]["r"]).to eq "3" end end end diff --git a/spec/lib/roo/weak_instance_cache_spec.rb b/spec/lib/roo/weak_instance_cache_spec.rb new file mode 100644 index 0000000..c2ad9b4 --- /dev/null +++ b/spec/lib/roo/weak_instance_cache_spec.rb @@ -0,0 +1,92 @@ +require 'spec_helper' + +if RUBY_PLATFORM == "java" + require 'java' + java_import 'java.lang.System' +end + +describe Roo::Helpers::WeakInstanceCache do + let(:klass) do + Class.new do + include Roo::Helpers::WeakInstanceCache + + def memoized_data + instance_cache(:@memoized_data) do + "Some Costly Operation #{rand(1000)}" * 1_000 + end + end + end + end + + subject do + klass.new + end + + it 'should be lazy' do + expect(subject.instance_variables).to_not include(:@memoized_data) + data = subject.memoized_data + expect(subject.instance_variables).to include(:@memoized_data) + end + + + it 'should be memoized' do + data = subject.memoized_data + expect(subject.memoized_data).to equal(data) + end + + it 'should recalculate after GC' do + expect(subject.instance_variables).to_not include(:@memoized_data) + GC.disable + subject.memoized_data && nil + expect(subject.instance_variables).to include(:@memoized_data) + + force_gc + expect(subject.instance_variables).to_not include(:@memoized_data) + GC.disable + subject.memoized_data && nil + expect(subject.instance_variables).to include(:@memoized_data) + end + + it 'must remove instance variable' do + expect(subject.instance_variables).to_not include(:@memoized_data) + GC.disable + subject.memoized_data && nil + expect(subject.instance_variables).to include(:@memoized_data) + + force_gc + expect(subject.instance_variables).to_not include(:@memoized_data) + end + + context '#inspect must not raise' do + it 'before calculation' do + expect{subject.inspect}.to_not raise_error + end + it 'after calculation' do + GC.disable + subject.memoized_data && nil + expect{subject.inspect}.to_not raise_error + expect(subject.inspect).to include("Some Costly Operation") + force_gc + end + it 'after GC' do + subject.memoized_data && nil + force_gc + expect(subject.instance_variables).to_not include(:@memoized_data) + expect{subject.inspect}.to_not raise_error + expect(subject.inspect).to_not include("Some Costly Operation") + end + end + + if RUBY_PLATFORM == "java" + def force_gc + System.gc + sleep(0.1) + end + else + def force_gc + GC.start(full_mark: true, immediate_sweep: true) + sleep(0.1) + GC.start(full_mark: true, immediate_sweep: true) + end + end +end \ No newline at end of file diff --git a/spec/lib/roo_spec.rb b/spec/lib/roo_spec.rb new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3