Sha256: bee41d2938b86b291604d905e9b4ce6316bd11b32ad76f0c68631ab85b9ff9b1

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

require 'dry-logic'

RSpec.describe 'Rules' do
  specify 'defining an anonymous rule with an arbitrary predicate' do
    rule = Dry::Logic.Rule { |value| value.is_a?(Integer) }

    expect(rule.(1)).to be_success
    expect(rule[1]).to be(true)
  end

  specify 'defining a conjunction' do
    rule = Dry::Logic.Rule(&:even?) & Dry::Logic.Rule { |v| v > 4 }

    expect(rule.(3)).to be_failure
    expect(rule.(4)).to be_failure
    expect(rule.(5)).to be_failure
    expect(rule.(6)).to be_success
  end

  specify 'defining a disjunction' do
    rule = Dry::Logic.Rule { |v| v < 4 } | Dry::Logic.Rule { |v| v > 6 }

    expect(rule.(5)).to be_failure
    expect(rule.(3)).to be_success
    expect(rule.(7)).to be_success
  end

  specify 'defining an implication' do
    rule = Dry::Logic.Rule(&:empty?) > Dry::Logic.Rule { |v| v.is_a?(Array) }

    expect(rule.('foo')).to be_success
    expect(rule.([1, 2])).to be_success
    expect(rule.([])).to be_success
    expect(rule.('')).to be_failure
  end

  specify 'defining an exclusive disjunction' do
    rule = Dry::Logic.Rule(&:empty?) ^ Dry::Logic.Rule { |v| v.is_a?(Array) }

    expect(rule.('foo')).to be_failure
    expect(rule.([])).to be_failure
    expect(rule.([1, 2])).to be_success
    expect(rule.('')).to be_success
  end

  specify 'defining a rule with options' do
    rule = Dry::Logic::Rule(id: :empty?) { |value| value.empty? }

    expect(rule.('foo')).to be_failure
    expect(rule.('')).to be_success
    expect(rule.ast('foo')).to eql([:predicate, [:empty?, [[:value, 'foo']]]])
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-logic-1.0.0 spec/integration/rule_spec.rb
dry-logic-0.6.1 spec/integration/rule_spec.rb
dry-logic-0.6.0 spec/integration/rule_spec.rb
dry-logic-0.5.0 spec/integration/rule_spec.rb
dry-logic-0.4.2 spec/integration/rule_spec.rb
dry-logic-0.4.1 spec/integration/rule_spec.rb
dry-logic-0.4.0 spec/integration/rule_spec.rb