dentaku 4.0.2 → 4.2.0
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 +87 -0
- data/README.md +176 -11
- data/lib/dentaku/ast/access.rb +14 -11
- data/lib/dentaku/ast/array.rb +2 -0
- data/lib/dentaku/ast/combinators.rb +105 -4
- data/lib/dentaku/ast/functions/duration.rb +3 -3
- data/lib/dentaku/ast/functions/rounddown.rb +8 -2
- data/lib/dentaku/ast/functions/roundup.rb +8 -2
- data/lib/dentaku/ast/functions/xor.rb +6 -6
- 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 +48 -7
- 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_parser.rb +7 -6
- data/lib/dentaku/parser.rb +15 -2
- data/lib/dentaku/print_visitor.rb +38 -2
- data/lib/dentaku/token_scanner.rb +37 -19
- data/lib/dentaku/tokenizer.rb +6 -2
- data/lib/dentaku/version.rb +1 -1
- data/lib/dentaku/visitor/infix.rb +4 -0
- data/mise.toml +2 -0
- data/spec/ast/member_access_spec.rb +124 -0
- data/spec/ast/xor_spec.rb +16 -3
- data/spec/builtin_functions_spec.rb +26 -0
- data/spec/bulk_expression_solver_spec.rb +88 -0
- data/spec/calculator_spec.rb +181 -1
- data/spec/case_sensitivity_spec.rb +54 -0
- data/spec/humanize_visitor_spec.rb +217 -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 +9 -3
- data/lib/dentaku/token_matchers.rb +0 -29
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
require 'bigdecimal'
|
|
2
2
|
require 'time'
|
|
3
|
-
require 'dentaku/string_casing'
|
|
4
3
|
require 'dentaku/token'
|
|
5
4
|
require 'dentaku/numeric_parser'
|
|
6
5
|
|
|
7
6
|
module Dentaku
|
|
8
7
|
class TokenScanner
|
|
9
|
-
extend StringCasing
|
|
10
|
-
|
|
11
8
|
DATE_TIME_REGEXP = /\d{2}\d{2}?-\d{1,2}-\d{1,2}([ |T]\d{1,2}:\d{1,2}:\d{1,2}(\.\d*)?)? ?(Z|((\+|\-)\d{2}\:?\d{2}))?(?!\d)/.freeze
|
|
12
9
|
|
|
13
10
|
def initialize(category, regexp, converter = nil, condition = nil)
|
|
@@ -31,8 +28,6 @@ module Dentaku
|
|
|
31
28
|
end
|
|
32
29
|
|
|
33
30
|
class << self
|
|
34
|
-
attr_reader :case_sensitive
|
|
35
|
-
|
|
36
31
|
def available_scanners
|
|
37
32
|
[
|
|
38
33
|
:null,
|
|
@@ -44,10 +39,12 @@ module Dentaku
|
|
|
44
39
|
:single_quoted_string,
|
|
45
40
|
:negate,
|
|
46
41
|
:combinator,
|
|
42
|
+
:logical_not,
|
|
47
43
|
:operator,
|
|
48
44
|
:grouping,
|
|
49
45
|
:array,
|
|
50
46
|
:access,
|
|
47
|
+
:member_access,
|
|
51
48
|
:case_statement,
|
|
52
49
|
:comparator,
|
|
53
50
|
:boolean,
|
|
@@ -76,7 +73,6 @@ module Dentaku
|
|
|
76
73
|
end
|
|
77
74
|
|
|
78
75
|
def scanners(options = {})
|
|
79
|
-
@case_sensitive = options.fetch(:case_sensitive, false)
|
|
80
76
|
raw_date_literals = options.fetch(:raw_date_literals, true)
|
|
81
77
|
|
|
82
78
|
@scanners.select { |k, _| raw_date_literals || k != :datetime }.values
|
|
@@ -111,17 +107,22 @@ module Dentaku
|
|
|
111
107
|
new(:string, "'[^']*'", lambda { |raw| raw.gsub(/^'|'$/, '') })
|
|
112
108
|
end
|
|
113
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
|
+
|
|
114
124
|
def negate
|
|
115
|
-
new(:operator, '-', lambda { |raw| :negate }, lambda { |last_token|
|
|
116
|
-
last_token.nil? ||
|
|
117
|
-
last_token.is?(:operator) ||
|
|
118
|
-
last_token.is?(:comparator) ||
|
|
119
|
-
last_token.is?(:combinator) ||
|
|
120
|
-
last_token.value == :open ||
|
|
121
|
-
last_token.value == :comma ||
|
|
122
|
-
last_token.value == :lbracket ||
|
|
123
|
-
last_token.value == :array_start
|
|
124
|
-
})
|
|
125
|
+
new(:operator, '-', lambda { |raw| :negate }, lambda { |last_token| operand_expected?(last_token) })
|
|
125
126
|
end
|
|
126
127
|
|
|
127
128
|
def operator
|
|
@@ -146,6 +147,16 @@ module Dentaku
|
|
|
146
147
|
new(:access, '\[|\]', lambda { |raw| names[raw] })
|
|
147
148
|
end
|
|
148
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
|
+
|
|
149
160
|
def case_statement
|
|
150
161
|
names = { open: 'case', close: 'end', then: 'then', when: 'when', else: 'else' }.invert
|
|
151
162
|
new(:case, '(case|end|then|when|else)\b', lambda { |raw| names[raw.downcase] })
|
|
@@ -157,12 +168,19 @@ module Dentaku
|
|
|
157
168
|
new(:comparator, '<=|>=|!=|<>|<|>|==|=', lambda { |raw| names[raw] || alternate[raw] })
|
|
158
169
|
end
|
|
159
170
|
|
|
171
|
+
# binary, so only after an operand: elsewhere `xor (a, b)` is a call
|
|
160
172
|
def combinator
|
|
161
173
|
names = { and: '&&', or: '||' }.invert
|
|
162
|
-
new(:combinator, '(and|or|&&|\|\|)\s', lambda { |raw|
|
|
174
|
+
new(:combinator, '(and|or|xor|&&|\|\|)\s', lambda { |raw|
|
|
163
175
|
norm = raw.strip.downcase
|
|
164
176
|
names.fetch(norm) { norm.to_sym }
|
|
165
|
-
})
|
|
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) })
|
|
166
184
|
end
|
|
167
185
|
|
|
168
186
|
def boolean
|
|
@@ -180,7 +198,7 @@ module Dentaku
|
|
|
180
198
|
end
|
|
181
199
|
|
|
182
200
|
def identifier
|
|
183
|
-
new(:identifier, '[[[:word:]]\.]+\b', lambda { |raw|
|
|
201
|
+
new(:identifier, '[[[:word:]]\.]+\b', lambda { |raw| raw.strip })
|
|
184
202
|
end
|
|
185
203
|
|
|
186
204
|
def quoted_identifier
|
data/lib/dentaku/tokenizer.rb
CHANGED
|
@@ -12,12 +12,12 @@ 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)
|
|
18
19
|
|
|
19
20
|
scanner_options = {
|
|
20
|
-
case_sensitive: options.fetch(:case_sensitive, false),
|
|
21
21
|
raw_date_literals: options.fetch(:raw_date_literals, true)
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -53,7 +53,11 @@ module Dentaku
|
|
|
53
53
|
@nesting -= 1 if RPAREN == token
|
|
54
54
|
fail! :too_many_closing_parentheses if @nesting < 0
|
|
55
55
|
|
|
56
|
-
|
|
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
|
|
57
61
|
end
|
|
58
62
|
|
|
59
63
|
match_length = tokens.map(&:length).reduce(:+)
|
data/lib/dentaku/version.rb
CHANGED
data/mise.toml
ADDED
|
@@ -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
|
data/spec/ast/xor_spec.rb
CHANGED
|
@@ -15,9 +15,22 @@ describe 'Dentaku::AST::Xor' do
|
|
|
15
15
|
expect(result).to eq(true)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
it 'returns false if
|
|
19
|
-
|
|
20
|
-
expect(
|
|
18
|
+
it 'returns false if an even number of the arguments are true' do
|
|
19
|
+
expect(Dentaku('XOR(false, true, true)')).to eq(false)
|
|
20
|
+
expect(Dentaku('XOR(true, true, true, true)')).to eq(false)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'returns true if an odd number of the arguments are true' do
|
|
24
|
+
expect(Dentaku('XOR(true, true, true)')).to eq(true)
|
|
25
|
+
expect(Dentaku('XOR(true, false, true, true)')).to eq(true)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'matches XOR applied pairwise' do
|
|
29
|
+
[true, false].repeated_permutation(4).each do |a, b, c, d|
|
|
30
|
+
context = { a: a, b: b, c: c, d: d }
|
|
31
|
+
expect(Dentaku('XOR(a, b, c, d)', context))
|
|
32
|
+
.to eq(Dentaku('XOR(XOR(a, b), XOR(c, d))', context))
|
|
33
|
+
end
|
|
21
34
|
end
|
|
22
35
|
|
|
23
36
|
it 'supports nested expressions' do
|
|
@@ -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
|
|
@@ -8,6 +8,30 @@ RSpec.describe Dentaku::BulkExpressionSolver do
|
|
|
8
8
|
let(:calculator) { Dentaku::Calculator.new }
|
|
9
9
|
|
|
10
10
|
describe "#solve!" do
|
|
11
|
+
it 'does not reuse a cached order after formulas change with the same variable names' do
|
|
12
|
+
calculator = Dentaku::Calculator.new(cache_dependency_order: true)
|
|
13
|
+
calculator.store(seed: 1)
|
|
14
|
+
expect(calculator.solve!(a: 'b + 1', b: 'seed + 1')).to eq(a: 3, b: 2)
|
|
15
|
+
expect(calculator.solve!(a: 'seed + 1', b: 'a + 1')).to eq(a: 2, b: 3)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'does not share a cached order with a calculator using different dependencies' do
|
|
19
|
+
first = Dentaku::Calculator.new(cache_dependency_order: true).store(seed: 1)
|
|
20
|
+
second = Dentaku::Calculator.new(cache_dependency_order: true).store(seed: 10)
|
|
21
|
+
expect(first.solve!(left: 'right + 1', right: 'seed + 1')).to eq(left: 3, right: 2)
|
|
22
|
+
expect(second.solve!(left: 'seed + 1', right: 'left + 1')).to eq(left: 11, right: 12)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'includes memory-dependent edges in the cached order' do
|
|
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)
|
|
33
|
+
end
|
|
34
|
+
|
|
11
35
|
it "evaluates properly with variables, even if some in memory" do
|
|
12
36
|
expressions = {
|
|
13
37
|
weekly_fruit_budget: "weekly_apple_budget + pear * 4",
|
|
@@ -63,6 +87,51 @@ RSpec.describe Dentaku::BulkExpressionSolver do
|
|
|
63
87
|
expect(solver.solve!).to eq(x: 6, y: 12) # x = 6 by the time y is calculated
|
|
64
88
|
end
|
|
65
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
|
+
|
|
66
135
|
it "does not execute functions unnecessarily" do
|
|
67
136
|
calls = 0
|
|
68
137
|
external = ->() { calls += 1 }
|
|
@@ -111,6 +180,25 @@ RSpec.describe Dentaku::BulkExpressionSolver do
|
|
|
111
180
|
discounts: [0.8, 0.6]
|
|
112
181
|
)
|
|
113
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
|
|
114
202
|
end
|
|
115
203
|
|
|
116
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
|
|
@@ -556,6 +578,14 @@ describe Dentaku::Calculator do
|
|
|
556
578
|
expect(calculator.evaluate!('value2 - value', { value: value, value2: value2 })).to eq(value2 - value)
|
|
557
579
|
expect(calculator.evaluate!('value - 7200', { value: value })).to eq(Time.local(2023, 7, 13, 8, 42, 11))
|
|
558
580
|
end
|
|
581
|
+
|
|
582
|
+
it 'does not accept unknown durations' do
|
|
583
|
+
expect { calculator.evaluate!('duration(1, foo)') }.to raise_error(Dentaku::ArgumentError, /is not a valid duration unit/)
|
|
584
|
+
expect { calculator.evaluate!('duration(1, ay)') }.to raise_error(Dentaku::ArgumentError, /is not a valid duration unit/)
|
|
585
|
+
expect { calculator.evaluate!('duration(1, daysy)') }.to raise_error(Dentaku::ArgumentError, /is not a valid duration unit/)
|
|
586
|
+
expect { calculator.evaluate!('duration(1, ayear)') }.to raise_error(Dentaku::ArgumentError, /is not a valid duration unit/)
|
|
587
|
+
expect { calculator.evaluate!('duration(1, dayofmonthofyear)') }.to raise_error(Dentaku::ArgumentError, /is not a valid duration unit/)
|
|
588
|
+
end
|
|
559
589
|
end
|
|
560
590
|
|
|
561
591
|
describe 'functions' do
|
|
@@ -699,6 +729,17 @@ describe Dentaku::Calculator do
|
|
|
699
729
|
it 'should accept second precision argument like in Office formula' do
|
|
700
730
|
expect(calculator.evaluate('roundup(1.234, 2)')).to eq(1.24)
|
|
701
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
|
|
702
743
|
end
|
|
703
744
|
|
|
704
745
|
describe 'rounddown' do
|
|
@@ -709,6 +750,17 @@ describe Dentaku::Calculator do
|
|
|
709
750
|
it 'should accept second precision argument like in Office formula' do
|
|
710
751
|
expect(calculator.evaluate('rounddown(1.234, 2)')).to eq(1.23)
|
|
711
752
|
end
|
|
753
|
+
|
|
754
|
+
it 'does not round down a float that is already exact' do
|
|
755
|
+
expect(calculator.evaluate('rounddown(x, 2)', x: 0.29)).to eq(0.29)
|
|
756
|
+
expect(calculator.evaluate('rounddown(x, 2)', x: 1.15)).to eq(1.15)
|
|
757
|
+
end
|
|
758
|
+
|
|
759
|
+
it 'returns the same type as its input, like round' do
|
|
760
|
+
expect(calculator.evaluate('rounddown(1.2349, 3)')).to eql(BigDecimal('1.234'))
|
|
761
|
+
expect(calculator.evaluate('rounddown(x, 3)', x: 1.2349)).to eql(1.234)
|
|
762
|
+
expect(calculator.evaluate('rounddown(x, -1)', x: 1234.5)).to eql(1230)
|
|
763
|
+
end
|
|
712
764
|
end
|
|
713
765
|
end
|
|
714
766
|
|
|
@@ -1001,6 +1053,134 @@ describe Dentaku::Calculator do
|
|
|
1001
1053
|
end
|
|
1002
1054
|
end
|
|
1003
1055
|
|
|
1056
|
+
describe 'XOR operator' do
|
|
1057
|
+
it 'is true when exactly one operand is true' do
|
|
1058
|
+
expect(calculator.evaluate!('true XOR false')).to eq(true)
|
|
1059
|
+
expect(calculator.evaluate!('a xor b', a: true, b: true)).to eq(false)
|
|
1060
|
+
expect(calculator.evaluate!('a XOR b', a: false, b: false)).to eq(false)
|
|
1061
|
+
expect(calculator.evaluate!('1 > 2 XOR 3 < 4')).to eq(true)
|
|
1062
|
+
end
|
|
1063
|
+
|
|
1064
|
+
it 'treats null as false, like the XOR function' do
|
|
1065
|
+
expect(calculator.evaluate!('a XOR true', a: nil)).to eq(true)
|
|
1066
|
+
end
|
|
1067
|
+
|
|
1068
|
+
it 'needs both operands' do
|
|
1069
|
+
expect { calculator.evaluate!('a XOR b', a: true) }
|
|
1070
|
+
.to raise_error(Dentaku::UnboundVariableError)
|
|
1071
|
+
expect(calculator.dependencies('a XOR b', a: true)).to eq(['b'])
|
|
1072
|
+
end
|
|
1073
|
+
|
|
1074
|
+
it 'shares precedence with AND and OR, left to right' do
|
|
1075
|
+
expect(calculator.evaluate!('true XOR true OR true')).to eq(true)
|
|
1076
|
+
expect(calculator.evaluate!('true OR true XOR true')).to eq(false)
|
|
1077
|
+
end
|
|
1078
|
+
|
|
1079
|
+
it 'requires logical operands' do
|
|
1080
|
+
expect { calculator.ast('1 XOR true') }.to raise_error(Dentaku::ParseError, /logical/)
|
|
1081
|
+
end
|
|
1082
|
+
|
|
1083
|
+
it 'is still a function when followed by parentheses' do
|
|
1084
|
+
expect(calculator.evaluate!('XOR(true, false, true)')).to eq(false)
|
|
1085
|
+
expect(calculator.evaluate!('XOR (true, false)')).to eq(true)
|
|
1086
|
+
end
|
|
1087
|
+
end
|
|
1088
|
+
|
|
1089
|
+
describe 'NOT operator' do
|
|
1090
|
+
it 'negates its operand' do
|
|
1091
|
+
expect(calculator.evaluate!('NOT true')).to eq(false)
|
|
1092
|
+
expect(calculator.evaluate!('not a', a: false)).to eq(true)
|
|
1093
|
+
expect(calculator.evaluate!('NOT NOT true')).to eq(true)
|
|
1094
|
+
end
|
|
1095
|
+
|
|
1096
|
+
it 'is aliased as !' do
|
|
1097
|
+
expect(calculator.evaluate!('!true')).to eq(false)
|
|
1098
|
+
expect(calculator.evaluate!('! a', a: true)).to eq(false)
|
|
1099
|
+
expect(calculator.evaluate!('!!a', a: true)).to eq(true)
|
|
1100
|
+
expect(calculator.evaluate!('!(1 > 2)')).to eq(true)
|
|
1101
|
+
end
|
|
1102
|
+
|
|
1103
|
+
it 'leaves != alone' do
|
|
1104
|
+
expect(calculator.evaluate!('a != 1', a: 2)).to eq(true)
|
|
1105
|
+
expect(calculator.evaluate!('!a != true', a: true)).to eq(true)
|
|
1106
|
+
end
|
|
1107
|
+
|
|
1108
|
+
it 'binds looser than comparisons and tighter than AND, OR and XOR' do
|
|
1109
|
+
expect(calculator.evaluate!('NOT a > 5', a: 3)).to eq(true)
|
|
1110
|
+
expect(calculator.evaluate!('!a = b', a: 1, b: 1)).to eq(false)
|
|
1111
|
+
expect(calculator.evaluate!('NOT a AND b', a: true, b: true)).to eq(false)
|
|
1112
|
+
expect(calculator.evaluate!('NOT a OR b', a: true, b: true)).to eq(true)
|
|
1113
|
+
expect(calculator.evaluate!('a AND NOT b', a: true, b: false)).to eq(true)
|
|
1114
|
+
expect(calculator.evaluate!('x = NOT a', x: false, a: true)).to eq(true)
|
|
1115
|
+
end
|
|
1116
|
+
|
|
1117
|
+
it 'works inside function arguments and case statements' do
|
|
1118
|
+
expect(calculator.evaluate!('IF(NOT a, 1, 2)', a: false)).to eq(1)
|
|
1119
|
+
expect(calculator.evaluate!('CASE x WHEN 1 THEN NOT a ELSE !b END', x: 2, a: true, b: false)).to eq(true)
|
|
1120
|
+
end
|
|
1121
|
+
|
|
1122
|
+
it 'requires a logical operand' do
|
|
1123
|
+
expect { calculator.ast('NOT 1') }.to raise_error(Dentaku::ParseError, /logical/)
|
|
1124
|
+
end
|
|
1125
|
+
|
|
1126
|
+
it 'reports the dependencies of its operand' do
|
|
1127
|
+
expect(calculator.dependencies('NOT a')).to eq(['a'])
|
|
1128
|
+
end
|
|
1129
|
+
|
|
1130
|
+
it 'is still a function when followed by parentheses' do
|
|
1131
|
+
expect(calculator.evaluate!('NOT(true)')).to eq(false)
|
|
1132
|
+
expect(calculator.evaluate!('NOT (a) = b', a: true, b: false)).to eq(true)
|
|
1133
|
+
end
|
|
1134
|
+
end
|
|
1135
|
+
|
|
1136
|
+
describe 'AND and OR followed by parentheses without a left operand' do
|
|
1137
|
+
it 'are functions' do
|
|
1138
|
+
expect(calculator.evaluate!('AND (true, false)')).to eq(false)
|
|
1139
|
+
expect(calculator.evaluate!('OR (true, false)')).to eq(true)
|
|
1140
|
+
end
|
|
1141
|
+
end
|
|
1142
|
+
|
|
1143
|
+
describe 'negation after case keywords' do
|
|
1144
|
+
it 'negates' do
|
|
1145
|
+
expect(calculator.evaluate!('CASE x WHEN 1 THEN -1 ELSE -2 END', x: 1)).to eq(-1)
|
|
1146
|
+
expect(calculator.evaluate!('CASE x WHEN -1 THEN 2 END', x: -1)).to eq(2)
|
|
1147
|
+
expect(calculator.evaluate!('CASE -x WHEN -1 THEN 2 END', x: 1)).to eq(2)
|
|
1148
|
+
end
|
|
1149
|
+
end
|
|
1150
|
+
|
|
1151
|
+
describe 'logical operators whose short-circuit probe raises' do
|
|
1152
|
+
it 'falls back to the dependencies of both operands' do
|
|
1153
|
+
expect(calculator.dependencies('a / b > 1 OR c', 'a' => 1, 'b' => 0)).to eq(['c'])
|
|
1154
|
+
expect(calculator.dependencies('a <= 1 OR c', 'a' => nil)).to eq(['c'])
|
|
1155
|
+
end
|
|
1156
|
+
|
|
1157
|
+
it 'still prunes when the probe is decisive' do
|
|
1158
|
+
expect(calculator.dependencies('1 > 2 AND a')).to eq([])
|
|
1159
|
+
expect(calculator.dependencies('1 < 2 OR a')).to eq([])
|
|
1160
|
+
expect(calculator.dependencies('a AND 1 > 2')).to eq([])
|
|
1161
|
+
expect(calculator.dependencies('a OR 1 < 2')).to eq([])
|
|
1162
|
+
end
|
|
1163
|
+
|
|
1164
|
+
it 'still reports the other operand when the probe is not decisive' do
|
|
1165
|
+
expect(calculator.dependencies('1 < 2 AND a')).to eq(['a'])
|
|
1166
|
+
expect(calculator.dependencies('1 > 2 OR a')).to eq(['a'])
|
|
1167
|
+
end
|
|
1168
|
+
|
|
1169
|
+
# Regression: the raising branch used to re-derive the dependency union via
|
|
1170
|
+
# `super`, walking both subtrees a second time at every level of the chain.
|
|
1171
|
+
# Twelve terms probed 2048 times instead of 12.
|
|
1172
|
+
it 'probes each operand of a chain once rather than exponentially' do
|
|
1173
|
+
call_count = { count: 0 }
|
|
1174
|
+
calculator.add_function(:blank, :numeric, -> { call_count[:count] += 1; nil })
|
|
1175
|
+
|
|
1176
|
+
terms = 12
|
|
1177
|
+
formula = (1..terms).map { |i| "(blank() <= #{i})" }.join(' OR ')
|
|
1178
|
+
|
|
1179
|
+
expect(calculator.evaluate(formula)).to be_nil
|
|
1180
|
+
expect(call_count[:count]).to eq(terms)
|
|
1181
|
+
end
|
|
1182
|
+
end
|
|
1183
|
+
|
|
1004
1184
|
describe 'aliases' do
|
|
1005
1185
|
it 'accepts aliases as instance option' do
|
|
1006
1186
|
expect(with_aliases.evaluate('rrround(5.1)')).to eq(5)
|