dentaku 3.2.0 → 3.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +5 -10
- data/.travis.yml +4 -6
- data/CHANGELOG.md +86 -2
- data/README.md +7 -6
- data/dentaku.gemspec +1 -1
- data/lib/dentaku/ast/access.rb +21 -1
- data/lib/dentaku/ast/arithmetic.rb +51 -15
- data/lib/dentaku/ast/array.rb +41 -0
- data/lib/dentaku/ast/bitwise.rb +30 -5
- data/lib/dentaku/ast/case/case_conditional.rb +17 -2
- data/lib/dentaku/ast/case/case_else.rb +17 -3
- data/lib/dentaku/ast/case/case_switch_variable.rb +14 -0
- data/lib/dentaku/ast/case/case_then.rb +17 -3
- data/lib/dentaku/ast/case/case_when.rb +21 -3
- data/lib/dentaku/ast/case.rb +19 -3
- data/lib/dentaku/ast/comparators.rb +38 -28
- data/lib/dentaku/ast/function.rb +11 -3
- data/lib/dentaku/ast/function_registry.rb +21 -0
- data/lib/dentaku/ast/functions/all.rb +23 -0
- data/lib/dentaku/ast/functions/and.rb +2 -2
- data/lib/dentaku/ast/functions/any.rb +23 -0
- data/lib/dentaku/ast/functions/avg.rb +2 -2
- data/lib/dentaku/ast/functions/count.rb +8 -0
- data/lib/dentaku/ast/functions/duration.rb +51 -0
- data/lib/dentaku/ast/functions/enum.rb +37 -0
- data/lib/dentaku/ast/functions/filter.rb +23 -0
- data/lib/dentaku/ast/functions/if.rb +19 -2
- data/lib/dentaku/ast/functions/map.rb +23 -0
- data/lib/dentaku/ast/functions/or.rb +4 -4
- data/lib/dentaku/ast/functions/pluck.rb +30 -0
- data/lib/dentaku/ast/functions/round.rb +1 -1
- data/lib/dentaku/ast/functions/rounddown.rb +1 -1
- data/lib/dentaku/ast/functions/roundup.rb +1 -1
- data/lib/dentaku/ast/functions/ruby_math.rb +50 -3
- data/lib/dentaku/ast/functions/string_functions.rb +105 -12
- data/lib/dentaku/ast/functions/xor.rb +44 -0
- data/lib/dentaku/ast/grouping.rb +3 -1
- data/lib/dentaku/ast/identifier.rb +16 -4
- data/lib/dentaku/ast/literal.rb +10 -0
- data/lib/dentaku/ast/negation.rb +7 -1
- data/lib/dentaku/ast/nil.rb +4 -0
- data/lib/dentaku/ast/node.rb +8 -0
- data/lib/dentaku/ast/operation.rb +17 -0
- data/lib/dentaku/ast/string.rb +7 -0
- data/lib/dentaku/ast.rb +8 -0
- data/lib/dentaku/bulk_expression_solver.rb +38 -27
- data/lib/dentaku/calculator.rb +21 -8
- data/lib/dentaku/date_arithmetic.rb +45 -0
- data/lib/dentaku/exceptions.rb +11 -8
- data/lib/dentaku/flat_hash.rb +9 -2
- data/lib/dentaku/parser.rb +57 -16
- data/lib/dentaku/print_visitor.rb +101 -0
- data/lib/dentaku/token_matcher.rb +1 -1
- data/lib/dentaku/token_scanner.rb +9 -3
- data/lib/dentaku/tokenizer.rb +7 -2
- data/lib/dentaku/version.rb +1 -1
- data/lib/dentaku/visitor/infix.rb +82 -0
- data/lib/dentaku.rb +20 -7
- data/spec/ast/addition_spec.rb +7 -1
- data/spec/ast/all_spec.rb +25 -0
- data/spec/ast/and_function_spec.rb +6 -6
- data/spec/ast/and_spec.rb +1 -1
- data/spec/ast/any_spec.rb +23 -0
- data/spec/ast/arithmetic_spec.rb +64 -29
- data/spec/ast/avg_spec.rb +9 -5
- data/spec/ast/comparator_spec.rb +31 -1
- data/spec/ast/count_spec.rb +7 -7
- data/spec/ast/division_spec.rb +7 -1
- data/spec/ast/filter_spec.rb +25 -0
- data/spec/ast/function_spec.rb +20 -15
- data/spec/ast/map_spec.rb +27 -0
- data/spec/ast/max_spec.rb +16 -3
- data/spec/ast/min_spec.rb +16 -3
- data/spec/ast/mul_spec.rb +11 -6
- data/spec/ast/negation_spec.rb +48 -0
- data/spec/ast/node_spec.rb +11 -8
- data/spec/ast/numeric_spec.rb +1 -1
- data/spec/ast/or_spec.rb +7 -7
- data/spec/ast/pluck_spec.rb +32 -0
- data/spec/ast/round_spec.rb +14 -4
- data/spec/ast/rounddown_spec.rb +14 -4
- data/spec/ast/roundup_spec.rb +14 -4
- data/spec/ast/string_functions_spec.rb +73 -0
- data/spec/ast/sum_spec.rb +11 -6
- data/spec/ast/switch_spec.rb +5 -5
- data/spec/ast/xor_spec.rb +35 -0
- data/spec/bulk_expression_solver_spec.rb +37 -1
- data/spec/calculator_spec.rb +341 -32
- data/spec/dentaku_spec.rb +19 -6
- data/spec/external_function_spec.rb +32 -6
- data/spec/parser_spec.rb +100 -123
- data/spec/print_visitor_spec.rb +66 -0
- data/spec/spec_helper.rb +6 -4
- data/spec/token_matcher_spec.rb +8 -8
- data/spec/token_scanner_spec.rb +4 -4
- data/spec/tokenizer_spec.rb +56 -13
- data/spec/visitor/infix_spec.rb +31 -0
- data/spec/visitor_spec.rb +138 -0
- metadata +52 -7
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'dentaku/ast/functions/any'
|
3
|
+
require 'dentaku'
|
4
|
+
|
5
|
+
describe Dentaku::AST::Any do
|
6
|
+
let(:calculator) { Dentaku::Calculator.new }
|
7
|
+
it 'performs ANY operation' do
|
8
|
+
result = Dentaku('ANY(vals, val, val > 1)', vals: [1, 2, 3])
|
9
|
+
expect(result).to eq(true)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'works with a single value if needed for some reason' do
|
13
|
+
result = Dentaku('ANY(vals, val, val > 1)', vals: 1)
|
14
|
+
expect(result).to eq(false)
|
15
|
+
|
16
|
+
result = Dentaku('ANY(vals, val, val > 1)', vals: 2)
|
17
|
+
expect(result).to eq(true)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'raises argument error if a string is passed as identifier' do
|
21
|
+
expect { calculator.evaluate!('ANY({1, 2, 3}, "val", val % 2 == 0)') }.to raise_error(Dentaku::ArgumentError)
|
22
|
+
end
|
23
|
+
end
|
data/spec/ast/arithmetic_spec.rb
CHANGED
@@ -4,53 +4,88 @@ require 'dentaku/ast/arithmetic'
|
|
4
4
|
require 'dentaku/token'
|
5
5
|
|
6
6
|
describe Dentaku::AST::Arithmetic do
|
7
|
-
let(:one)
|
8
|
-
let(:two)
|
9
|
-
let(:x)
|
10
|
-
let(:y)
|
11
|
-
let(:ctx)
|
7
|
+
let(:one) { Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, 1) }
|
8
|
+
let(:two) { Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, 2) }
|
9
|
+
let(:x) { Dentaku::AST::Identifier.new Dentaku::Token.new(:identifier, 'x') }
|
10
|
+
let(:y) { Dentaku::AST::Identifier.new Dentaku::Token.new(:identifier, 'y') }
|
11
|
+
let(:ctx) { {'x' => 1, 'y' => 2} }
|
12
|
+
let(:date) { Dentaku::AST::DateTime.new Dentaku::Token.new(:datetime, DateTime.new(2020, 4, 16)) }
|
12
13
|
|
13
14
|
it 'performs an arithmetic operation with numeric operands' do
|
14
|
-
expect(add(one, two)).to eq
|
15
|
-
expect(sub(one, two)).to eq
|
16
|
-
expect(mul(one, two)).to eq
|
17
|
-
expect(div(one, two)).to eq
|
15
|
+
expect(add(one, two)).to eq(3)
|
16
|
+
expect(sub(one, two)).to eq(-1)
|
17
|
+
expect(mul(one, two)).to eq(2)
|
18
|
+
expect(div(one, two)).to eq(0.5)
|
19
|
+
expect(neg(one)).to eq(-1)
|
18
20
|
end
|
19
21
|
|
20
22
|
it 'performs an arithmetic operation with one numeric operand and one string operand' do
|
21
|
-
expect(add(one, x)).to eq
|
22
|
-
expect(sub(one, x)).to eq
|
23
|
-
expect(mul(one, x)).to eq
|
24
|
-
expect(div(one, x)).to eq
|
23
|
+
expect(add(one, x)).to eq(2)
|
24
|
+
expect(sub(one, x)).to eq(0)
|
25
|
+
expect(mul(one, x)).to eq(1)
|
26
|
+
expect(div(one, x)).to eq(1)
|
25
27
|
|
26
|
-
expect(add(y, two)).to eq
|
27
|
-
expect(sub(y, two)).to eq
|
28
|
-
expect(mul(y, two)).to eq
|
29
|
-
expect(div(y, two)).to eq
|
28
|
+
expect(add(y, two)).to eq(4)
|
29
|
+
expect(sub(y, two)).to eq(0)
|
30
|
+
expect(mul(y, two)).to eq(4)
|
31
|
+
expect(div(y, two)).to eq(1)
|
30
32
|
end
|
31
33
|
|
32
34
|
it 'performs an arithmetic operation with string operands' do
|
33
|
-
expect(add(x, y)).to eq
|
34
|
-
expect(sub(x, y)).to eq
|
35
|
-
expect(mul(x, y)).to eq
|
36
|
-
expect(div(x, y)).to eq
|
35
|
+
expect(add(x, y)).to eq(3)
|
36
|
+
expect(sub(x, y)).to eq(-1)
|
37
|
+
expect(mul(x, y)).to eq(2)
|
38
|
+
expect(div(x, y)).to eq(0.5)
|
39
|
+
expect(neg(x)).to eq(-1)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'correctly parses string operands to numeric values' do
|
43
|
+
expect(add(x, one, 'x' => '1')).to eq(2)
|
44
|
+
expect(add(x, one, 'x' => '1.1')).to eq(2.1)
|
45
|
+
expect(add(x, one, 'x' => '.1')).to eq(1.1)
|
46
|
+
expect { add(x, one, 'x' => 'invalid') }.to raise_error(Dentaku::ArgumentError)
|
47
|
+
expect { add(x, one, 'x' => '') }.to raise_error(Dentaku::ArgumentError)
|
48
|
+
|
49
|
+
int_one = Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, "1")
|
50
|
+
decimal_one = Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, "1.0")
|
51
|
+
|
52
|
+
expect(add(int_one, int_one).class).to eq(Integer)
|
53
|
+
expect(add(int_one, decimal_one).class).to eq(BigDecimal)
|
54
|
+
expect(add(decimal_one, decimal_one).class).to eq(BigDecimal)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'performs arithmetic on arrays' do
|
58
|
+
expect(add(x, y, 'x' => [1], 'y' => [2])).to eq([1, 2])
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'performs date arithmetic' do
|
62
|
+
expect(add(date, one)).to eq(DateTime.new(2020, 4, 17))
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'raises ArgumentError if given individually valid but incompatible arguments' do
|
66
|
+
expect { add(one, date) }.to raise_error(Dentaku::ArgumentError)
|
67
|
+
expect { add(x, one, 'x' => [1]) }.to raise_error(Dentaku::ArgumentError)
|
37
68
|
end
|
38
69
|
|
39
70
|
private
|
40
71
|
|
41
|
-
def add(left, right)
|
42
|
-
Dentaku::AST::Addition.new(left, right).value(
|
72
|
+
def add(left, right, context = ctx)
|
73
|
+
Dentaku::AST::Addition.new(left, right).value(context)
|
74
|
+
end
|
75
|
+
|
76
|
+
def sub(left, right, context = ctx)
|
77
|
+
Dentaku::AST::Subtraction.new(left, right).value(context)
|
43
78
|
end
|
44
79
|
|
45
|
-
def
|
46
|
-
Dentaku::AST::
|
80
|
+
def mul(left, right, context = ctx)
|
81
|
+
Dentaku::AST::Multiplication.new(left, right).value(context)
|
47
82
|
end
|
48
83
|
|
49
|
-
def
|
50
|
-
Dentaku::AST::
|
84
|
+
def div(left, right, context = ctx)
|
85
|
+
Dentaku::AST::Division.new(left, right).value(context)
|
51
86
|
end
|
52
87
|
|
53
|
-
def
|
54
|
-
Dentaku::AST::
|
88
|
+
def neg(node, context = ctx)
|
89
|
+
Dentaku::AST::Negation.new(node).value(context)
|
55
90
|
end
|
56
91
|
end
|
data/spec/ast/avg_spec.rb
CHANGED
@@ -5,29 +5,33 @@ require 'dentaku'
|
|
5
5
|
describe 'Dentaku::AST::Function::Avg' do
|
6
6
|
it 'returns the average of an array of Numeric values' do
|
7
7
|
result = Dentaku('AVG(1, x, 1.8)', x: 2.3)
|
8
|
-
expect(result).to eq
|
8
|
+
expect(result).to eq(1.7)
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'returns the average of a single entry array of a Numeric value' do
|
12
12
|
result = Dentaku('AVG(x)', x: 2.3)
|
13
|
-
expect(result).to eq
|
13
|
+
expect(result).to eq(2.3)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'returns the average even if a String is passed' do
|
17
17
|
result = Dentaku('AVG(1, x, 1.8)', x: '2.3')
|
18
|
-
expect(result).to eq
|
18
|
+
expect(result).to eq(1.7)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'returns the average even if an array is passed' do
|
22
22
|
result = Dentaku('AVG(1, x, 2.3)', x: [4, 5])
|
23
|
-
expect(result).to eq
|
23
|
+
expect(result).to eq(3.075)
|
24
24
|
end
|
25
25
|
|
26
26
|
context 'checking errors' do
|
27
27
|
let(:calculator) { Dentaku::Calculator.new }
|
28
28
|
|
29
29
|
it 'raises an error if no arguments are passed' do
|
30
|
-
expect { calculator.evaluate!('AVG()') }.to raise_error(ArgumentError)
|
30
|
+
expect { calculator.evaluate!('AVG()') }.to raise_error(Dentaku::ArgumentError)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'raises an error if an empty array is passed' do
|
34
|
+
expect { calculator.evaluate!('AVG(x)', x: []) }.to raise_error(Dentaku::ArgumentError)
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
data/spec/ast/comparator_spec.rb
CHANGED
@@ -5,10 +5,15 @@ require 'dentaku/token'
|
|
5
5
|
|
6
6
|
describe Dentaku::AST::Comparator do
|
7
7
|
let(:one) { Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, 1) }
|
8
|
+
let(:one_str) { Dentaku::AST::String.new Dentaku::Token.new(:string, '1') }
|
8
9
|
let(:two) { Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, 2) }
|
10
|
+
let(:two_str) { Dentaku::AST::String.new Dentaku::Token.new(:string, '2') }
|
9
11
|
let(:x) { Dentaku::AST::Identifier.new Dentaku::Token.new(:identifier, 'x') }
|
10
12
|
let(:y) { Dentaku::AST::Identifier.new Dentaku::Token.new(:identifier, 'y') }
|
11
|
-
let(:
|
13
|
+
let(:nilly) do
|
14
|
+
Dentaku::AST::Identifier.new Dentaku::Token.new(:identifier, 'nilly')
|
15
|
+
end
|
16
|
+
let(:ctx) { { 'x' => 'hello', 'y' => 'world', 'nilly' => nil } }
|
12
17
|
|
13
18
|
it 'performs comparison with numeric operands' do
|
14
19
|
expect(less_than(one, two).value(ctx)).to be_truthy
|
@@ -18,6 +23,31 @@ describe Dentaku::AST::Comparator do
|
|
18
23
|
expect(equal(x, y).value(ctx)).to be_falsey
|
19
24
|
end
|
20
25
|
|
26
|
+
it 'performs conversion from string to numeric operands' do
|
27
|
+
expect(less_than(one, two_str).value(ctx)).to be_truthy
|
28
|
+
expect(less_than(one_str, two_str).value(ctx)).to be_truthy
|
29
|
+
expect(less_than(one_str, two).value(ctx)).to be_truthy
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'raises a dentaku argument error when incorrect arguments are passed in' do
|
33
|
+
expect { less_than(one, nilly).value(ctx) }.to raise_error Dentaku::ArgumentError
|
34
|
+
expect { less_than_or_equal(one, nilly).value(ctx) }.to raise_error Dentaku::ArgumentError
|
35
|
+
expect { greater_than(one, nilly).value(ctx) }.to raise_error Dentaku::ArgumentError
|
36
|
+
expect { greater_than_or_equal(one, nilly).value(ctx) }.to raise_error Dentaku::ArgumentError
|
37
|
+
expect { greater_than_or_equal(one, x).value(ctx) }.to raise_error Dentaku::ArgumentError
|
38
|
+
expect { not_equal(one, nilly).value(ctx) }.to_not raise_error
|
39
|
+
expect { equal(one, nilly).value(ctx) }.to_not raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'raises a dentaku argument error when nil is passed in as first argument' do
|
43
|
+
expect { less_than(nilly, one).value(ctx) }.to raise_error Dentaku::ArgumentError
|
44
|
+
expect { less_than_or_equal(nilly, one).value(ctx) }.to raise_error Dentaku::ArgumentError
|
45
|
+
expect { greater_than(nilly, one).value(ctx) }.to raise_error Dentaku::ArgumentError
|
46
|
+
expect { greater_than_or_equal(nilly, one).value(ctx) }.to raise_error Dentaku::ArgumentError
|
47
|
+
expect { not_equal(nilly, one).value(ctx) }.to_not raise_error
|
48
|
+
expect { equal(nilly, one).value(ctx) }.to_not raise_error
|
49
|
+
end
|
50
|
+
|
21
51
|
it 'returns correct operator symbols' do
|
22
52
|
expect(less_than(one, two).operator).to eq(:<)
|
23
53
|
expect(less_than_or_equal(one, two).operator).to eq(:<=)
|
data/spec/ast/count_spec.rb
CHANGED
@@ -5,36 +5,36 @@ require 'dentaku'
|
|
5
5
|
describe 'Dentaku::AST::Count' do
|
6
6
|
it 'returns the length of an array' do
|
7
7
|
result = Dentaku('COUNT(1, x, 1.8)', x: 2.3)
|
8
|
-
expect(result).to eq
|
8
|
+
expect(result).to eq(3)
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'returns the length of a single number object' do
|
12
12
|
result = Dentaku('COUNT(x)', x: 2.3)
|
13
|
-
expect(result).to eq
|
13
|
+
expect(result).to eq(1)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'returns the length if a single String is passed' do
|
17
17
|
result = Dentaku('COUNT(x)', x: 'dentaku')
|
18
|
-
expect(result).to eq
|
18
|
+
expect(result).to eq(7)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'returns the length if an array is passed' do
|
22
22
|
result = Dentaku('COUNT(x)', x: [4, 5])
|
23
|
-
expect(result).to eq
|
23
|
+
expect(result).to eq(2)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'returns the length if an array with one element is passed' do
|
27
27
|
result = Dentaku('COUNT(x)', x: [4])
|
28
|
-
expect(result).to eq
|
28
|
+
expect(result).to eq(1)
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'returns the length if an array even if it has nested array' do
|
32
32
|
result = Dentaku('COUNT(1, x, 3)', x: [4, 5])
|
33
|
-
expect(result).to eq
|
33
|
+
expect(result).to eq(3)
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'returns the length if an array is passed' do
|
37
37
|
result = Dentaku('COUNT()')
|
38
|
-
expect(result).to eq
|
38
|
+
expect(result).to eq(0)
|
39
39
|
end
|
40
40
|
end
|
data/spec/ast/division_spec.rb
CHANGED
@@ -9,9 +9,15 @@ describe Dentaku::AST::Division do
|
|
9
9
|
|
10
10
|
let(:t) { Dentaku::AST::Numeric.new Dentaku::Token.new(:logical, true) }
|
11
11
|
|
12
|
+
it 'allows access to its sub-trees' do
|
13
|
+
node = described_class.new(five, six)
|
14
|
+
expect(node.left).to eq(five)
|
15
|
+
expect(node.right).to eq(six)
|
16
|
+
end
|
17
|
+
|
12
18
|
it 'performs division' do
|
13
19
|
node = described_class.new(five, six)
|
14
|
-
expect(node.value.round(4)).to eq
|
20
|
+
expect(node.value.round(4)).to eq(0.8333)
|
15
21
|
end
|
16
22
|
|
17
23
|
it 'requires numeric operands' do
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'dentaku/ast/functions/filter'
|
3
|
+
require 'dentaku'
|
4
|
+
|
5
|
+
describe Dentaku::AST::Filter do
|
6
|
+
let(:calculator) { Dentaku::Calculator.new }
|
7
|
+
it 'excludes unmatched values' do
|
8
|
+
result = Dentaku('SUM(FILTER(vals, val, val > 1))', vals: [1, 2, 3])
|
9
|
+
expect(result).to eq(5)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'works with a single value if needed for some reason' do
|
13
|
+
result = Dentaku('FILTER(vals, val, val > 1)', vals: 1)
|
14
|
+
expect(result).to eq([])
|
15
|
+
|
16
|
+
result = Dentaku('FILTER(vals, val, val > 1)', vals: 2)
|
17
|
+
expect(result).to eq([2])
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'raises argument error if a string is passed as identifier' do
|
21
|
+
expect { calculator.evaluate!('FILTER({1, 2, 3}, "val", val % 2 == 0)') }.to raise_error(
|
22
|
+
Dentaku::ArgumentError, 'FILTER() requires second argument to be an identifier'
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
data/spec/ast/function_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe Dentaku::AST::Function do
|
|
14
14
|
described_class.register("flarble", :string, -> { "flarble" })
|
15
15
|
expect { described_class.get("flarble") }.not_to raise_error
|
16
16
|
function = described_class.get("flarble").new
|
17
|
-
expect(function.value).to eq
|
17
|
+
expect(function.value).to eq("flarble")
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'does not throw an error when registering a function with a name that matches a currently defined constant' do
|
@@ -22,15 +22,15 @@ describe Dentaku::AST::Function do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
describe "#arity" do
|
25
|
-
it "
|
25
|
+
it "returns the correct arity for custom functions" do
|
26
26
|
zero = described_class.register("zero", :numeric, ->() { 0 })
|
27
|
-
expect(zero.arity).to eq
|
27
|
+
expect(zero.arity).to eq(0)
|
28
28
|
|
29
29
|
one = described_class.register("one", :numeric, ->(x) { x * 2 })
|
30
|
-
expect(one.arity).to eq
|
30
|
+
expect(one.arity).to eq(1)
|
31
31
|
|
32
32
|
two = described_class.register("two", :numeric, ->(x, y) { x + y })
|
33
|
-
expect(two.arity).to eq
|
33
|
+
expect(two.arity).to eq(2)
|
34
34
|
|
35
35
|
many = described_class.register("many", :numeric, ->(*args) { args.max })
|
36
36
|
expect(many.arity).to be_nil
|
@@ -38,27 +38,32 @@ describe Dentaku::AST::Function do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'casts a String to an Integer if possible' do
|
41
|
-
expect(described_class.numeric('3')).to eq
|
41
|
+
expect(described_class.numeric('3')).to eq(3)
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'casts a String to a BigDecimal if possible and if Integer would loose information' do
|
45
|
-
expect(described_class.numeric('3.2')).to eq
|
45
|
+
expect(described_class.numeric('3.2')).to eq(3.2)
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'casts a String to a BigDecimal with a negative number' do
|
49
|
-
expect(described_class.numeric('-3.2')).to eq
|
49
|
+
expect(described_class.numeric('-3.2')).to eq(-3.2)
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'casts a String to a BigDecimal without a leading zero' do
|
53
|
-
expect(described_class.numeric('-.2')).to eq
|
53
|
+
expect(described_class.numeric('-.2')).to eq(-0.2)
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'raises an error if the value could not be cast to a Numeric' do
|
57
|
-
expect { described_class.numeric('flarble') }.to raise_error
|
58
|
-
expect { described_class.numeric('-') }.to raise_error
|
59
|
-
expect { described_class.numeric('') }.to raise_error
|
60
|
-
expect { described_class.numeric(nil) }.to raise_error
|
61
|
-
expect { described_class.numeric('7.') }.to raise_error
|
62
|
-
expect { described_class.numeric(true) }.to raise_error
|
57
|
+
expect { described_class.numeric('flarble') }.to raise_error Dentaku::ArgumentError
|
58
|
+
expect { described_class.numeric('-') }.to raise_error Dentaku::ArgumentError
|
59
|
+
expect { described_class.numeric('') }.to raise_error Dentaku::ArgumentError
|
60
|
+
expect { described_class.numeric(nil) }.to raise_error Dentaku::ArgumentError
|
61
|
+
expect { described_class.numeric('7.') }.to raise_error Dentaku::ArgumentError
|
62
|
+
expect { described_class.numeric(true) }.to raise_error Dentaku::ArgumentError
|
63
|
+
end
|
64
|
+
|
65
|
+
it "allows read access to arguments" do
|
66
|
+
fn = described_class.new(1, 2, 3)
|
67
|
+
expect(fn.args).to eq([1, 2, 3])
|
63
68
|
end
|
64
69
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'dentaku/ast/functions/map'
|
3
|
+
require 'dentaku'
|
4
|
+
|
5
|
+
describe Dentaku::AST::Map do
|
6
|
+
let(:calculator) { Dentaku::Calculator.new }
|
7
|
+
it 'operates on each value in an array' do
|
8
|
+
result = Dentaku('SUM(MAP(vals, val, val + 1))', vals: [1, 2, 3])
|
9
|
+
expect(result).to eq(9)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'works with an empty array' do
|
13
|
+
result = Dentaku('MAP(vals, val, val + 1)', vals: [])
|
14
|
+
expect(result).to eq([])
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'works with a single value if needed for some reason' do
|
18
|
+
result = Dentaku('MAP(vals, val, val + 1)', vals: 1)
|
19
|
+
expect(result).to eq([2])
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'raises argument error if a string is passed as identifier' do
|
23
|
+
expect { calculator.evaluate!('MAP({1, 2, 3}, "val", val + 1)') }.to raise_error(
|
24
|
+
Dentaku::ArgumentError, 'MAP() requires second argument to be an identifier'
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
data/spec/ast/max_spec.rb
CHANGED
@@ -5,16 +5,29 @@ require 'dentaku'
|
|
5
5
|
describe 'Dentaku::AST::Function::Max' do
|
6
6
|
it 'returns the largest numeric value in an array of Numeric values' do
|
7
7
|
result = Dentaku('MAX(1, x, 1.8)', x: 2.3)
|
8
|
-
expect(result).to eq
|
8
|
+
expect(result).to eq(2.3)
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'returns the largest value even if a String is passed' do
|
12
12
|
result = Dentaku('MAX(1, x, 1.8)', x: '2.3')
|
13
|
-
expect(result).to eq
|
13
|
+
expect(result).to eq(2.3)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'returns the largest value even if an Array is passed' do
|
17
17
|
result = Dentaku('MAX(1, x, 1.8)', x: [1.5, 2.3, 1.7])
|
18
|
-
expect(result).to eq
|
18
|
+
expect(result).to eq(2.3)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns the largest value if only an Array is passed' do
|
22
|
+
result = Dentaku('MAX(x)', x: [1.5, 2.3, 1.7])
|
23
|
+
expect(result).to eq(2.3)
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'checking errors' do
|
27
|
+
let(:calculator) { Dentaku::Calculator.new }
|
28
|
+
|
29
|
+
it 'does not raise an error if an empty array is passed' do
|
30
|
+
expect(calculator.evaluate!('MAX(x)', x: [])).to eq(nil)
|
31
|
+
end
|
19
32
|
end
|
20
33
|
end
|
data/spec/ast/min_spec.rb
CHANGED
@@ -5,16 +5,29 @@ require 'dentaku'
|
|
5
5
|
describe 'Dentaku::AST::Function::Min' do
|
6
6
|
it 'returns the smallest numeric value in an array of Numeric values' do
|
7
7
|
result = Dentaku('MIN(1, x, 1.8)', x: 2.3)
|
8
|
-
expect(result).to eq
|
8
|
+
expect(result).to eq(1)
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'returns the smallest value even if a String is passed' do
|
12
12
|
result = Dentaku('MIN(1, x, 1.8)', x: '0.3')
|
13
|
-
expect(result).to eq
|
13
|
+
expect(result).to eq(0.3)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'returns the smallest value even if an Array is passed' do
|
17
17
|
result = Dentaku('MIN(1, x, 1.8)', x: [1.5, 0.3, 1.7])
|
18
|
-
expect(result).to eq
|
18
|
+
expect(result).to eq(0.3)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns the smallest value if only an Array is passed' do
|
22
|
+
result = Dentaku('MIN(x)', x: [1.5, 2.3, 1.7])
|
23
|
+
expect(result).to eq(1.5)
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'checking errors' do
|
27
|
+
let(:calculator) { Dentaku::Calculator.new }
|
28
|
+
|
29
|
+
it 'does not raise an error if an empty array is passed' do
|
30
|
+
expect(calculator.evaluate!('MIN(x)', x: [])).to eq(nil)
|
31
|
+
end
|
19
32
|
end
|
20
33
|
end
|
data/spec/ast/mul_spec.rb
CHANGED
@@ -5,34 +5,39 @@ require 'dentaku'
|
|
5
5
|
describe 'Dentaku::AST::Function::Mul' do
|
6
6
|
it 'returns the product of an array of Numeric values' do
|
7
7
|
result = Dentaku('MUL(1, x, 1.8)', x: 2.3)
|
8
|
-
expect(result).to eq
|
8
|
+
expect(result).to eq(4.14)
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'returns the product of a single entry array of a Numeric value' do
|
12
12
|
result = Dentaku('MUL(x)', x: 2.3)
|
13
|
-
expect(result).to eq
|
13
|
+
expect(result).to eq(2.3)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'coerces string inputs to numeric' do
|
17
17
|
result = Dentaku('mul(1, x, 1.8)', x: '2.3')
|
18
|
-
expect(result).to eq
|
18
|
+
expect(result).to eq(4.14)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'returns the product even if an array is passed' do
|
22
22
|
result = Dentaku('mul(1, x, 2.3)', x: [4, 5])
|
23
|
-
expect(result).to eq
|
23
|
+
expect(result).to eq(46)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'handles nested calls' do
|
27
27
|
result = Dentaku('mul(1, x, mul(4, 5))', x: '2.3')
|
28
|
-
expect(result).to eq
|
28
|
+
expect(result).to eq(46)
|
29
29
|
end
|
30
30
|
|
31
31
|
context 'checking errors' do
|
32
32
|
let(:calculator) { Dentaku::Calculator.new }
|
33
33
|
|
34
34
|
it 'raises an error if no arguments are passed' do
|
35
|
-
expect { calculator.evaluate!('MUL()') }.to raise_error(ArgumentError)
|
35
|
+
expect { calculator.evaluate!('MUL()') }.to raise_error(Dentaku::ArgumentError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'does not raise an error if an empty array is passed' do
|
39
|
+
result = calculator.evaluate!('MUL(x)', x: [])
|
40
|
+
expect(result).to eq(1)
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'dentaku/ast/arithmetic'
|
3
|
+
|
4
|
+
require 'dentaku/token'
|
5
|
+
|
6
|
+
describe Dentaku::AST::Negation do
|
7
|
+
let(:five) { Dentaku::AST::Numeric.new Dentaku::Token.new(:numeric, 5) }
|
8
|
+
let(:t) { Dentaku::AST::Logical.new Dentaku::Token.new(:logical, true) }
|
9
|
+
let(:x) { Dentaku::AST::Identifier.new Dentaku::Token.new(:identifier, 'x') }
|
10
|
+
|
11
|
+
it 'allows access to its sub-node' do
|
12
|
+
node = described_class.new(five)
|
13
|
+
expect(node.node).to eq(five)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'performs negation' do
|
17
|
+
node = described_class.new(five)
|
18
|
+
expect(node.value).to eq(-5)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'requires numeric operands' do
|
22
|
+
expect {
|
23
|
+
described_class.new(t)
|
24
|
+
}.to raise_error(Dentaku::NodeError, /requires numeric operands/)
|
25
|
+
|
26
|
+
expression = Dentaku::AST::Negation.new(five)
|
27
|
+
group = Dentaku::AST::Grouping.new(expression)
|
28
|
+
|
29
|
+
expect {
|
30
|
+
described_class.new(group)
|
31
|
+
}.not_to raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'correctly parses string operands to numeric values' do
|
35
|
+
node = described_class.new(x)
|
36
|
+
expect(node.value('x' => '5')).to eq(-5)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'raises error if input string is not coercible to numeric' do
|
40
|
+
node = described_class.new(x)
|
41
|
+
expect { node.value('x' => 'invalid') }.to raise_error(Dentaku::ArgumentError)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'raises error if given a non-numeric argument' do
|
45
|
+
node = described_class.new(x)
|
46
|
+
expect { node.value('x' => true) }.to raise_error(Dentaku::ArgumentError)
|
47
|
+
end
|
48
|
+
end
|
data/spec/ast/node_spec.rb
CHANGED
@@ -6,30 +6,33 @@ require 'dentaku/parser'
|
|
6
6
|
describe Dentaku::AST::Node do
|
7
7
|
it 'returns list of dependencies' do
|
8
8
|
node = make_node('x + 5')
|
9
|
-
expect(node.dependencies).to eq
|
9
|
+
expect(node.dependencies).to eq(['x'])
|
10
10
|
|
11
11
|
node = make_node('5 < x')
|
12
|
-
expect(node.dependencies).to eq
|
12
|
+
expect(node.dependencies).to eq(['x'])
|
13
13
|
|
14
14
|
node = make_node('5 < 7')
|
15
|
-
expect(node.dependencies).to eq
|
15
|
+
expect(node.dependencies).to eq([])
|
16
16
|
|
17
17
|
node = make_node('(y * 7)')
|
18
|
-
expect(node.dependencies).to eq
|
18
|
+
expect(node.dependencies).to eq(['y'])
|
19
19
|
|
20
20
|
node = make_node('if(x > 5, y, z)')
|
21
|
-
expect(node.dependencies).to eq
|
21
|
+
expect(node.dependencies).to eq(['x', 'y', 'z'])
|
22
22
|
|
23
23
|
node = make_node('if(x > 5, y, z)')
|
24
|
-
expect(node.dependencies('x' => 7)).to eq
|
24
|
+
expect(node.dependencies('x' => 7)).to eq(['y'])
|
25
|
+
|
26
|
+
node = make_node('if(x > 5, y, z)')
|
27
|
+
expect(node.dependencies('x' => 2)).to eq(['z'])
|
25
28
|
|
26
29
|
node = make_node('')
|
27
|
-
expect(node.dependencies).to eq
|
30
|
+
expect(node.dependencies).to eq([])
|
28
31
|
end
|
29
32
|
|
30
33
|
it 'returns unique list of dependencies' do
|
31
34
|
node = make_node('x + x')
|
32
|
-
expect(node.dependencies).to eq
|
35
|
+
expect(node.dependencies).to eq(['x'])
|
33
36
|
end
|
34
37
|
|
35
38
|
private
|
data/spec/ast/numeric_spec.rb
CHANGED