Sha256: 1eaab4b4a1af8697be6e7f2b2e727fd28347b643b61333a337079c0a734763a7

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

require 'spec_helper'
require 'dentaku/ast/functions/avg'
require 'dentaku'

describe 'Dentaku::AST::Function::Avg' do
  it 'returns the average of an array of Numeric values' do
    result = Dentaku('AVG(1, x, 1.8)', x: 2.3)
    expect(result).to eq(1.7)
  end

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

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

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

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

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

    it 'raises an error if an empty array is passed' do
      expect { calculator.evaluate!('AVG(x)', x: []) }.to raise_error(Dentaku::ArgumentError)
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dentaku_zevo-3.5.2 spec/ast/avg_spec.rb
dentaku-3.5.1 spec/ast/avg_spec.rb
dentaku-3.5.0 spec/ast/avg_spec.rb
dentaku-3.4.2 spec/ast/avg_spec.rb
dentaku-3.4.1 spec/ast/avg_spec.rb
dentaku-3.4.0 spec/ast/avg_spec.rb