Sha256: 7c445cefb751f59233dcdb296acc0da1bc3fdcb12162d379767bb2e49212cfdc

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

RSpec.describe Rule::Predicate do
  subject(:rule) { Rule::Predicate.new(predicate) }

  let(:predicate) { str? }

  include_context 'predicates'

  it_behaves_like Rule

  describe '#name' do
    it 'returns predicate identifier' do
      expect(rule.name).to be(:str?)
    end
  end

  describe '#to_ast' do
    context 'without a result' do
      it 'returns rule ast' do
        expect(rule.to_ast).to eql([:predicate, [:str?, [[:input, Undefined]]]])
      end

      it 'returns :failure with an id' do
        email = rule.with(id: :email)

        expect(email.(11).to_ast).to eql([:failure, [:email, [:predicate, [:str?, [[:input, 11]]]]]])
      end
    end

    context 'with a result' do
      it 'returns success ast' do
        expect(rule.('foo').to_ast).to eql([:predicate, [:str?, [[:input, 'foo']]]])
      end

      it 'returns ast' do
        expect(rule.(5).to_ast).to eql([:predicate, [:str?, [[:input, 5]]]])
      end
    end

    context 'with a zero-arity predicate' do
      let(:predicate) { Module.new { def self.test?; true; end }.method(:test?) }

      it 'returns ast' do
        expect(rule.to_ast).to eql([:predicate, [:test?, []]])
      end
    end
  end

  describe '#to_s' do
    it 'returns string representation' do
      expect(rule.curry('foo').to_s).to eql('str?("foo")')
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-logic-0.5.0 spec/unit/rule/predicate_spec.rb
dry-logic-0.4.2 spec/unit/rule/predicate_spec.rb
dry-logic-0.4.1 spec/unit/rule/predicate_spec.rb
dry-logic-0.4.0 spec/unit/rule/predicate_spec.rb