File: predicate_spec.rb

package info (click to toggle)
ruby-dry-logic 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 728 kB
  • sloc: ruby: 4,929; makefile: 6
file content (61 lines) | stat: -rw-r--r-- 1,407 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

RSpec.describe Dry::Logic::Rule::Predicate do
  subject(:rule) { described_class.build(predicate) }

  let(:predicate) { str? }

  include_context "predicates"

  it_behaves_like Dry::Logic::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" do
        expect(rule.("foo")).to be_success
      end

      it "returns failure 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