File: or_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 (75 lines) | stat: -rw-r--r-- 2,083 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

RSpec.describe Dry::Logic::Operations::Or do
  subject(:operation) { described_class.new(left, right) }

  include_context "predicates"

  let(:left) { Dry::Logic::Rule::Predicate.build(nil?) }
  let(:right) { Dry::Logic::Rule::Predicate.build(gt?).curry(18) }

  let(:other) do
    Dry::Logic::Rule::Predicate.build(int?) & Dry::Logic::Rule::Predicate.build(lt?).curry(14)
  end

  describe "#call" do
    it "calls left and right" do
      expect(operation.(nil)).to be_success
      expect(operation.(19)).to be_success
      expect(operation.(18)).to be_failure
    end
  end

  describe "#to_ast" do
    it "returns ast" do
      expect(operation.to_ast).to eql(
        [:or, [
          [:predicate, [:nil?, [[:input, undefined]]]],
          [:predicate, [:gt?, [[:num, 18], [:input, undefined]]]]
        ]]
      )
    end

    it "returns result ast" do
      expect(operation.(17).to_ast).to eql(
        [:or, [
          [:predicate, [:nil?, [[:input, 17]]]],
          [:predicate, [:gt?, [[:num, 18], [:input, 17]]]]
        ]]
      )
    end

    it "returns failure result ast" do
      expect(operation.with(id: :age).(17).to_ast).to eql(
        [:failure, [:age, [:or, [
          [:predicate, [:nil?, [[:input, 17]]]],
          [:predicate, [:gt?, [[:num, 18], [:input, 17]]]]
        ]]]]
      )
    end
  end

  describe "#and" do
    it "creates and with the other" do
      expect(operation.and(other).(nil)).to be_failure
      expect(operation.and(other).(19)).to be_failure
      expect(operation.and(other).(13)).to be_failure
      expect(operation.and(other).(14)).to be_failure
    end
  end

  describe "#or" do
    it "creates or with the other" do
      expect(operation.or(other).(nil)).to be_success
      expect(operation.or(other).(19)).to be_success
      expect(operation.or(other).(13)).to be_success
      expect(operation.or(other).(14)).to be_failure
    end
  end

  describe "#to_s" do
    it "returns string representation" do
      expect(operation.to_s).to eql("nil? OR gt?(18)")
    end
  end
end