Skip to content

Commit

Permalink
add ABS function (#284)
Browse files Browse the repository at this point in the history
* feat: abs function

* test: cover abs

* feat: require new function

* chore: update readme

* test: cover within calculator spec
  • Loading branch information
fizvlad authored Dec 28, 2022
1 parent 0a1e014 commit 99d2e71
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Comparison: `<`, `>`, `<=`, `>=`, `<>`, `!=`, `=`,

Logic: `IF`, `AND`, `OR`, `XOR`, `NOT`, `SWITCH`

Numeric: `MIN`, `MAX`, `SUM`, `AVG`, `COUNT`, `ROUND`, `ROUNDDOWN`, `ROUNDUP`
Numeric: `MIN`, `MAX`, `SUM`, `AVG`, `COUNT`, `ROUND`, `ROUNDDOWN`, `ROUNDUP`, `ABS`

Selections: `CASE` (syntax see [spec](https://github.com/rubysolo/dentaku/blob/master/spec/calculator_spec.rb#L593))

Expand Down
1 change: 1 addition & 0 deletions lib/dentaku/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require_relative './ast/grouping'
require_relative './ast/case'
require_relative './ast/function_registry'
require_relative './ast/functions/abs'
require_relative './ast/functions/all'
require_relative './ast/functions/and'
require_relative './ast/functions/any'
Expand Down
5 changes: 5 additions & 0 deletions lib/dentaku/ast/functions/abs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative '../function'

Dentaku::AST::Function.register(:abs, :numeric, lambda { |numeric|
Dentaku::AST::Function.numeric(numeric).abs
})
26 changes: 26 additions & 0 deletions spec/ast/abs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'
require 'dentaku/ast/functions/abs'
require 'dentaku'

describe 'Dentaku::AST::Function::Abs' do
it 'returns the absolute value of number' do
result = Dentaku('ABS(-4.2)')
expect(result).to eq(4.2)
end

it 'returns the correct value for positive number' do
result = Dentaku('ABS(1.3)')
expect(result).to eq(1.3)
end

it 'returns the correct value for zero' do
result = Dentaku('ABS(0)')
expect(result).to eq(0)
end

context 'checking errors' do
it 'raises an error if argument is not numeric' do
expect { Dentaku!("ABS(2020-1-1)") }.to raise_error(Dentaku::ArgumentError)
end
end
end
7 changes: 7 additions & 0 deletions spec/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,13 @@
expect(calculator.evaluate('ROUND(apples * 0.93)', apples: 10)).to eq(9)
end

it 'include ABS' do
expect(calculator.evaluate('abs(-2.2)')).to eq(2.2)
expect(calculator.evaluate('abs(5)')).to eq(5)

expect(calculator.evaluate('ABS(x * -1)', x: 10)).to eq(10)
end

it 'include NOT' do
expect(calculator.evaluate('NOT(some_boolean)', some_boolean: true)).to be_falsey
expect(calculator.evaluate('NOT(some_boolean)', some_boolean: false)).to be_truthy
Expand Down

0 comments on commit 99d2e71

Please sign in to comment.