dentaku 4.1.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +1 -1
- data/.github/workflows/rubocop.yml +1 -1
- data/CHANGELOG.md +75 -0
- data/README.md +176 -11
- data/lib/dentaku/ast/access.rb +14 -11
- data/lib/dentaku/ast/arithmetic.rb +17 -1
- data/lib/dentaku/ast/array.rb +2 -0
- data/lib/dentaku/ast/bitwise.rb +16 -1
- data/lib/dentaku/ast/combinators.rb +83 -0
- data/lib/dentaku/ast/functions/directed_rounding.rb +37 -0
- data/lib/dentaku/ast/functions/mul.rb +2 -1
- data/lib/dentaku/ast/functions/rounddown.rb +2 -4
- data/lib/dentaku/ast/functions/roundup.rb +2 -4
- data/lib/dentaku/ast/member_access.rb +56 -0
- data/lib/dentaku/ast/negation.rb +4 -0
- data/lib/dentaku/ast/operation.rb +6 -0
- data/lib/dentaku/ast.rb +2 -0
- data/lib/dentaku/bulk_expression_solver.rb +40 -2
- data/lib/dentaku/calculator.rb +26 -3
- data/lib/dentaku/dependency_resolver.rb +12 -0
- data/lib/dentaku/humanize_visitor.rb +276 -0
- data/lib/dentaku/numeric_limits.rb +67 -0
- data/lib/dentaku/numeric_parser.rb +6 -3
- data/lib/dentaku/parser.rb +15 -2
- data/lib/dentaku/print_visitor.rb +38 -2
- data/lib/dentaku/token_scanner.rb +36 -12
- data/lib/dentaku/tokenizer.rb +6 -1
- data/lib/dentaku/version.rb +1 -1
- data/lib/dentaku/visitor/infix.rb +4 -0
- data/mise.toml +2 -0
- data/spec/ast/arithmetic_spec.rb +1 -1
- data/spec/ast/member_access_spec.rb +124 -0
- data/spec/builtin_functions_spec.rb +26 -0
- data/spec/bulk_expression_solver_spec.rb +71 -5
- data/spec/calculator_spec.rb +175 -1
- data/spec/humanize_visitor_spec.rb +217 -0
- data/spec/numeric_limits_spec.rb +59 -0
- data/spec/parser_spec.rb +8 -0
- data/spec/print_visitor_spec.rb +66 -0
- data/spec/tokenizer_spec.rb +19 -0
- data/spec/visitor_spec.rb +7 -0
- metadata +10 -2
- data/lib/dentaku/token_matchers.rb +0 -29
|
@@ -84,12 +84,29 @@ module Dentaku
|
|
|
84
84
|
end
|
|
85
85
|
|
|
86
86
|
def visit_access(node)
|
|
87
|
-
node.structure.
|
|
87
|
+
visit_accessed(node.structure, group_access_structure?(node.structure))
|
|
88
88
|
@output << "["
|
|
89
89
|
node.index.accept(self)
|
|
90
90
|
@output << "]"
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
+
def visit_member_access(node)
|
|
94
|
+
visit_accessed(node.structure, group_member_structure?(node.structure))
|
|
95
|
+
@output << "." << node.member
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# `[...]` can directly follow a variable, an array literal, another
|
|
99
|
+
# access or a function call; anything else is grouped, e.g. `(a + b)[0]`
|
|
100
|
+
def group_access_structure?(node)
|
|
101
|
+
!(node.is_a?(Dentaku::AST::Identifier) || node.is_a?(Dentaku::AST::Array) || chainable?(node))
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# anything else would re-parse differently, e.g. `(user).name` as the
|
|
105
|
+
# variable `user.name`
|
|
106
|
+
def group_member_structure?(node)
|
|
107
|
+
!chainable?(node)
|
|
108
|
+
end
|
|
109
|
+
|
|
93
110
|
def visit_literal(node)
|
|
94
111
|
@output << node.quoted
|
|
95
112
|
end
|
|
@@ -103,11 +120,30 @@ module Dentaku
|
|
|
103
120
|
end
|
|
104
121
|
|
|
105
122
|
def visit_array(node)
|
|
106
|
-
@output <<
|
|
123
|
+
@output << "{"
|
|
124
|
+
element_count = node.elements.length
|
|
125
|
+
node.elements.each_with_index do |element, index|
|
|
126
|
+
element.accept(self)
|
|
127
|
+
@output << ", " unless index >= element_count - 1
|
|
128
|
+
end
|
|
129
|
+
@output << "}"
|
|
107
130
|
end
|
|
108
131
|
|
|
109
132
|
def to_s
|
|
110
133
|
@output
|
|
111
134
|
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def visit_accessed(node, grouped)
|
|
139
|
+
@output << "(" if grouped
|
|
140
|
+
node.accept(self)
|
|
141
|
+
@output << ")" if grouped
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# `[...]` and `.name` can follow these without grouping
|
|
145
|
+
def chainable?(node)
|
|
146
|
+
node.is_a?(Dentaku::AST::Access) || node.is_a?(Dentaku::AST::Function)
|
|
147
|
+
end
|
|
112
148
|
end
|
|
113
149
|
end
|
|
@@ -39,10 +39,12 @@ module Dentaku
|
|
|
39
39
|
:single_quoted_string,
|
|
40
40
|
:negate,
|
|
41
41
|
:combinator,
|
|
42
|
+
:logical_not,
|
|
42
43
|
:operator,
|
|
43
44
|
:grouping,
|
|
44
45
|
:array,
|
|
45
46
|
:access,
|
|
47
|
+
:member_access,
|
|
46
48
|
:case_statement,
|
|
47
49
|
:comparator,
|
|
48
50
|
:boolean,
|
|
@@ -105,17 +107,22 @@ module Dentaku
|
|
|
105
107
|
new(:string, "'[^']*'", lambda { |raw| raw.gsub(/^'|'$/, '') })
|
|
106
108
|
end
|
|
107
109
|
|
|
110
|
+
# whether the next token starts an operand, which makes `-` a negation
|
|
111
|
+
# and `not` the prefix operator rather than a binary one
|
|
112
|
+
def operand_expected?(last_token)
|
|
113
|
+
last_token.nil? ||
|
|
114
|
+
last_token.is?(:operator) ||
|
|
115
|
+
last_token.is?(:comparator) ||
|
|
116
|
+
last_token.is?(:combinator) ||
|
|
117
|
+
(last_token.is?(:case) && last_token.value != :close) ||
|
|
118
|
+
last_token.value == :open ||
|
|
119
|
+
last_token.value == :comma ||
|
|
120
|
+
last_token.value == :lbracket ||
|
|
121
|
+
last_token.value == :array_start
|
|
122
|
+
end
|
|
123
|
+
|
|
108
124
|
def negate
|
|
109
|
-
new(:operator, '-', lambda { |raw| :negate }, lambda { |last_token|
|
|
110
|
-
last_token.nil? ||
|
|
111
|
-
last_token.is?(:operator) ||
|
|
112
|
-
last_token.is?(:comparator) ||
|
|
113
|
-
last_token.is?(:combinator) ||
|
|
114
|
-
last_token.value == :open ||
|
|
115
|
-
last_token.value == :comma ||
|
|
116
|
-
last_token.value == :lbracket ||
|
|
117
|
-
last_token.value == :array_start
|
|
118
|
-
})
|
|
125
|
+
new(:operator, '-', lambda { |raw| :negate }, lambda { |last_token| operand_expected?(last_token) })
|
|
119
126
|
end
|
|
120
127
|
|
|
121
128
|
def operator
|
|
@@ -140,6 +147,16 @@ module Dentaku
|
|
|
140
147
|
new(:access, '\[|\]', lambda { |raw| names[raw] })
|
|
141
148
|
end
|
|
142
149
|
|
|
150
|
+
# `.name` after `]`, `)` or another `.name`; elsewhere a dot is part of
|
|
151
|
+
# an identifier (`item.name`)
|
|
152
|
+
def member_access
|
|
153
|
+
new(:member_access, '\.[[:alpha:]_][[:word:]]*', lambda { |raw| raw[1..] }, lambda { |last_token|
|
|
154
|
+
last_token&.is?(:member_access) ||
|
|
155
|
+
last_token&.value == :rbracket ||
|
|
156
|
+
last_token&.close?
|
|
157
|
+
})
|
|
158
|
+
end
|
|
159
|
+
|
|
143
160
|
def case_statement
|
|
144
161
|
names = { open: 'case', close: 'end', then: 'then', when: 'when', else: 'else' }.invert
|
|
145
162
|
new(:case, '(case|end|then|when|else)\b', lambda { |raw| names[raw.downcase] })
|
|
@@ -151,12 +168,19 @@ module Dentaku
|
|
|
151
168
|
new(:comparator, '<=|>=|!=|<>|<|>|==|=', lambda { |raw| names[raw] || alternate[raw] })
|
|
152
169
|
end
|
|
153
170
|
|
|
171
|
+
# binary, so only after an operand: elsewhere `xor (a, b)` is a call
|
|
154
172
|
def combinator
|
|
155
173
|
names = { and: '&&', or: '||' }.invert
|
|
156
|
-
new(:combinator, '(and|or|&&|\|\|)\s', lambda { |raw|
|
|
174
|
+
new(:combinator, '(and|or|xor|&&|\|\|)\s', lambda { |raw|
|
|
157
175
|
norm = raw.strip.downcase
|
|
158
176
|
names.fetch(norm) { norm.to_sym }
|
|
159
|
-
})
|
|
177
|
+
}, lambda { |last_token| !operand_expected?(last_token) })
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# `not` followed by `(`, with or without whitespace, stays the NOT
|
|
181
|
+
# function; `!` must not start `!=`
|
|
182
|
+
def logical_not
|
|
183
|
+
new(:combinator, 'not(?=\s)(?!\s*\()|!(?!=)', lambda { |raw| :not }, lambda { |last_token| operand_expected?(last_token) })
|
|
160
184
|
end
|
|
161
185
|
|
|
162
186
|
def boolean
|
data/lib/dentaku/tokenizer.rb
CHANGED
|
@@ -12,6 +12,7 @@ module Dentaku
|
|
|
12
12
|
def tokenize(string, options = {})
|
|
13
13
|
@nesting = 0
|
|
14
14
|
@tokens = []
|
|
15
|
+
@after_whitespace = false
|
|
15
16
|
@aliases = options.fetch(:aliases, global_aliases)
|
|
16
17
|
input = strip_comments(string.to_s.dup)
|
|
17
18
|
input = replace_aliases(input)
|
|
@@ -52,7 +53,11 @@ module Dentaku
|
|
|
52
53
|
@nesting -= 1 if RPAREN == token
|
|
53
54
|
fail! :too_many_closing_parentheses if @nesting < 0
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
# `a[0] .name` is rejected like `a .name`
|
|
57
|
+
fail! :parse_error, at: string if token.is?(:member_access) && @after_whitespace
|
|
58
|
+
|
|
59
|
+
@after_whitespace = token.is?(:whitespace)
|
|
60
|
+
@tokens << token unless @after_whitespace
|
|
56
61
|
end
|
|
57
62
|
|
|
58
63
|
match_length = tokens.map(&:length).reduce(:+)
|
data/lib/dentaku/version.rb
CHANGED
data/mise.toml
ADDED
data/spec/ast/arithmetic_spec.rb
CHANGED
|
@@ -182,7 +182,7 @@ describe Dentaku::AST::Arithmetic do
|
|
|
182
182
|
# non-finite result guard turns into the equivalent Dentaku error.
|
|
183
183
|
expect {
|
|
184
184
|
Dentaku::Calculator.new.evaluate!(oversized)
|
|
185
|
-
}.to raise_error(Dentaku::ArgumentError, /exponent is too large|is not a finite number/)
|
|
185
|
+
}.to raise_error(Dentaku::ArgumentError, /more than \d+ digits|exponent is too large|is not a finite number/)
|
|
186
186
|
end
|
|
187
187
|
|
|
188
188
|
it 'is rescuable as Dentaku::Error' do
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'dentaku'
|
|
3
|
+
|
|
4
|
+
describe Dentaku::AST::MemberAccess do
|
|
5
|
+
let(:calculator) { Dentaku::Calculator.new }
|
|
6
|
+
let(:users) do
|
|
7
|
+
[
|
|
8
|
+
{ age: 20, name: 'Ann', account: { year: 2019 } },
|
|
9
|
+
{ :age => 21, 'Name' => 'Bob', :account => { year: 2022 } }
|
|
10
|
+
]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def evaluate!(formula, data = { users: users })
|
|
14
|
+
calculator.evaluate!(formula, data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'reads an attribute of an indexed value' do
|
|
18
|
+
expect(evaluate!('users[0].name')).to eq('Ann')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'reads an attribute of a function result' do
|
|
22
|
+
expect(evaluate!('FILTER(users, u, u.age > 20)[0].age')).to eq(21)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'reads an attribute of a parenthesized expression' do
|
|
26
|
+
expect(evaluate!('(users[0]).age')).to eq(20)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'chains attributes' do
|
|
30
|
+
expect(evaluate!('users[1].account.year')).to eq(2022)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'binds tighter than operators' do
|
|
34
|
+
expect(evaluate!('-users[0].age + 2 * users[1].age')).to eq(22)
|
|
35
|
+
expect(evaluate!('users[1].account.year > users[0].account.year')).to eq(true)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'can be used inside an index' do
|
|
39
|
+
expect(evaluate!('users[users[0].age - 19].account.year')).to eq(2022)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'can be used as a function argument' do
|
|
43
|
+
expect(evaluate!('MAX(users[0].age, users[1].age)')).to eq(21)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'leaves dotted identifiers alone' do
|
|
47
|
+
expect(evaluate!('user.age + 1', user: { age: 30 })).to eq(31)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe 'key matching' do
|
|
51
|
+
let(:data) { { h: [{ 'a' => 1, :A => 2, 'Mixed' => 3 }] } }
|
|
52
|
+
|
|
53
|
+
it 'accepts string and symbol keys' do
|
|
54
|
+
expect(evaluate!('users[0].age')).to eq(20)
|
|
55
|
+
expect(evaluate!('h[0].a', data)).to eq(1)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'prefers an exact match and otherwise ignores case' do
|
|
59
|
+
expect(evaluate!('h[0].A', data)).to eq(2)
|
|
60
|
+
expect(evaluate!('h[0].mixed', data)).to eq(3)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'matches case exactly in case-sensitive mode' do
|
|
64
|
+
calculator = Dentaku::Calculator.new(case_sensitive: true)
|
|
65
|
+
expect(calculator.evaluate!('h[0].A', data)).to eq(2)
|
|
66
|
+
expect { calculator.evaluate!('h[0].mixed', data) }.to raise_error(Dentaku::ArgumentError)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe 'errors' do
|
|
71
|
+
it 'raises for a missing key' do
|
|
72
|
+
expect { evaluate!('users[0].agee') }.to raise_error(Dentaku::ArgumentError, /no key 'agee'/)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it 'points to brackets for keys that do not look like identifiers' do
|
|
76
|
+
data = { users: [{ 'a.b' => 1 }] }
|
|
77
|
+
expect { evaluate!('users[0].a.b', data) }.to raise_error(Dentaku::ArgumentError, /use brackets for 'a.b'/)
|
|
78
|
+
expect(evaluate!('users[0]["a.b"]', data)).to eq(1)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'raises when the operand is not a hash' do
|
|
82
|
+
expect { evaluate!('users[0].age.x') }.to raise_error(Dentaku::ArgumentError, /requires a hash, but got Integer/)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it 'is nil-safe' do
|
|
86
|
+
expect(evaluate!('FILTER(users, u, u.age > 99)[0].age')).to be_nil
|
|
87
|
+
expect(evaluate!('FILTER(users, u, u.age > 99)[0].account.year')).to be_nil
|
|
88
|
+
expect(evaluate!('(x).age', x: nil)).to be_nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it 'raises for literal operands' do
|
|
92
|
+
expect { evaluate!('("abc").x') }.to raise_error(Dentaku::ArgumentError, /but got String/)
|
|
93
|
+
expect { evaluate!('(1).x') }.to raise_error(Dentaku::ArgumentError, /but got Integer/)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it 'does not allow whitespace before the dot' do
|
|
97
|
+
expect { evaluate!('users[0] .name') }.to raise_error(Dentaku::TokenizerError)
|
|
98
|
+
expect { evaluate!('(users[0]) .name') }.to raise_error(Dentaku::TokenizerError)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it 'does not apply after CASE ... END' do
|
|
102
|
+
expect { calculator.ast('CASE 1 WHEN 1 THEN a END.x') }.to raise_error(Dentaku::ParseError)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it 'does not apply to array literals' do
|
|
106
|
+
expect { calculator.ast('{1, 2}.a') }.to raise_error(Dentaku::ParseError)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'reports dependencies of the structure' do
|
|
111
|
+
expect(calculator.dependencies('users[0].age + x', users: users)).to eq(['x'])
|
|
112
|
+
expect(calculator.identifiers('FILTER(users, u, u.age > x)[0].age')).to eq(%w[users x])
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'is an Access' do
|
|
116
|
+
node = calculator.ast('users[0].age')
|
|
117
|
+
expect(node).to be_a(Dentaku::AST::Access)
|
|
118
|
+
expect(node.member).to eq('age')
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it 'has no static type' do
|
|
122
|
+
expect(calculator.ast('users[0].age').type).to be_nil
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'open3'
|
|
3
|
+
require 'rbconfig'
|
|
4
|
+
|
|
5
|
+
# The function specs require their own files, so a function that
|
|
6
|
+
# `require 'dentaku'` never loads still passes them. Load the gem in a
|
|
7
|
+
# fresh process to see what users actually get.
|
|
8
|
+
describe 'built-in functions' do
|
|
9
|
+
it 'are all loaded by requiring dentaku' do
|
|
10
|
+
lib = File.expand_path('../lib', __dir__)
|
|
11
|
+
script = <<~RUBY
|
|
12
|
+
require 'dentaku'
|
|
13
|
+
loaded = $LOADED_FEATURES.map { |f| File.expand_path(f) }
|
|
14
|
+
puts Dir[File.join(#{lib.inspect}, 'dentaku/ast/functions/*.rb')].reject { |f| loaded.include?(f) }
|
|
15
|
+
RUBY
|
|
16
|
+
|
|
17
|
+
output, status = Open3.capture2e(RbConfig.ruby, '-I', lib, '-e', script)
|
|
18
|
+
|
|
19
|
+
expect(status).to be_success, output
|
|
20
|
+
expect(output.lines.map { |f| File.basename(f.chomp) }).to eq([])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'include MUL' do
|
|
24
|
+
expect(Dentaku::Calculator.new.evaluate!('MUL(2, 3, 4)')).to eq(24)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -23,11 +23,13 @@ RSpec.describe Dentaku::BulkExpressionSolver do
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
it 'includes memory-dependent edges in the cached order' do
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
# the stored value decides where the cycle is broken, so each calculator
|
|
27
|
+
# needs a different order for the same formulas
|
|
28
|
+
first = Dentaku::Calculator.new(cache_dependency_order: true).store(x: 5)
|
|
29
|
+
second = Dentaku::Calculator.new(cache_dependency_order: true).store(y: 3)
|
|
30
|
+
formulas = { x: 'y + 3', y: 'x * 2' }
|
|
31
|
+
expect(first.solve!(formulas)).to eq(x: 13, y: 10)
|
|
32
|
+
expect(second.solve!(formulas)).to eq(x: 6, y: 12)
|
|
31
33
|
end
|
|
32
34
|
|
|
33
35
|
it "evaluates properly with variables, even if some in memory" do
|
|
@@ -85,6 +87,51 @@ RSpec.describe Dentaku::BulkExpressionSolver do
|
|
|
85
87
|
expect(solver.solve!).to eq(x: 6, y: 12) # x = 6 by the time y is calculated
|
|
86
88
|
end
|
|
87
89
|
|
|
90
|
+
describe "when a key being solved is also stored in memory" do
|
|
91
|
+
it "gives formulas the solved value regardless of key order" do
|
|
92
|
+
calculator.store(discount: 20, foo: 20)
|
|
93
|
+
expected = { discount: 50, discount_not_applicable: true }
|
|
94
|
+
|
|
95
|
+
expect(calculator.solve!(
|
|
96
|
+
discount: "foo + 30",
|
|
97
|
+
discount_not_applicable: "IF(discount > 30, true, false)"
|
|
98
|
+
)).to eq(expected)
|
|
99
|
+
|
|
100
|
+
expect(calculator.solve!(
|
|
101
|
+
discount_not_applicable: "IF(discount > 30, true, false)",
|
|
102
|
+
discount: "foo + 30"
|
|
103
|
+
)).to eq(expected)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it "follows the solved value through a chain of formulas" do
|
|
107
|
+
calculator.store(seed: 1, middle: 100)
|
|
108
|
+
expect(calculator.solve!(
|
|
109
|
+
top: "middle * 2",
|
|
110
|
+
middle: "bottom + 1",
|
|
111
|
+
bottom: "seed + 1"
|
|
112
|
+
)).to eq(top: 6, middle: 3, bottom: 2)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "still uses the stored value to break a cycle elsewhere in the system" do
|
|
116
|
+
calculator.store(x: 5, y: 3, d: 20, foo: 1)
|
|
117
|
+
expect(calculator.solve!(
|
|
118
|
+
z: "d + 1",
|
|
119
|
+
x: "y + 3",
|
|
120
|
+
y: "x * 2",
|
|
121
|
+
d: "foo + 1"
|
|
122
|
+
)).to eq(z: 3, x: 6, y: 12, d: 2)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "does not change the reported dependencies" do
|
|
126
|
+
calculator.store(discount: 20, foo: 20)
|
|
127
|
+
solver = described_class.new(
|
|
128
|
+
{ discount_not_applicable: "IF(discount > 30, true, false)", discount: "foo + 30" },
|
|
129
|
+
calculator
|
|
130
|
+
)
|
|
131
|
+
expect(solver.dependencies).to eq("discount_not_applicable" => [], "discount" => [])
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
88
135
|
it "does not execute functions unnecessarily" do
|
|
89
136
|
calls = 0
|
|
90
137
|
external = ->() { calls += 1 }
|
|
@@ -133,6 +180,25 @@ RSpec.describe Dentaku::BulkExpressionSolver do
|
|
|
133
180
|
discounts: [0.8, 0.6]
|
|
134
181
|
)
|
|
135
182
|
end
|
|
183
|
+
|
|
184
|
+
it "solves arrays of hashes" do
|
|
185
|
+
calculator.store(bonus: 5)
|
|
186
|
+
expressions = {
|
|
187
|
+
percents: [{ percent: 75 }, { percent: "30 + bonus" }],
|
|
188
|
+
average: "(percents[0].percent + percents[1].percent) / 2",
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
expect(calculator.solve!(expressions)).to eq(
|
|
192
|
+
percents: [{ percent: 75 }, { percent: 35 }],
|
|
193
|
+
average: 55
|
|
194
|
+
)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
it "solves nested arrays" do
|
|
198
|
+
expect(calculator.solve!(grid: [[1, "1 + 1"], [{ cell: "1 + 2" }]])).to eq(
|
|
199
|
+
grid: [[1, 2], [{ cell: 3 }]]
|
|
200
|
+
)
|
|
201
|
+
end
|
|
136
202
|
end
|
|
137
203
|
|
|
138
204
|
describe "#solve" do
|
data/spec/calculator_spec.rb
CHANGED
|
@@ -163,6 +163,23 @@ describe Dentaku::Calculator do
|
|
|
163
163
|
expect(calculator.evaluate!('second')).to eq(2)
|
|
164
164
|
end
|
|
165
165
|
|
|
166
|
+
it 'collapses differently cased symbol keys unless case sensitive' do
|
|
167
|
+
calculator.store(Apples: 1)
|
|
168
|
+
calculator.store(apples: 2)
|
|
169
|
+
expect(calculator.memory).to eq("apples" => 2)
|
|
170
|
+
expect(with_case_sensitivity.store(Apples: 1).memory).to eq("Apples" => 1)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
it 'flattens a hash that mixes flat and nested values' do
|
|
174
|
+
calculator.store(a: 1, b: {c: 2})
|
|
175
|
+
expect(calculator.memory).to eq("a" => 1, "b" => {c: 2}, "b.c" => 2)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
it 'stores nested values as-is without nested data support' do
|
|
179
|
+
without_nested_data.store(a: 1, b: {c: 2})
|
|
180
|
+
expect(without_nested_data.memory).to eq("a" => 1, "b" => {c: 2})
|
|
181
|
+
end
|
|
182
|
+
|
|
166
183
|
it 'stores formulas' do
|
|
167
184
|
calculator.store_formula('area', 'length * width')
|
|
168
185
|
expect(calculator.evaluate!('area', length: 5, width: 5)).to eq(25)
|
|
@@ -202,10 +219,15 @@ describe Dentaku::Calculator do
|
|
|
202
219
|
|
|
203
220
|
it 'raises an argument error when accessing into a non-indexable value' do
|
|
204
221
|
expect { calculator.evaluate!('a[0]', a: 5) }.to raise_error(Dentaku::ArgumentError)
|
|
205
|
-
expect { calculator.evaluate!('a[0]', a: nil) }.to raise_error(Dentaku::ArgumentError)
|
|
206
222
|
expect { calculator.evaluate!('a[0]', a: true) }.to raise_error(Dentaku::ArgumentError)
|
|
207
223
|
end
|
|
208
224
|
|
|
225
|
+
it 'returns nil when accessing into nil' do
|
|
226
|
+
expect(calculator.evaluate!('a[0]', a: nil)).to be_nil
|
|
227
|
+
expect(calculator.evaluate!('a[0][1]', a: [nil])).to be_nil
|
|
228
|
+
expect(calculator.evaluate!('FILTER(a, x, x > 9)[0][0]', a: [1])).to be_nil
|
|
229
|
+
end
|
|
230
|
+
|
|
209
231
|
it 'raises an argument error when the index type is incompatible' do
|
|
210
232
|
expect { calculator.evaluate!("a['one']", a: [1, 2, 3]) }.to raise_error(Dentaku::ArgumentError)
|
|
211
233
|
end
|
|
@@ -707,6 +729,35 @@ describe Dentaku::Calculator do
|
|
|
707
729
|
it 'should accept second precision argument like in Office formula' do
|
|
708
730
|
expect(calculator.evaluate('roundup(1.234, 2)')).to eq(1.24)
|
|
709
731
|
end
|
|
732
|
+
|
|
733
|
+
it 'does not round up a float that is already exact' do
|
|
734
|
+
expect(calculator.evaluate('roundup(x, 1)', x: 0.7)).to eq(0.7)
|
|
735
|
+
expect(calculator.evaluate('roundup(x, 2)', x: 0.07)).to eq(0.07)
|
|
736
|
+
end
|
|
737
|
+
|
|
738
|
+
it 'returns the same type as its input, like round' do
|
|
739
|
+
expect(calculator.evaluate('roundup(1.2341, 3)')).to eql(BigDecimal('1.235'))
|
|
740
|
+
expect(calculator.evaluate('roundup(x, 3)', x: 1.2341)).to eql(1.235)
|
|
741
|
+
expect(calculator.evaluate('roundup(x, -1)', x: 1234.5)).to eql(1240)
|
|
742
|
+
end
|
|
743
|
+
|
|
744
|
+
it 'rounds to a power of ten past the number of digits' do
|
|
745
|
+
expect(calculator.evaluate('roundup(7, -2)')).to eql(100)
|
|
746
|
+
expect(calculator.evaluate('roundup(x, -3)', x: 7.5)).to eql(1000)
|
|
747
|
+
expect(calculator.evaluate('roundup(-7, -2)')).to eql(0)
|
|
748
|
+
expect(calculator.evaluate('roundup(0, -2)')).to eql(0)
|
|
749
|
+
end
|
|
750
|
+
|
|
751
|
+
it 'returns zero for a very negative precision without computing the power of ten' do
|
|
752
|
+
expect(calculator.evaluate!('roundup(-7, -1000000000)')).to eql(0)
|
|
753
|
+
expect(calculator.evaluate!('roundup(x, -1000000000)', x: -7.5)).to eql(0)
|
|
754
|
+
end
|
|
755
|
+
|
|
756
|
+
it 'rejects a very negative precision when the result would be too long' do
|
|
757
|
+
expect(calculator.evaluate!('roundup(7, -9999)')).to eq(10**9999)
|
|
758
|
+
expect { calculator.evaluate!('roundup(7, -10000)') }.to raise_error(Dentaku::ArgumentError, /more than 10000 digits/)
|
|
759
|
+
expect { calculator.evaluate!('roundup(7.5, -1000000000)') }.to raise_error(Dentaku::ArgumentError)
|
|
760
|
+
end
|
|
710
761
|
end
|
|
711
762
|
|
|
712
763
|
describe 'rounddown' do
|
|
@@ -717,6 +768,34 @@ describe Dentaku::Calculator do
|
|
|
717
768
|
it 'should accept second precision argument like in Office formula' do
|
|
718
769
|
expect(calculator.evaluate('rounddown(1.234, 2)')).to eq(1.23)
|
|
719
770
|
end
|
|
771
|
+
|
|
772
|
+
it 'does not round down a float that is already exact' do
|
|
773
|
+
expect(calculator.evaluate('rounddown(x, 2)', x: 0.29)).to eq(0.29)
|
|
774
|
+
expect(calculator.evaluate('rounddown(x, 2)', x: 1.15)).to eq(1.15)
|
|
775
|
+
end
|
|
776
|
+
|
|
777
|
+
it 'returns the same type as its input, like round' do
|
|
778
|
+
expect(calculator.evaluate('rounddown(1.2349, 3)')).to eql(BigDecimal('1.234'))
|
|
779
|
+
expect(calculator.evaluate('rounddown(x, 3)', x: 1.2349)).to eql(1.234)
|
|
780
|
+
expect(calculator.evaluate('rounddown(x, -1)', x: 1234.5)).to eql(1230)
|
|
781
|
+
end
|
|
782
|
+
|
|
783
|
+
it 'rounds to a power of ten past the number of digits' do
|
|
784
|
+
expect(calculator.evaluate('rounddown(7, -2)')).to eql(0)
|
|
785
|
+
expect(calculator.evaluate('rounddown(-7, -2)')).to eql(-100)
|
|
786
|
+
expect(calculator.evaluate('rounddown(x, -3)', x: -7.5)).to eql(-1000)
|
|
787
|
+
end
|
|
788
|
+
|
|
789
|
+
it 'returns zero for a very negative precision without computing the power of ten' do
|
|
790
|
+
expect(calculator.evaluate!('rounddown(7, -1000000000)')).to eql(0)
|
|
791
|
+
expect(calculator.evaluate!('rounddown(x, -1000000000)', x: 7.5)).to eql(0)
|
|
792
|
+
end
|
|
793
|
+
|
|
794
|
+
it 'rejects a very negative precision when the result would be too long' do
|
|
795
|
+
expect(calculator.evaluate!('rounddown(-7, -9999)')).to eq(-10**9999)
|
|
796
|
+
expect { calculator.evaluate!('rounddown(-7, -10000)') }.to raise_error(Dentaku::ArgumentError, /more than 10000 digits/)
|
|
797
|
+
expect { calculator.evaluate!('rounddown(-7.5, -1000000000)') }.to raise_error(Dentaku::ArgumentError)
|
|
798
|
+
end
|
|
720
799
|
end
|
|
721
800
|
end
|
|
722
801
|
|
|
@@ -1009,6 +1088,101 @@ describe Dentaku::Calculator do
|
|
|
1009
1088
|
end
|
|
1010
1089
|
end
|
|
1011
1090
|
|
|
1091
|
+
describe 'XOR operator' do
|
|
1092
|
+
it 'is true when exactly one operand is true' do
|
|
1093
|
+
expect(calculator.evaluate!('true XOR false')).to eq(true)
|
|
1094
|
+
expect(calculator.evaluate!('a xor b', a: true, b: true)).to eq(false)
|
|
1095
|
+
expect(calculator.evaluate!('a XOR b', a: false, b: false)).to eq(false)
|
|
1096
|
+
expect(calculator.evaluate!('1 > 2 XOR 3 < 4')).to eq(true)
|
|
1097
|
+
end
|
|
1098
|
+
|
|
1099
|
+
it 'treats null as false, like the XOR function' do
|
|
1100
|
+
expect(calculator.evaluate!('a XOR true', a: nil)).to eq(true)
|
|
1101
|
+
end
|
|
1102
|
+
|
|
1103
|
+
it 'needs both operands' do
|
|
1104
|
+
expect { calculator.evaluate!('a XOR b', a: true) }
|
|
1105
|
+
.to raise_error(Dentaku::UnboundVariableError)
|
|
1106
|
+
expect(calculator.dependencies('a XOR b', a: true)).to eq(['b'])
|
|
1107
|
+
end
|
|
1108
|
+
|
|
1109
|
+
it 'shares precedence with AND and OR, left to right' do
|
|
1110
|
+
expect(calculator.evaluate!('true XOR true OR true')).to eq(true)
|
|
1111
|
+
expect(calculator.evaluate!('true OR true XOR true')).to eq(false)
|
|
1112
|
+
end
|
|
1113
|
+
|
|
1114
|
+
it 'requires logical operands' do
|
|
1115
|
+
expect { calculator.ast('1 XOR true') }.to raise_error(Dentaku::ParseError, /logical/)
|
|
1116
|
+
end
|
|
1117
|
+
|
|
1118
|
+
it 'is still a function when followed by parentheses' do
|
|
1119
|
+
expect(calculator.evaluate!('XOR(true, false, true)')).to eq(false)
|
|
1120
|
+
expect(calculator.evaluate!('XOR (true, false)')).to eq(true)
|
|
1121
|
+
end
|
|
1122
|
+
end
|
|
1123
|
+
|
|
1124
|
+
describe 'NOT operator' do
|
|
1125
|
+
it 'negates its operand' do
|
|
1126
|
+
expect(calculator.evaluate!('NOT true')).to eq(false)
|
|
1127
|
+
expect(calculator.evaluate!('not a', a: false)).to eq(true)
|
|
1128
|
+
expect(calculator.evaluate!('NOT NOT true')).to eq(true)
|
|
1129
|
+
end
|
|
1130
|
+
|
|
1131
|
+
it 'is aliased as !' do
|
|
1132
|
+
expect(calculator.evaluate!('!true')).to eq(false)
|
|
1133
|
+
expect(calculator.evaluate!('! a', a: true)).to eq(false)
|
|
1134
|
+
expect(calculator.evaluate!('!!a', a: true)).to eq(true)
|
|
1135
|
+
expect(calculator.evaluate!('!(1 > 2)')).to eq(true)
|
|
1136
|
+
end
|
|
1137
|
+
|
|
1138
|
+
it 'leaves != alone' do
|
|
1139
|
+
expect(calculator.evaluate!('a != 1', a: 2)).to eq(true)
|
|
1140
|
+
expect(calculator.evaluate!('!a != true', a: true)).to eq(true)
|
|
1141
|
+
end
|
|
1142
|
+
|
|
1143
|
+
it 'binds looser than comparisons and tighter than AND, OR and XOR' do
|
|
1144
|
+
expect(calculator.evaluate!('NOT a > 5', a: 3)).to eq(true)
|
|
1145
|
+
expect(calculator.evaluate!('!a = b', a: 1, b: 1)).to eq(false)
|
|
1146
|
+
expect(calculator.evaluate!('NOT a AND b', a: true, b: true)).to eq(false)
|
|
1147
|
+
expect(calculator.evaluate!('NOT a OR b', a: true, b: true)).to eq(true)
|
|
1148
|
+
expect(calculator.evaluate!('a AND NOT b', a: true, b: false)).to eq(true)
|
|
1149
|
+
expect(calculator.evaluate!('x = NOT a', x: false, a: true)).to eq(true)
|
|
1150
|
+
end
|
|
1151
|
+
|
|
1152
|
+
it 'works inside function arguments and case statements' do
|
|
1153
|
+
expect(calculator.evaluate!('IF(NOT a, 1, 2)', a: false)).to eq(1)
|
|
1154
|
+
expect(calculator.evaluate!('CASE x WHEN 1 THEN NOT a ELSE !b END', x: 2, a: true, b: false)).to eq(true)
|
|
1155
|
+
end
|
|
1156
|
+
|
|
1157
|
+
it 'requires a logical operand' do
|
|
1158
|
+
expect { calculator.ast('NOT 1') }.to raise_error(Dentaku::ParseError, /logical/)
|
|
1159
|
+
end
|
|
1160
|
+
|
|
1161
|
+
it 'reports the dependencies of its operand' do
|
|
1162
|
+
expect(calculator.dependencies('NOT a')).to eq(['a'])
|
|
1163
|
+
end
|
|
1164
|
+
|
|
1165
|
+
it 'is still a function when followed by parentheses' do
|
|
1166
|
+
expect(calculator.evaluate!('NOT(true)')).to eq(false)
|
|
1167
|
+
expect(calculator.evaluate!('NOT (a) = b', a: true, b: false)).to eq(true)
|
|
1168
|
+
end
|
|
1169
|
+
end
|
|
1170
|
+
|
|
1171
|
+
describe 'AND and OR followed by parentheses without a left operand' do
|
|
1172
|
+
it 'are functions' do
|
|
1173
|
+
expect(calculator.evaluate!('AND (true, false)')).to eq(false)
|
|
1174
|
+
expect(calculator.evaluate!('OR (true, false)')).to eq(true)
|
|
1175
|
+
end
|
|
1176
|
+
end
|
|
1177
|
+
|
|
1178
|
+
describe 'negation after case keywords' do
|
|
1179
|
+
it 'negates' do
|
|
1180
|
+
expect(calculator.evaluate!('CASE x WHEN 1 THEN -1 ELSE -2 END', x: 1)).to eq(-1)
|
|
1181
|
+
expect(calculator.evaluate!('CASE x WHEN -1 THEN 2 END', x: -1)).to eq(2)
|
|
1182
|
+
expect(calculator.evaluate!('CASE -x WHEN -1 THEN 2 END', x: 1)).to eq(2)
|
|
1183
|
+
end
|
|
1184
|
+
end
|
|
1185
|
+
|
|
1012
1186
|
describe 'logical operators whose short-circuit probe raises' do
|
|
1013
1187
|
it 'falls back to the dependencies of both operands' do
|
|
1014
1188
|
expect(calculator.dependencies('a / b > 1 OR c', 'a' => 1, 'b' => 0)).to eq(['c'])
|