spec/calculator_spec.rb in dentaku-3.5.0 vs spec/calculator_spec.rb in dentaku-3.5.1

- old
+ new

@@ -38,10 +38,12 @@ expect(calculator.evaluate('a/b', a: '10', b: '2')).to eq(5) 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) expect(calculator.evaluate("5%")).to eq (0.05) + expect(calculator.evaluate('1 << 3')).to eq (8) + expect(calculator.evaluate('0xFF >> 6')).to eq (3) end describe 'evaluate' do it 'returns nil when formula has error' do expect(calculator.evaluate('1 + + 1')).to be_nil @@ -65,10 +67,11 @@ expect(calculator.evaluate('MIN(a,b)', a: nil, b: nil)).to be_nil expect(calculator.evaluate('ROUND(a)', a: nil)).to be_nil expect(calculator.evaluate('ROUNDDOWN(a)', a: nil)).to be_nil expect(calculator.evaluate('ROUNDUP(a)', a: nil)).to be_nil expect(calculator.evaluate('SUM(a,b)', a: nil, b: nil)).to be_nil + expect(calculator.evaluate('1.0 & "bar"')).to be_nil end it 'treats explicit nil as logical false' do expect(calculator.evaluate('AND(a,b)', a: nil, b: nil)).to be_falsy expect(calculator.evaluate('IF(a,1,0)', a: nil, b: nil)).to eq(0) @@ -80,10 +83,17 @@ expect(calculator.evaluate('x + 1', x: -> { 1 })).to eq(2) expect { calculator.evaluate('2', x: -> { raise 'boom' }) }.not_to raise_error end end + describe 'ast' do + it 'raises parsing errors' do + expect { calculator.ast('()') }.to raise_error(Dentaku::ParseError) + expect { calculator.ast('(}') }.to raise_error(Dentaku::TokenizerError) + end + end + describe 'evaluate!' do it 'raises exception when formula has error' do expect { calculator.evaluate!('1 + + 1') }.to raise_error(Dentaku::ParseError) expect { calculator.evaluate!('(1 > 5) OR LEFT("abc", 1)') }.to raise_error(Dentaku::ParseError) end @@ -106,9 +116,12 @@ expect { calculator.evaluate!('MIN(a,b)', a: nil, b: nil) }.to raise_error(Dentaku::ArgumentError) expect { calculator.evaluate!('ROUND(a)', a: nil) }.to raise_error(Dentaku::ArgumentError) expect { calculator.evaluate!('ROUNDDOWN(a)', a: nil) }.to raise_error(Dentaku::ArgumentError) expect { calculator.evaluate!('ROUNDUP(a)', a: nil) }.to raise_error(Dentaku::ArgumentError) expect { calculator.evaluate!('SUM(a,b)', a: nil, b: nil) }.to raise_error(Dentaku::ArgumentError) + expect { calculator.evaluate!('"foo" & "bar"') }.to raise_error(Dentaku::ArgumentError) + expect { calculator.evaluate!('1.0 & "bar"') }.to raise_error(Dentaku::ArgumentError) + expect { calculator.evaluate!('1 & "bar"') }.to raise_error(Dentaku::ArgumentError) end it 'raises argument error if a function is called with incorrect arity' do expect { calculator.evaluate!('IF(a,b)', a: 1, b: 1) }.to raise_error(Dentaku::ParseError) end