Sha256: 0e464d2bac541bb6928e12d1570a5479f3b6da230acc652c521cd727f6bca4a6

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

shared_examples 'a class with alias to a clazz' do |aliaz, clazz, *variables|
  it do
    expect(subject).to respond_to(aliaz)
  end

  it "has an alias #{aliaz} for #{clazz}" do
    expect(subject.public_send(aliaz, *variables)).to eq(clazz.new(*variables))
  end
end

shared_examples 'a class with mapped dsl' do
  {
    addition: Danica::Operator::Addition,
    sum: Danica::Operator::Addition,
    multiplication: Danica::Operator::Multiplication,
    product: Danica::Operator::Multiplication,
    division: Danica::Operator::Division,
    power: Danica::Operator::Power
  }.each do |aliaz, clazz|
    it_behaves_like 'a class with alias to a clazz', aliaz, clazz, 2, 3
  end
  {
    squared_root: Danica::Operator::SquaredRoot,
    sqrt: Danica::Operator::SquaredRoot,
    exponential: Danica::Operator::Exponential,
    sin: Danica::Operator::Sin,
    cos: Danica::Operator::Cos,
    group: Danica::Wrapper::Group,
    negative: Danica::Wrapper::Negative,
    number: Danica::Wrapper::Number,
    num: Danica::Wrapper::Number,
    plus_minus: Danica::Wrapper::PlusMinus,
    constant: Danica::Wrapper::Constant
  }.each do |aliaz, clazz|
    it_behaves_like 'a class with alias to a clazz', aliaz, clazz, 9
  end
end

describe Danica::DSL do
  class Danica::DSL::Dummy
    include Danica::DSL
  end

  let(:subject) { described_class::Dummy.new }

  it_behaves_like 'a class with mapped dsl'

  describe '.build' do
    let(:expected) do
      Danica::Operator::Addition.new(
        Danica::Wrapper::Number.new(2),
        Danica::Wrapper::Variable.new(:x)
      )
    end

    it 'executes the build block' do
      expect(described_class.build { number(2) + variable(:x) }).to eq(expected)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
danica-2.7.7 spec/lib/danica/dsl_spec.rb
danica-2.7.6 spec/lib/danica/dsl_spec.rb