-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: abs function * test: cover abs * feat: require new function * chore: update readme * test: cover within calculator spec
- Loading branch information
Showing
5 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters