dentaku 4.1.0 → 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.
@@ -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
- first = Dentaku::Calculator.new(cache_dependency_order: true).store(seed: 1, base: 5)
27
- second = Dentaku::Calculator.new(cache_dependency_order: true).store(seed: 1)
28
- formulas = { result: 'base + 1', base: 'seed + 1' }
29
- expect(first.solve!(formulas)).to eq(result: 6, base: 2)
30
- expect(second.solve!(formulas)).to eq(result: 3, base: 2)
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
@@ -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,17 @@ 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
710
743
  end
711
744
 
712
745
  describe 'rounddown' do
@@ -717,6 +750,17 @@ describe Dentaku::Calculator do
717
750
  it 'should accept second precision argument like in Office formula' do
718
751
  expect(calculator.evaluate('rounddown(1.234, 2)')).to eq(1.23)
719
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
720
764
  end
721
765
  end
722
766
 
@@ -1009,6 +1053,101 @@ describe Dentaku::Calculator do
1009
1053
  end
1010
1054
  end
1011
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
+
1012
1151
  describe 'logical operators whose short-circuit probe raises' do
1013
1152
  it 'falls back to the dependencies of both operands' do
1014
1153
  expect(calculator.dependencies('a / b > 1 OR c', 'a' => 1, 'b' => 0)).to eq(['c'])
