spec/calculator_spec.rb in dentaku-2.0.5 vs spec/calculator_spec.rb in dentaku-2.0.6
- old
+ new
@@ -128,10 +128,21 @@
it "allows passing in a custom value to an error handler" do
expressions = {more_apples: "apples + 1"}
expect(calculator.solve(expressions) { :foo })
.to eq(more_apples: :foo)
end
+
+ it "solves remainder of expressions with unbound variable" do
+ calculator.store(peaches: 1, oranges: 1)
+ expressions = { more_apples: "apples + 1", more_peaches: "peaches + 1" }
+ result = calculator.solve(expressions)
+ expect(calculator.memory).to eq("peaches" => 1, "oranges" => 1)
+ expect(result).to eq(
+ more_apples: :undefined,
+ more_peaches: 2
+ )
+ end
end
it 'evaluates a statement with no variables' do
expect(calculator.evaluate('5+3')).to eq(8)
expect(calculator.evaluate('(1+1+1)/3*100')).to eq(100)
@@ -224,9 +235,107 @@
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)
result = calculator.evaluate('number_of_sheets / if(multi_color, sheets_per_minute_color, sheets_per_minute_black)')
expect(result).to eq(5)
+ end
+
+ describe 'roundup' do
+ it 'should work with one argument' do
+ expect(calculator.evaluate('roundup(1.234)')).to eq(2)
+ end
+
+ it 'should accept second precision argument like in Office formula' do
+ expect(calculator.evaluate('roundup(1.234, 2)')).to eq(1.24)
+ end
+ end
+
+ describe 'rounddown' do
+ it 'should work with one argument' do
+ expect(calculator.evaluate('rounddown(1.234)')).to eq(1)
+ end
+
+ it 'should accept second precision argument like in Office formula' do
+ expect(calculator.evaluate('rounddown(1.234, 2)')).to eq(1.23)
+ end
+ end
+ end
+
+ describe 'case statements' do
+ it 'handles complex then statements' do
+ formula = <<-FORMULA
+ CASE fruit
+ WHEN 'apple'
+ THEN (1 * quantity)
+ WHEN 'banana'
+ THEN (2 * quantity)
+ END
+ FORMULA
+ expect(calculator.evaluate(formula, quantity: 3, fruit: 'apple')).to eq(3)
+ expect(calculator.evaluate(formula, quantity: 3, fruit: 'banana')).to eq(6)
+ end
+
+ it 'handles complex when statements' do
+ formula = <<-FORMULA
+ CASE number
+ WHEN (2 * 2)
+ THEN 1
+ WHEN (2 * 3)
+ THEN 2
+ END
+ 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
+ formula = <<-FORMULA
+ CASE number
+ WHEN 42
+ THEN 1
+ END
+ FORMULA
+ expect { calculator.evaluate(formula, number: 2) }
+ .to raise_error("No block matched the switch value '2'")
+ end
+
+ it 'handles a default else statement' do
+ formula = <<-FORMULA
+ CASE fruit
+ WHEN 'apple'
+ THEN 1 * quantity
+ WHEN 'banana'
+ THEN 2 * quantity
+ ELSE
+ 3 * quantity
+ END
+ FORMULA
+ expect(calculator.evaluate(formula, quantity: 1, fruit: 'banana')).to eq(2)
+ expect(calculator.evaluate(formula, quantity: 1, fruit: 'orange')).to eq(3)
+ end
+
+ it 'handles nested case statements' do
+ formula = <<-FORMULA
+ CASE fruit
+ WHEN 'apple'
+ THEN 1 * quantity
+ WHEN 'banana'
+ THEN
+ CASE quantity
+ WHEN 1 THEN 2
+ WHEN 10 THEN
+ CASE type
+ WHEN 'organic' THEN 5
+ END
+ END
+ END
+ FORMULA
+ value = calculator.evaluate(
+ formula,
+ type: 'organic',
+ quantity: 10,
+ fruit: 'banana')
+ expect(value).to eq(5)
end
end
describe 'math functions' do
Math.methods(false).each do |method|