spec/calculator_spec.rb in dentaku-1.2.1 vs spec/calculator_spec.rb in dentaku-1.2.2

- old
+ new

@@ -4,10 +4,26 @@ let(:calculator) { described_class.new } let(:with_memory) { described_class.new.store(:apples => 3) } it 'evaluates an expression' do expect(calculator.evaluate('7+3')).to eq(10) + expect(calculator.evaluate('2 -1')).to eq(1) + expect(calculator.evaluate('-1 + 2')).to eq(1) + expect(calculator.evaluate('1 - 2')).to eq(-1) + expect(calculator.evaluate('1 - - 2')).to eq(3) + expect(calculator.evaluate('-1 - - 2')).to eq(1) + expect(calculator.evaluate('1 - - - 2')).to eq(-1) + expect(calculator.evaluate('(-1 + 2)')).to eq(1) + expect(calculator.evaluate('-(1 + 2)')).to eq(-3) + expect(calculator.evaluate('2 ^ - 1')).to eq(0.5) + expect(calculator.evaluate('2 ^ -(3 - 2)')).to eq(0.5) + expect(calculator.evaluate('(2 + 3) - 1')).to eq(4) + expect(calculator.evaluate('(-2 + 3) - 1')).to eq(0) + expect(calculator.evaluate('(-2 - 3) - 1')).to eq(-6) + expect(calculator.evaluate('1 + -2 ^ 2')).to eq(-3) + expect(calculator.evaluate('3 + -num', :num => 2)).to eq(1) + expect(calculator.evaluate('-num + 3', :num => 2)).to eq(1) end describe 'memory' do it { expect(calculator).to be_empty } it { expect(with_memory).not_to be_empty } @@ -30,14 +46,14 @@ end end describe 'dependencies' do it "finds dependencies in a generic statement" do - expect(calculator.dependencies("bob + dole / 3")).to eq([:bob, :dole]) + expect(calculator.dependencies("bob + dole / 3")).to eq(['bob', 'dole']) end it "doesn't consider variables in memory as dependencies" do - expect(with_memory.dependencies("apples + oranges")).to eq([:oranges]) + expect(with_memory.dependencies("apples + oranges")).to eq(['oranges']) end end describe 'solve!' do it "evaluates properly with variables, even if some in memory" do @@ -46,10 +62,18 @@ weekly_apple_budget: "apples * 7", pear: "1" )).to eq(pear: 1, weekly_apple_budget: 21, weekly_fruit_budget: 25) end + it "preserves hash keys" do + expect(calculator.solve!( + 'meaning_of_life' => 'age + kids', + 'age' => 40, + 'kids' => 2 + )).to eq('age' => 40, 'kids' => 2, 'meaning_of_life' => 42) + end + it "lets you know about a cycle if one occurs" do expect do calculator.solve!(health: "happiness", happiness: "health") end.to raise_error (TSort::Cyclic) end @@ -62,11 +86,11 @@ 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(unbound)).to be_nil expect(calculator.evaluate(unbound) { :bar }).to eq :bar expect(calculator.evaluate(unbound) { |e| e }).to eq unbound end @@ -110,11 +134,10 @@ expect(calculator.evaluate('some_boolean AND 7 > 5', :some_boolean => false)).to be_falsey expect(calculator.evaluate('some_boolean OR 7 > 5', :some_boolean => true)).to be_truthy expect(calculator.evaluate('some_boolean OR 7 < 5', :some_boolean => true)).to be_truthy expect(calculator.evaluate('some_boolean OR 7 < 5', :some_boolean => false)).to be_falsey - end describe 'functions' do it 'include IF' do expect(calculator.evaluate('if(foo < 8, 10, 20)', :foo => 2)).to eq(10) @@ -135,8 +158,15 @@ expect(calculator.evaluate('NOT(some_boolean)', :some_boolean => true)).to be_falsey expect(calculator.evaluate('NOT(some_boolean)', :some_boolean => false)).to be_truthy expect(calculator.evaluate('NOT(some_boolean) AND 7 > 5', :some_boolean => true)).to be_falsey expect(calculator.evaluate('NOT(some_boolean) OR 7 < 5', :some_boolean => false)).to be_truthy + 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('NOT(some_boolean) AND -1 > 3', :some_boolean => true)).to be_falsey end end end