@@ -0,0 +1,217 @@
1
+ require 'spec_helper'
2
+ require 'dentaku'
3
+ require 'dentaku/humanize_visitor'
4
+
5
+ describe Dentaku::HumanizeVisitor do
6
+ let(:calculator) { Dentaku::Calculator.new }
7
+
8
+ def humanize(expression, values = {})
9
+ described_class.new(calculator.ast(expression), values).to_s
10
+ end
11
+
12
+ it 'verbalizes comparison operators' do
13
+ expect(humanize('a >= 5')).to eq('a is greater than or equal to 5')
14
+ expect(humanize('a <= 5')).to eq('a is less than or equal to 5')
15
+ expect(humanize('a > 5')).to eq('a is greater than 5')
16
+ expect(humanize('a < 5')).to eq('a is less than 5')
17
+ expect(humanize('a = 5')).to eq('a equals 5')
18
+ expect(humanize('a != 5')).to eq('a does not equal 5')
19
+ end
20
+
21
+ it 'verbalizes arithmetic operators' do
22
+ expect(humanize('1 + 2')).to eq('1 plus 2')
23
+ expect(humanize('3 - 1')).to eq('3 minus 1')
24
+ expect(humanize('2 * 3')).to eq('2 times 3')
25
+ expect(humanize('10 / 2')).to eq('10 divided by 2')
26
+ expect(humanize('2 ^ 3')).to eq('2 to the power of 3')
27
+ expect(humanize('7 % 3')).to eq('7 modulo 3')
28
+ end
29
+
30
+ it 'verbalizes bitwise operators' do
31
+ expect(humanize('1 & 2')).to eq('1 bitwise and 2')
32
+ expect(humanize('1 | 2')).to eq('1 bitwise or 2')
33
+ expect(humanize('1 << 2')).to eq('1 shifted left by 2')
34
+ expect(humanize('1 >> 2')).to eq('1 shifted right by 2')
35
+ end
36
+
37
+ it 'verbalizes logical combinators' do
38
+ expect(humanize('a >= 5 and a <= 10'))
39
+ .to eq('a is greater than or equal to 5 and a is less than or equal to 10')
40
+ expect(humanize('a < 0 or a > 100')).to eq('a is less than 0 or a is greater than 100')
41
+ end
42
+
43
+ it 'verbalizes XOR and NOT operators' do
44
+ expect(humanize('a XOR b')).to eq('a exclusive-or b')
45
+ expect(humanize('NOT a > 5')).to eq('not a is greater than 5')
46
+ expect(humanize('!(a OR b)')).to eq('not (a or b)')
47
+ end
48
+
49
+ it 'verbalizes negation' do
50
+ expect(humanize('-a')).to eq('negative a')
51
+ end
52
+
53
+ it 'verbalizes nil literal' do
54
+ expect(humanize('a = NULL')).to eq('a equals null')
55
+ end
56
+
57
+ it 'substitutes identifiers with provided values' do
58
+ expect(humanize('days >= min and days <= max', min: 5, max: 20))
59
+ .to eq('days is greater than or equal to 5 and days is less than or equal to 20')
60
+ end
61
+
62
+ it 'quotes string values in substitution' do
63
+ expect(humanize('name = expected', expected: 'Buk')).to eq('name equals "Buk"')
64
+ end
65
+
66
+ it 'preserves identifiers with no substitution value' do
67
+ expect(humanize('a + b', a: 1)).to eq('1 plus b')
68
+ end
69
+
70
+ it 'verbalizes IF' do
71
+ expect(humanize('IF(a > 0, "yes", "no")')).to eq('if a is greater than 0 then "yes" else "no"')
72
+ end
73
+
74
+ it 'verbalizes AND function' do
75
+ expect(humanize('AND(a > 0, b > 0)')).to eq('a is greater than 0 and b is greater than 0')
76
+ end
77
+
78
+ it 'verbalizes OR function' do
79
+ expect(humanize('OR(a > 0, b > 0)')).to eq('a is greater than 0 or b is greater than 0')
80
+ end
81
+
82
+ it 'verbalizes XOR' do
83
+ expect(humanize('XOR(a, b)')).to eq('a exclusive-or b')
84
+ end
85
+
86
+ it 'verbalizes NOT' do
87
+ expect(humanize('NOT(a)')).to eq('not a')
88
+ end
89
+
90
+ it 'verbalizes SWITCH with default' do
91
+ expect(humanize('SWITCH(x, 1, "one", 2, "two", "other")'))
92
+ .to eq('x switch: when 1 use "one"; when 2 use "two"; otherwise "other"')
93
+ end
94
+
95
+ it 'verbalizes SWITCH without default' do
96
+ expect(humanize('SWITCH(x, 1, "one", 2, "two")'))
97
+ .to eq('x switch: when 1 use "one"; when 2 use "two"')
98
+ end
99
+
100
+ it 'verbalizes MIN / MAX / SUM / AVG / COUNT' do
101
+ expect(humanize('MIN(a, b, c)')).to eq('minimum of a, b, c')
102
+ expect(humanize('MAX(a, b)')).to eq('maximum of a, b')
103
+ expect(humanize('SUM(a, b, c)')).to eq('sum of a, b, c')
104
+ expect(humanize('AVG(a, b)')).to eq('average of a, b')
105
+ expect(humanize('COUNT(a, b, c)')).to eq('count of a, b, c')
106
+ end
107
+
108
+ it 'verbalizes ABS' do
109
+ expect(humanize('ABS(x)')).to eq('absolute value of x')
110
+ end
111
+
112
+ it 'verbalizes ROUND / ROUNDUP / ROUNDDOWN' do
113
+ expect(humanize('ROUND(x, 2)')).to eq('x rounded to 2 decimal places')
114
+ expect(humanize('ROUNDUP(x, 2)')).to eq('x rounded up to 2 decimal places')
115
+ expect(humanize('ROUNDDOWN(x, 2)')).to eq('x rounded down to 2 decimal places')
116
+ end
117
+
118
+ it 'verbalizes INTERCEPT' do
119
+ expect(humanize('INTERCEPT(xs, ys)')).to eq('linear intercept of xs and ys')
120
+ end
121
+
122
+ it 'verbalizes LEFT / RIGHT' do
123
+ expect(humanize('LEFT(s, 3)')).to eq('first 3 characters of s')
124
+ expect(humanize('RIGHT(s, 3)')).to eq('last 3 characters of s')
125
+ end
126
+
127
+ it 'verbalizes MID' do
128
+ expect(humanize('MID(s, 2, 4)')).to eq('4 characters of s starting at position 2')
129
+ end
130
+
131
+ it 'verbalizes LEN' do
132
+ expect(humanize('LEN(s)')).to eq('length of s')
133
+ end
134
+
135
+ it 'verbalizes FIND' do
136
+ expect(humanize('FIND(needle, haystack)')).to eq('position of needle in haystack')
137
+ end
138
+
139
+ it 'verbalizes SUBSTITUTE' do
140
+ expect(humanize('SUBSTITUTE(text, "foo", "bar")')).to eq('text with "foo" replaced by "bar"')
141
+ end
142
+
143
+ it 'verbalizes CONCAT' do
144
+ expect(humanize('CONCAT(a, b, c)')).to eq('a, b and c joined')
145
+ end
146
+
147
+ it 'verbalizes CONTAINS' do
148
+ expect(humanize('CONTAINS(needle, haystack)')).to eq('haystack contains needle')
149
+ end
150
+
151
+ it 'verbalizes CASE' do
152
+ expr = 'CASE x WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "other" END'
153
+ expect(humanize(expr)).to eq('x case: when 1 then "one" when 2 then "two"; otherwise "other"')
154
+ end
155
+
156
+ it 'verbalizes MAP' do
157
+ expect(humanize('MAP(items, x, x * 2)')).to eq('for each x in items: x times 2')
158
+ end
159
+
160
+ it 'verbalizes FILTER' do
161
+ expect(humanize('FILTER(items, x, x > 0)')).to eq('filter x in items where x is greater than 0')
162
+ end
163
+
164
+ it 'verbalizes ALL' do
165
+ expect(humanize('ALL(items, x, x > 0)')).to eq('all x in items satisfy x is greater than 0')
166
+ end
167
+
168
+ it 'verbalizes ANY' do
169
+ expect(humanize('ANY(items, x, x > 0)')).to eq('any x in items satisfies x is greater than 0')
170
+ end
171
+
172
+ it 'verbalizes PLUCK' do
173
+ expect(humanize('PLUCK(items, name)')).to eq('values of name from items')
174
+ end
175
+
176
+ it 'verbalizes unary Math functions' do
177
+ expect(humanize('SIN(x)')).to eq('sine of x')
178
+ expect(humanize('COS(x)')).to eq('cosine of x')
179
+ expect(humanize('TAN(x)')).to eq('tangent of x')
180
+ expect(humanize('SQRT(x)')).to eq('square root of x')
181
+ expect(humanize('LOG(x)')).to eq('logarithm of x')
182
+ end
183
+
184
+ it 'verbalizes LOG with base' do
185
+ expect(humanize('LOG(x, 10)')).to eq('logarithm of x in base 10')
186
+ end
187
+
188
+ it 'verbalizes binary Math functions' do
189
+ expect(humanize('ATAN2(y, x)')).to eq('arctangent of y and x')
190
+ expect(humanize('HYPOT(a, b)')).to eq('hypotenuse of a and b')
191
+ end
192
+
193
+ it 'keeps member access in operands' do
194
+ expect(humanize('users[0].age >= 18')).to eq('users[0].age is greater than or equal to 18')
195
+ expect(humanize('(a + b).c = 1')).to eq('(a plus b).c equals 1')
196
+ end
197
+
198
+ it 'groups function calls before member access' do
199
+ expect(humanize('IF(a, b, c).d = 1')).to eq('(if a then b else c).d equals 1')
200
+ expect(humanize('FILTER(users, u, u.age > 20).name'))
201
+ .to eq('(filter u in users where u.age is greater than 20).name')
202
+ end
203
+
204
+ it 'groups function calls and operations before an index' do
205
+ expect(humanize('FILTER(users, u, u.age > 20)[0]'))
206
+ .to eq('(filter u in users where u.age is greater than 20)[0]')
207
+ expect(humanize('FILTER(users, u, u.age > 20)[0].name'))
208
+ .to eq('(filter u in users where u.age is greater than 20)[0].name')
209
+ expect(humanize('(a + b)[0] = 1')).to eq('(a plus b)[0] equals 1')
210
+ expect(humanize('users[0]')).to eq('users[0]')
211
+ end
212
+
213
+ it 'is exposed via Calculator#humanize' do
214
+ result = calculator.humanize('days >= min and days <= max', min: 5, max: 20)
215
+ expect(result).to eq('days is greater than or equal to 5 and days is less than or equal to 20')
216
+ end
217
+ end
data/spec/parser_spec.rb CHANGED
@@ -69,6 +69,11 @@ describe Dentaku::Parser do
69
69
  expect(node.value("a" => [1, 2, 3])).to eq(2)
70
70
  end
71
71
 
72
+ it 'evaluates access directly on array literal' do
73
+ expect(parse('{10, 20, 30}[0]').value).to eq(10)
74
+ expect(parse('{10, 20, 30}[-1]').value).to eq(30)
75
+ end
76
+
72
77
  it 'evaluates boolean expressions' do
73
78
  node = parse('true AND false')
74
79
  expect(node.value).to eq(false)
@@ -107,6 +112,9 @@ describe Dentaku::Parser do
107
112
 
108
113
  node = parse('{1, 2, 3} - {2,3}')
109
114
  expect(node.value).to eq([1])
115
+
116
+ node = parse('{{1, 2}, {3, 4}}')
117
+ expect(node.value).to eq([[1, 2], [3, 4]])
110
118
  end
111
119
 
112
120
  context 'invalid expression' do