|
| 1 | +# typed: true |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'spec_helper' |
| 5 | + |
| 6 | +RSpec.describe Zaxcel::Arithmetic do |
| 7 | + let(:sheet) do |
| 8 | + doc = Zaxcel::Document.new |
| 9 | + calc_sheet = doc.add_sheet!('binary_expressions') |
| 10 | + |
| 11 | + calc_sheet.add_column!(:first) |
| 12 | + calc_sheet.add_column!(:last) |
| 13 | + |
| 14 | + calc_sheet.add_row!(:simple) |
| 15 | + .add!(:first, value: 0) |
| 16 | + .add!(:last, value: 1) |
| 17 | + |
| 18 | + calc_sheet |
| 19 | + end |
| 20 | + |
| 21 | + let(:a1_reference) { sheet.cell_ref(:first, :simple) } |
| 22 | + let(:b1_reference) { sheet.cell_ref(:last, :simple) } |
| 23 | + |
| 24 | + before do |
| 25 | + sheet.position_rows! |
| 26 | + end |
| 27 | + |
| 28 | + describe 'works with sums' do |
| 29 | + it 'const + ref' do |
| 30 | + formula = 1.2 + a1_reference |
| 31 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2+A1') |
| 32 | + end |
| 33 | + |
| 34 | + it '-const * ref' do |
| 35 | + formula = -1.2 + a1_reference |
| 36 | + expect(formula.format(on_sheet: sheet.name)).to eq('-1.2+A1') |
| 37 | + end |
| 38 | + |
| 39 | + it 'ref + const' do |
| 40 | + formula = a1_reference + 1.2 |
| 41 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1+1.2') |
| 42 | + end |
| 43 | + |
| 44 | + it 'ref * -const' do |
| 45 | + formula = 1.2 + -a1_reference |
| 46 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2+(-A1)') |
| 47 | + end |
| 48 | + |
| 49 | + it 'ref + ref' do |
| 50 | + formula = a1_reference + b1_reference |
| 51 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1+B1') |
| 52 | + end |
| 53 | + |
| 54 | + it 'ref + Zaxcel::Arithmetic.zero' do |
| 55 | + expect((a1_reference + described_class.zero).format(on_sheet: sheet.name)).to eq('A1') |
| 56 | + end |
| 57 | + |
| 58 | + it 'Zaxcel::Arithmetic.zero + ref' do |
| 59 | + expect((described_class.zero + a1_reference).format(on_sheet: sheet.name)).to eq('A1') |
| 60 | + end |
| 61 | + |
| 62 | + it 'numeric + Zaxcel::Arithmetic.zero' do |
| 63 | + expect((1.2 + described_class.zero).format(on_sheet: sheet.name)).to eq(1.2) |
| 64 | + end |
| 65 | + |
| 66 | + it 'Zaxcel::Arithmetic.zero + numeric' do |
| 67 | + expect((described_class.zero + 1.2).format(on_sheet: sheet.name)).to eq(1.2) |
| 68 | + end |
| 69 | + |
| 70 | + context 'with Array#sum' do |
| 71 | + it 'Array#sum leads with a zero' do |
| 72 | + formula = [a1_reference, b1_reference, 1.2] |
| 73 | + expect(formula.sum.format(on_sheet: sheet.name)).to eq('0+A1+B1+1.2') |
| 74 | + end |
| 75 | + |
| 76 | + it 'works with Array#sum when initialized with zero' do |
| 77 | + formula = [a1_reference, b1_reference, 1.2] |
| 78 | + expect(formula.sum(described_class.zero).format(on_sheet: sheet.name)).to eq('A1+B1+1.2') |
| 79 | + end |
| 80 | + |
| 81 | + it 'works with Array#sum when initialized with zero and first element is a numeric' do |
| 82 | + formula = [1.2, a1_reference, b1_reference] |
| 83 | + expect(formula.sum(described_class.zero).format(on_sheet: sheet.name)).to eq('1.2+A1+B1') |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe 'works with subtraction' do |
| 89 | + it 'const - ref' do |
| 90 | + formula = 1.2 - a1_reference |
| 91 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2-A1') |
| 92 | + end |
| 93 | + |
| 94 | + it '-const - ref' do |
| 95 | + formula = -1.2 - a1_reference |
| 96 | + expect(formula.format(on_sheet: sheet.name)).to eq('-1.2-A1') |
| 97 | + end |
| 98 | + |
| 99 | + it 'ref - const' do |
| 100 | + formula = a1_reference - 1.2 |
| 101 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1-1.2') |
| 102 | + end |
| 103 | + |
| 104 | + it 'ref - -const' do |
| 105 | + formula = 1.2 - -a1_reference |
| 106 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2-(-A1)') |
| 107 | + end |
| 108 | + |
| 109 | + it 'ref - ref' do |
| 110 | + formula = a1_reference - b1_reference |
| 111 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1-B1') |
| 112 | + end |
| 113 | + |
| 114 | + it 'ref - Zaxcel::Arithmetic.zero' do |
| 115 | + expect((a1_reference - described_class.zero).format(on_sheet: sheet.name)).to eq('A1') |
| 116 | + end |
| 117 | + |
| 118 | + it 'Zaxcel::Arithmetic.zero - ref' do |
| 119 | + expect((described_class.zero - a1_reference).format(on_sheet: sheet.name)).to eq('-A1') |
| 120 | + end |
| 121 | + |
| 122 | + it 'numeric - Zaxcel::Arithmetic.zero' do |
| 123 | + expect((1.2 - described_class.zero).format(on_sheet: sheet.name)).to eq(1.2) |
| 124 | + end |
| 125 | + |
| 126 | + it 'Zaxcel::Arithmetic.zero - numeric' do |
| 127 | + expect((described_class.zero - 1.2).format(on_sheet: sheet.name)).to eq(-1.2) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + describe 'works with products' do |
| 132 | + it 'const * ref' do |
| 133 | + formula = 1.2 * a1_reference |
| 134 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2*A1') |
| 135 | + end |
| 136 | + |
| 137 | + it '-const * ref' do |
| 138 | + formula = -1.2 * a1_reference |
| 139 | + expect(formula.format(on_sheet: sheet.name)).to eq('-1.2*A1') |
| 140 | + end |
| 141 | + |
| 142 | + it 'ref * const' do |
| 143 | + formula = a1_reference * 1.2 |
| 144 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1*1.2') |
| 145 | + end |
| 146 | + |
| 147 | + it 'ref * -const' do |
| 148 | + formula = 1.2 * -a1_reference |
| 149 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2*(-A1)') |
| 150 | + end |
| 151 | + |
| 152 | + it 'ref * ref' do |
| 153 | + formula = a1_reference * b1_reference |
| 154 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1*B1') |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + describe 'works with divison' do |
| 159 | + it 'const / ref' do |
| 160 | + formula = 1.2 / a1_reference |
| 161 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2/A1') |
| 162 | + end |
| 163 | + |
| 164 | + it '-const / ref' do |
| 165 | + formula = -1.2 / a1_reference |
| 166 | + expect(formula.format(on_sheet: sheet.name)).to eq('-1.2/A1') |
| 167 | + end |
| 168 | + |
| 169 | + it 'ref / const' do |
| 170 | + formula = a1_reference / 1.2 |
| 171 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1/1.2') |
| 172 | + end |
| 173 | + |
| 174 | + it 'ref / -const' do |
| 175 | + formula = 1.2 / -a1_reference |
| 176 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2/(-A1)') |
| 177 | + end |
| 178 | + |
| 179 | + it 'ref / ref' do |
| 180 | + formula = a1_reference / b1_reference |
| 181 | + expect(formula.format(on_sheet: sheet.name)).to eq('A1/B1') |
| 182 | + end |
| 183 | + end |
| 184 | + |
| 185 | + it 'handles associativity correctly' do |
| 186 | + formula = 1.2 - (3 * (a1_reference + (4 / b1_reference))) |
| 187 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2-3*(A1+4/B1)') |
| 188 | + |
| 189 | + formula = (((1.2 - 3) * a1_reference) + 4) / b1_reference |
| 190 | + expect(formula.format(on_sheet: sheet.name)).to eq('(-1.8*A1+4)/B1') |
| 191 | + |
| 192 | + formula = 1.2 - 3 * (a1_reference + 4) / b1_reference |
| 193 | + expect(formula.format(on_sheet: sheet.name)).to eq('1.2-3*(A1+4)/B1') |
| 194 | + end |
| 195 | +end |
0 commit comments