Sha256: f988212945c794827af4243c5eea3dfa612b101de69e69318020320e17f8e7d5

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

require 'spec_helper'
require 'dentaku/ast/functions/sum'
require 'dentaku'

describe 'Dentaku::AST::Function::Sum' do
  it 'returns the sum of an array of Numeric values' do
    result = Dentaku('SUM(1, x, 1.8)', x: 2.3)
    expect(result).to eq(5.1)
  end

  it 'returns the sum of a single entry array of a Numeric value' do
    result = Dentaku('SUM(x)', x: 2.3)
    expect(result).to eq(2.3)
  end

  it 'returns the sum even if a String is passed' do
    result = Dentaku('SUM(1, x, 1.8)', x: '2.3')
    expect(result).to eq(5.1)
  end

  it 'returns the sum even if an array is passed' do
    result = Dentaku('SUM(1, x, 2.3)', x: [4, 5])
    expect(result).to eq(12.3)
  end

  it 'returns the sum of nested sums' do
    result = Dentaku('SUM(1, x, SUM(4, 5))', x: '2.3')
    expect(result).to eq(12.3)
  end

  context 'checking errors' do
    let(:calculator) { Dentaku::Calculator.new }

    it 'raises an error if no arguments are passed' do
      expect { calculator.evaluate!('SUM()') }.to raise_error(Dentaku::ArgumentError)
    end

    it 'does not raise an error if an empty array is passed' do
      result = calculator.evaluate!('SUM(x)', x: [])
      expect(result).to eq(0)
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
dentaku-3.5.4 spec/ast/sum_spec.rb
dentaku-3.5.3 spec/ast/sum_spec.rb
dentaku-3.5.2 spec/ast/sum_spec.rb
dentaku_zevo-3.5.2 spec/ast/sum_spec.rb
dentaku-3.5.1 spec/ast/sum_spec.rb
dentaku-3.5.0 spec/ast/sum_spec.rb
dentaku-3.4.2 spec/ast/sum_spec.rb