spec/calculator_spec.rb in dentaku-3.1.0 vs spec/calculator_spec.rb in dentaku-3.2.0

- old
+ new

@@ -39,10 +39,14 @@ expect(calculator.evaluate('t + 1*24*60*60', t: Time.local(2017, 1, 1))).to eq(Time.local(2017, 1, 2)) expect(calculator.evaluate("2 | 3 * 9")).to eq (27) expect(calculator.evaluate("2 & 3 * 9")).to eq (2) end + it 'supports unicode characters in identifiers' do + expect(calculator.evaluate("ρ * 2", ρ: 2)).to eq (4) + end + describe 'memory' do it { expect(calculator).to be_empty } it { expect(with_memory).not_to be_empty } it { expect(with_memory.clear).to be_empty } @@ -88,10 +92,14 @@ describe 'dependencies' do it "finds dependencies in a generic statement" do expect(calculator.dependencies("bob + dole / 3")).to eq(['bob', 'dole']) end + it "ignores dependencies passed in context" do + expect(calculator.dependencies("a + b", a: 1)).to eq(['b']) + end + it "finds dependencies in formula arguments" do allow(Dentaku).to receive(:cache_ast?) { true } expect(calculator.dependencies("CONCAT(bob, dole)")).to eq(['bob', 'dole']) end @@ -369,23 +377,37 @@ }.to raise_error(Dentaku::ArgumentError) end end describe 'case statements' do - it 'handles complex then statements' do - formula = <<-FORMULA + let(:formula) { + <<-FORMULA CASE fruit WHEN 'apple' - THEN (1 * quantity) + THEN 1 * quantity WHEN 'banana' - THEN (2 * quantity) + THEN 2 * quantity + ELSE + 3 * quantity END FORMULA + } + + it 'handles complex then statements' do expect(calculator.evaluate(formula, quantity: 3, fruit: 'apple')).to eq(3) expect(calculator.evaluate(formula, quantity: 3, fruit: 'banana')).to eq(6) end + it 'evaluates case statement as part of a larger expression' do + expect(calculator.evaluate("2 + #{formula}", quantity: 3, fruit: 'apple')).to eq(5) + expect(calculator.evaluate("2 + #{formula}", quantity: 3, fruit: 'banana')).to eq(8) + expect(calculator.evaluate("2 + #{formula}", quantity: 3, fruit: 'kiwi')).to eq(11) + expect(calculator.evaluate("#{formula} + 2", quantity: 3, fruit: 'apple')).to eq(5) + expect(calculator.evaluate("#{formula} + 2", quantity: 3, fruit: 'banana')).to eq(8) + expect(calculator.evaluate("#{formula} + 2", quantity: 3, fruit: 'kiwi')).to eq(11) + end + it 'handles complex when statements' do formula = <<-FORMULA CASE number WHEN (2 * 2) THEN 1 @@ -407,19 +429,9 @@ expect { calculator.evaluate(formula, number: 2) } .to raise_error("No block matched the switch value '2'") end it 'handles a default else statement' do - formula = <<-FORMULA - CASE fruit - WHEN 'apple' - THEN 1 * quantity - WHEN 'banana' - THEN 2 * quantity - ELSE - 3 * quantity - END - FORMULA expect(calculator.evaluate(formula, quantity: 1, fruit: 'banana')).to eq(2) expect(calculator.evaluate(formula, quantity: 1, fruit: 'orange')).to eq(3) end it 'handles nested case statements' do