spec/calculator_spec.rb in dentaku-2.0.8 vs spec/calculator_spec.rb in dentaku-2.0.9
- old
+ new
@@ -140,10 +140,24 @@
expect(result).to eq(
more_apples: :undefined,
more_peaches: 2
)
end
+
+ it "solves remainder of expressions when one cannot be evaluated" do
+ result = calculator.solve(
+ conditional: "IF(d != 0, ratio, 0)",
+ ratio: "10/d",
+ d: 0,
+ )
+
+ expect(result).to eq(
+ conditional: 0,
+ ratio: :undefined,
+ d: 0,
+ )
+ 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)
@@ -374,9 +388,56 @@
expect(calculator.evaluate("#{method}(1,2)")).to eq Math.send(method, 1, 2)
else
expect(calculator.evaluate("#{method}(1)")).to eq Math.send(method, 1)
end
end
+ end
+ end
+
+ describe 'disable_cache' do
+ before do
+ allow(Dentaku).to receive(:cache_ast?) { true }
+ end
+
+ it 'disables the AST cache' do
+ expect(calculator.disable_cache{ |c| c.cache_ast? }).to be false
+ end
+
+ it 'calculates normally' do
+ expect(calculator.disable_cache{ |c| c.evaluate("2 + 2") }).to eq(4)
+ end
+ end
+
+ describe 'clear_cache' do
+ before do
+ allow(Dentaku).to receive(:cache_ast?) { true }
+
+ calculator.ast("1+1")
+ calculator.ast("pineapples * 5")
+ calculator.ast("pi * radius ^ 2")
+
+ def calculator.ast_cache
+ @ast_cache
+ end
+ end
+
+ it 'clears all items from cache' do
+ 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
+ calculator.clear_cache("1+1")
+ expect(calculator.ast_cache.keys.sort).to eq([
+ 'pi * radius ^ 2',
+ 'pineapples * 5',
+ ])
+ end
+
+ it 'clears items matching regex from cache' do
+ calculator.clear_cache(/^pi/)
+ expect(calculator.ast_cache.keys.sort).to eq(['1+1'])
end
end
describe 'string functions' do
it 'concatenates two strings' do