Sha256: 654b829e790da9bcf33089ab19194df434749e3461c50d9ef34cbcc5a497890c

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'dentaku/bulk_expression_solver'

RSpec.describe Dentaku::BulkExpressionSolver do
  describe "#solve!" do
    it "evaluates properly with variables, even if some in memory" do
      expressions = {
        weekly_fruit_budget: "weekly_apple_budget + pear * 4",
        weekly_apple_budget: "apples * 7",
        pear: "1"
      }
      memory = {apples: 3}
      solver = described_class.new(expressions, memory)
      expect(solver.solve!)
        .to eq(pear: 1, weekly_apple_budget: 21, weekly_fruit_budget: 25)
    end

    it "lets you know if a variable is unbound" do
      expressions = {more_apples: "apples + 1"}
      expect {
        described_class.new(expressions, {}).solve!()
      }.to raise_error(Dentaku::UnboundVariableError)
    end

    it "lets you know if the result is a div/0 error" do
      expressions = {more_apples: "1/0"}
      expect {
        described_class.new(expressions, {}).solve!()
      }.to raise_error(ZeroDivisionError)
    end
  end

  describe "#solve" do
    it "returns :undefined when variables are unbound" do
      expressions = {more_apples: "apples + 1"}
      expect(described_class.new(expressions, {}).solve())
        .to eq(more_apples: :undefined)
    end

    it "allows passing in a custom value to an error handler when a variable is unbound" do
      expressions = {more_apples: "apples + 1"}
      expect(described_class.new(expressions, {}).solve() { :foo })
        .to eq(more_apples: :foo)
    end

    it "allows passing in a custom value to an error handler when there is a div/0 error" do
      expressions = {more_apples: "1/0"}
      expect(described_class.new(expressions, {}).solve() { :foo })
        .to eq(more_apples: :foo)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dentaku-1.2.6 spec/bulk_expression_solver_spec.rb