aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/roo/utils_spec.rb
blob: c000ae722a2ceecd9431a9368f2622199e93ed5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require 'spec_helper'

RSpec.describe ::Roo::Utils do
  subject { described_class }

  context '#number_to_letter' do
    described_class::LETTERS.each_with_index do |letter, index|
      it "should return '#{ letter }' when passed #{ index + 1 }" do
        expect(described_class.number_to_letter(index + 1)).to eq(letter)
      end
    end

    {
      27 => 'AA', 26*2 => 'AZ', 26*3 => 'BZ', 26**2 + 26 => 'ZZ', 26**2 + 27 => 'AAA',
      26**3 + 26**2 + 26 => 'ZZZ', 1.0 => 'A', 676 => 'YZ', 677 => 'ZA'
    }.each do |key, value|
      it "should return '#{value}' when passed #{key}" do
        expect(described_class.number_to_letter(key)).to eq(value)
      end
    end
  end

  context '#letter_to_number' do
    it "should give 1 for 'A' and 'a'" do
      expect(described_class.letter_to_number('A')).to eq(1)
      expect(described_class.letter_to_number('a')).to eq(1)
    end

    it "should give the correct value for 'Z'" do
      expect(described_class.letter_to_number('Z')).to eq(26)
    end

    it "should give the correct value for 'AA' regardless of case mixing" do
      %w(AA aA Aa aa).each do |key|
        expect(described_class.letter_to_number(key)).to eq(27)
      end
    end

    { 'AB' => 28, 'AZ' => 26*2, 'BZ' => 26*3, 'ZZ' => 26**2 + 26 }.each do |key, value|
      it "should give the correct value for '#{key}'" do
        expect(described_class.letter_to_number(key)).to eq(value)
      end
    end
  end

  context '.split_coordinate' do
    it "returns the expected result" do
      expect(described_class.split_coordinate('A1')).to eq [1, 1]
      expect(described_class.split_coordinate('B2')).to eq [2, 2]
      expect(described_class.split_coordinate('R2')).to eq [2, 18]
      expect(described_class.split_coordinate('AR31')).to eq [31, 18 + 26]
    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]
      expect(described_class.split_coord('B2')).to eq ["B", 2]
      expect(described_class.split_coord('R2')).to eq ["R", 2]
      expect(described_class.split_coord('AR31')).to eq ["AR", 31]
    end

    it "raises an error when appropriate" do
      expect { described_class.split_coord('A') }.to raise_error(ArgumentError)
      expect { described_class.split_coord('2') }.to raise_error(ArgumentError)
    end
  end


  context '.num_cells_in_range' do
    it "returns the expected result" do
      expect(described_class.num_cells_in_range('A1:B2')).to eq 4
      expect(described_class.num_cells_in_range('B2:E3')).to eq 8
      expect(described_class.num_cells_in_range('R2:Z10')).to eq 81
      expect(described_class.num_cells_in_range('AR31:AR32')).to eq 2
      expect(described_class.num_cells_in_range('A1')).to eq 1
    end

    it "raises an error when appropriate" do
      expect { described_class.num_cells_in_range('A1:B1:B2') }.to raise_error(ArgumentError)
    end
  end

  context '.coordinates_in_range' do
    it "returns the expected result" do
      expect(described_class.coordinates_in_range('').to_a).to eq []
      expect(described_class.coordinates_in_range('B2').to_a).to eq [[2, 2]]
      expect(described_class.coordinates_in_range('D2:G3').to_a).to eq [[2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7]]
      expect(described_class.coordinates_in_range('G3:D2').to_a).to eq []
    end

    it "raises an error when appropriate" do
      expect { described_class.coordinates_in_range('D2:G3:I5').to_a }.to raise_error(ArgumentError)
    end
  end

  context '.load_xml' do
    it 'returns the expected result' 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["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["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]["r"]).to eq "3"
    end
  end
end