spec/calculator_spec.rb in dentaku-3.5.3 vs spec/calculator_spec.rb in dentaku-3.5.4
- old
+ new
@@ -28,25 +28,33 @@
expect(calculator.evaluate('-num + 3', num: 2)).to eq(1)
expect(calculator.evaluate('10 ^ 2')).to eq(100)
expect(calculator.evaluate('0 * 10 ^ -5')).to eq(0)
expect(calculator.evaluate('3 + 0 * -3')).to eq(3)
expect(calculator.evaluate('3 + 0 / -3')).to eq(3)
- expect(calculator.evaluate('15 % 8')).to eq(7)
expect(calculator.evaluate('(((695759/735000)^(1/(1981-1991)))-1)*1000').round(4)).to eq(5.5018)
expect(calculator.evaluate('0.253/0.253')).to eq(1)
expect(calculator.evaluate('0.253/d', d: 0.253)).to eq(1)
expect(calculator.evaluate('10 + x', x: 'abc')).to be_nil
expect(calculator.evaluate('x * y', x: '.123', y: '100')).to eq(12.3)
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
+ it "differentiates between percentage and modulo operators" do
+ expect(calculator.evaluate('15 % 8')).to eq(7)
+ expect(calculator.evaluate('15 % (4 * 2)')).to eq(7)
+ expect(calculator.evaluate("5%")).to eq (0.05)
+ expect(calculator.evaluate("400/60%").round(2)).to eq (666.67)
+ expect(calculator.evaluate("(400/60%)*1").round(2)).to eq (666.67)
+ expect(calculator.evaluate("60% * 1").round(2)).to eq (0.60)
+ expect(calculator.evaluate("50% + 50%")).to eq (1.0)
+ end
+
describe 'evaluate' do
it 'returns nil when formula has error' do
expect(calculator.evaluate('1 + + 1')).to be_nil
end
@@ -163,11 +171,10 @@
calculator.store("d1", Date.parse("2024/01/02"))
calculator.store("d2", Date.parse("2024/01/06"))
expect(calculator.solve(diff: "d1 - d2")).to eq(diff: -4)
end
-
it 'stores nested hashes' do
calculator.store(a: {basket: {of: 'apples'}}, b: 2)
expect(calculator.evaluate!('a.basket.of')).to eq('apples')
expect(calculator.evaluate!('a.basket')).to eq(of: 'apples')
expect(calculator.evaluate!('b')).to eq(2)
@@ -513,9 +520,20 @@
value2 = Date.parse('2022-12-31').to_date
expect(calculator.evaluate!('value + duration(1, month)', { value: value }).to_date).to eq(Date.parse('2023-02-01'))
expect(calculator.evaluate!('value - duration(1, month)', { value: value }).to_date).to eq(Date.parse('2022-12-01'))
expect(calculator.evaluate!('value - value2', { value: value, value2: value2 })).to eq(1)
+ end
+
+ it 'from time object' do
+ value = Time.local(2023, 7, 13, 10, 42, 11)
+ value2 = Time.local(2023, 12, 1, 9, 42, 10)
+
+ expect(calculator.evaluate!('value + duration(1, month)', { value: value })).to eq(Time.local(2023, 8, 13, 10, 42, 11))
+ expect(calculator.evaluate!('value - duration(1, day)', { value: value })).to eq(Time.local(2023, 7, 12, 10, 42, 11))
+ expect(calculator.evaluate!('value - duration(1, year)', { value: value })).to eq(Time.local(2022, 7, 13, 10, 42, 11))
+ expect(calculator.evaluate!('value2 - value', { value: value, value2: value2 })).to eq(12_182_399.0)
+ expect(calculator.evaluate!('value - 7200', { value: value })).to eq(Time.local(2023, 7, 13, 8, 42, 11))
end
end
describe 'functions' do
it 'include IF' do