spec/calculator_spec.rb in dentaku-3.3.0 vs spec/calculator_spec.rb in dentaku-3.3.1

- old
+ new

@@ -101,10 +101,14 @@ 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) 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::ArgumentError) + end end it 'supports unicode characters in identifiers' do expect(calculator.evaluate("ρ * 2", ρ: 2)).to eq (4) end @@ -119,35 +123,35 @@ expect(calculator).to be_empty end it 'can store the value `false`' do calculator.store('i_am_false', false) - expect(calculator.evaluate!('i_am_false')).to eq false + expect(calculator.evaluate!('i_am_false')).to eq(false) end it 'can store multiple values' do calculator.store(first: 1, second: 2) - expect(calculator.evaluate!('first')).to eq 1 - expect(calculator.evaluate!('second')).to eq 2 + expect(calculator.evaluate!('first')).to eq(1) + expect(calculator.evaluate!('second')).to eq(2) end it 'stores formulas' do calculator.store_formula('area', 'length * width') - expect(calculator.evaluate!('area', length: 5, width: 5)).to eq 25 + expect(calculator.evaluate!('area', length: 5, width: 5)).to eq(25) 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!('b')).to eq 2 + expect(calculator.evaluate!('a.basket.of')).to eq('apples') + expect(calculator.evaluate!('b')).to eq(2) end it 'stores arrays' do calculator.store(a: [1, 2, 3]) - expect(calculator.evaluate!('a[0]')).to eq 1 - expect(calculator.evaluate!('a[x]', x: 1)).to eq 2 - expect(calculator.evaluate!('a[x+1]', x: 1)).to eq 3 + expect(calculator.evaluate!('a[0]')).to eq(1) + expect(calculator.evaluate!('a[x]', x: 1)).to eq(2) + expect(calculator.evaluate!('a[x+1]', x: 1)).to eq(3) end it 'evaluates arrays' do expect(calculator.evaluate([1, 2, 3])).to eq([1, 2, 3]) expect(calculator.evaluate!('{1,2,3}')).to eq([1, 2, 3]) @@ -201,11 +205,11 @@ end.to raise_error(TSort::Cyclic) end it 'is case-insensitive' do result = with_memory.solve!(total_fruit: "Apples + pears", pears: 10) - expect(result[:total_fruit]).to eq 13 + expect(result[:total_fruit]).to eq(13) end it "lets you know if a variable is unbound" do expect { calculator.solve!(more_apples: "apples + 1") @@ -221,11 +225,11 @@ height: "3", length: "2", width: "length * 2", ) - expect(result[:weight]).to eq 130.368 + expect(result[:weight]).to eq(130.368) end it 'raises an exception if there are cyclic dependencies' do expect { calculator.solve!( @@ -302,18 +306,18 @@ it 'fails to evaluate unbound statements' do unbound = 'foo * 1.5' expect { calculator.evaluate!(unbound) }.to raise_error(Dentaku::UnboundVariableError) expect { calculator.evaluate!(unbound) }.to raise_error do |error| - expect(error.unbound_variables).to eq ['foo'] + expect(error.unbound_variables).to eq(['foo']) end expect { calculator.evaluate!('a + b') }.to raise_error do |error| - expect(error.unbound_variables).to eq ['a', 'b'] + expect(error.unbound_variables).to eq(['a', 'b']) end expect(calculator.evaluate(unbound)).to be_nil - expect(calculator.evaluate(unbound) { :bar }).to eq :bar - expect(calculator.evaluate(unbound) { |e| e }).to eq unbound + expect(calculator.evaluate(unbound) { :bar }).to eq(:bar) + expect(calculator.evaluate(unbound) { |e| e }).to eq(unbound) end it 'fails to evaluate incomplete statements' do ['true AND', 'a a ^&'].each do |statement| expect { @@ -388,11 +392,11 @@ end it 'include ROUND' do expect(calculator.evaluate('round(8.2)')).to eq(8) expect(calculator.evaluate('round(8.8)')).to eq(9) - expect(calculator.evaluate('round(8.75, 1)')).to eq(BigDecimal.new('8.8')) + expect(calculator.evaluate('round(8.75, 1)')).to eq(BigDecimal('8.8')) expect(calculator.evaluate('ROUND(apples * 0.93)', apples: 10)).to eq(9) end it 'include NOT' do @@ -404,11 +408,11 @@ end it 'evaluates functions with negative numbers' do expect(calculator.evaluate('if (-1 < 5, -1, 5)')).to eq(-1) expect(calculator.evaluate('if (-1 = -1, -1, 5)')).to eq(-1) - expect(calculator.evaluate('round(-1.23, 1)')).to eq(BigDecimal.new('-1.2')) + expect(calculator.evaluate('round(-1.23, 1)')).to eq(BigDecimal('-1.2')) expect(calculator.evaluate('NOT(some_boolean) AND -1 > 3', some_boolean: true)).to be_falsey end it 'evaluates functions with stored variables' do calculator.store("multi_color" => true, "number_of_sheets" => 5000, "sheets_per_minute_black" => 2000, "sheets_per_minute_color" => 1000) @@ -504,18 +508,18 @@ FORMULA expect(calculator.evaluate(formula, number: 4)).to eq(1) expect(calculator.evaluate(formula, number: 6)).to eq(2) end - it 'throws an exception when no match and there is no default value' do + it 'raises an exception when no match and there is no default value' do formula = <<-FORMULA CASE number WHEN 42 THEN 1 END FORMULA - expect { calculator.evaluate(formula, number: 2) } + expect { calculator.evaluate!(formula, number: 2) } .to raise_error("No block matched the switch value '2'") end it 'handles a default else statement' do expect(calculator.evaluate(formula, quantity: 1, fruit: 'banana')).to eq(2) @@ -577,13 +581,13 @@ describe 'math functions' do Math.methods(false).each do |method| it method do if Math.method(method).arity == 2 - expect(calculator.evaluate("#{method}(x,y)", x: 1, y: '2')).to eq Math.send(method, 1, 2) + expect(calculator.evaluate("#{method}(x,y)", x: 1, y: '2')).to eq(Math.send(method, 1, 2)) else - expect(calculator.evaluate("#{method}(1)")).to eq Math.send(method, 1) + expect(calculator.evaluate("#{method}(1)")).to eq(Math.send(method, 1)) end end end end @@ -613,11 +617,11 @@ @ast_cache end end it 'clears all items from cache' do - expect(calculator.ast_cache.length).to eq 3 + expect(calculator.ast_cache.length).to eq(3) calculator.clear_cache expect(calculator.ast_cache.keys).to be_empty end it 'clears one item from cache' do @@ -636,24 +640,24 @@ describe 'string functions' do it 'concatenates strings' do expect( calculator.evaluate('CONCAT(s1, s2, s3)', 's1' => 'ab', 's2' => 'cd', 's3' => 'ef') - ).to eq 'abcdef' + ).to eq('abcdef') end end describe 'zero-arity functions' do it 'can be used in formulas' do calculator.add_function(:two, :numeric, -> { 2 }) - expect(calculator.evaluate("max(two(), 1)")).to eq 2 - expect(calculator.evaluate("max(1, two())")).to eq 2 + expect(calculator.evaluate("max(two(), 1)")).to eq(2) + expect(calculator.evaluate("max(1, two())")).to eq(2) end end describe 'aliases' do it 'accepts aliases as instance option' do - expect(with_aliases.evaluate('rrround(5.1)')).to eq 5 + expect(with_aliases.evaluate('rrround(5.1)')).to eq(5) end end describe 'nested_data' do it 'default to nested data enabled' do