|
82 | 82 | )
|
83 | 83 |
|
84 | 84 | fns = [
|
85 |
| - [:biggest_callback, :numeric, ->(*args) { args.max }, ->(args) { args.each { |arg| raise Dentaku::ArgumentError unless arg.type == :numeric } }], |
86 |
| - [:pythagoras, :numeric, ->(l1, l2) { Math.sqrt(l1**2 + l2**2) }, ->(e) { @last_time = Time.now.to_s }], |
87 |
| - [:callback_lambda, :string, ->() { " " }, ->() { "lambda executed" }], |
88 |
| - [:no_lambda_function, :numeric, ->(a) { a**a }], |
| 85 | + [:biggest_callback, :numeric, ->(*args) { args.max }, ->(args) { args.each { |arg| raise Dentaku::ArgumentError unless arg.type == :numeric } }], |
| 86 | + [:pythagoras, :numeric, ->(l1, l2) { Math.sqrt(l1**2 + l2**2) }, ->(e) { @last_time = Time.now.to_s }], |
| 87 | + [:callback_lambda, :string, ->() { " " }, ->() { "lambda executed" }], |
| 88 | + [:no_lambda_function, :numeric, ->(a) { a**a }], |
89 | 89 | ]
|
90 | 90 |
|
91 | 91 | c.add_functions(fns)
|
|
141 | 141 | end
|
142 | 142 |
|
143 | 143 | it 'does not store functions across all calculators' do
|
144 |
| - calculator1 = Dentaku::Calculator.new |
| 144 | + calculator1 = described_class.new |
145 | 145 | calculator1.add_function(:my_function, :numeric, ->(x) { 2 * x + 1 })
|
146 | 146 |
|
147 |
| - calculator2 = Dentaku::Calculator.new |
| 147 | + calculator2 = described_class.new |
148 | 148 | calculator2.add_function(:my_function, :numeric, ->(x) { 4 * x + 3 })
|
149 | 149 |
|
150 | 150 | expect(calculator1.evaluate!("1 + my_function(2)")). to eq(1 + 2 * 2 + 1)
|
151 | 151 | expect(calculator2.evaluate!("1 + my_function(2)")). to eq(1 + 4 * 2 + 3)
|
152 | 152 |
|
153 | 153 | expect {
|
154 |
| - Dentaku::Calculator.new.evaluate!("1 + my_function(2)") |
| 154 | + described_class.new.evaluate!("1 + my_function(2)") |
155 | 155 | }.to raise_error(Dentaku::ParseError)
|
156 | 156 | end
|
157 | 157 |
|
158 | 158 | describe 'Dentaku::Calculator.add_function' do
|
159 |
| - it 'adds to default/global function registry' do |
160 |
| - Dentaku::Calculator.add_function(:global_function, :numeric, ->(x) { 10 + x**2 }) |
161 |
| - expect(Dentaku::Calculator.new.evaluate("global_function(3) + 5")).to eq(10 + 3**2 + 5) |
| 159 | + it 'adds a function to default/global function registry' do |
| 160 | + described_class.add_function(:global_function, :numeric, ->(x) { 10 + x**2 }) |
| 161 | + expect(described_class.new.evaluate("global_function(3) + 5")).to eq(10 + 3**2 + 5) |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + describe 'Dentaku::Calculator.add_functions' do |
| 166 | + it 'adds multiple functions to default/global function registry' do |
| 167 | + described_class.add_functions([ |
| 168 | + [:cube, :numeric, ->(x) { x**3 }], |
| 169 | + [:spongebob, :string, ->(x) { x.split("").each_with_index().map { |c,i| i.even? ? c.upcase : c.downcase }.join() }], |
| 170 | + ]) |
| 171 | + |
| 172 | + expect(described_class.new.evaluate("1 + cube(3)")).to eq(28) |
| 173 | + expect(described_class.new.evaluate("spongebob('How are you today?')")).to eq("HoW ArE YoU ToDaY?") |
162 | 174 | end
|
163 | 175 | end
|
164 | 176 | end
|
|
0 commit comments