spec/calculator_spec.rb in dentaku-2.0.3 vs spec/calculator_spec.rb in dentaku-2.0.4
- old
+ new
@@ -27,10 +27,12 @@
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)
end
describe 'memory' do
it { expect(calculator).to be_empty }
it { expect(with_memory).not_to be_empty }
@@ -49,10 +51,15 @@
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
end
+
+ it 'stores formulas' do
+ calculator.store_formula('area', 'length * width')
+ expect(calculator.evaluate!('area', length: 5, width: 5)).to eq 25
+ end
end
describe 'dependencies' do
it "finds dependencies in a generic statement" do
expect(calculator.dependencies("bob + dole / 3")).to eq(['bob', 'dole'])
@@ -66,19 +73,19 @@
describe 'solve!' do
it "evaluates properly with variables, even if some in memory" do
expect(with_memory.solve!(
weekly_fruit_budget: "weekly_apple_budget + pear * 4",
weekly_apple_budget: "apples * 7",
- pear: "1"
+ 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
+ '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
@@ -93,9 +100,23 @@
it "lets you know if a variable is unbound" do
expect {
calculator.solve!(more_apples: "apples + 1")
}.to raise_error(Dentaku::UnboundVariableError)
+ end
+
+ it 'can reference stored formulas' do
+ calculator.store_formula("base_area", "length * width")
+ calculator.store_formula("volume", "base_area * height")
+
+ result = calculator.solve!(
+ weight: "volume * 5.432",
+ height: "3",
+ length: "2",
+ width: "length * 2",
+ )
+
+ expect(result[:weight]).to eq 130.368
end
end
describe 'solve' do
it "returns :undefined when variables are unbound" do